'Test the credit requirements, eligibility notification, email
content caching for a credit course.'

@httpretty.activate
@override_settings(ECOMMERCE_API_URL=TEST_API_URL, ECOMMERCE_SERVICE_WORKER_USERNAME=TEST_ECOMMERCE_WORKER)
def test_satisfy_all_requirements(self):

    self._mock_ecommerce_courses_api(self.course_key, self.COURSE_API_RESPONSE)
    worker_user = User.objects.create_user(username=TEST_ECOMMERCE_WORKER)
    self.assertFalse(hasattr(worker_user, 'profile'))
    self.add_credit_course()
    user = self.create_and_enroll_user(username=self.USER_INFO['username'], password=self.USER_INFO['password'])
    CourseFactory.create(org='edX', number='DemoX', display_name='Demo_Course')
    requirements = [{'namespace': 'grade', 'name': 'grade', 'display_name': 'Grade', 'criteria': {'min_grade': 0.8}}, {'namespace': 'grade', 'name': 'other_grade', 'display_name': 'Assessment    1', 'criteria': {}}]
    api.set_credit_requirements(self.course_key, requirements)
    with self.assertNumQueries(12):
        api.set_credit_requirement_status(user, self.course_key, requirements[0]['namespace'], requirements[0]['name'])
    self.assertFalse(api.is_user_eligible_for_credit(user.username, self.course_key))
    with self.assertNumQueries(24):
        api.set_credit_requirement_status(user, self.course_key, requirements[1]['namespace'], requirements[1]['name'])
    self.assertTrue(api.is_user_eligible_for_credit(user.username, self.course_key))
    self.assertEqual(len(mail.outbox), 1)
    self.assertEqual(mail.outbox[0].subject, 'You    are    eligible    for    credit    from    Hogwarts    School    of    Witchcraft    and    Wizardry')
    email_payload_first = mail.outbox[0].attachments[0]._payload
    self.assertEqual(len(email_payload_first), 2)
    self.assertIn('text/plain', email_payload_first[0]._payload[0]['Content-Type'])
    self.assertIn('text/html', email_payload_first[0]._payload[1]['Content-Type'])
    self.assertIn('image/png', email_payload_first[1]['Content-Type'])
    email_image = email_payload_first[1]
    html_content_first = email_payload_first[0]._payload[1]._payload
    image_id = email_image.get('Content-ID', '')[1:(-1)]
    self.assertIsNotNone(image_id)
    self.assertIn(image_id, html_content_first)
    self.assertIn('credit    from    Hogwarts    School    of    Witchcraft    and    Wizardry    for', html_content_first)
    text_content_first = email_payload_first[0]._payload[0]._payload
    self.assertIn('credit    from    Hogwarts    School    of    Witchcraft    and    Wizardry    for', text_content_first)
    CreditEligibility.objects.all().delete()
    with self.assertNumQueries(16):
        api.set_credit_requirement_status(user, self.course_key, requirements[1]['namespace'], requirements[1]['name'])
    self.assertEqual(len(mail.outbox), 2)
    email_payload_second = mail.outbox[1].attachments[0]._payload
    html_content_second = email_payload_second[0]._payload[1]._payload
    self.assertIn(image_id, html_content_second)
    api.set_credit_requirement_status(user, self.course_key, requirements[0]['namespace'], requirements[0]['name'], status='failed')
    self.assertTrue(api.is_user_eligible_for_credit(user.username, self.course_key))
