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 1 #114

Merged
merged 3 commits into from
Jun 6, 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 @@ -7,7 +7,7 @@ const Locators = {
submitBtn: 'role=button[name="Submit"]',
createAccountBtn: 'role=button[name="create a new account"]',
registerBtn: 'role=button[name="Register"]',
logOutBtn: 'role=button[name="Logout"]',
logOutBtn: 'text=Logout',
} as const;

type LocatorsKey = keyof typeof Locators;
Expand Down
112 changes: 112 additions & 0 deletions e2e/tests/endpoints.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { Page, test, BrowserContext, expect } from '@playwright/test';
import { quickstartPage } from '../pages/common.page';

test.describe('Endpoint Tests', () => {
let page: Page;
let quickstart: quickstartPage;
let browserContext: BrowserContext;

test.beforeAll(async ({ browser }) => {
browserContext = await browser.newContext();
page = await browserContext.newPage();
quickstart = new quickstartPage(page);
await page.goto('/');
await quickstart.navToLogIn();
await quickstart.authenticate();
});

test('GET /app/me', async () => {
const cookies = await browserContext.cookies();
const appAtCookie = cookies.find(cookie => cookie.name === 'app.at');

const response = await browserContext.request.get(
'http://localhost:9011/app/me',
{
headers: {
Authorization: `Bearer ${appAtCookie?.value}`,
Origin: `http://localhost:${process.env.PORT}`,
Referer: `http://localhost:${process.env.PORT}/`,
},
},
);

expect(response.headers()['content-type']).toContain('application/json');
expect(response.ok()).toBeTruthy();
});

test('GET /app/logout', async () => {
const cookieNames = ['app.at', 'app.idt', 'app.rt', 'app.at_exp'];
const cookiesBeforeLogout = await browserContext.cookies();

cookieNames.forEach(cookieName => {
const cookie = cookiesBeforeLogout.find(
cookie => cookie.name === cookieName,
);
expect(cookie).toBeDefined();
});

let is302 = false;

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

page.on('response', response => {
if (response.url().includes('/app/logout') && response.status() === 302) {
is302 = true;
expect(response.headers()?.location).toContain('oauth2/logout?');
}
});

await quickstart.logOut();
expect(is302).toBe(true);

const cookiesAfterLogout = await browserContext.cookies();
cookieNames.forEach(cookieName => {
const cookie = cookiesAfterLogout.find(
cookie => cookie.name === cookieName,
);
expect(cookie).toBeUndefined();
});
});

test('GET /app/login', async () => {
await quickstart.logOut();

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

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

page.on('response', async response => {
if (response.url().includes('/app/login') && response.status() === 302) {
is302 = true;
const locationHeader = response.headers()?.location;
if (locationHeader) {
expect(locationHeader).toContain('oauth2/authorize?');
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.navToLogIn();

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();
});
});
15 changes: 3 additions & 12 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { defineConfig, devices } from '@playwright/test';

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './e2e',
/* Run tests in files in parallel */
Expand All @@ -22,7 +13,7 @@ export default defineConfig({
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: 'http://localhost:3000',
baseURL: `http://localhost:${process.env.PORT}`,

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
screenshot: 'on',
Expand All @@ -48,8 +39,8 @@ export default defineConfig({

/* Run your local dev server before starting the tests */
webServer: {
command: 'npm run dev',
url: 'http://localhost:3000',
command: `${process.env.SERVER_COMMAND}`,
url: `http://localhost:${process.env.PORT}`,
reuseExistingServer: !process.env.CI,
},
});