def test_patch_normal_title(self): rem = Reminder(creator=self.user, title="xxx") rem.save() self.validate_response( f"{self.endpoint}{rem.pk}/", method="patch", headers=self.auth_headers, data={ "title": "yyy", }, expected_status_code=status.HTTP_200_OK, ) reminders = Reminder.objects.filter(title="yyy") self.assertEqual(1, reminders.count()) reminder = reminders.first() self.assertTrue(reminder) self.assertEqual("yyy", reminder.title) self.assertEqual(rem.creator, reminder.creator) self.assertListEqual(list(rem.participants.all()), list(reminder.participants.all())) self.assertEqual(rem.pk, reminder.pk)
def test_user_normal(self): reminder = Reminder(creator=self.user) reminder.save() self.validate_response( self.endpoint, headers=self.auth_headers, expected_response_payload=[ { "created_at": reminder.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), "creator": self.user.pk, "description": reminder.description, "id": reminder.pk, "location": reminder.location, "notified_at": reminder.notified_at, "notify_at": reminder.notify_at, "participants": [], "status": ReminderStatus.CREATED.name, "title": reminder.title, }, ], )
def test_patch_status(self): reminder = Reminder(creator=self.user) reminder.save() self.validate_response( f"{self.endpoint}{reminder.pk}/", method="patch", headers=self.auth_headers, data={"status": ReminderStatus.NOTIFIED.name}, expected_status_code=status.HTTP_400_BAD_REQUEST, ) reminder.refresh_from_db() self.assertEqual("CREATED", reminder.status) self.validate_response( f"{self.endpoint}{reminder.pk}/", method="patch", headers=self.auth_headers, data={"status": ReminderStatus.DONE.name}, expected_status_code=status.HTTP_200_OK, ) reminder.refresh_from_db() self.assertEqual("DONE", reminder.status)
def test_patch_normal_readonly(self): reminder = Reminder(creator=self.user) reminder.save() dtm = utcnow() - timedelta(days=1) dataset = { "created_at": dtm, "creator": self.user2.pk, "notified_at": dtm, } old = {_f: getattr(reminder, _f) for _f in dataset} for field, value in dataset.items(): self.validate_response( f"{self.endpoint}{reminder.pk}/", method="patch", headers=self.auth_headers, data={field: value}, expected_status_code=status.HTTP_200_OK, ) reminder.refresh_from_db() for field, expected_value in old.items(): self.assertEqual(expected_value, getattr(reminder, field), field)
def test_get_anonymous(self): user = self.create_user() rem = Reminder(creator=user, title=f"title_{user.username}") rem.save() self.validate_response( url="/r/", expected_status_code=404, content_filters=(lambda _c: b"Not Found" in _c,), ) self.validate_response( url=f"/r/{rem.pk * 1001}/delete/", expected_status_code=404, content_filters=(lambda _c: b"Not Found" in _c,), ) self.validate_response( url=f"{rem.get_absolute_url()}delete/", expected_status_code=200, expected_view=ReminderDeleteView, expected_view_name="reminders:delete", expected_template="reminders/form_delete.html", content_filters=(lambda _c: b"Feel free to register" in _c,), )
def test_post_user(self): user = self.create_user() rem = Reminder(creator=user, title=user.username) rem.save() client = Client() client.login(username=user.username, password=user.username) self.validate_response( client=client, method="post", url=f"{rem.get_absolute_url()}delete/", expected_view_name="reminders:all_reminders", expected_view=AllRemindersView, expected_redirect_chain=[("/", 302)], expected_template="reminders/all_reminders.html", content_filters=( lambda _c: b"error" not in _c, lambda _c: f"title_{user.username}".encode() not in _c, lambda _c: ReminderStatus.NOTIFIED.value.encode() not in _c, ), ) with self.assertRaises(Reminder.DoesNotExist): rem.refresh_from_db()
def test_patch_anon(self): reminder = Reminder(creator=self.user) reminder.save() self.validate_response( f"{self.endpoint}{reminder.pk}/", method="patch", expected_status_code=status.HTTP_401_UNAUTHORIZED, )
def test_delete_normal(self): reminder = Reminder(creator=self.user) reminder.save() self.validate_response( f"{self.endpoint}{reminder.pk}/", method="delete", headers=self.auth_headers, expected_status_code=status.HTTP_204_NO_CONTENT, ) with self.assertRaises(Reminder.DoesNotExist): reminder.refresh_from_db()
def test_get_user(self): user = self.create_user() rem = Reminder(creator=user, title=f"title_{user.username}") rem.save() client = Client() client.login(username=user.username, password=user.username) self.validate_response( client=client, url=f"{rem.get_absolute_url()}delete/", expected_view_name="reminders:delete", expected_view=ReminderDeleteView, expected_template="reminders/form_delete.html", content_filters=(lambda _c: f"title_{user.username}".encode() in _c,), )
def test_get(self): user = self.create_user() reminder = Reminder(creator=user, title=f"title_{user.username}") reminder.save() view_url = f"{reminder.get_absolute_url()}done/" sign_in_url = reverse_lazy("onboarding:sign_in") redirect_url = f"{sign_in_url}?next={view_url}" self.validate_response( url=view_url, expected_view_name="onboarding:sign_in", expected_view=SignInView, expected_redirect_chain=[(redirect_url, 302)], expected_template="onboarding/sign_in.html", content_filters=(lambda _c: b"error" not in _c, ), )
def test_post_user(self): user = self.create_user() client = Client() client.login(username=user.username, password=user.username) rem = Reminder(creator=user, title=user.username) rem.save() created_at = rem.created_at dtm = utcnow() + timedelta(days=1) self.validate_response( client=client, method="post", url=f"{rem.get_absolute_url()}update/", form_data={ "created_at": dtm, "status": ReminderStatus.NOTIFIED, "title": f"title_{user.username}", "participants": [ user.pk, ], }, expected_view_name="reminders:reminder", expected_view=ReminderView, expected_redirect_chain=[(rem.get_absolute_url(), 302)], expected_template="reminders/reminder.html", content_filters=( lambda _c: b"error" not in _c, lambda _c: f"title_{user.username}".encode() in _c, lambda _c: ReminderStatus.NOTIFIED.value.encode() not in _c, ), ) rem.refresh_from_db() self.assertEqual(rem.title, f"title_{user.username}") self.assertEqual(rem.status, ReminderStatus.CREATED.name) self.assertEqual(rem.created_at, created_at) self.assertNotEqual(rem.created_at, dtm)
def test_reminder_model(self): placeholder = urandom(4).hex() user = self.create_user(placeholder) reminder = Reminder(creator=user) reminder.save() self.assertEqual( f"Reminder(pk={reminder.pk}, status=CREATED, notify_at=None, title=`None`)", str(reminder), ) self.assertEqual("never", reminder.notify_at_full) self.assertEqual("never", reminder.notify_at_humanized) self.assertFalse(reminder.is_notified) self.assertFalse(reminder.is_done) dtm = delorean.parse("2020-01-02 03:45", timezone="UTC", dayfirst=False) reminder.notify_at = dtm.datetime reminder.save() self.assertEqual("03:45 at Thursday, January 02, 2020", reminder.notify_at_full) self.assertEqual("a year from now", reminder.notify_at_humanized) self.assertFalse(reminder.is_notified) self.assertFalse(reminder.is_done) reminder.status = ReminderStatus.NOTIFIED.name reminder.save() self.assertFalse(reminder.is_notified) self.assertFalse(reminder.is_done) reminder.notified_at = dtm.datetime reminder.save() self.assertTrue(reminder.is_notified) self.assertFalse(reminder.is_done) reminder.status = ReminderStatus.DONE.name reminder.save() self.assertFalse(reminder.is_notified) self.assertTrue(reminder.is_done)
def test_post_user(self): user = self.create_user() client = Client() client.login(username=user.username, password=user.username) reminder = Reminder(creator=user, title=user.username) reminder.save() view_url = f"{reminder.get_absolute_url()}done/" self.validate_response( client=client, method="post", url=view_url, expected_view_name="reminders:reminder", expected_view=ReminderView, expected_redirect_chain=[(reminder.get_absolute_url(), 301)], expected_template="reminders/reminder.html", content_filters=(lambda _c: b"error" not in _c, ), ) reminder.refresh_from_db() self.assertEqual(reminder.status, ReminderStatus.DONE.name)
class Test(TestCase, ApiTestMixin, UserTestMixin): def setUp(self) -> None: self.endpoint = "/api/v1/reminder/" self.user = self.create_user() self.token = self.create_auth_token(self.user) self.auth_headers = {"HTTP_AUTHORIZATION": f"Token {self.token}"} self.user2 = self.create_user() self.reminder2 = Reminder(creator=self.user2) self.reminder2.save() def test_user_get_anon(self): self.validate_response( self.endpoint, expected_status_code=status.HTTP_401_UNAUTHORIZED, ) def test_user_normal(self): reminder = Reminder(creator=self.user) reminder.save() self.validate_response( self.endpoint, headers=self.auth_headers, expected_response_payload=[ { "created_at": reminder.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), "creator": self.user.pk, "description": reminder.description, "id": reminder.pk, "location": reminder.location, "notified_at": reminder.notified_at, "notify_at": reminder.notify_at, "participants": [], "status": ReminderStatus.CREATED.name, "title": reminder.title, }, ], ) def test_post_anon(self): self.validate_response( self.endpoint, method="post", expected_status_code=status.HTTP_401_UNAUTHORIZED, ) def test_post_normal(self): placeholder = urandom(4).hex() title = f"title_{placeholder}" self.validate_response( self.endpoint, method="post", headers=self.auth_headers, data={ "title": title, }, expected_status_code=status.HTTP_201_CREATED, ) reminders = Reminder.objects.filter(title=title) self.assertEqual(1, reminders.count()) reminder = reminders.first() self.assertTrue(reminder) self.assertEqual(title, reminder.title) self.assertEqual(self.user, reminder.creator) self.assertTrue(near(utcnow(), reminder.created_at, 4)) self.assertTrue(ReminderStatus.CREATED.name, reminder.status) def test_patch_anon(self): reminder = Reminder(creator=self.user) reminder.save() self.validate_response( f"{self.endpoint}{reminder.pk}/", method="patch", expected_status_code=status.HTTP_401_UNAUTHORIZED, ) def test_patch_normal_title(self): rem = Reminder(creator=self.user, title="xxx") rem.save() self.validate_response( f"{self.endpoint}{rem.pk}/", method="patch", headers=self.auth_headers, data={ "title": "yyy", }, expected_status_code=status.HTTP_200_OK, ) reminders = Reminder.objects.filter(title="yyy") self.assertEqual(1, reminders.count()) reminder = reminders.first() self.assertTrue(reminder) self.assertEqual("yyy", reminder.title) self.assertEqual(rem.creator, reminder.creator) self.assertListEqual(list(rem.participants.all()), list(reminder.participants.all())) self.assertEqual(rem.pk, reminder.pk) def test_patch_normal_readonly(self): reminder = Reminder(creator=self.user) reminder.save() dtm = utcnow() - timedelta(days=1) dataset = { "created_at": dtm, "creator": self.user2.pk, "notified_at": dtm, } old = {_f: getattr(reminder, _f) for _f in dataset} for field, value in dataset.items(): self.validate_response( f"{self.endpoint}{reminder.pk}/", method="patch", headers=self.auth_headers, data={field: value}, expected_status_code=status.HTTP_200_OK, ) reminder.refresh_from_db() for field, expected_value in old.items(): self.assertEqual(expected_value, getattr(reminder, field), field) def test_patch_status(self): reminder = Reminder(creator=self.user) reminder.save() self.validate_response( f"{self.endpoint}{reminder.pk}/", method="patch", headers=self.auth_headers, data={"status": ReminderStatus.NOTIFIED.name}, expected_status_code=status.HTTP_400_BAD_REQUEST, ) reminder.refresh_from_db() self.assertEqual("CREATED", reminder.status) self.validate_response( f"{self.endpoint}{reminder.pk}/", method="patch", headers=self.auth_headers, data={"status": ReminderStatus.DONE.name}, expected_status_code=status.HTTP_200_OK, ) reminder.refresh_from_db() self.assertEqual("DONE", reminder.status) def test_patch_not_own(self): self.validate_response( f"{self.endpoint}{self.reminder2.pk}/", method="patch", headers=self.auth_headers, data={"title": "xxx"}, expected_status_code=status.HTTP_404_NOT_FOUND, ) def test_delete_anon(self): self.validate_response( f"{self.endpoint}{self.reminder2.pk}/", method="delete", expected_status_code=status.HTTP_401_UNAUTHORIZED, ) def test_delete_normal(self): reminder = Reminder(creator=self.user) reminder.save() self.validate_response( f"{self.endpoint}{reminder.pk}/", method="delete", headers=self.auth_headers, expected_status_code=status.HTTP_204_NO_CONTENT, ) with self.assertRaises(Reminder.DoesNotExist): reminder.refresh_from_db()