def test_sudo_required(self): urls = [ urlresolvers.reverse("user-twofactor-setup"), urlresolvers.reverse("user-twofactor-backup"), urlresolvers.reverse("user-twofactor-disable"), urlresolvers.reverse("user-twofactor-qrcode"), ] grant_otp(self.client, self.user) for url in urls: response = self.client.get(url) try: self.assertEqual(response.status_code, 302) self.assertEqual(response["Location"], "{}?next={}".format(urlresolvers.reverse("user-sudo"), url)) except AssertionError as exp: raise AssertionError("{} did not redirect correcrlty: {}".format(url, exp)) grant_sudo(self.client) for url in urls: response = self.client.get(url) try: self.assertIn(response.status_code, [200, 404]) except AssertionError as exp: raise AssertionError("{} did not give an expected response code: {}".format(url, exp))
def test_post(self): grant_sudo(self.client) other_user = factories.UserFactory(username=self.user.username + "2") new_username = self.user.username + "1" old_username = self.user.username other_username = other_user.username user_count = get_user_model().objects.count() # invalid form params = {"username": new_username, "username2": old_username} response = self.client.post(self.get_url(), params) self.user.refresh_from_db() other_user.refresh_from_db() # username should remain unchanged self.assertEqual(response.status_code, 200) self.assertEqual(self.user.username, old_username) self.assertEqual(other_user.username, other_username) self.assertEqual(get_user_model().objects.count(), user_count) # valid form params = {"username": new_username, "username2": new_username} response = self.client.post(self.get_url(), params) self.user.refresh_from_db() other_user.refresh_from_db() # username should changed self.assertEqual(response.status_code, 302) self.assertEqual(response["Location"], urlresolvers.reverse("user-settings")) self.assertEqual(self.user.username, new_username) self.assertEqual(other_user.username, other_username) self.assertEqual(get_user_model().objects.count(), user_count)
def test_sudo_required(self): test_urls = [ urls.reverse("user-twofactor-backup"), urls.reverse("user-twofactor-backup-download"), urls.reverse("user-twofactor-disable"), urls.reverse("user-twofactor-qrcode"), urls.reverse("user-twofactor-setup"), ] grant_otp(self.client, self.user) for url in test_urls: response = self.client.get(url) try: self.assertEqual(response.status_code, 302) self.assertEqual( response["Location"], "{}?next={}".format(urls.reverse("user-sudo"), url)) except AssertionError as exp: raise AssertionError( "{} did not redirect correcrlty: {}".format(url, exp)) grant_sudo(self.client) for url in test_urls: response = self.client.get(url) try: self.assertIn(response.status_code, [200, 404]) except AssertionError as exp: raise AssertionError( "{} did not give an expected response code: {}".format( url, exp))
def test_get(self): response = self.client.get(self.get_url()) self.assertEqual(response.status_code, 302) self.assertEqual(response["Location"], "{}?next={}".format(urlresolvers.reverse("user-sudo"), self.get_url())) grant_sudo(self.client) response = self.client.get(self.get_url()) self.assertEqual(response.status_code, 200)
def test_url(self): assert self.client.login(username=self.user.username, password="******", request=MockRequest(self.user)),\ "Could not log in" grant_otp(self.client, self.user) grant_sudo(self.client) response = self.client.get(urlresolvers.reverse("admin:domains:edit", kwargs={"domain_pk": self.domain.pk})) self.assertEqual(response.resolver_match.func, admin.domain_admin_edit) self.assertEqual(response.status_code, 200)
def test_url(self): assert self.client.login(username=self.user.username, password="******", request=MockRequest(self.user)),\ "Could not log in" grant_otp(self.client, self.user) grant_sudo(self.client) response = self.client.get(urlresolvers.reverse("admin:blog:create")) self.assertEqual(response.resolver_match.func, views.blog_admin_create) self.assertEqual(response.status_code, 200)
def test_url(self): assert self.client.login(username=self.user.username, password="******", request=MockRequest(self.user)),\ "Could not log in" grant_otp(self.client, self.user) grant_sudo(self.client) response = self.client.get(urls.reverse("admin:domains:index")) self.assertEqual(response.resolver_match.func, admin.domain_admin_index) self.assertEqual(response.status_code, 200)
def test_url(self): """Check URLs are attached to the correct view""" assert self.client.login(username=self.user.username, password="******", request=MockRequest(self.user)), \ "Could not log in" grant_otp(self.client, self.user) grant_sudo(self.client) response = self.client.get(reverse("admin:delete-page", kwargs={"page_pk": 1})) self.assertEqual(response.resolver_match.func, views.delete_page) self.assertEqual(response.status_code, 404)
def setUp(self): self.user = factories.UserFactory() login = self.client.login(username=self.user.username, password="******", request=MockRequest(self.user)) if not login: raise Exception("Could not log in") grant_sudo(self.client) grant_otp(self.client, self.user)
def test_url(self): """Check URLs are attached to the correct view""" assert self.client.login(username=self.user.username, password="******", request=MockRequest(self.user)), \ "Could not log in" grant_otp(self.client, self.user) grant_sudo(self.client) response = self.client.get( reverse("admin:delete-page", kwargs={"page_pk": 1})) self.assertEqual(response.resolver_match.func, views.delete_page) self.assertEqual(response.status_code, 404)
def test_url(self): assert self.client.login(username=self.user.username, password="******", request=MockRequest(self.user)),\ "Could not log in" grant_otp(self.client, self.user) grant_sudo(self.client) post = BlogPostFactory(author=self.user) response = self.client.get(urlresolvers.reverse("admin:blog:delete", kwargs={"blog_pk": post.pk})) self.assertEqual(response.resolver_match.func, views.blog_admin_delete) self.assertEqual(response.status_code, 200)
def test_url(self): """Check URLs are attached to the correct view""" assert self.client.login(username=self.user.username, password="******", request=MockRequest(self.user)), \ "Could not log in" grant_otp(self.client, self.user) grant_sudo(self.client) parent_pk = models.HelpIndex.objects.get().pk response = self.client.get(reverse("admin:edit-page", kwargs={"page_pk": parent_pk})) self.assertEqual(response.resolver_match.func, views.edit_page) self.assertEqual(response.status_code, 200)
def test_url(self): """Check URLs are attached to the correct view""" assert self.client.login(username=self.user.username, password="******", request=MockRequest(self.user)), \ "Could not log in" grant_otp(self.client, self.user) grant_sudo(self.client) parent_pk = models.HelpIndex.objects.get().pk response = self.client.get( reverse("admin:edit-page", kwargs={"page_pk": parent_pk})) self.assertEqual(response.resolver_match.func, views.edit_page) self.assertEqual(response.status_code, 200)
def test_url(self): assert self.client.login(username=self.user.username, password="******", request=MockRequest(self.user)),\ "Could not log in" grant_otp(self.client, self.user) grant_sudo(self.client) question = QuestionFactory() response = self.client.get( urlresolvers.reverse("admin:tickets:response", kwargs={"question_pk": question.pk})) self.assertEqual(response.resolver_match.func, views.question_admin_response) self.assertEqual(response.status_code, 200)
def test_url(self): assert self.client.login(username=self.user.username, password="******", request=MockRequest(self.user)),\ "Could not log in" grant_otp(self.client, self.user) grant_sudo(self.client) request_obj = factories.RequestFactory() response = self.client.get( urlresolvers.reverse("admin:requests:edit", kwargs={"request_pk": request_obj.pk})) self.assertEqual(response.resolver_match.func, admin.request_admin_edit) self.assertEqual(response.status_code, 200)
def test_missing_mgmt_data(self): grant_sudo(self.client) good_data = { "two_factor_setup_view-current_step": "generator", "generator-token": "123456", } response = self.client.post(urlresolvers.reverse("user-twofactor-setup"), good_data) # form was validated and *form* errors returned self.assertEqual(response.status_code, 200) bad_data = { "generator-token": "123456", } response = self.client.post(urlresolvers.reverse("user-twofactor-setup"), bad_data) # Bad request, but no exception generated self.assertEqual(response.status_code, 400)
def test_otp_required(self): urls = [ urlresolvers.reverse("user-twofactor-backup"), urlresolvers.reverse("user-twofactor-disable"), ] grant_sudo(self.client) for url in urls: response = self.client.get(url) try: self.assertEqual(response.status_code, 302) self.assertEqual( response["Location"], "{}?next={}".format(urlresolvers.reverse("user-login"), url)) except AssertionError as exp: raise AssertionError( "{} did not give an expected response code: {}".format( url, exp))
def test_missing_mgmt_data(self): grant_sudo(self.client) good_data = { "two_factor_setup_view-current_step": "generator", "generator-token": "123456", } response = self.client.post(urls.reverse("user-twofactor-setup"), good_data) # form was validated and *form* errors returned self.assertEqual(response.status_code, 200) bad_data = { "generator-token": "123456", } response = self.client.post(urls.reverse("user-twofactor-setup"), bad_data) # Bad request, but no exception generated self.assertEqual(response.status_code, 400)