Init e2e tests

parent 7194bb00
......@@ -74,6 +74,9 @@ local.properties
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
.idea/
*.iml
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
......
......@@ -54,7 +54,7 @@ The service worker initialization code is commented out by default. To enable it
```html
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./service-worker.js').then(function() {
navigator.serviceWorker.register('./service-worker.js').then(function () {
console.log('Service Worker Registered');
});
}
......
import { AxiosInstance } from 'axios';
import {createAdminAxiosInstance, createUserAxiosInstance} from './util/TokenUtils';
let userAxios: AxiosInstance;
let adminAxios: AxiosInstance;
describe('Users', () => {
beforeAll(async () => {
userAxios = await createUserAxiosInstance();
adminAxios = await createAdminAxiosInstance();
});
it('Should a standard user not be allowed to get all authorities', async (done) =>
await userAxios.get('/api/users/authorities')
.then(() => done('Should status be 401'))
.catch((error) => {
expect(error.message).toContain("Vous n'avez pas les droits");
done();
})
);
it('Should an admin get all authorities', async () => {
const { data: authorities } = await adminAxios.get('/api/users/authorities');
expect(authorities.length).toBe(2);
});
});
import ProvidesCallback = jest.ProvidesCallback;
import DoneCallback = jest.DoneCallback;
import { waitFor } from './TimerUtils';
const runTest = (handler: ProvidesCallback) =>
new Promise((resolve, reject) => {
const doneCallback = (): DoneCallback => {
const callback: any = (err: any) => (err ? reject(err) : resolve());
callback.fail = () => {};
return callback;
};
const result = handler(doneCallback());
if (result && result.then) {
result.catch(reject).then(resolve);
} else {
resolve();
}
});
const waitBeforeRetry = (time: number) => waitFor(time);
const withRetriesIt = (name: string, fn: ProvidesCallback, maxTries: number, delay: number, timeout?: number) => {
it(
name,
async () => {
let latestError;
for (let retry = 1; retry <= maxTries; retry++) {
try {
await runTest(fn);
return;
} catch (error) {
latestError = error;
await waitBeforeRetry(delay);
}
}
throw latestError;
},
timeout
);
};
export interface RetryConfiguration {
maxTries: number;
delay: number;
}
export const TREEZOR_RETRIES: RetryConfiguration = { maxTries: 20, delay: 500 };
export interface WithRetries {
it: (name: string, fn: ProvidesCallback, timeout?: number) => void;
}
export const withRetries = (conf: RetryConfiguration): WithRetries => ({
it: (name: string, fn: ProvidesCallback, timeout?: number) => withRetriesIt(name, fn, conf.maxTries, conf.delay, timeout),
});
export const waitFor = (delay: number) => new Promise((resolve) => setTimeout(resolve, delay));
import axios, { AxiosInstance } from 'axios';
const SERVER_URL = 'http://localhost:8080';
const enhanceErrorMessage = (error: any) => {
error.message += ` → ${error.response.data.errorType} ("${error.response.data.message}")`;
return Promise.reject(error);
};
export const createAnonymousAxios = () =>
axios.create({
baseURL: SERVER_URL,
});
const createUserAxios = (token: string): AxiosInstance => {
const instance = axios.create({
...{
baseURL: SERVER_URL,
},
headers: {
Authorization: `Bearer ${token}`,
},
});
instance.interceptors.response.use((response) => response, enhanceErrorMessage);
return instance;
};
export const createGivenUserAxios = async (username: string, password: string): Promise<AxiosInstance> => {
const axiosInstance = createAnonymousAxios();
const { data: { 'id_token': token } } = await axiosInstance.post('/api/authenticate', {username, password});
return createUserAxios(token);
};
export const createAdminAxiosInstance = () => createGivenUserAxios('admin', 'admin');
export const createUserAxiosInstance = () => createGivenUserAxios('user', 'user');
module.exports = {
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.ts?$',
testEnvironment: 'node',
setupFilesAfterEnv: ['./jest.setup.js'],
verbose: true,
};
// eslint-disable-next-line no-undef
jest.setTimeout(55000);
This diff is collapsed.
{
"name": "e2e",
"version": "1.0.0",
"description": "End to end tests",
"main": "index.js",
"scripts": {
"e2e": "jest"
},
"author": "Hippolyte Durix",
"license": "",
"dependencies": {
"@types/axios": "^0.14.0",
"axios": "^0.20.0",
"typescript": "^4.0.2"
},
"devDependencies": {
"@types/jest": "^26.0.10",
"@types/node": "^14.6.0",
"jest": "^26.4.1",
"ts-jest": "^26.2.0"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment