[docs]deftest_email_fwd(self):"""Email Forward sanity check."""user=get_user_model().objects.get_or_create(username="awilliam",graduation_year=get_senior_graduation_year()-1,user_type="student")[0]self.login()response=self.client.get(reverse("senior_emailfwd"),follow=True)self.assertIn("Only seniors can set their forwarding address.",list(map(str,list(response.context["messages"]))))user.graduation_year=get_senior_graduation_year()user.save()response=self.client.get(reverse("senior_emailfwd"))self.assertEqual(response.status_code,200)# Now, test setting an emailresponse=self.client.post(reverse("senior_emailfwd"),data={"email":"nonexistent@tjhsst.edu"},follow=True)self.assertEqual(response.status_code,200)self.assertEqual(1,SeniorEmailForward.objects.filter(user=user,email="nonexistent@tjhsst.edu").count())# Test invalid emailresponse=self.client.post(reverse("senior_emailfwd"),data={"email":"nonexistenttjhsst.edu"},follow=True)self.assertEqual(response.status_code,200)self.assertEqual(1,SeniorEmailForward.objects.filter(user=user,email="nonexistent@tjhsst.edu").count())self.assertIn("Error adding forwarding address.",list(map(str,list(response.context["messages"]))))
[docs]deftest_getting_forwards(self):"""Test ability to retrieve senior forwards."""user=get_user_model().objects.get_or_create(username="awilliam",graduation_year=get_senior_graduation_year(),user_type="student")[0]user.save()self.login()# Set the emailresponse=self.client.post(reverse("senior_emailfwd"),data={"email":"nonexistent@tjhsst.edu"},follow=True)self.assertEqual(response.status_code,200)self.assertEqual(1,SeniorEmailForward.objects.filter(user=user,email="nonexistent@tjhsst.edu").count())# Run the testwithStringIO()asdata:call_command("get_senior_forwards",stdout=data)self.assertEqual(data.getvalue(),"awilliam:\t\tnonexistent@tjhsst.edu\n")