class SurveyRemindersTest(MailTestCase, GSoCDjangoTestCase, TaskQueueTestCase):
    """Tests for accept_proposals task.
  """

    SPAWN_URL = '/tasks/gsoc/surveys/send_reminder/spawn'
    SEND_URL = '/tasks/gsoc/surveys/send_reminder/send'

    def setUp(self):
        super(SurveyRemindersTest, self).setUp()
        self.init()
        self.createMentor()
        self.createStudent()
        self.createSurveys()

    def createMentor(self):
        """Creates a new mentor.
    """
        profile_helper = GSoCProfileHelper(self.gsoc, self.dev_test)
        profile_helper.createOtherUser('*****@*****.**')
        self.mentor = profile_helper.createMentor(self.org)

    def createStudent(self):
        """Creates a Student with a project.
    """
        profile_helper = GSoCProfileHelper(self.gsoc, self.dev_test)
        profile_helper.createOtherUser('*****@*****.**')
        self.student = profile_helper.createStudentWithProject(
            self.org, self.mentor)
        self.project = GSoCProject.all().ancestor(self.student).get()

    def createSurveys(self):
        """Creates the surveys and records required for the tests in the old
    format.
    """
        survey_values = {
            'author': self.founder,
            'title': 'Title',
            'modified_by': self.founder,
            'link_id': 'link_id',
            'scope': self.gsoc,
            'scope_path': self.gsoc.key().id_or_name()
        }

        self.project_survey = ProjectSurvey(key_name='key_name',
                                            **survey_values)
        self.project_survey.put()

        self.grading_survey = GradingProjectSurvey(key_name='key_name',
                                                   **survey_values)
        self.grading_survey.put()

    def testSpawnSurveyRemindersForProjectSurvey(self):
        """Test spawning reminder tasks for a ProjectSurvey.
    """
        post_data = {
            'program_key': self.gsoc.key().id_or_name(),
            'survey_key': self.project_survey.key().id_or_name(),
            'survey_type': 'project'
        }

        response = self.post(self.SPAWN_URL, post_data)

        self.assertEqual(response.status_code, httplib.OK)
        self.assertTasksInQueue(n=2)
        self.assertTasksInQueue(n=1, url=self.SPAWN_URL)
        self.assertTasksInQueue(n=1, url=self.SEND_URL)

    def testSpawnSurveyRemindersForGradingSurvey(self):
        """Test spawning reminder tasks for a GradingProjectSurvey.
    """
        post_data = {
            'program_key': self.gsoc.key().id_or_name(),
            'survey_key': self.grading_survey.key().id_or_name(),
            'survey_type': 'grading'
        }

        response = self.post(self.SPAWN_URL, post_data)

        self.assertEqual(response.status_code, httplib.OK)
        self.assertTasksInQueue(n=2)
        self.assertTasksInQueue(n=1, url=self.SPAWN_URL)
        self.assertTasksInQueue(n=1, url=self.SEND_URL)

    def testSendSurveyReminderForProjectSurvey(self):
        """Test sending out a reminder for a ProjectSurvey.
    """
        post_data = {
            'survey_key': self.project_survey.key().id_or_name(),
            'survey_type': 'project',
            'project_key': str(self.project.key())
        }

        response = self.post(self.SEND_URL, post_data)

        self.assertEqual(response.status_code, httplib.OK)
        # URL explicitly added since the email task is in there
        self.assertTasksInQueue(n=0, url=self.SEND_URL)
        self.assertEmailSent(to=self.student.email)
        self.assertEmailNotSent(to=self.mentor.email)

    def testSendSurveyReminderForGradingSurvey(self):
        """Test sending out a reminder for a GradingProjectSurvey.
    """
        post_data = {
            'survey_key': self.grading_survey.key().id_or_name(),
            'survey_type': 'grading',
            'project_key': str(self.project.key())
        }

        response = self.post(self.SEND_URL, post_data)

        self.assertEqual(response.status_code, httplib.OK)
        # URL explicitly added since the email task is in there
        self.assertTasksInQueue(n=0, url=self.SEND_URL)
        self.assertEmailSent(to=self.mentor.email)
        self.assertEmailNotSent(to=self.student.email)
