class DiveDetailTests(TestCase): def setUp(self): self.user = UserFactory() self.dive = DiveFactory(created_by=self.user) def test_dive_detail_view_resolves_divedetailview(self): view = resolve(self.dive.get_absolute_url()) self.assertEqual(view.func.__name__, DiveDetailView.as_view().__name__) def test_dive_detail_redirects_for_logged_out_user(self): response = self.client.get(self.dive.get_absolute_url()) no_response = self.client.get('/dive/123456/') self.assertEqual(response.status_code, 302) self.assertTemplateNotUsed(response, 'dive/dive_detail.html') self.assertEqual(no_response.status_code, 404) def test_dive_detail_works_for_logged_in_user(self): self.client.force_login(self.user) response = self.client.get(self.dive.get_absolute_url()) no_response = self.client.get('/dive/123456/') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'dive/dive_detail.html') self.assertContains(response, 'Log Book') self.assertContains(response, self.dive.description) self.assertNotContains(response, 'Hi I should not be on this page!') self.assertEqual(no_response.status_code, 404)
def test_dive_list_shows_dives_of_logged_in_user_only(self): user_1 = UserFactory() user_2 = UserFactory() _ = DiveFactory(created_by=user_1) _ = DiveFactory(created_by=user_1) _ = DiveFactory(created_by=user_2) self.client.force_login(user_1) response = self.client.get(reverse('dive_list')) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context_data['dive_list']), 2)
def test_dive_list_view_does_not_shows_dives_of_other_user(self): user1 = UserFactory() user2 = UserFactory() _ = DiveFactory(created_by=user1) _ = DiveFactory(created_by=user1) _ = DiveFactory(created_by=user1) self.client.force_login(user2) response = self.client.get(reverse('dive_list')) self.assertContains(response, 'You do not have any dives registered yet!') self.assertEqual(response.status_code, 200)
def test_dive_create_with_valid_post_data_works(self): self.client.force_login(self.user) self.assertEqual(Dive.objects.count(), 1) data = model_to_dict(DiveFactory()) response = self.client.post(reverse('dive_create'), data=data) self.assertEqual(response.status_code, 200) self.assertEqual(Dive.objects.count(), 2)
class DiveUpdateTests(TestCase): def setUp(self): self.user = UserFactory() self.unauthorized_user = UserFactory() self.dive = DiveFactory(created_by=self.user) def test_dive_update_for_non_existing_dive_routes_to_not_found(self): pass def test_dive_update_redirects_for_user_who_did_not_create_the_dive(self): pass def test_dive_update_works_for_super_user(self): pass def test_dive_update_resolves_diveupdateview(self): view = resolve(self.dive.get_update_url()) self.assertEqual(view.func.__name__, DiveUpdate.as_view().__name__) def test_dive_update_redirects_for_logged_out_user(self): response = self.client.get(self.dive.get_update_url()) no_response = self.client.get('/dive/123456/update/') self.assertEqual(response.status_code, 302) self.assertTemplateNotUsed(response, 'dive/dive_update_form.html') self.assertEqual(no_response.status_code, 404) def test_dive_update_works_for_logged_in_user(self): self.client.force_login(self.user) response = self.client.get(self.dive.get_update_url()) no_response = self.client.get('/dive/123456/update/') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'dive/dive_update_form.html') self.assertContains(response, 'Log Book') self.assertContains(response, 'Update') self.assertNotContains(response, 'Hi I should not be on this page!') self.assertEqual(no_response.status_code, 404)
def setUp(self): self.superuser = get_user_model().objects.create_superuser( username='******', email='*****@*****.**', password='******') self.user_1 = UserFactory() self.user_2 = UserFactory() self.dive_1 = DiveFactory(created_by=self.user_1) self.dive_2 = DiveFactory(created_by=self.user_2) self.dive_3 = DiveFactory(created_by=self.user_2)
def setUp(self): self.user = UserFactory() self.dive = DiveFactory(created_by=self.user)
class DiveModelTests(TestCase): def setUp(self): self.superuser = get_user_model().objects.create_superuser( username='******', email='*****@*****.**', password='******') self.user_1 = UserFactory() self.user_2 = UserFactory() self.dive_1 = DiveFactory(created_by=self.user_1) self.dive_2 = DiveFactory(created_by=self.user_2) self.dive_3 = DiveFactory(created_by=self.user_2) def test_dive_model_creation(self): self.assertEqual(Dive.objects.count(), 3) self.assertEqual(self.dive_1.created_by, self.user_1) self.assertEqual(self.dive_2.created_by, self.user_2) self.assertEqual(self.dive_3.created_by, self.user_2) def test_most_recent_dives_are_listed_first(self): yesterday = timezone.now() + timezone.timedelta(days=-1) last_week = timezone.now() + timezone.timedelta(days=-7) today = timezone.now() self.dive_1.date = last_week self.dive_2.date = yesterday self.dive_3.date = today self.dive_1.save() self.dive_2.save() self.dive_3.save() latest_dive = Dive.objects.all()[0] self.assertEqual(latest_dive, self.dive_3) def test_only_user_who_created_a_dive_can_update_it(self): self.assertTrue(self.dive_1.can_update(self.user_1)) self.assertTrue(self.dive_2.can_update(self.user_2)) self.assertTrue(self.dive_3.can_update(self.user_2)) self.assertFalse(self.dive_1.can_update(self.user_2)) self.assertFalse(self.dive_2.can_update(self.user_1)) self.assertFalse(self.dive_3.can_update(self.user_1)) def test_only_user_who_created_a_dive_can_delete_it(self): self.assertTrue(self.dive_1.can_delete(self.user_1)) self.assertTrue(self.dive_2.can_delete(self.user_2)) self.assertTrue(self.dive_3.can_delete(self.user_2)) self.assertFalse(self.dive_1.can_delete(self.user_2)) self.assertFalse(self.dive_2.can_delete(self.user_1)) self.assertFalse(self.dive_3.can_delete(self.user_1)) def test_superuser_can_update_any_dive(self): self.assertTrue(self.dive_1.can_update(self.superuser)) self.assertTrue(self.dive_2.can_update(self.superuser)) self.assertTrue(self.dive_3.can_update(self.superuser)) def test_superuser_can_delete_any_dive(self): self.assertTrue(self.dive_1.can_delete(self.superuser)) self.assertTrue(self.dive_2.can_delete(self.superuser)) self.assertTrue(self.dive_3.can_delete(self.superuser))