From 1b4bb09e0be1ca3277afc5ba6b7443ef80859219 Mon Sep 17 00:00:00 2001 From: ViolanteCodes Date: Wed, 27 Jan 2021 14:32:57 -0500 Subject: [PATCH 1/5] added test skeleton to fill in for gitlab create views and finished first test --- anonticket/tests.py | 72 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/anonticket/tests.py b/anonticket/tests.py index 3fb4c4f..ac1717f 100644 --- a/anonticket/tests.py +++ b/anonticket/tests.py @@ -2,7 +2,7 @@ from django.conf import settings from django.test import SimpleTestCase, Client, tag from test_plus.test import TestCase, CBVTestCase from django.urls import reverse, resolve -from anonticket.models import UserIdentifier, Project, Issue +from anonticket.models import UserIdentifier, Project, Issue, GitlabAccountRequest from django.contrib.auth.models import User, Group, Permission # from django.views.generic import TemplateView, DetailView, CreateView, UpdateView from anonticket.views import * @@ -475,6 +475,76 @@ class TestNotesViews(TestCase): response = self.client.post(url, form_data) self.assertRedirects(response, expected_url) +@tag('gitlab') +class TestGitlabAccountRequestViews(TestCase): + """Test the views associated with user-created Gitlab + account requests.""" + + def setUp(self): + """Set up a project, user identifier, and issue in the test database.""" + # Setup project + new_user = UserIdentifier.objects.create( + user_identifier = 'duo-atlas-hypnotism-curry-creatable-rubble' + ) + gitlab_url_no_user = reverse('create-gitlab-no-user') + gitlab_url_current_user = reverse( + 'create-gitlab-with-user', args=[new_user.user_identifier]) + self.client=Client() + self.working_user = new_user + self.gitlab_url_no_user = gitlab_url_no_user + self.gitlab_url_current_user = gitlab_url_current_user + + def test_gitlab_account_create_GET_no_user(self): + """Test that the GitlabAccountRequestCreateView GET works correctly + with no user_string in URL path.""" + response = self.client.get(self.gitlab_url_no_user) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed( + 'anonticket/gitlabaccountrequest_user_create.html') + self.assertInHTML( + needle="""

You are not logged in at this time.

""", + haystack="""
+

You are not logged in + at this time.

