Skip to content

Commit

Permalink
SS-1177 add e2e test checking that changes in the user profile are sa…
Browse files Browse the repository at this point in the history
…ved in the database (#256)

Co-authored-by: akochari <[email protected]>

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).
  • Loading branch information
anondo1969 authored Nov 27, 2024
1 parent f9e08bd commit 36eb700
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
8 changes: 8 additions & 0 deletions common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}"

Expand Down
9 changes: 9 additions & 0 deletions cypress/e2e/setup-scripts/seed_login_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
33 changes: 33 additions & 0 deletions cypress/e2e/ui-tests/test-login-account-handling.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("/")
Expand Down
4 changes: 4 additions & 0 deletions cypress/fixtures/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
"username": "e2e_tests_deploy_app_user"
},
"login_user": {
"affiliation": "uu",
"department": "e2e-tests-login-user-department-name",
"email": "[email protected]",
"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"
Expand Down
29 changes: 17 additions & 12 deletions static/js/form-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
};

0 comments on commit 36eb700

Please sign in to comment.