Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example assurance test #1022

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
461 changes: 382 additions & 79 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
},
"devDependencies": {
"@adobe/alloy": "^2.19.2",
"@adobe/jwt-auth": "^2.0.0",
"@babel/cli": "^7.12.8",
"@babel/core": "^7.2.2",
"@babel/plugin-proposal-object-rest-spread": "^7.3.2",
Expand All @@ -92,6 +93,7 @@
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-testcafe": "^0.2.1",
"form-data": "^4.0.0",
"force-resolutions": "^1.0.11",
"glob": "^7.1.3",
"handlebars": "^4.7.7",
Expand All @@ -111,7 +113,7 @@
"karma-sauce-launcher": "^4.3.6",
"karma-spec-reporter": "0.0.32",
"lint-staged": "^13.2.1",
"node-fetch": "^2.6.7",
"node-fetch": "^2.6.9",
"prettier": "^1.16.4",
"promise-polyfill": "^8.1.0",
"read-cache": "^1.0.0",
Expand Down
97 changes: 97 additions & 0 deletions test/functional/helpers/assurance/AssuranceRequestHook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
Copyright 2023 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import { RequestHook, t } from "testcafe";

const ASSERTION_DELAY = 200;
const ASSERTION_TIMEOUT = 10000;
const delay = () => {
return new Promise(resolve => setTimeout(resolve, ASSERTION_DELAY));
};

const createRequest = (requestLogs, fetchMore) => {
return {
find: async predicate => {
let i = 0;
for (i = 0; i < requestLogs.length; i += 1) {
if (predicate(requestLogs[i])) {
return requestLogs[i];
}
}
const expirationTime = new Date().getTime() + ASSERTION_TIMEOUT;
while (new Date().getTime() < expirationTime) {
// eslint-disable-next-line no-await-in-loop
await fetchMore();
for (; i < requestLogs.length; i += 1) {
if (predicate(requestLogs[i])) {
return requestLogs[i];
}
}
// eslint-disable-next-line no-await-in-loop
await delay();
}
await t
.expect()
.ok(
"Assurance logs did not contain an event matching the predicate after 10 seconds."
);
return undefined;
},
debug() {
// eslint-disable-next-line no-console
console.log(JSON.stringify(requestLogs, null, 2));
}
};
};

export default class AssuranceRequestHook extends RequestHook {
constructor(assuranceToken, events) {
super();
this.assuranceToken = assuranceToken;
this.events = events;
this.eventsByRequestId = {};
this.requests = [];
}

onRequest(e) {
const url = new URL(e.requestOptions.url);
const requestId = url.searchParams.get("requestId");
if (requestId) {
e.requestOptions.headers[
"X-Adobe-AEP-Validation-Token"
] = this.assuranceToken;
const requestLogs = [];
this.eventsByRequestId[requestId] = requestLogs;
this.requests.push(createRequest(requestLogs, this.fetchMore.bind(this)));
}
}

// eslint-disable-next-line class-methods-use-this
onResponse() {}

async fetchMore() {
// eslint-disable-next-line no-await-in-loop
while (await this.events.advance()) {
const event = this.events.current();
const {
payload: {
attributes: { requestId } = {},
header: { xactionId } = {}
} = {}
} = event;

const id = requestId || xactionId;
if (id && this.eventsByRequestId[id]) {
this.eventsByRequestId[id].push(event);
}
}
}
}
103 changes: 103 additions & 0 deletions test/functional/helpers/assurance/createAssuranceApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2023 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import fetch from "node-fetch";

const CREATE_SESSION_QUERY = `
mutation createSession($session: SessionInput!) {
createSession(session: $session) {
orgId
uuid
name
link
token
}
}`;

const FETCH_EVENTS_QUERY = `
query eventsQuery($sessionUuid: UUID!) {
events(sessionUuid:$sessionUuid,first:100) {
uuid
eventNumber
clientId
timestamp
vendor
type
payload
annotations {
uuid
type
payload
}
}
}`;

export default authHeaders => {
const makeGraphRequest = async (query, variables) => {
const response = await fetch("https://graffias.adobe.io/graffias/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
...authHeaders
},
body: JSON.stringify({ query, variables })
});
const json = await response.json();
if (json.errors) {
throw new Error(json.errors.message);
}
return json.data;
};