class SurveyRemindersTest(
    test_utils.GSoCDjangoTestCase, test_utils.TaskQueueTestCase):
  """Tests for accept_proposals task.
  """

  SPAWN_URL = '/tasks/gsoc/surveys/send_reminder/spawn'
  SEND_URL = '/tasks/gsoc/surveys/send_reminder/send'

  def setUp(self):
    super(SurveyRemindersTest, self).setUp()
    self.init()

    self.mentor = profile_utils.seedNDBProfile(
        self.program.key(), mentor_for=[self.org.key])
    self.student = profile_utils.seedNDBStudent(self.program)
    self.project = project_utils.seedProject(
        self.student, self.program.key(), org_key=self.org.key,
        mentor_key=self.mentor.key)

    self.createSurveys()

  def createSurveys(self):
    """Creates the surveys and records required for the tests in the old
    format.
    """
    user = profile_utils.seedNDBUser()
    survey_values = {
        'author': user.key.to_old_key(),
        'title': 'Title',
        'modified_by': user.key.to_old_key(),
        'link_id': 'link_id',
        'scope': self.program,
        'survey_start': timeline_utils.past(),
        'survey_end': timeline_utils.past(),
    }

    self.project_survey = ProjectSurvey(
        key_name='key_name', **survey_values)
    self.project_survey.put()

    self.grading_survey = GradingProjectSurvey(
        key_name='key_name', **survey_values)
    self.grading_survey.put()

  def testSpawnSurveyRemindersForProjectSurvey(self):
    """Test spawning reminder tasks for a ProjectSurvey."""
    post_data = {
        'program_key': self.program.key().id_or_name(),
        'survey_key': self.project_survey.key().id_or_name(),
        'survey_type': 'project'
        }

    response = self.post(self.SPAWN_URL, post_data)

    self.assertResponseOK(response)
    self.assertTasksInQueue(n=2)
    self.assertTasksInQueue(n=1, url=self.SPAWN_URL)
    self.assertTasksInQueue(n=1, url=self.SEND_URL)

  def testSpawnSurveyRemindersForGradingSurvey(self):
    """Test spawning reminder tasks for a GradingProjectSurvey."""
    post_data = {
        'program_key': self.program.key().id_or_name(),
        'survey_key': self.grading_survey.key().id_or_name(),
        'survey_type': 'grading'
        }

    response = self.post(self.SPAWN_URL, post_data)

    self.assertResponseOK(response)
    self.assertTasksInQueue(n=2)
    self.assertTasksInQueue(n=1, url=self.SPAWN_URL)
    self.assertTasksInQueue(n=1, url=self.SEND_URL)

  def testSendSurveyReminderForProjectSurvey(self):
    """Test sending out a reminder for a ProjectSurvey."""
    post_data = {
        'survey_key': self.project_survey.key().id_or_name(),
        'survey_type': 'project',
        'project_key': str(self.project.key())
        }

    response = self.post(self.SEND_URL, post_data)

    self.assertResponseOK(response)
    # URL explicitly added since the email task is in there
    self.assertTasksInQueue(n=0, url=self.SEND_URL)
    self.assertEmailSent(to=self.student.contact.email)
    # TODO(daniel): add assertEmailNotSent to DjangoTestCase
    #self.assertEmailNotSent(to=self.mentor.email)

  def testSendSurveyReminderForGradingSurvey(self):
    """Test sending out a reminder for a GradingProjectSurvey."""
    post_data = {
        'survey_key': self.grading_survey.key().id_or_name(),
        'survey_type': 'grading',
        'project_key': str(self.project.key())
        }

    response = self.post(self.SEND_URL, post_data)

    self.assertResponseOK(response)
    # URL explicitly added since the email task is in there
    self.assertTasksInQueue(n=0, url=self.SEND_URL)
    self.assertEmailSent(to=self.mentor.contact.email)
    # TODO(daniel): add assertEmailNotSent to DjangoTestCase
    #self.assertEmailNotSent(to=self.student.email)

  def testDoesNotSpawnProjectSurveyReminderForWithdrawnProject(self):
    """Test withdrawn projects don't spawn reminder tasks for
    student evaluation.

    This covers all the evaluations created (midterm and final).
    """
    # seed a withdrawn project
    student = profile_utils.seedNDBStudent(self.program)
    project_utils.seedProject(
        student, self.program.key(), org_key=self.org.key, status='withdrawn')

    post_data = {
        'program_key': self.gsoc.key().id_or_name(),
        'survey_key': self.project_survey.key().id_or_name(),
        'survey_type': 'project'
        }

    response = self.post(self.SPAWN_URL, post_data)

    self.assertResponseOK(response)
    self.assertTasksInQueue(n=2)
    self.assertTasksInQueue(n=1, url=self.SPAWN_URL)
    # We have two projects in datastore and one is withdrawn, so we expect
    # to spawn the task for only one project.
    self.assertTasksInQueue(n=1, url=self.SEND_URL)

  def testDoesNotGradingProjectSurveyReminderForWithdrawnProject(self):
    """Test withdrawn projects don't spawn reminder tasks for
    mentor evaluation.

    This covers all the evaluations created (midterm and final).
    """
    student = profile_utils.seedNDBStudent(self.program)
    project_utils.seedProject(
        student, self.program.key(), org_key=self.org.key, status='withdrawn')

    self.project.put()
    post_data = {
        'program_key': self.gsoc.key().id_or_name(),
        'survey_key': self.grading_survey.key().id_or_name(),
        'survey_type': 'grading'
        }

    response = self.post(self.SPAWN_URL, post_data)

    self.assertResponseOK(response)
    self.assertTasksInQueue(n=2)
    self.assertTasksInQueue(n=1, url=self.SPAWN_URL)
    # We have two projects in datastore and one is withdrawn, so we expect
    # to spawn the task for only one project.
    self.assertTasksInQueue(n=1, url=self.SEND_URL)
