'Tests that the admin account is notified when a user is in the \'pending\' state.'

@mock.patch('course_creators.admin.render_to_string', mock.Mock(side_effect=mock_render_to_string, autospec=True))
def test_mail_admin_on_pending(self):

    def check_admin_message_state(state, expect_sent_to_admin, expect_sent_to_user):
        '    Changes    user    state    and    verifies    e-mail    sent    to    admin    address    only    when    pending.    '
        mail.outbox = []
        self._change_state(state)
        base_num_emails = (1 if expect_sent_to_user else 0)
        if expect_sent_to_admin:
            context = {'user_name': 'test_user', 'user_email': u'test_user+courses@edx.org'}
            self.assertEquals((base_num_emails + 1), len(mail.outbox), 'Expected    admin    message    to    be    sent')
            sent_mail = mail.outbox[base_num_emails]
            self.assertEquals(mock_render_to_string('emails/course_creator_admin_subject.txt', context), sent_mail.subject)
            self.assertEquals(mock_render_to_string('emails/course_creator_admin_user_pending.txt', context), sent_mail.body)
            self.assertEquals(self.studio_request_email, sent_mail.from_email)
            self.assertEqual([self.studio_request_email], sent_mail.to)
        else:
            self.assertEquals(base_num_emails, len(mail.outbox))
    with mock.patch.dict('django.conf.settings.FEATURES', self.enable_creator_group_patch):
        check_admin_message_state(CourseCreator.UNREQUESTED, expect_sent_to_admin=False, expect_sent_to_user=False)
        check_admin_message_state(CourseCreator.PENDING, expect_sent_to_admin=True, expect_sent_to_user=False)
        check_admin_message_state(CourseCreator.GRANTED, expect_sent_to_admin=False, expect_sent_to_user=True)
        check_admin_message_state(CourseCreator.DENIED, expect_sent_to_admin=False, expect_sent_to_user=True)
        check_admin_message_state(CourseCreator.GRANTED, expect_sent_to_admin=False, expect_sent_to_user=True)
        check_admin_message_state(CourseCreator.PENDING, expect_sent_to_admin=True, expect_sent_to_user=True)
        check_admin_message_state(CourseCreator.PENDING, expect_sent_to_admin=False, expect_sent_to_user=False)
        check_admin_message_state(CourseCreator.DENIED, expect_sent_to_admin=False, expect_sent_to_user=True)
