def test_user_agrees_multiple_times(self): login_response = self.client.post(reverse('login'), { 'username': '******', 'password': '******', }) self.assertTrue(login_response) response = self.client.post(reverse('tos_check_tos'), {'accept': 'accept'}) self.assertEqual(response.status_code, 302) self.assertEqual( UserAgreement.objects.filter(user=self.user2).count(), 1) response = self.client.post(reverse('tos_check_tos'), {'accept': 'accept'}) self.assertEqual(response.status_code, 302) self.assertEqual( UserAgreement.objects.filter(user=self.user2).count(), 1) response = self.client.post(reverse('tos_check_tos'), {'accept': 'accept'}) self.assertEqual(response.status_code, 302) self.assertEqual( UserAgreement.objects.filter(user=self.user2).count(), 1)
def setUp(self): # User that has agreed to TOS self.user1 = get_runtime_user_model().objects.create_user( 'user1', '*****@*****.**', 'user1pass') # User that has not yet agreed to TOS self.user2 = get_runtime_user_model().objects.create_user( 'user2', '*****@*****.**', 'user2pass') self.user3 = get_runtime_user_model().objects.create_user( 'user3', '*****@*****.**', 'user3pass') self.tos1 = TermsOfService.objects.create( content="first edition of the terms of service", active=True) self.tos2 = TermsOfService.objects.create( content="second edition of the terms of service", active=False) self.login_url = getattr(settings, 'LOGIN_URL', '/login/') UserAgreement.objects.create(terms_of_service=self.tos1, user=self.user1) self.redirect_page = '{0}?{1}={2}'.format( reverse('tos_check_tos'), REDIRECT_FIELD_NAME, reverse('index'), )
def test_accept_after_middleware_redirects_properly(self): self.client.login(username='******', password='******') response = self.client.get(reverse('index'), follow=True) self.assertRedirects(response, self.redirect_page) # Agree response = self.client.post(self.redirect_page, {'accept': 'accept'}) # Confirm redirects back to the index page self.assertEqual(response.status_code, 302) self.assertEqual(response.url.replace('http://testserver', ''), str(reverse('index')))
def test_middleware_redirects(self): """ User that hasn't accepted TOS should be redirected to confirm. Also make sure confirm works. """ self.client.login(username='******', password='******') response = self.client.get(reverse('index')) self.assertRedirects(response, self.redirect_page) # Make sure confirm works after middleware redirect. response = self.client.post(reverse('tos_check_tos'), {'accept': 'accept'}) # Confirm redirects. self.assertEqual(response.status_code, 302)
def test_reject_agreement(self): self.assertFalse(has_user_agreed_latest_tos(self.user2)) response = self.client.post( self.login_url, dict(username='******', password='******')) self.assertContains(response, "first edition of the terms of service") url = reverse('tos_check_tos') response = self.client.post(url, {'accept': 'reject'}) self.assertFalse(has_user_agreed_latest_tos(self.user2))
def test_skip_for_user(self): cache = get_cache(getattr(settings, 'TOS_CACHE_NAME', 'default')) key_version = cache.get('django:tos:key_version') cache.set('django:tos:skip_tos_check:{0}'.format(str(self.user1.id)), True, version=key_version) self.client.login(username='******', password='******') response = self.client.get(reverse('index')) self.assertEqual(response.status_code, 200)
def test_invalidate_cache_on_accept_fix_redirect_loop(self): """ Make sure accepting doesnt send you right back to tos page. """ self.assertFalse( UserAgreement.objects.filter(terms_of_service=self.tos1, user=self.user2).exists()) self.client.login(username='******', password='******') response = self.client.get(reverse('index')) self.assertRedirects(response, self.redirect_page) # Make sure confirm works after middleware redirect. response = self.client.post(reverse('tos_check_tos'), {'accept': 'accept'}) self.assertTrue( UserAgreement.objects.filter(terms_of_service=self.tos1, user=self.user2).exists()) response = self.client.get(reverse('index')) self.assertEqual(response.status_code, 200) self.assertIn('index', str(response.content))
def test_bump_new_agreement(self): # Change the tos self.tos2.active = True self.tos2.save() # is user1 agreed now? self.assertFalse(has_user_agreed_latest_tos(self.user1)) # user1 agrees again response = self.client.post( self.login_url, dict(username='******', password='******')) self.assertContains(response, "second edition of the terms of service") self.assertFalse(has_user_agreed_latest_tos(self.user2)) url = reverse('tos_check_tos') response = self.client.post(url, {'accept': 'accept'}) self.assertTrue(has_user_agreed_latest_tos(self.user1))
def test_anonymous_user_200(self): response = self.client.get(reverse('index')) self.assertEqual(response.status_code, 200)
def test_middleware_doesnt_redirect(self): """User that has accepted TOS should get 200.""" self.client.login(username='******', password='******') response = self.client.get(reverse('index')) self.assertEqual(response.status_code, 200)