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

111 Add Endpoints E2E Tests Phase 2 #118

Merged
merged 1 commit into from
Jun 12, 2024
Merged
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
2 changes: 1 addition & 1 deletion e2e/pages/common.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Locators = {
emailInput: 'role=textbox[name="Email"]',
passwordInput: 'role=textbox[name="Password"]',
submitBtn: 'role=button[name="Submit"]',
createAccountBtn: 'role=button[name="create a new account"]',
createAccountBtn: 'role=button[name="Create a new account."]',
registerBtn: 'role=button[name="Register"]',
logOutBtn: 'text=Logout',
} as const;
Expand Down
94 changes: 94 additions & 0 deletions e2e/tests/endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ test.describe('Endpoint Tests', () => {
await quickstart.authenticate();
});

test('Baseline Authentication', async () => {
await quickstart.logOut();
await quickstart.navToRegister();
});

test('GET /app/me', async () => {
const cookies = await browserContext.cookies();
const appAtCookie = cookies.find(cookie => cookie.name === 'app.at');
Expand Down Expand Up @@ -109,4 +114,93 @@ test.describe('Endpoint Tests', () => {
const pkceCookie = cookies.find(cookie => cookie.name === 'app.pkce_v');
expect(pkceCookie).toBeDefined();
});
test('GET /app/register', async () => {
await quickstart.logOut();

let is302 = false;
let codeChallengePassed = false;
let redirectUrlEncoded: string | null = null;

await page.route('**/app/register', route => route.continue());

page.on('response', async response => {
if (
response.url().includes('/app/register') &&
response.status() === 302
) {
is302 = true;
const locationHeader = response.headers()?.location;
if (locationHeader) {
expect(locationHeader).toContain('oauth2/register?');
const queryParameters = new URLSearchParams(
locationHeader.split('?')[1],
);
const codeChallenge = queryParameters.get('code_challenge');
const codeChallengeMethod = queryParameters.get(
'code_challenge_method',
);
expect(codeChallenge).toBeDefined();
expect(codeChallengeMethod).toBe('S256');
codeChallengePassed = true;
redirectUrlEncoded = queryParameters.get('redirect_uri');
}
}
});

await quickstart.navToRegister();

expect(is302).toBe(true);
expect(codeChallengePassed).toBe(true);
expect(redirectUrlEncoded).not.toBeNull();
expect(redirectUrlEncoded!).toContain('/app/callback');

const cookies = await browserContext.cookies();
const pkceCookie = cookies.find(cookie => cookie.name === 'app.pkce_v');
expect(pkceCookie).toBeDefined();
});

test('POST /app/refresh/{clientId}', async ({ browserName }) => {
const cookies = await browserContext.cookies();
const appRtCookie = cookies.find(cookie => cookie.name === 'app.rt');
const appAtCookie = cookies.find(cookie => cookie.name === 'app.at');
const appAtExpCookie = cookies.find(cookie => cookie.name === 'app.at_exp');
const clientId = 'e9fdb985-9173-4e01-9d73-ac2d60d1dc8e';

expect(appRtCookie).toBeDefined();
expect(appAtExpCookie).toBeDefined();

const originalExpireTime = appAtExpCookie?.value;

const response = await browserContext.request.post(
`http://localhost:9011/app/refresh/${clientId}`,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${appAtCookie?.value}`,
'app.rt': `${appRtCookie?.value}`,
Origin: `http://localhost:${process.env.PORT}`,
Referer: `http://localhost:${process.env.PORT}/`,
},
},
);

expect(response.status()).toBe(200);

const newCookies = await browserContext.cookies();
const newAppAtExpCookie = newCookies.find(
cookie => cookie.name === 'app.at_exp',
);
if (browserName === 'webkit') {
const filterCookie = newCookies.filter(
cookie => cookie.name === 'app.at_exp',
);
const lastCookie = filterCookie[filterCookie.length - 1];
const newExpireTime = lastCookie?.value;

expect(newExpireTime).not.toEqual(originalExpireTime);
} else {
expect(newAppAtExpCookie?.value).not.toEqual(originalExpireTime);
}
expect(newAppAtExpCookie).toBeDefined();
});
});