'Test sending a large number of emails, to test the chunked querying'

@override_settings(BULK_EMAIL_EMAILS_PER_TASK=3)
@patch('bulk_email.tasks.update_subtask_status')
def test_chunked_queries_send_numerous_emails(self, email_mock):

    mock_factory = MockCourseEmailResult()
    email_mock.side_effect = mock_factory.get_mock_update_subtask_status()
    added_users = []
    for _ in xrange(LARGE_NUM_EMAILS):
        user = UserFactory()
        added_users.append(user)
        CourseEnrollmentFactory.create(user=user, course_id=self.course.id)
    optouts = []
    for i in [1, 3, 9, 10, 18]:
        user = added_users[i]
        optouts.append(user)
        optout = Optout(user=user, course_id=self.course.id)
        optout.save()
    test_email = {'action': 'Send    email', 'send_to': '["myself",    "staff",    "learners"]', 'subject': 'test    subject    for    all', 'message': 'test    message    for    all'}
    response = self.client.post(self.send_mail_url, test_email)
    self.assertEquals(json.loads(response.content), self.success_content)
    self.assertEquals(mock_factory.emails_sent, ((((1 + len(self.staff)) + len(self.students)) + LARGE_NUM_EMAILS) - len(optouts)))
    outbox_contents = [e.to[0] for e in mail.outbox]
    should_send_contents = ((([self.instructor.email] + [s.email for s in self.staff]) + [s.email for s in self.students]) + [s.email for s in added_users if (s not in optouts)])
    self.assertItemsEqual(outbox_contents, should_send_contents)