""") + + def test_gitlab_account_create_POST_no_user(self): + """Test that the GitlabAccountRequestCreateView POST works correctly + with no user_string in URL path.""" + pass + + def test_gitlab_account_create_GET_new_user(self): + """Test that the GitlabAccountRequestCreateView GET works correctly + with a user that does NOT have a current database entry.""" + pass + + def test_gitlab_account_create_GET_current_user(self): + """Test that the GitlabAccountRequestCreateView GET works correctly + with a user that does have a current database entry.""" + pass + + def test_gitlab_account_create_POST_new_user(self): + """Test that the GitlabAccountRequestCreateView POST works correctly + with a user that does NOT have a current database entry.""" + pass + + def test_gitlab_account_create_POST_current_user_no_requests(self): + """Test that the GitlabAccountRequestCreateView GET works correctly + with a user that does have a current database entry but no + current GitlabAccountRequests.""" + pass + + def test_gitlab_account_create_POST_current_user_rejected_request(self): + """Test that the GitlabAccountRequestCreateView GET works correctly + with a user that DOES have a current database entry AND has + a GitlabAccountRequest, but no PENDING request.""" + pass + + def test_gitlab_account_create_POST_current_user_pending_request(self): + """Test that the GitlabAccountRequestCreateView GET works correctly + with a user that DOES have a current database entry AND has + a GitlabAccountRequest AND has a PENDING request.""" + pass + @tag('other_with_db') class TestViewsOtherWithDatabase(TestCase): """Test the functions in views.py not directly related to one of the above -- GitLab From a112e0a9c113321fb3a723056471f7adf8442456 Mon Sep 17 00:00:00 2001 From: ViolanteCodes Date: Wed, 27 Jan 2021 15:12:00 -0500 Subject: [PATCH 2/5] added test for gitlab_account_create GET and POST with no user and GET with user already in database --- anonticket/tests.py | 48 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/anonticket/tests.py b/anonticket/tests.py index ac1717f..f6495ed 100644 --- a/anonticket/tests.py +++ b/anonticket/tests.py @@ -489,10 +489,12 @@ class TestGitlabAccountRequestViews(TestCase): gitlab_url_no_user = reverse('create-gitlab-no-user') gitlab_url_current_user = reverse( 'create-gitlab-with-user', args=[new_user.user_identifier]) + success_url_no_user = reverse('created-no-user') self.client=Client() self.working_user = new_user self.gitlab_url_no_user = gitlab_url_no_user self.gitlab_url_current_user = gitlab_url_current_user + self.success_url_no_user = success_url_no_user def test_gitlab_account_create_GET_no_user(self): """Test that the GitlabAccountRequestCreateView GET works correctly @@ -510,17 +512,57 @@ class TestGitlabAccountRequestViews(TestCase): def test_gitlab_account_create_POST_no_user(self): """Test that the GitlabAccountRequestCreateView POST works correctly with no user_string in URL path.""" - pass + form_data = { + 'username': 'test_username_number_one', + 'email' : 'test@test.com', + 'reason' : "I can't wait to collaborate with TOR!", + } + response = self.client.post(self.gitlab_url_no_user, form_data) + self.assertEqual(response.status_code, 302) + self.assertRedirects(response, self.success_url_no_user, 302) + self.assertTemplateUsed('anonticket/create_issue_success.html') + new_gl_request = GitlabAccountRequest.objects.get( + username='test_username_number_one') + self.assertEqual(new_gl_request.email, 'test@test.com') + self.assertEqual( + new_gl_request.reason, "I can't wait to collaborate with TOR!") def test_gitlab_account_create_GET_new_user(self): """Test that the GitlabAccountRequestCreateView GET works correctly with a user that does NOT have a current database entry.""" - pass + new_user_string = 'nastily-arming-canon-calcium-footsie-jaundice' + new_url = reverse( + 'create-gitlab-with-user', args=[new_user_string]) + response = self.client.get(new_url) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed( + 'anonticket/gitlabaccountrequest_user_create.html') + self.assertInHTML( + needle="""

You are logged in as: + + nastily-arming-canon-calcium-footsie-jaundice

""", + haystack="""
+

You are logged in as: + + nastily-arming-canon-calcium-footsie-jaundice +

""") def test_gitlab_account_create_GET_current_user(self): """Test that the GitlabAccountRequestCreateView GET works correctly with a user that does have a current database entry.""" - pass + response = self.client.get(self.gitlab_url_current_user) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed( + 'anonticket/gitlabaccountrequest_user_create.html') + self.assertInHTML( + needle="""

You are logged in as: + + duo-atlas-hypnotism-curry-creatable-rubble

""", + haystack="""
+

You are logged in as: + + duo-atlas-hypnotism-curry-creatable-rubble +

