Skip to content

Commit

Permalink
add gw auth
Browse files Browse the repository at this point in the history
Issue: AAP-27751
  • Loading branch information
jerabekjiri committed Jul 25, 2024
1 parent 1030d3f commit ac20caa
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions test/cypress/support/login.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
// https://on.cypress.io/custom-commands
const apiPrefix = Cypress.env('apiPrefix');

function apiLogin(username, password, url = '/', title = 'Collections') {
const setFormData = (username, password) => {
const formData = new FormData();
formData.set('username', username);
formData.set('password', password);
return formData;
};

function apiLogin(
username,
password,
url,
title = 'Collections',
isGateway = false,
) {
cy.session(
['apiLogin', username],
() => {
const loginUrl = `${apiPrefix}_ui/v1/auth/login/`;
const loginUrl = isGateway
? `/api/gateway/v1/login/`
: `${apiPrefix}_ui/v1/auth/login/`;
cy.request('GET', loginUrl).then(() => {
cy.getCookie('csrftoken').then((csrftoken) => {
cy.getCookie('csrftoken').then((csrf) => {
const csrfToken = csrf.value;

const headers = {
'X-CSRFToken': csrfToken,
Referer: Cypress.config().baseUrl,
};

if (isGateway) {
headers['content-type'] = 'multipart/form-data';
headers['Set-Cookie'] = `csrftoken=${csrfToken}`;
}

const body = isGateway
? setFormData(username, password)
: { username, password };

cy.request({
method: 'POST',
url: loginUrl,
body: { username, password },
headers: {
'X-CSRFToken': csrftoken.value,
Referer: Cypress.config().baseUrl,
},
body,
headers,
});
});
});
Expand All @@ -30,12 +58,22 @@ function apiLogin(username, password, url = '/', title = 'Collections') {
cy.assertTitle(title);
}

Cypress.Commands.add('login', {}, (username, password, url, title) => {
Cypress.Commands.add('login', {}, (username, password, url = '/', title) => {
if (!username && !password) {
// default to admin
username = Cypress.env('username');
password = Cypress.env('password');
}

apiLogin(username, password, url, title);
cy.log('gateway', Cypress.env('HUB_GATEWAY'));

const isGateway =
['1', 1, 'true', true].includes(Cypress.env('HUB_GATEWAY')) || false;

// redirects to HUB full experience /ui/
const gwHubPrefix = '/ui/';

const loginUrl = isGateway ? gwHubPrefix : url;

apiLogin(username, password, loginUrl, title, isGateway);
});

0 comments on commit ac20caa

Please sign in to comment.