diff --git a/app/my_presentation.pptx b/app/my_presentation.pptx new file mode 100644 index 00000000..89f41339 Binary files /dev/null and b/app/my_presentation.pptx differ diff --git a/app/website/tests/test_contactUs.py b/app/website/tests/test_contactUs.py index d4945308..8a0f6429 100644 --- a/app/website/tests/test_contactUs.py +++ b/app/website/tests/test_contactUs.py @@ -7,20 +7,11 @@ class TestContactForm(TestCase): def setUp(self) -> None: - user = User.objects.create_user(username='test3', email = 'test3@hotmailtestttt.com', password='passpass22') + self.user = User.objects.create_user(username='test3', email = 'test3@hotmailtestttt.com', password='passpass22') + self.client.force_login(self.user) return super().setUp() def test_contact_form_successful(self): - response = self.client.post(reverse("home"), { - 'username':'test3', - 'password':'passpass22'}, follow=True) - - #check if post response was a success - self.assertEqual(response.status_code, 200) - - #Should return true if user is logged in - self.assertTrue(response.context['user'].is_authenticated) - #Makes a post request to the contact form on contact us page response = self.client.post(reverse("contact_us"), { 'email':'test3@hotmailtestttt.com', @@ -36,16 +27,6 @@ def test_contact_form_successful(self): self.assertEquals(mail.outbox[0].subject, "Question from test3@hotmailtestttt.com") def test_contact_form_invalid_email(self): - response = self.client.post(reverse("home"), { - 'username':'test3', - 'password':'passpass22'}, follow=True) - - #check if post response was a success - self.assertEqual(response.status_code, 200) - - #Should return true if user is logged in - self.assertTrue(response.context['user'].is_authenticated) - #Makes a post request to the contact form on contact us page passing an invalid email response = self.client.post(reverse("contact_us"), { 'email':'h', @@ -59,16 +40,6 @@ def test_contact_form_invalid_email(self): self.assertEqual(len(mail.outbox), 0) def test_contact_form_invalid_question(self): - response = self.client.post(reverse("home"), { - 'username':'test3', - 'password':'passpass22'}, follow=True) - - #check if post response was a success - self.assertEqual(response.status_code, 200) - - #Should return true if user is logged in - self.assertTrue(response.context['user'].is_authenticated) - #Makes a post request to the contact form on contact us page passing an empty question response = self.client.post(reverse("contact_us"), { 'email':'bob12@hotmaill.commm', @@ -82,4 +53,3 @@ def test_contact_form_invalid_question(self): - diff --git a/app/website/tests/test_editprofile.py b/app/website/tests/test_editprofile.py index 8e05d38b..7063e33a 100644 --- a/app/website/tests/test_editprofile.py +++ b/app/website/tests/test_editprofile.py @@ -219,15 +219,25 @@ def test_profile_picture_change_unsuccessful(self): #check if profile picture remains the same (default image) self.assertEqual(user.profile.profile_pic.url, '/media/default.jpg') + #Test without specifying name of form being passed + def test_post_no_form_specified(self): + response = self.client.post(reverse("edit_profile"), { + 'bio':'Test11 Updated Bio' + }, follow=True) + #Check if post request was a success + self.assertEqual(response.status_code, 200) + #Check to see if correct template was used + self.assertTemplateUsed(response, 'edit_profile.html') + user = User.objects.get(username=self.user.username) + #check if bio updated, should return true as bio did not update + self.assertNotEqual(user.profile.bio, 'Test11 Updated Bio') + def tearDown(self) -> None: #remove 'test.jpg' from storage if it exists fs = FileSystemStorage("media/profile_pictures/") if fs.exists('test.jpg'): fs.delete('test.jpg') - #remove 'test.bmp' - if fs.exists('test.bmp'): - fs.delete('test.bmp') #enable logging again after tests are completed logging.disable(logging.NOTSET) return super().tearDown() \ No newline at end of file diff --git a/app/website/tests/test_forms.py b/app/website/tests/test_forms.py index 0bc2eb1a..18a38531 100644 --- a/app/website/tests/test_forms.py +++ b/app/website/tests/test_forms.py @@ -1,6 +1,9 @@ from django.test import TestCase -from website.forms import SignUpForm - +from website.forms import SignUpForm, ChangePasswordForm, EditProfileForm, EditProfilePageForm +from django.contrib.auth.models import User +from django.core.files.uploadedfile import SimpleUploadedFile +from django.core.files.storage import FileSystemStorage +import logging class TestSignUpForm(TestCase): @@ -112,3 +115,150 @@ def test_form_with_invalid_userGroup(self): self.assertFalse(form.is_valid()) #print(form.errors) self.assertEqual(len(form.errors), 1) #error invalid user_group + + +class TestChangePasswordForm(TestCase): + + def setUp(self) -> None: + user = User.objects.create_user(username='test33', email = 'test3@hotmailtestttt.com', password='passpass22', first_name = 'Bob', last_name='Johnson') + user = User.objects.get(username = 'test33') + self.user = user + return super().setUp() + + def test_form_successful(self): + form = ChangePasswordForm(user=self.user,data={ + 'old_password' : 'passpass22', + 'new_password1' : 'passpass222', + 'new_password2' : 'passpass222' + }) + + self.assertTrue(form.is_valid()) + #print(form.errors) + self.assertEqual(len(form.errors), 0) + + def test_form_invalid_too_common(self): + form = ChangePasswordForm(user=self.user,data={ + 'old_password' : 'passpass22', + 'new_password1' : 'password123', + 'new_password2' : 'password123' + }) + + self.assertFalse(form.is_valid()) + #print(form.errors) + self.assertEqual(len(form.errors), 1) #Error password is too common + + def test_form_invalid_too_short(self): + form = ChangePasswordForm(user=self.user, data={ + 'old_password' : 'passpass22', + 'new_password1' : 'pass22', + 'new_password2' : 'pass22' + }) + + self.assertFalse(form.is_valid()) + #print(form.errors) + self.assertEqual(len(form.errors), 1) #1 error due to length + + def test_form_invalid_all_numeric(self): + form = ChangePasswordForm(user=self.user,data={ + 'old_password' : 'passpass22', + 'new_password1' : '123456789', + 'new_password2' : '123456789' + }) + + self.assertFalse(form.is_valid()) + #print(form.errors) + self.assertEqual(len(form.errors), 1) #1 error password can't be entirely numeric + + def test_form_invalid_empty(self): + form = ChangePasswordForm(user=self.user, data={ }) + self.assertFalse(form.is_valid()) + #print(form.errors) + self.assertEqual(len(form.errors), 3) #3 errors due to empty fields + + def test_form_password_personal_info(self): + form = ChangePasswordForm(user=self.user, data={ + 'old_password' : 'passpass22', + 'new_password1' : 'Johnson2244', + 'new_password2' : 'Johnson2244' + }) + + self.assertFalse(form.is_valid()) + #print(form.errors) + self.assertEqual(len(form.errors), 1) #1 error password can't contain personal information + + + +class TestEditProfileForm(TestCase): + def setUp(self) -> None: + user = User.objects.create_user(username='test44', email = 'test4@hotmailtestttt.com', password='passpass22') + user = User.objects.get(username = 'test44') + self.user = user + return super().setUp() + + def test_form_valid(self): + form = EditProfileForm(instance = self.user, data={ + 'username' : 'test44', + 'first_name' : 'bob', + 'last_name' : 'johnson', + 'email' : 'test23@test.com' + }) + + self.assertTrue(form.is_valid()) + #print(form.errors) + self.assertEqual(len(form.errors), 0) + + def test_form_username_taken(self): + form = EditProfileForm(data={ + 'username' : 'test44', + 'first_name' : 'bob', + 'last_name' : 'johnson', + 'email' : 'test23@test.com' + }) + + self.assertFalse(form.is_valid()) + #print(form.errors) + self.assertEqual(len(form.errors), 1) #Error username already taken + + def test_form_invalid_email(self): + form = EditProfileForm(instance = self.user, data={ + 'username' : 'test44', + 'first_name' : 'bob', + 'last_name' : 'johnson', + 'email' : 'test23com' + }) + + self.assertFalse(form.is_valid()) + #print(form.errors) + self.assertEqual(len(form.errors), 1) #Error email is invalid + + def test_form_invalid_name_length(self): + name = 'bobby' * 35 #175 char + last_name = 'davis' * 35 #175 char + form = EditProfileForm(instance = self.user, data={ + 'username' : 'test44', + 'first_name' : name, + 'last_name' : last_name, + 'email' : 'test23@test.com' + }) + + self.assertFalse(form.is_valid()) + #print(form.errors) + self.assertEqual(len(form.errors), 2) #Error char limit first_name and last_name + + def test_form_invalid_username(self): + form = EditProfileForm(data={ + 'username' : 'test&*@', + 'first_name' : 'bob', + 'last_name' : 'johnson', + 'email' : 'test23@test.com' + }) + + self.assertFalse(form.is_valid()) + #print(form.errors) + self.assertEqual(len(form.errors), 1) #Error invalid username + + def test_form_invalid_form(self): + form = EditProfileForm(data={}) + self.assertFalse(form.is_valid()) + #print(form.errors) + self.assertEqual(len(form.errors), 4) #4 errors due to 4 empty fields diff --git a/app/website/tests/test_urls.py b/app/website/tests/test_urls.py index d1cc4fe4..f79522d0 100644 --- a/app/website/tests/test_urls.py +++ b/app/website/tests/test_urls.py @@ -45,3 +45,45 @@ def test_url_faq_is_resolved(self): def test_url_contactUs_is_resolved(self): url = reverse("contact_us") self.assertEquals(resolve(url).func, contact_us) + + def test_url_profile_is_resolved(self): + url = reverse("profile", kwargs={'username' : 'test'}) + self.assertEquals(resolve(url).func, Profile) + + def test_url_editProfile_is_resolved(self): + url = reverse("edit_profile") + self.assertEquals(resolve(url).func, edit_profile) + + def test_url_changePassword_is_resolved(self): + url = reverse("change_password") + self.assertEquals(resolve(url).func, change_password) + + def test_url_presentationPreview_is_resolved(self): + url = reverse("presentation_preview") + self.assertEquals(resolve(url).func, presentation_preview) + + def test_url_loadingPage_is_resolved(self): + url = reverse("loading_page") + self.assertEquals(resolve(url).func, loading_page_view) + + def test_url_exerciseLoadingPage_is_resolved(self): + url = reverse("exercise_loading_page") + self.assertEquals(resolve(url).func, exercise_loading_page_view) + + def test_url_generateAdaptedContent_is_resolved(self): + url = reverse("generate_adapted_content") + self.assertEquals(resolve(url).func, generate_adapted_content_view) + + def test_url_openChats_is_resolved(self): + url = reverse("open_chats") + self.assertEquals(resolve(url).func, open_chats) + + def test_url_newChats_is_resolved(self): + url = reverse("new_chats") + self.assertEquals(resolve(url).func, new_chats) + + def test_url_chats_is_resolved(self): + url = reverse("chat", kwargs={'username' : 'test'}) + self.assertEquals(resolve(url).func, chat) + + \ No newline at end of file diff --git a/app/website/tests/test_views.py b/app/website/tests/test_views.py index 725dd2a6..ec77a58a 100644 --- a/app/website/tests/test_views.py +++ b/app/website/tests/test_views.py @@ -9,10 +9,13 @@ class TestViews(TestCase): def setUp(self) -> None: Group.objects.get_or_create(name='teacher') Group.objects.get_or_create(name='student') + user = User.objects.create_user(username='test111', email = 'test111@hotmailtestttt.com', password='passpass22', first_name='bob', last_name='johnson') + user2 = User.objects.create_user(username='test112', email = 'test112@hotmailtestttt.com', password='passpass22', first_name='bob', last_name='johnson') + self.user = user return super().setUp() """ - Testing to see if Users can view our pages correctly. + Testing to see if Users can view our unauthenticated pages correctly. """ def test_home_view(self): response = self.client.get(reverse("home")) @@ -47,35 +50,19 @@ def test_contactUs_view(self): if they do they should be redirected back to the home page. """ def test_authenticated_register_view(self): - #creates the user and saves it in the test database - user = User.objects.create_user(username='test1', email = 'test1@hotmailtestttt.com', password='passpass22') #login - response = self.client.post(reverse("home"), { - 'username':'test1', - 'password':'passpass22'}, follow=True) - #check if post request was successful - self.assertEqual(response.status_code, 200) - #check if user is authenticated (logged in) - self.assertTrue(response.context['user'].is_authenticated) + self.client.force_login(self.user) response = self.client.get(reverse("register")) #Should redirect to home page as user has already registered an account self.assertEqual(response.status_code, 302) self.assertRedirects(response, reverse("home")) - """These two tests below check to see if an authenticated user is - viewing the generateSummary and generatePresentation pages correctly + """These tests below check to see if an authenticated user is + viewing the pages that require authentication correctly """ def test_generateSummary_view_authenticated(self): - #creates the user and saves it in the test database - user = User.objects.create_user(username='test2', email = 'test2@hotmailtestttt.com', password='passpass22') #login - response = self.client.post(reverse("home"), { - 'username':'test2', - 'password':'passpass22'}, follow=True) - #check if post request was successful - self.assertEqual(response.status_code, 200) - #check if user is authenticated (logged in) - self.assertTrue(response.context['user'].is_authenticated) + self.client.force_login(self.user) response = self.client.get(reverse("generate_summary")) #check to see if get request was successful self.assertEqual(response.status_code, 200) @@ -83,16 +70,8 @@ def test_generateSummary_view_authenticated(self): self.assertTemplateUsed(response, 'summary_generation.html') def test_generatePresentation_view_authenticated(self): - #creates the user and saves it in the test database - user = User.objects.create_user(username='test3', email = 'test3@hotmailtestttt.com', password='passpass22') #login - response = self.client.post(reverse("home"), { - 'username':'test3', - 'password':'passpass22'}, follow=True) - #check if post request was successful - self.assertEqual(response.status_code, 200) - #check if user is authenticated (logged in) - self.assertTrue(response.context['user'].is_authenticated) + self.client.force_login(self.user) response = self.client.get(reverse("generate_presentation")) #check if get request was successful self.assertEqual(response.status_code, 200) @@ -100,16 +79,8 @@ def test_generatePresentation_view_authenticated(self): self.assertTemplateUsed(response, 'presentation_generation.html') def test_generateExercise_view_authenticated(self): - #creates the user and saves it in the test database - user = User.objects.create_user(username='test4', email = 'test4@hotmailtestttt.com', password='passpass22') #login - response = self.client.post(reverse("home"), { - 'username':'test4', - 'password':'passpass22'}, follow=True) - #check if post request was successful - self.assertEqual(response.status_code, 200) - #check if user is authenticated (logged in) - self.assertTrue(response.context['user'].is_authenticated) + self.client.force_login(self.user) response = self.client.get(reverse("generate_exercise")) #check if get request was successful self.assertEqual(response.status_code, 200) @@ -117,16 +88,8 @@ def test_generateExercise_view_authenticated(self): self.assertTemplateUsed(response, 'exercise_generation.html') def test_virtualAssistant_view_authenticated(self): - #creates the user and saves it in the test database - user = User.objects.create_user(username='test5', email = 'test5@hotmailtestttt.com', password='passpass22') #login - response = self.client.post(reverse("home"), { - 'username':'test5', - 'password':'passpass22'}, follow=True) - #check if post request was successful - self.assertEqual(response.status_code, 200) - #check if user is authenticated (logged in) - self.assertTrue(response.context['user'].is_authenticated) + self.client.force_login(self.user) response = self.client.get(reverse("chatbot")) #check if get request was successful self.assertEqual(response.status_code, 200) @@ -174,9 +137,120 @@ def test_detectPlagiarism_view_authenticated_student_group(self): self.assertEqual(response.status_code, 302) #Redirects user back to home page since user is part of the student group self.assertRedirects(response, reverse("home")) + + def test_generateAdaptedContent_view_authenticated(self): + #login + self.client.force_login(self.user) + response = self.client.get(reverse("generate_adapted_content")) + #check if get request was successful + self.assertEqual(response.status_code, 200) + #check to see if correct template was used + self.assertTemplateUsed(response, 'adapted_content_generation.html') + + #tests viewing your own profile page + def test_profile_view_authenticated(self): + #login + self.client.force_login(self.user) + response = self.client.get(reverse("profile", kwargs={'username' : 'test111'})) + #check if get request was successful + self.assertEqual(response.status_code, 200) + #check to see if correct template was used + self.assertTemplateUsed(response, 'profile.html') + + #tests profile page with htmx request + def test_profile_htmx_view_authenticated(self): + #login + self.client.force_login(self.user) + headers = {'HTTP_HX-REQUEST' : 'true'} + response = self.client.get(reverse("profile", kwargs={'username' : 'test111'}), **headers) + #check if get request was successful + self.assertEqual(response.status_code, 200) + #check to see if correct template was used + self.assertTemplateUsed(response, 'partials/profile_list.html') + + #Tests viewing a different users profile page + def test_profileDifferentUser_view_authenticated(self): + #login + self.client.force_login(self.user) + response = self.client.get(reverse("profile", kwargs={'username' : 'test112'})) + #check if get request was successful + self.assertEqual(response.status_code, 200) + #check to see if correct template was used + self.assertTemplateUsed(response, 'profile_different_user.html') + + #Tests viewing a different users profile page with htmx request + def test_profileDifferentUser_htmx_view_authenticated(self): + #login + self.client.force_login(self.user) + headers = {'HTTP_HX-REQUEST' : 'true'} + response = self.client.get(reverse("profile", kwargs={'username' : 'test112'}), **headers) + #check if get request was successful + self.assertEqual(response.status_code, 200) + #check to see if correct template was used + self.assertTemplateUsed(response, 'partials/profile_different_user_list.html') + - """These two tests below check to see if an unauthenticated user is trying to - view the generateSummary and generatePresentation pages. If they are they will + def test_editProfile_view_authenticated(self): + #login + self.client.force_login(self.user) + response = self.client.get(reverse("edit_profile")) + #check if get request was successful + self.assertEqual(response.status_code, 200) + #check to see if correct template was used + self.assertTemplateUsed(response, 'edit_profile.html') + + def test_changePassword_view_authenticated(self): + #login + self.client.force_login(self.user) + response = self.client.get(reverse("change_password")) + #check if get request was successful + self.assertEqual(response.status_code, 200) + #check to see if correct template was used + self.assertTemplateUsed(response, 'change_password.html') + + def test_chat_view_authenticated(self): + #login + self.client.force_login(self.user) + response = self.client.get(reverse("chat", kwargs={'username' : 'test111'})) + #check if get request was successful + self.assertEqual(response.status_code, 200) + #check to see if correct template was used + self.assertTemplateUsed(response, 'chat.html') + + #test chat view with htmx request + def test_chat_htmx_view_authenticated(self): + #login + self.client.force_login(self.user) + headers = {'HTTP_HX-REQUEST' : 'true'} + response = self.client.get(reverse("chat", kwargs={'username' : 'test111'}), **headers) + #check if get request was successful + self.assertEqual(response.status_code, 200) + #check to see if correct template was used + self.assertTemplateUsed(response, 'partials/chat_messages_load.html') + + + def test_newChat_view_authenticated(self): + #login + self.client.force_login(self.user) + response = self.client.get(reverse("new_chats")) + #check if get request was successful + self.assertEqual(response.status_code, 200) + #check to see if correct template was used + self.assertTemplateUsed(response, 'new_chats.html') + + def test_openChat_view_authenticated(self): + #login + self.client.force_login(self.user) + response = self.client.get(reverse("open_chats")) + #check if get request was successful + self.assertEqual(response.status_code, 200) + #check to see if correct template was used + self.assertTemplateUsed(response, 'open_chats.html') + + + + """These tests below check to see if an unauthenticated user is trying to + view pages that require authentication. If they are they will be redirected back to the home page. """ def test_generateSummary_view_not_authenticated(self): @@ -213,4 +287,33 @@ def test_detectPlagiarism_view_not_authenticated(self): self.assertEqual(response.status_code, 302) #Redirects user back to home page since user is not authenticated self.assertRedirects(response, reverse("home")) + + def test_generateAdaptedContent_view_not_authenticated(self): + response = self.client.get(reverse("generate_adapted_content")) + #If user is not authenticated then it will redirect to homepage + self.assertEqual(response.status_code, 302) + #Redirects user back to home page since user is not authenticated + self.assertRedirects(response, reverse("home")) + + def test_profile_view_not_authenticated(self): + response = self.client.get(reverse("profile", kwargs={'username' : 'test111'})) + #If user is not authenticated then it will redirect to homepage + self.assertEqual(response.status_code, 302) + #Redirects user back to home page since user is not authenticated + self.assertRedirects(response, reverse("home")) + + def test_editProfile_view_not_authenticated(self): + response = self.client.get(reverse("edit_profile")) + #If user is not authenticated then it will redirect to homepage + self.assertEqual(response.status_code, 302) + #Redirects user back to home page since user is not authenticated + self.assertRedirects(response, reverse("home")) + + def test_changePassword_view_not_authenticated(self): + response = self.client.get(reverse("change_password")) + #If user is not authenticated then it will redirect to homepage + self.assertEqual(response.status_code, 302) + #Redirects user back to home page since user is not authenticated + self.assertRedirects(response, reverse("home")) + diff --git a/app/website/views.py b/app/website/views.py index 87614796..9946bc46 100644 --- a/app/website/views.py +++ b/app/website/views.py @@ -45,7 +45,7 @@ from .models import Message -from django.core.mail import send_mail +from django.core.mail import send_mail, BadHeaderError from django.core.exceptions import ValidationError from django.core.validators import validate_email from util import config @@ -243,10 +243,6 @@ def contact_us(request): return render(request, 'contact_us.html') try: validate_email(email) - except ValidationError as err: - messages.error(request, "An invalid email address was entered please try again.") - return render(request, 'contact_us.html') - else: subject = 'Question from ' + email #Sends question from user as email to noreplyeduprompt@gmail.com send_mail(subject, @@ -256,7 +252,12 @@ def contact_us(request): fail_silently=False) messages.success(request, "Your question has been sent.") return render(request, 'contact_us.html') - + except ValidationError as err: + messages.error(request, "An invalid email address was entered please try again.") + return render(request, 'contact_us.html') + except BadHeaderError as err: + messages.error(request, "An invalid header was found please try again.") + return render(request, 'contact_us.html') else: return render(request, 'contact_us.html')