""") def test_gitlab_account_create_POST_new_user(self): """Test that the GitlabAccountRequestCreateView POST works correctly -- GitLab From 7cdf78c1272daa2f1284910a94006a43d91d9dc0 Mon Sep 17 00:00:00 2001 From: ViolanteCodes Date: Wed, 27 Jan 2021 15:30:06 -0500 Subject: [PATCH 3/5] fnished tests for Post new_user and Post current user_no_requests --- anonticket/tests.py | 46 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/anonticket/tests.py b/anonticket/tests.py index f6495ed..47b736c 100644 --- a/anonticket/tests.py +++ b/anonticket/tests.py @@ -490,11 +490,17 @@ class TestGitlabAccountRequestViews(TestCase): gitlab_url_current_user = reverse( 'create-gitlab-with-user', args=[new_user.user_identifier]) success_url_no_user = reverse('created-no-user') + new_user_string = 'nastily-arming-canon-calcium-footsie-jaundice' + gitlab_url_new_user = reverse( + 'create-gitlab-with-user', args=[new_user_string] + ) self.client=Client() self.working_user = new_user self.gitlab_url_no_user = gitlab_url_no_user self.gitlab_url_current_user = gitlab_url_current_user + self.gitlab_url_new_user = gitlab_url_new_user self.success_url_no_user = success_url_no_user + self.new_user_string = new_user_string def test_gitlab_account_create_GET_no_user(self): """Test that the GitlabAccountRequestCreateView GET works correctly @@ -530,9 +536,9 @@ class TestGitlabAccountRequestViews(TestCase): def test_gitlab_account_create_GET_new_user(self): """Test that the GitlabAccountRequestCreateView GET works correctly with a user that does NOT have a current database entry.""" - new_user_string = 'nastily-arming-canon-calcium-footsie-jaundice' + new_url = reverse( - 'create-gitlab-with-user', args=[new_user_string]) + 'create-gitlab-with-user', args=[self.new_user_string]) response = self.client.get(new_url) self.assertEqual(response.status_code, 200) self.assertTemplateUsed( @@ -567,13 +573,45 @@ class TestGitlabAccountRequestViews(TestCase): def test_gitlab_account_create_POST_new_user(self): """Test that the GitlabAccountRequestCreateView POST works correctly with a user that does NOT have a current database entry.""" - pass + form_data = { + 'username': 'test_username_number_two', + 'email' : 'test@test.com', + 'reason' : "I can't wait to collaborate with TOR!", + } + success_url_new_user = reverse( + 'issue-created', args=[self.new_user_string]) + response = self.client.post( + self.gitlab_url_new_user, form_data) + self.assertEqual(response.status_code, 302) + self.assertRedirects(response, success_url_new_user, 302) + self.assertTemplateUsed('anonticket/create_issue_success.html') + new_gl_request = GitlabAccountRequest.objects.get( + username='test_username_number_two') + self.assertEqual(new_gl_request.email, 'test@test.com') + self.assertEqual( + new_gl_request.reason, "I can't wait to collaborate with TOR!") def test_gitlab_account_create_POST_current_user_no_requests(self): """Test that the GitlabAccountRequestCreateView GET works correctly with a user that does have a current database entry but no current GitlabAccountRequests.""" - pass + form_data = { + 'username': 'test_username_number_three', + 'email' : 'test@test.com', + 'reason' : "I can't wait to collaborate with TOR!", + } + success_url_current_user = reverse( + 'issue-created', args=[self.working_user.user_identifier]) + response = self.client.post( + self.gitlab_url_current_user, form_data) + self.assertEqual(response.status_code, 302) + self.assertRedirects(response, success_url_current_user, 302) + self.assertTemplateUsed('anonticket/create_issue_success.html') + new_gl_request = GitlabAccountRequest.objects.get( + username="test_username_number_three") + self.assertEqual(new_gl_request.email, 'test@test.com') + self.assertEqual( + new_gl_request.reason, "I can't wait to collaborate with TOR!") def test_gitlab_account_create_POST_current_user_rejected_request(self): """Test that the GitlabAccountRequestCreateView GET works correctly -- GitLab From 4c12ec5e7a9e2352fd1cbf4c58081f6a10e8914c Mon Sep 17 00:00:00 2001 From: ViolanteCodes Date: Wed, 27 Jan 2021 15:30:40 -0500 Subject: [PATCH 4/5] removed redirect line that was causing the CreateGitlabAccountRequest view to redirect to success view before object was created --- anonticket/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/anonticket/views.py b/anonticket/views.py index 57af705..00b132b 100644 --- a/anonticket/views.py +++ b/anonticket/views.py @@ -284,7 +284,6 @@ class GitlabAccountRequestCreateView( # if the User Identifier exists, but does not have a pending GL request, make one. else: form.instance.linked_user = url_user - return redirect ('issue-created', user_identifier=user_identifier) # If the user_identifier doesn't exist, create it and save the request. except ObjectDoesNotExist: new_user = UserIdentifier(user_identifier=user_identifier) -- GitLab From f9461b1dba772bb969b8967ce4d07e58c90e65f4 Mon Sep 17 00:00:00 2001 From: ViolanteCodes Date: Wed, 27 Jan 2021 15:54:28 -0500 Subject: [PATCH 5/5] added final tests for CreateGitlabAccountRequestView --- anonticket/tests.py | 70 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/anonticket/tests.py b/anonticket/tests.py index 47b736c..681078b 100644 --- a/anonticket/tests.py +++ b/anonticket/tests.py @@ -617,13 +617,79 @@ class TestGitlabAccountRequestViews(TestCase): """Test that the GitlabAccountRequestCreateView GET works correctly with a user that DOES have a current database entry AND has a GitlabAccountRequest, but no PENDING request.""" - pass + rejected_request = GitlabAccountRequest.objects.create( + username='test_username_number_four', + linked_user=self.working_user, + reviewer_status='R', + email='test@test.com', + reason="""I can't wait to collaborate with TOR!""" + ) + form_data = { + 'username': 'test_username_number_five', + 'email' : 'test@test.com', + 'reason' : "I can't wait to collaborate with TOR!", + } + success_url_current_user = reverse( + 'issue-created', args=[self.working_user.user_identifier]) + response = self.client.post( + self.gitlab_url_current_user, form_data) + self.assertEqual(response.status_code, 302) + self.assertRedirects(response, success_url_current_user, 302) + self.assertTemplateUsed('anonticket/create_issue_success.html') + new_gl_request = GitlabAccountRequest.objects.get( + username="test_username_number_five") + self.assertEqual(new_gl_request.email, 'test@test.com') + self.assertEqual( + new_gl_request.reason, "I can't wait to collaborate with TOR!") def test_gitlab_account_create_POST_current_user_pending_request(self): """Test that the GitlabAccountRequestCreateView GET works correctly with a user that DOES have a current database entry AND has a GitlabAccountRequest AND has a PENDING request.""" - pass + pending_request = GitlabAccountRequest.objects.create( + username='test_username_number_six', + linked_user=self.working_user, + reviewer_status='P', + email='test@test.com', + reason="""I can't wait to collaborate with TOR!""" + ) + form_data = { + 'username': 'test_username_number_seven', + 'email' : 'test@test.com', + 'reason' : "I can't wait to collaborate with TOR!", + } + failure_url_current_user = reverse( + 'cannot-create-with-user', args=[self.working_user.user_identifier]) + response = self.client.post( + self.gitlab_url_current_user, form_data) + self.assertEqual(response.status_code, 302) + self.assertRedirects(response, failure_url_current_user, 302) + self.assertTemplateUsed('anonticket/cannot_create.html') + # assert object wasn't created + try_to_get_new_request = GitlabAccountRequest.objects.filter( + username='test_username_number_seven') + self.assertEqual(len(try_to_get_new_request), 0) + + def test_gitlab_account_create_POST_invalid_user(self): + """Test that CreateGitlabAccountRequestView does not allow for + creation of db objects with a bad user.""" + + bad_username_string = 'word-word-word-word-word-word' + form_data = { + 'username': 'test_username_number_eight', + 'email' : 'test@test.com', + 'reason' : "I can't wait to collaborate with TOR!", + } + create_url = reverse('create-gitlab-with-user', args=[bad_username_string]) + login_error_url = reverse('user-login-error', args=[bad_username_string]) + response = self.client.post(create_url, form_data) + self.assertEqual(response.status_code, 302) + self.assertRedirects(response, login_error_url, 302) + self.assertTemplateUsed('anonticket/user_login_error.html') + # assert object wasn't created + try_to_get_new_request = GitlabAccountRequest.objects.filter( + username='test_username_number_eight') + self.assertEqual(len(try_to_get_new_request), 0) @tag('other_with_db') class TestViewsOtherWithDatabase(TestCase): -- GitLab