class SurveyRemindersTest(MailTestCase, GSoCDjangoTestCase, TaskQueueTestCase):
  """Tests for accept_proposals task.
  """

  SPAWN_URL = '/tasks/gsoc/surveys/send_reminder/spawn'
  SEND_URL = '/tasks/gsoc/surveys/send_reminder/send'

  def setUp(self):
    super(SurveyRemindersTest, self).setUp()
    self.init()
    self.createMentor()
    self.createStudent()
    self.createSurveys()

  def createMentor(self):
    """Creates a new mentor.
    """
    profile_helper = GSoCProfileHelper(self.gsoc, self.dev_test)
    profile_helper.createOtherUser('*****@*****.**')
    self.mentor = profile_helper.createMentor(self.org)

  def createStudent(self):
    """Creates a Student with a project.
    """
    profile_helper = GSoCProfileHelper(self.gsoc, self.dev_test)
    profile_helper.createOtherUser('*****@*****.**')
    self.student = profile_helper.createStudentWithProject(self.org,
                                                           self.mentor)
    self.project = GSoCProject.all().ancestor(self.student).get()

  def createSurveys(self):
    """Creates the surveys and records required for the tests in the old
    format.
    """
    survey_values = {
        'author': self.founder,
        'title': 'Title',
        'modified_by': self.founder,
        'link_id': 'link_id',
        'scope': self.gsoc,
        'scope_path': self.gsoc.key().id_or_name()}

    self.project_survey = ProjectSurvey(key_name='key_name',
                                        **survey_values)
    self.project_survey.put()

    self.grading_survey = GradingProjectSurvey(key_name='key_name',
                                               **survey_values)
    self.grading_survey.put()

  def testSpawnSurveyRemindersForProjectSurvey(self):
    """Test spawning reminder tasks for a ProjectSurvey.
    """
    post_data = {
        'program_key': self.gsoc.key().id_or_name(),
        'survey_key': self.project_survey.key().id_or_name(),
        'survey_type': 'project'}

    response = self.post(self.SPAWN_URL, post_data)

    self.assertEqual(response.status_code, httplib.OK)
    self.assertTasksInQueue(n=2)
    self.assertTasksInQueue(n=1, url=self.SPAWN_URL)
    self.assertTasksInQueue(n=1, url=self.SEND_URL)

  def testSpawnSurveyRemindersForGradingSurvey(self):
    """Test spawning reminder tasks for a GradingProjectSurvey.
    """
    post_data = {
        'program_key': self.gsoc.key().id_or_name(),
        'survey_key': self.grading_survey.key().id_or_name(),
        'survey_type': 'grading'}

    response = self.post(self.SPAWN_URL, post_data)

    self.assertEqual(response.status_code, httplib.OK)
    self.assertTasksInQueue(n=2)
    self.assertTasksInQueue(n=1, url=self.SPAWN_URL)
    self.assertTasksInQueue(n=1, url=self.SEND_URL)

  def testSendSurveyReminderForProjectSurvey(self):
    """Test sending out a reminder for a ProjectSurvey.
    """
    post_data = {
        'survey_key': self.project_survey.key().id_or_name(),
        'survey_type': 'project',
        'project_key': str(self.project.key())}

    response = self.post(self.SEND_URL, post_data)

    self.assertEqual(response.status_code, httplib.OK)
    # URL explicitly added since the email task is in there
    self.assertTasksInQueue(n=0, url=self.SEND_URL)
    self.assertEmailSent(to=self.student.email)
    self.assertEmailNotSent(to=self.mentor.email)

  def testSendSurveyReminderForGradingSurvey(self):
    """Test sending out a reminder for a GradingProjectSurvey.
    """
    post_data = {
        'survey_key': self.grading_survey.key().id_or_name(),
        'survey_type': 'grading',
        'project_key': str(self.project.key())}

    response = self.post(self.SEND_URL, post_data)

    self.assertEqual(response.status_code, httplib.OK)
    # URL explicitly added since the email task is in there
    self.assertTasksInQueue(n=0, url=self.SEND_URL)
    self.assertEmailSent(to=self.mentor.email)
    self.assertEmailNotSent(to=self.student.email)