Skip to content

Commit

Permalink
- added pwa test cases
Browse files Browse the repository at this point in the history
- refactored test code
  • Loading branch information
temi committed Sep 24, 2024
1 parent 57af071 commit c757ab3
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 73 deletions.
1 change: 1 addition & 0 deletions src/integration-test/resources/dataset1/loadDataSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var activityProject = {
"isWorks" : false,
"legalCustodianOrganisation" : "Atlas of Living Australia",
"legalCustodianOrganisationType" : "",
"projLifecycleStatus": "published",
"mapLayersConfig" : {
"baseLayers" : [],
"overlays" : []
Expand Down
3 changes: 2 additions & 1 deletion src/test/js/integration/pageobjects/AddBioActivityPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class AddBioActivityPage extends StubbedCasSpec {
}

async at() {
return super.at('Create \| .* \| BioCollect');
let title = await browser.getTitle();
return /Create \| .* \| BioCollect/i.test(title);
}

async setSite(site) {
Expand Down
32 changes: 32 additions & 0 deletions src/test/js/integration/pageobjects/AdminToolsPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const ReloadablePage = require('./ReloadablePage');
class AdminToolsPage extends ReloadablePage {

url = browser.options.testConfig.baseUrl + "/admin/tools";

async open() {
await browser.url(this.url);
}
async at () {
let title = await browser.getTitle();
return /Tools | Admin/i.test(title);
}

get reindexButton () { return $('#btnReindexAll'); }
get clearMetaDataCacheButton () { return $("#btnClearMetadataCache"); }

async clearMetadata() {
await this.clearMetaDataCacheButton.click();
}

async reindex() {
await browser.url(this.url)
await this.reindexButton.click();
await this.hasBeenReloaded();
}

async clearCache() {
await this.clearMetadata();
}
}

module.exports = new AdminToolsPage();
37 changes: 37 additions & 0 deletions src/test/js/integration/pageobjects/PwaAppPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const StubbedCasSpec = require('./StubbedCasSpec.js')
class PwaAppPage extends StubbedCasSpec {
url = browser.options.testConfig.pwaUrl;

get getStarted() {
return $('#getStarted');
}

async open() {
await browser.url(this.url);
}

async at() {
return /BioCollect PWA/i.test(await browser.getTitle());
}


async saveToken() {
await browser.execute(function () {
return localStorage
});
}

async start() {
await this.getStarted.click();
}

async serviceWorkerReady() {
return await browser.execute( async () => {
return await navigator.serviceWorker.ready
.then(() => {console.log("installed!"); return true})
.catch(() => {console.log("not installed!"); return false});
});
}
}

module.exports = new PwaAppPage();
45 changes: 45 additions & 0 deletions src/test/js/integration/pageobjects/ReloadablePage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const StubbedCasSpec = require('./StubbedCasSpec.js')
class ReloadablePage extends StubbedCasSpec {

atCheckTime = 0

/**
* Extends the standard at check to set a javascript variable that can later be
* checked to detect a pageload.
*/
async verifyAt() {
let result = await this.at();
if (result) {
await this.saveAtCheckTime()
}
return result
}

async saveAtCheckTime() {
this.atCheckTime = Date.now().toString()
let self = this;
await browser.execute(() => {
window.atCheckTime = self.atCheckTime;
});
}

async getAtCheckTime() {
return await browser.execute(() => {
return window.atCheckTime;
});
}


/** Returns true if the page has been reloaded since the most recent "at" check */
async hasBeenReloaded() {
return browser.waitUntil(async() => {
return ! await this.getAtCheckTime();
},
{
timeout: browser.options.testConfig.waitforTimeout,
timeoutMsg: "Page did not reload after reindexing"
});
}
}

module.exports = ReloadablePage;
149 changes: 90 additions & 59 deletions src/test/js/integration/pageobjects/StubbedCasSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const util = require('node:util');
const execFile = util.promisify(require('node:child_process').execFile);
const path = require('node:path');
class StubbedCasSpec {
READ_ONLY_USER_ID = '1000'
GRANT_MANAGER_USER_ID = '1001'
MERIT_ADMIN_USER_ID = '1002'
ALA_ADMIN_USER_ID = '2000'

loggedInUser = null;
baseUrl = '';
serverUrl = '';
Expand All @@ -28,22 +33,37 @@ class StubbedCasSpec {
this.serverUrl = this.testConfig.serverUrl;
}

async at(regexTitle) {
return await browser.waitUntil( async () => {
const title = await browser.getTitle();
const regex = new RegExp(regexTitle);
return regex.test(title);
}, {
timeout: 5000,
});
async at() {
throw new Error('Method at() is not implemented');
}

async loginAsAlaAdmin() {
await this.login({userId:this.ALA_ADMIN_USER_ID, role:"ROLE_ADMIN", userName: '[email protected]', email: '[email protected]', firstName:"ALA", lastName:"Administrator"})
}

async loginAsPwaUser() {
let userDetails = this.getUserDetails('1');
let {tokenSet, profile} = await this.setupOidcAuthForUser(userDetails);
tokenSet.profile = profile;
let key = this.localStorageTokenKey;
await browser.execute(function (key, tokenSet) {
localStorage.setItem(key, JSON.stringify(tokenSet));
console.log('Token set in local storage');
console.log(localStorage.getItem(key));
}, key, tokenSet);
return tokenSet;
}

get localStorageTokenKey() {
return `oidc.user:${browser.options.testConfig.wireMockBaseUrl}/cas/oidc/.well-known:${this.testConfig.oidc.clientId}`
}

async loginAsUser(userId) {
const reservedUserIds = [
'MERIT_ADMIN_USER_ID',
'READ_ONLY_USER_ID',
'GRANT_MANAGER_USER_ID',
'ALA_ADMIN_USER_ID'
this.MERIT_ADMIN_USER_ID,
this.READ_ONLY_USER_ID,
this.GRANT_MANAGER_USER_ID,
this.ALA_ADMIN_USER_ID
];

// Check if userId is one of the reserved IDs
Expand All @@ -52,15 +72,19 @@ class StubbedCasSpec {
}

// User login data
const userDetails = {
const userDetails = this.getUserDetails(userId);

// Call the login function, assuming it exists
await this.login(userDetails);
}

getUserDetails(userId) {
return {
userId: userId,
email: `user${userId}@nowhere.com`,
firstName: "MERIT",
lastName: `User ${userId}`
};

// Call the login function, assuming it exists
await this.login(userDetails);
}

async login(userDetails) {
Expand Down Expand Up @@ -124,7 +148,31 @@ class StubbedCasSpec {
}

// Define ID Token Claims
const idTokenClaims = {
const idTokenClaims = this.getUserTokenClaim(userDetails, roles, clientId);
const idToken = await this.signPayload(idTokenClaims);
let tokenSet = this.getUserToken(idToken);

// Stub the POST request for token exchange (using nock)
await this.setupAccessToken('/cas/oidc/oidcAccessToken', tokenSet, base64EncodedAuth);
const profile = await this.setupProfileEndpoint(userDetails, base64EncodedAuth);

// Return the ID token
return {tokenSet, profile};
}

getUserToken(idToken) {
return {
access_token: idToken,
id_token: idToken,
refresh_token: null,
token_type: 'bearer',
expires_in: 86400,
scope: 'openid profile ala roles email'
};
}

getUserTokenClaim(userDetails, roles, clientId) {
return {
at_hash: 'KX-L2Fj6Z9ow-gOpYfehRA',
sub: userDetails.userId,
email_verified: true,
Expand All @@ -148,22 +196,6 @@ class StubbedCasSpec {
email: userDetails.email,
scope: this.testConfig.oidc.scope
};

const idToken = await this.signPayload(idTokenClaims);

// Stub the POST request for token exchange (using nock)
await this.setupAccessToken('/cas/oidc/oidcAccessToken', {
access_token: idToken,
id_token: idToken,
refresh_token: null,
token_type: 'bearer',
expires_in: 86400,
scope: 'openid profile ala roles email'
}, base64EncodedAuth);
await this.setupProfileEndpoint(userDetails, base64EncodedAuth);

// Return the ID token
return idToken;
}

async setupAccessToken(url, body, base64EncodedAuth){
Expand Down Expand Up @@ -224,6 +256,7 @@ class StubbedCasSpec {
}
});

return profile;
}

async setupTokenForSystem() {
Expand All @@ -232,7 +265,29 @@ class StubbedCasSpec {
let clientSecret = this.testConfig.webservice["client-secret"]
let base64EncodedAuth = "Basic " + Buffer.from(`${clientId}:${clientSecret}`).toString('base64');

let idTokenClaims = {
let idTokenClaims = this.getTokenClaims(clientId)
// Simulate generating JWT (signing token) - Assuming you have RSA private key in 'privateKey'
let idToken = await this.signPayload(idTokenClaims);
let token = this.getToken(idToken);

await this.setupAccessToken("/cas/oidc/oidcAccessToken", token, base64EncodedAuth);

return idToken
}

getToken(idToken) {
let token = {}
token.access_token = idToken
token.id_token = idToken
token.refresh_token = null
token.token_type = "bearer"
token.expires_in = 86400
token.scope = this.testConfig.webservice["jwt-scopes"]
return token;
}

getTokenClaims(clientId) {
return {
at_hash: "KX-L2Fj6Z9ow-gOpYfehRA",
sub: clientId,
amr: "DelegatedClientAuthenticationHandler",
Expand All @@ -247,20 +302,7 @@ class StubbedCasSpec {
iat: Math.floor(Date.now() / 1000), // Issued at
jti: "id-system",
scope: this.testConfig.webservice["jwt-scopes"]
}
// Simulate generating JWT (signing token) - Assuming you have RSA private key in 'privateKey'
let idToken = await this.signPayload(idTokenClaims);
let token = {}
token.access_token = idToken
token.id_token = idToken
token.refresh_token = null
token.token_type = "bearer"
token.expires_in = 86400
token.scope = this.testConfig.webservice["jwt-scopes"]

await this.setupAccessToken("/cas/oidc/oidcAccessToken", token, base64EncodedAuth);

return idToken
};
}

async createPrivateKey() {
Expand Down Expand Up @@ -296,17 +338,6 @@ class StubbedCasSpec {
args.push(this.testConfig.databasePassword);
}
const {error, stdout, stderr} = await execFile(this.testConfig.datasetLoadScript, args)
// , (error, stdout, stderr) => {
// if (error) {
// console.error(`Error loading dataset ${dataSet}: ${error.message}`);
// return;
// }
// if (stderr) {
// console.error(`Error loading dataset ${dataSet}: ${stderr}`);
// return;
// }
// console.log(`Dataset ${dataSet} loaded successfully: ${stdout}`);
// });
console.log(`result of command ${JSON.stringify(error)} \n\n ${JSON.stringify(stderr)} \n\n ${JSON.stringify(stdout)}`);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/js/integration/pageobjects/ViewBioActivityPage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const StubbedCasSpec = require('./StubbedCasSpec.js')
class ViewBioActivityPage extends StubbedCasSpec {
async at() {
return await super.at('View \\| .* \\| BioCollect');
var title = await browser.getTitle();
return /View \\| .* \\| BioCollect/i.test(title);
}
}

Expand Down
Loading

0 comments on commit c757ab3

Please sign in to comment.