From 36eb700c0c92a00ed4e7fb5efac9b1ea94373493 Mon Sep 17 00:00:00 2001 From: Mahbub Ul Alam Date: Wed, 27 Nov 2024 08:24:48 +0100 Subject: [PATCH] SS-1177 add e2e test checking that changes in the user profile are saved in the database (#256) Co-authored-by: akochari Source: https://scilifelab.atlassian.net/browse/SS-1177 This task is to add a new test to our e2e tests that would check whether provided new information on the Edit profile page is actually saved in the database (i.e. displays when the user goes to the user profile page after saving the changes). --- common/models.py | 8 +++++ cypress/e2e/setup-scripts/seed_login_user.py | 9 +++++ .../test-login-account-handling.cy.js | 33 +++++++++++++++++++ cypress/fixtures/users.json | 4 +++ static/js/form-helpers.js | 29 +++++++++------- 5 files changed, 71 insertions(+), 12 deletions(-) diff --git a/common/models.py b/common/models.py index e1019d878..9df2e296f 100644 --- a/common/models.py +++ b/common/models.py @@ -7,6 +7,12 @@ logger = get_logger(__name__) +class UserProfileManager(models.Manager): + def create_user_profile(self, user: User): + user_profile = self.create(user=user) + return user_profile + + class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) affiliation = models.CharField(max_length=100, blank=True) @@ -19,6 +25,8 @@ class UserProfile(models.Model): note = models.TextField(max_length=1000, blank=True) + objects = UserProfileManager() + def __str__(self): return f"{self.user.email}" diff --git a/cypress/e2e/setup-scripts/seed_login_user.py b/cypress/e2e/setup-scripts/seed_login_user.py index cbfe132e5..4e73a632e 100755 --- a/cypress/e2e/setup-scripts/seed_login_user.py +++ b/cypress/e2e/setup-scripts/seed_login_user.py @@ -6,6 +6,7 @@ from django.conf import settings from django.contrib.auth.models import User +from common.models import UserProfile from projects.models import Project cypress_path = os.path.join(settings.BASE_DIR, "cypress/fixtures") @@ -22,4 +23,12 @@ # Create the login user user = User.objects.create_user(username, email, pwd) + user.first_name = userdata["first_name"] + user.last_name = userdata["last_name"] user.save() + + # needed for editing profile information (see SS-1177) + user_profile = UserProfile.objects.create_user_profile(user) + user_profile.department = userdata["department"] + user_profile.affiliation = userdata["affiliation"] + user_profile.save() diff --git a/cypress/e2e/ui-tests/test-login-account-handling.cy.js b/cypress/e2e/ui-tests/test-login-account-handling.cy.js index 1312da8e3..e2259a4a8 100644 --- a/cypress/e2e/ui-tests/test-login-account-handling.cy.js +++ b/cypress/e2e/ui-tests/test-login-account-handling.cy.js @@ -60,6 +60,39 @@ describe("Test login, profile page view, password change, password reset", () => cy.get('div.col-8').should("contain", users.login_user.email) }) + it("can edit user profile information", () => { + + function editProfile(firstName, lastName, department) { + + cy.url().should("include", "edit-profile/") + + cy.get('#id_first_name').clear().type(firstName); + cy.get('#id_last_name').clear().type(lastName); + cy.get('#id_department').clear().type(department); + cy.get('#submit-id-save').click(); + + cy.contains(firstName).should('exist'); + cy.contains(lastName).should('exist'); + cy.contains(department).should('exist'); + } + + + cy.loginViaUI(users.login_user.email, users.login_user.password) + + cy.log("Editing and verifying userprofile by accessing it from the navbar") + cy.visit("/") + cy.get('button.btn-profile').click() + cy.get('li.btn-group').find('a').contains("Edit profile").click() + editProfile('changing first name', 'changing last name', 'changing department name'); + + cy.log("Editing and verifying userprofile by accessing it from the profile view") + cy.visit("/") + cy.get('button.btn-profile').click() + cy.get('li.btn-group').find('a').contains("My profile").click() + cy.get('button.btn-profile').contains('a', 'Edit').click(); + editProfile('changing first name again', 'changing last name again', 'changing department name again'); + }) + it("can change user password", () => { cy.loginViaUI(users.login_user.email, users.login_user.password) cy.visit("/") diff --git a/cypress/fixtures/users.json b/cypress/fixtures/users.json index f6319ecb7..660094cae 100644 --- a/cypress/fixtures/users.json +++ b/cypress/fixtures/users.json @@ -25,7 +25,11 @@ "username": "e2e_tests_deploy_app_user" }, "login_user": { + "affiliation": "uu", + "department": "e2e-tests-login-user-department-name", "email": "no-reply-login@scilifelab.uu.se", + "first_name": "e2e-tests-login-user-first-name", + "last_name": "e2e-tests-login-user-last-name", "password": "tesT12345@", "reset_password": "^x#^PbLnDY!{J0ju", "username": "e2e_tests_login_user" diff --git a/static/js/form-helpers.js b/static/js/form-helpers.js index 87dda2698..7dc0cb054 100644 --- a/static/js/form-helpers.js +++ b/static/js/form-helpers.js @@ -8,6 +8,7 @@ window.onload = (event) => { const domainRegex = /^(?:(?!\b(?:student|stud)\b\.)[A-Z0-9](?:[\.A-Z0-9-]{0,61}[A-Z0-9])?\.)*?(uu|lu|gu|su|umu|liu|ki|kth|chalmers|ltu|hhs|slu|kau|lth|lnu|oru|miun|mau|mdu|bth|fhs|gih|hb|du|hig|hh|hkr|his|hv|ju|sh)\.se$/i; function changeVisibility() { + let shouldHide = false; let match; @@ -30,23 +31,27 @@ window.onload = (event) => { department_label.classList.remove('required'); } - if (shouldHide) { - request_account_field.classList.add('hidden'); - } else { - request_account_field.classList.remove('hidden'); - request_account_label.classList.add('required'); + if (request_account_field){ // to prevent Uncaught TypeError for null value + if (shouldHide) { + request_account_field.classList.add('hidden'); + } else { + request_account_field.classList.remove('hidden'); + request_account_label.classList.add('required'); + } } } - // Temporarily disable transitions - request_account_field.style.transition = 'none'; + if (request_account_field){ // to prevent Uncaught TypeError for null value + // Temporarily disable transitions + request_account_field.style.transition = 'none'; - changeVisibility(); + changeVisibility(); - // Restore transitions after a short delay - setTimeout(() => { - request_account_field.style.transition = ''; - }, 50); + // Restore transitions after a short delay + setTimeout(() => { + request_account_field.style.transition = ''; + }, 50); + } email.addEventListener('input', changeVisibility); };