Skip to content

Commit

Permalink
test: move api mocks to separate folders
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalinDe committed Jan 17, 2024
1 parent 1f06204 commit 21b91cf
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 60 deletions.
2 changes: 1 addition & 1 deletion web/frontend/tests/footer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from '@playwright/test';
import { default as i18n } from 'i18next';
import { initI18n, setUp } from './shared';
import { mockPersonalInfo } from './mocks';
import { mockPersonalInfo } from './mocks/api';

initI18n();

Expand Down
7 changes: 4 additions & 3 deletions web/frontend/tests/formIndex.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { expect, test } from '@playwright/test';
import { default as i18n } from 'i18next';
import { assertHasFooter, assertHasNavBar, initI18n, logIn, setUp, translate } from './shared';
import { SCIPER_ADMIN, SCIPER_USER, mockEvoting, mockPersonalInfo } from './mocks';
import { SCIPER_ADMIN, SCIPER_USER, mockPersonalInfo } from './mocks/api';
import { mockEvoting } from './mocks';
import Forms from './json/formList.json';
import User from './json/personal_info/789012.json';
import Admin from './json/personal_info/123456.json';
import User from './json/api/personal_info/789012.json';
import Admin from './json/api/personal_info/123456.json';

initI18n();

Expand Down
2 changes: 1 addition & 1 deletion web/frontend/tests/forms.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from '@playwright/test';
import { default as i18n } from 'i18next';
import { assertHasFooter, assertHasNavBar, initI18n, logIn, setUp, translate } from './shared';
import { SCIPER_ADMIN, SCIPER_USER, mockPersonalInfo } from './mocks';
import { SCIPER_ADMIN, SCIPER_USER, mockPersonalInfo } from './mocks/api';
import { FORMID, mockFormsFormID, mockDKGActors } from './mocks/evoting';
import { mockProxies } from './mocks/api';

Expand Down
53 changes: 0 additions & 53 deletions web/frontend/tests/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,3 @@
export const SCIPER_ADMIN = '123456';
export const SCIPER_USER = '789012';

export async function mockPersonalInfo(page: page, sciper?: string) {
// clear current mock
await page.unroute('/api/personal_info');
await page.route('/api/personal_info', async (route) => {
if (sciper) {
route.fulfill({ path: `./tests/json/personal_info/${sciper}.json` });
} else {
route.fulfill({ status: 401, contentType: 'text/html', body: 'Unauthenticated' });
}
});
}

export async function mockGetDevLogin(page: page) {
await page.route(`/api/get_dev_login/${SCIPER_ADMIN}`, async (route) => {
await route.fulfill({});
});
await page.route(`/api/get_dev_login/${SCIPER_USER}`, async (route) => {
await route.fulfill({});
});
if (
process.env.REACT_APP_SCIPER_ADMIN !== undefined &&
process.env.REACT_APP_SCIPER_ADMIN !== SCIPER_ADMIN
) {
// dummy route for "Login" button depending on local configuration
await page.route(`/api/get_dev_login/${process.env.REACT_APP_SCIPER_ADMIN}`, async (route) => {
await route.fulfill({});
});
}
}

export async function mockLogout(page: page) {
await page.route('/api/logout', async (route) => {
await route.fulfill({});
});
}

export async function mockProxy(page: page) {
await page.route('/api/config/proxy', async (route) => {
await route.fulfill({
status: 200,
contentType: 'text/html',
body: `${process.env.DELA_PROXY_URL}`,
headers: {
'set-cookie':
'connect.sid=s%3A5srES5h7hQ2fN5T71W59qh3cUSQL3Mix.fPoO3rOxui8yfTG7tFd7RPyasaU5VTkhxgdzVRWJyNk',
},
});
});
}

export async function mockEvoting(page: page, empty: boolean = true) {
// clear current mock
await page.unroute(`${process.env.DELA_PROXY_URL}/evoting/forms`);
Expand Down
52 changes: 52 additions & 0 deletions web/frontend/tests/mocks/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
export const SCIPER_ADMIN = '123456';
export const SCIPER_USER = '789012';

export async function mockProxy(page: page) {
await page.route('/api/config/proxy', async (route) => {
await route.fulfill({
status: 200,
contentType: 'text/html',
body: `${process.env.DELA_PROXY_URL}`,
headers: {
'set-cookie':
'connect.sid=s%3A5srES5h7hQ2fN5T71W59qh3cUSQL3Mix.fPoO3rOxui8yfTG7tFd7RPyasaU5VTkhxgdzVRWJyNk',
},
});
});
}

export async function mockProxies(page: page, workerNumber: number) {
await page.route(`/api/proxies/grpc%3A%2F%2Fdela-worker-${workerNumber}%3A2000`, async (route) => {
if (route.request().method() === 'OPTIONS') {
Expand All @@ -16,3 +33,38 @@ export async function mockProxies(page: page, workerNumber: number) {
});
}

export async function mockPersonalInfo(page: page, sciper?: string) {
// clear current mock
await page.unroute('/api/personal_info');
await page.route('/api/personal_info', async (route) => {
if (sciper) {
route.fulfill({ path: `./tests/json/api/personal_info/${sciper}.json` });
} else {
route.fulfill({ status: 401, contentType: 'text/html', body: 'Unauthenticated' });
}
});
}

export async function mockGetDevLogin(page: page) {
await page.route(`/api/get_dev_login/${SCIPER_ADMIN}`, async (route) => {
await route.fulfill({});
});
await page.route(`/api/get_dev_login/${SCIPER_USER}`, async (route) => {
await route.fulfill({});
});
if (
process.env.REACT_APP_SCIPER_ADMIN !== undefined &&
process.env.REACT_APP_SCIPER_ADMIN !== SCIPER_ADMIN
) {
// dummy route for "Login" button depending on local configuration
await page.route(`/api/get_dev_login/${process.env.REACT_APP_SCIPER_ADMIN}`, async (route) => {
await route.fulfill({});
});
}
}

export async function mockLogout(page: page) {
await page.route('/api/logout', async (route) => {
await route.fulfill({});
});
}
2 changes: 1 addition & 1 deletion web/frontend/tests/navbar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
logIn,
setUp,
} from './shared';
import { SCIPER_USER, mockLogout, mockPersonalInfo } from './mocks';
import { SCIPER_USER, mockLogout, mockPersonalInfo } from './mocks/api';

initI18n();

Expand Down
2 changes: 1 addition & 1 deletion web/frontend/tests/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
mockLogout,
mockPersonalInfo,
mockProxy,
} from './mocks';
} from './mocks/api';

export function initI18n() {
i18n.init({
Expand Down

0 comments on commit 21b91cf

Please sign in to comment.