return {
createSession: async sessionName => {
const response = await makeGraphRequest(CREATE_SESSION_QUERY, {
session: {
name: sessionName,
link: "testUrl://default"
}
});
const {
createSession: { uuid }
} = response;
return uuid;
},
fetchEvents: sessionUuid => {
const seenUuids = new Set();
let events = [];
let i = 0;
return {
current: () => {
return events[i];
},
advance: async () => {
if (i >= events.length) {
const response = await makeGraphRequest(FETCH_EVENTS_QUERY, {
sessionUuid
});
events = response.events.filter(e => {
if (seenUuids.has(e.uuid)) {
return false;
}
seenUuids.add(e.uuid);
return true;
});
i = 0;
return events.length > 0;
}
i += 1;

return i < events.length;
}
};
}
};
};
28 changes: 28 additions & 0 deletions test/functional/helpers/assurance/getAuthHeaders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2023 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const auth = require("@adobe/jwt-auth");

/**
* Retrieves an access token for the Adobe I/O integration used for
* running end-to-end tests.
* @returns {Promise<string>}
*/
module.exports = async credentials => {
const result = await auth(credentials);
const { access_token: accessToken } = result;
const { orgId, clientId } = credentials;
return {
"x-gw-ims-org-id": orgId,
"x-api-key": clientId,
Authorization: `Bearer ${accessToken}`
};
};
72 changes: 72 additions & 0 deletions test/functional/helpers/assurance/getCredentials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2023 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import chalk from "chalk";
import fs from "fs";

export const IMS_URL = process.env.IMS_URL || "https://ims-na1.adobelogin.com";
// default to Unified JS QE Only org
export const ASSURANCE_ORG_ID =
process.env.ASSURANCE_ORG_ID || "5BFE274A5F6980A50A495C08@AdobeOrg";
// default to "Alloy end to end testing" project
export const ASSURANCE_CLIENT_ID =
process.env.ASSURANCE_CLIENT_ID || "0c1c7478c4994c69866b64c8341578ed";
export const ASSURANCE_TECHNICAL_ACCOUNT_ID =
process.env.ASSURANCE_TECHNICAL_ACCOUNT_ID ||
"[email protected]";
// Use the Adobe developer console to get the client secret for the client ID above.
export const ASSURANCE_CLIENT_SECRET = process.env.ASSURANCE_CLIENT_SECRET;
// Use the Adobe developer console to generate a private key
export const ASSURANCE_PRIVATE_KEY_FILE =
process.env.ASSURANCE_PRIVATE_KEY_FILE;
export const ASSURANCE_PRIVATE_KEY_CONTENTS =
process.env.ASSURANCE_PRIVATE_KEY_CONTENTS;

if (!ASSURANCE_CLIENT_SECRET) {
// eslint-disable-next-line no-console
console.error(
chalk.redBright(
`Failed to read client secret. Please ensure the ASSURANCE_CLIENT_SECRET environment variable is set.`
)
);
}

if (!ASSURANCE_PRIVATE_KEY_FILE && !ASSURANCE_PRIVATE_KEY_CONTENTS) {
// eslint-disable-next-line no-console
console.error(
chalk.redBright(
`Failed to read private key. Please ensure the ASSURANCE_PRIVATE_KEY_FILE or ASSURANCE_PRIVATE_KEY_CONTENTS environment variable is set.`
)
);
}

const privateKey =
ASSURANCE_PRIVATE_KEY_CONTENTS ||
fs.readFileSync(ASSURANCE_PRIVATE_KEY_FILE, "utf8");

if (!privateKey) {
// eslint-disable-next-line no-console
console.error(
chalk.redBright(
`Failed to read private key. Please ensure ASSURANCE_PRIVATE_KEY_FILE exists and is readable.`
)
);
}

export default () => ({
clientId: ASSURANCE_CLIENT_ID,
technicalAccountId: ASSURANCE_TECHNICAL_ACCOUNT_ID,
orgId: ASSURANCE_ORG_ID,
clientSecret: ASSURANCE_CLIENT_SECRET,
privateKey,
metaScopes: ["https://ims-na1.adobelogin.com/s/assurance_automation"],
ims: IMS_URL
});
34 changes: 34 additions & 0 deletions test/functional/helpers/assurance/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2023 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import createAssuranceApi from "./createAssuranceApi";
import getAuthHeaders from "./getAuthHeaders";
import getCredentials from "./getCredentials";
import AssuranceRequestHook from "./AssuranceRequestHook";

let sessionUuid;
let events;

export const createAssuranceRequestHook = async () => {
if (!sessionUuid) {
const credentials = getCredentials();
const authHeaders = await getAuthHeaders(credentials);
const api = createAssuranceApi(authHeaders);
const sessionName = `Tests - ${process.env.USER} - ${Math.random()
.toString(36)
.slice(2)}`;
// eslint-disable-next-line no-console
console.log(`Creating assurance session: ${sessionName}`);
sessionUuid = await api.createSession(sessionName);
events = api.fetchEvents(sessionUuid);
}
return new AssuranceRequestHook(sessionUuid, events);
};
Loading
Loading