mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
Update Cypress to read REST API URL dynamically from config.json
This commit is contained in:
@@ -11,7 +11,6 @@
|
|||||||
"openMode": 0
|
"openMode": 0
|
||||||
},
|
},
|
||||||
"env": {
|
"env": {
|
||||||
"DSPACE_TEST_REST_BASE_URL": "http://localhost:8080",
|
|
||||||
"DSPACE_TEST_ADMIN_USER": "dspacedemo+admin@gmail.com",
|
"DSPACE_TEST_ADMIN_USER": "dspacedemo+admin@gmail.com",
|
||||||
"DSPACE_TEST_ADMIN_PASSWORD": "dspace",
|
"DSPACE_TEST_ADMIN_PASSWORD": "dspace",
|
||||||
"DSPACE_TEST_COMMUNITY": "0958c910-2037-42a9-81c7-dca80e3892b4",
|
"DSPACE_TEST_COMMUNITY": "0958c910-2037-42a9-81c7-dca80e3892b4",
|
||||||
|
@@ -4,7 +4,7 @@
|
|||||||
// ***********************************************
|
// ***********************************************
|
||||||
|
|
||||||
import { AuthTokenInfo, TOKENITEM } from 'src/app/core/auth/models/auth-token-info.model';
|
import { AuthTokenInfo, TOKENITEM } from 'src/app/core/auth/models/auth-token-info.model';
|
||||||
import { TEST_REST_BASE_URL } from '.';
|
import { FALLBACK_TEST_REST_BASE_URL } from '.';
|
||||||
|
|
||||||
// Declare Cypress namespace to help with Intellisense & code completion in IDEs
|
// Declare Cypress namespace to help with Intellisense & code completion in IDEs
|
||||||
// ALL custom commands MUST be listed here for code completion to work
|
// ALL custom commands MUST be listed here for code completion to work
|
||||||
@@ -30,34 +30,53 @@ declare global {
|
|||||||
* @param password password to login as
|
* @param password password to login as
|
||||||
*/
|
*/
|
||||||
function login(email: string, password: string): void {
|
function login(email: string, password: string): void {
|
||||||
// To login via REST, first we have to do a GET to obtain a valid CSRF token
|
// Cypress doesn't have access to the running application in Node.js.
|
||||||
cy.request( TEST_REST_BASE_URL + '/server/api/authn/status' )
|
// So, it's not possible to inject or load the AppConfig or environment of the Angular UI.
|
||||||
.then((response) => {
|
// Instead, we'll read our running application's config.json, which contains the configs &
|
||||||
// We should receive a CSRF token returned in a response header
|
// is regenerated at runtime each time the Angular UI application starts up.
|
||||||
expect(response.headers).to.have.property('dspace-xsrf-token');
|
cy.readFile('./src/assets/config.json').then((str) => {
|
||||||
const csrfToken = response.headers['dspace-xsrf-token'];
|
// Parse JSON file into a JSON object
|
||||||
|
const config = JSON.parse(JSON.stringify(str));
|
||||||
|
|
||||||
// Now, send login POST request including that CSRF token
|
// Find the URL of our REST API. Have a fallback ready, just in case 'rest.baseUrl' cannot be found.
|
||||||
cy.request({
|
let baseRestUrl = FALLBACK_TEST_REST_BASE_URL;
|
||||||
method: 'POST',
|
if (!config.rest.baseUrl) {
|
||||||
url: TEST_REST_BASE_URL + '/server/api/authn/login',
|
console.warn("Could not load 'rest.baseUrl' from config.json. Falling back to " + FALLBACK_TEST_REST_BASE_URL);
|
||||||
headers: { 'X-XSRF-TOKEN' : csrfToken},
|
} else {
|
||||||
form: true, // indicates the body should be form urlencoded
|
console.log("Found 'rest.baseUrl' in config.json. Using this REST API for login: " + config.rest.baseUrl);
|
||||||
body: { user: email, password: password }
|
baseRestUrl = config.rest.baseUrl;
|
||||||
}).then((resp) => {
|
}
|
||||||
// We expect a successful login
|
|
||||||
expect(resp.status).to.eq(200);
|
|
||||||
// We expect to have a valid authorization header returned (with our auth token)
|
|
||||||
expect(resp.headers).to.have.property('authorization');
|
|
||||||
|
|
||||||
// Initialize our AuthTokenInfo object from the authorization header.
|
// To login via REST, first we have to do a GET to obtain a valid CSRF token
|
||||||
const authheader = resp.headers.authorization as string;
|
cy.request( baseRestUrl + '/api/authn/status' )
|
||||||
const authinfo: AuthTokenInfo = new AuthTokenInfo(authheader);
|
.then((response) => {
|
||||||
|
// We should receive a CSRF token returned in a response header
|
||||||
|
expect(response.headers).to.have.property('dspace-xsrf-token');
|
||||||
|
const csrfToken = response.headers['dspace-xsrf-token'];
|
||||||
|
|
||||||
// Save our AuthTokenInfo object to our dsAuthInfo UI cookie
|
// Now, send login POST request including that CSRF token
|
||||||
// This ensures the UI will recognize we are logged in on next "visit()"
|
cy.request({
|
||||||
cy.setCookie(TOKENITEM, JSON.stringify(authinfo));
|
method: 'POST',
|
||||||
|
url: baseRestUrl + '/api/authn/login',
|
||||||
|
headers: { 'X-XSRF-TOKEN' : csrfToken},
|
||||||
|
form: true, // indicates the body should be form urlencoded
|
||||||
|
body: { user: email, password: password }
|
||||||
|
}).then((resp) => {
|
||||||
|
// We expect a successful login
|
||||||
|
expect(resp.status).to.eq(200);
|
||||||
|
// We expect to have a valid authorization header returned (with our auth token)
|
||||||
|
expect(resp.headers).to.have.property('authorization');
|
||||||
|
|
||||||
|
// Initialize our AuthTokenInfo object from the authorization header.
|
||||||
|
const authheader = resp.headers.authorization as string;
|
||||||
|
const authinfo: AuthTokenInfo = new AuthTokenInfo(authheader);
|
||||||
|
|
||||||
|
// Save our AuthTokenInfo object to our dsAuthInfo UI cookie
|
||||||
|
// This ensures the UI will recognize we are logged in on next "visit()"
|
||||||
|
cy.setCookie(TOKENITEM, JSON.stringify(authinfo));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Add as a Cypress command (i.e. assign to 'cy.login')
|
// Add as a Cypress command (i.e. assign to 'cy.login')
|
||||||
|
@@ -42,7 +42,12 @@ afterEach(() => {
|
|||||||
// Default values listed here are all valid for the Demo Entities Data set available at
|
// Default values listed here are all valid for the Demo Entities Data set available at
|
||||||
// https://github.com/DSpace-Labs/AIP-Files/releases/tag/demo-entities-data
|
// https://github.com/DSpace-Labs/AIP-Files/releases/tag/demo-entities-data
|
||||||
// (This is the data set used in our CI environment)
|
// (This is the data set used in our CI environment)
|
||||||
export const TEST_REST_BASE_URL = Cypress.env('DSPACE_TEST_REST_BASE_URL') || 'http://localhost:8080';
|
|
||||||
|
// NOTE: FALLBACK_TEST_REST_BASE_URL is only used if Cypress cannot read the REST API BaseURL
|
||||||
|
// from the Angular UI's config.json. See 'getBaseRESTUrl()' in commands.ts
|
||||||
|
export const FALLBACK_TEST_REST_BASE_URL = 'http://localhost:8080/server';
|
||||||
|
|
||||||
|
// Admin account used for administrative tests
|
||||||
export const TEST_ADMIN_USER = Cypress.env('DSPACE_TEST_ADMIN_USER') || 'dspacedemo+admin@gmail.com';
|
export const TEST_ADMIN_USER = Cypress.env('DSPACE_TEST_ADMIN_USER') || 'dspacedemo+admin@gmail.com';
|
||||||
export const TEST_ADMIN_PASSWORD = Cypress.env('DSPACE_TEST_ADMIN_PASSWORD') || 'dspace';
|
export const TEST_ADMIN_PASSWORD = Cypress.env('DSPACE_TEST_ADMIN_PASSWORD') || 'dspace';
|
||||||
// Community/collection/publication used for view/edit tests
|
// Community/collection/publication used for view/edit tests
|
||||||
|
Reference in New Issue
Block a user