Skip to content

Commit

Permalink
Merge pull request #33 from kanopi/check-user-logged-in
Browse files Browse the repository at this point in the history
Use session cookie to check if the user is logged in
  • Loading branch information
paulsheldrake authored Nov 6, 2024
2 parents e8ff240 + 961cce5 commit edf8694
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@
// cy.login('username', 'password'); // login as a specific user.

Cypress.Commands.add('login', (usernameParam = '', password = '') => {
cy.logout()
cy.visit('/user/login')
let username = '';
let default_user = false;
if (usernameParam === '' && password === '') {
username = 'cypress';
default_user = true;
}
else {
username = usernameParam;
}
cy.logout();
cy.visit("/user/login");
let username = "";
let default_user = false;
if (usernameParam === "" && password === "") {
username = "cypress";
default_user = true;
} else {
username = usernameParam;
}

if (default_user) {
cy.get('form.user-login-form #edit-name').type(username);
cy.get('form.user-login-form #edit-pass').type('cypress');
}
else {
cy.get('form.user-login-form #edit-name').type(username);
cy.get('form.user-login-form #edit-pass').type(password);
}
if (default_user) {
cy.get("form.user-login-form #edit-name").type(username);
cy.get("form.user-login-form #edit-pass").type("cypress");
} else {
cy.get("form.user-login-form #edit-name").type(username);
cy.get("form.user-login-form #edit-pass").type(password);
}

// Login.
cy.get('form.user-login-form .form-submit').click();
cy.wait(500) // Wait for the UI to catch up.
// Login.
cy.get("form.user-login-form .form-submit").click();
cy.wait(500); // Wait for the UI to catch up.

// Check user page loads.
cy.get('main').contains(username)
cy.get('main').contains('Member for')
// Check user session cookie is set after logging in.
cy.getCookies().then((cookies) => {
// Search for cookie.name and value contains SESS.
const sessionCookie = cookies.find((cookie) => cookie.name.includes("SESS"));
expect(sessionCookie).to.exist;
});
})

0 comments on commit edf8694

Please sign in to comment.