Skip to content

Commit

Permalink
Merge pull request #493 from parlemonde/new-e2e-test
Browse files Browse the repository at this point in the history
New e2e test
  • Loading branch information
yudino authored Dec 12, 2022
2 parents 6a94754 + e95b4de commit 1b3c3c6
Show file tree
Hide file tree
Showing 27 changed files with 808 additions and 32 deletions.
263 changes: 244 additions & 19 deletions .pnp.cjs

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
19 changes: 13 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,24 @@
"testEnvironment": "node",
"verbose": true,
"forceExit": true,
"testMatch": [
"**/**/*.test.t(s|sx)"
],
"moduleDirectories": [
".",
"types",
"node_modules"
],
"roots": [
"<rootDir>/src/",
"<rootDir>/src/svg",
"<rootDir>/server/",
"<rootDir>/types/"
"<rootDir>/server/"
],
"transform": {
"^.+\\.(t|j)sx?$": "@swc/jest"
},
"moduleNameMapper": {
"\\.svg": "<rootDir>/__mocks__/svg.js",
"types/(.*)$": "<rootDir>/types/$1",
"server/(.*)$": "<rootDir>/server/$1",
"src/(.*)$": "<rootDir>/src/$1"
"\\.svg": "<rootDir>/__mocks__/svg.js"
}
},
"resolutions": {
Expand Down Expand Up @@ -83,6 +87,7 @@
"@types/sharp": "0.30.4",
"@types/sortablejs": "1.13.0",
"@types/string-similarity": "4.0.0",
"@types/supertest": "^2.0.12",
"@types/three": "0.141.0",
"@types/uuid": "8.3.4",
"@types/vimeo": "2.1.4",
Expand All @@ -103,6 +108,8 @@
"jest": "28.1.1",
"nodemon": "2.0.18",
"prettier": "2.7.1",
"sqlite3": "^5.1.2",
"supertest": "^6.3.3",
"typescript": "4.7.4",
"webpack": "5.73.0",
"webpack-bundle-analyzer": "4.5.0"
Expand Down
137 changes: 137 additions & 0 deletions server/__tests__/game.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import type { Request as ExpressRequest, Response as ExpressResponse } from 'express';
import supertest from 'supertest';

import { getApp } from '../app';
import { Activity } from '../entities/activity';
import { User } from '../entities/user';
import { Village } from '../entities/village';
import { appDataSource, fakeUser, loginUser } from './mock';

// Mock connection to database to avoid error message in console.
jest.mock('../utils/database', () => ({
connection: Promise.resolve(),
}));
// Mock nodemailer to avoid message in console.
jest.mock('../emails/nodemailer', () => ({
__esModule: true,
getNodeMailer: async () => ({
t: null,
}),
}));

// Mock frontend NextJS library. We don't need it for testing.
jest.mock('next', () => ({
__esModule: true,
default: () => ({
getRequestHandler: () => (_req: ExpressRequest, res: ExpressResponse) => {
res.sendJSON({ isFrontend: true });
},
prepare: () => Promise.resolve(),
}),
}));

jest.mock('../authentication/login', () => ({
__esModule: true,
login: async () => ({
user: fakeUser,
}),
}));

export const activity = {
id: 18,
type: 4,
phase: 2,
status: 0,
data: {
game1: { video: 'https://vimeo.com/36273186', gameId: 1, origine: 'b', signification: 'a', fakeSignification1: 'c', fakeSignification2: 'd' },
game2: {
video: 'https://vimeo.com/134108914',
gameId: 2,
origine: 'origin',
signification: 'bubble',
fakeSignification1: 'test',
fakeSignification2: 'test',
},
game3: {
video: 'https://vimeo.com/221414987',
gameId: 3,
origine: 'origin',
signification: 'real',
fakeSignification1: 'fake 1',
fakeSignification2: 'fake 2',
},
draftUrl: '/creer-un-jeu/mimique/3',
presentation: 'La classe de CE 2 à Paris',
},
content: [{ id: 0, type: 'text', value: '' }],
userId: 6,
villageId: 1,
createDate: Date,
updateDate: Date,
deleteDate: null,
subType: 0,
responseActivityId: null,
responseType: null,
isPinned: 0,
displayUser: 0,
games: [],
images: [],
user: User,
village: Village,
responseActivity: Activity,
};

export const mimique = {
id: 1,
type: 0,
content: { fakeSignification1: 'c', fakeSignification2: 'd', origine: 'b', signification: 'a', video: 'https://vimeo.com/36273186' },
userId: 6,
villageId: 1,
activityId: 18,
value: '',
};

describe('game', () => {
beforeAll(() => {
return appDataSource.initialize();
});
afterAll(() => {
// return conn.close();
return appDataSource.destroy();
});

describe('get games', () => {
const auth = { token: '' };
beforeAll(async () => {
return loginUser(auth);
});

describe('given game does not exist', () => {
it('should return 404', async () => {
const gameId = 'game-123';
try {
const app = await getApp();
await supertest(app).get(`/api/games/${gameId}`).expect(404);
} catch (e) {
expect(400);
}
});
});

describe('given game does exist', () => {
it('should return 200 status and the game', async () => {
const game = mimique;
try {
const app = await getApp();
const { body, statusCode } = await supertest(app)
.get(`/api/games/${game.id}`)
.set({ Authorization: 'bearer ' + auth.token, 'Content-Type': 'application/json' });
expect(statusCode).toBe(200);
expect(body.id).toEqual(game.id);
} catch (e) {
expect(400);
}
});
});
});
});
58 changes: 58 additions & 0 deletions server/__tests__/mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import path from 'path';
import supertest from 'supertest';
import { DataSource } from 'typeorm';

import { getApp } from '../app';

export const appDataSource = new DataSource({
type: 'sqlite',
database: ':memory:',
dropSchema: true,
entities: [path.join(__dirname, '../entities/*.js')],
synchronize: true,
logging: false,
});

/**
* function to create a token for the use of PLM's api
* @param auth empty object with token string property
* @returns auth with token inside
*/
export function loginUser(auth: { token: string }) {
return async function () {
const app = await getApp();
supertest(app)
.post('/login')
.send({
email: '[email protected]',
password: 'helloWorld*',
})
.expect(200)
.end(function (err, res) {
if (err) throw err;
return (auth.token = res.body.token);
});
};
}
/**
* Mock for fake user to the response for login request
*/
export const fakeUser = {
id: 1,
email: '[email protected]',
pseudo: 'teacher1',
level: 'CM1',
school: 'École polyvalente publique Tandou',
city: 'Paris',
postalCode: '75019',
address: '16 Rue Tandou, 75019 Paris',
avatar: null,
displayName: null,
accountRegistration: 0,
passwordHash: '$argon2i$v=19$m=16,t=2,p=1$cTY0aFpyUmF2ZkhERnRSQQ$j7XF79KQqmGGay1bqtxNuQ',
firstLogin: 3,
type: 0,
villageId: 1,
country: { isoCode: 'FR', name: 'France' },
position: { lat: 48.8863442, lng: 2.380321 },
};
70 changes: 70 additions & 0 deletions server/__tests__/server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { Request as ExpressRequest, Response as ExpressResponse } from 'express';
import supertest from 'supertest';

import { getApp } from '../app';
import { appDataSource, fakeUser } from './mock';

// Mock connection to database to avoid error message in console.
jest.mock('../utils/database', () => ({
connection: Promise.resolve(),
}));
// Mock nodemailer to avoid message in console.
jest.mock('../emails/nodemailer', () => ({
__esModule: true,
getNodeMailer: async () => ({
t: null,
}),
}));

// Mock frontend NextJS library. We don't need it for testing.
jest.mock('next', () => ({
__esModule: true,
default: () => ({
getRequestHandler: () => (_req: ExpressRequest, res: ExpressResponse) => {
res.sendJSON({ isFrontend: true });
},
prepare: () => Promise.resolve(),
}),
}));

jest.mock('../authentication/login', () => ({
__esModule: true,
login: async () => ({
user: fakeUser,
}),
}));

describe('test entry point', () => {
beforeAll(() => {
return appDataSource.initialize();
});
afterAll(() => {
// return conn.close();
return appDataSource.destroy();
});
describe('server and login', () => {
describe('server is OK', () => {
it('should return 200', async () => {
try {
const app = await getApp();

await supertest(app).get('/api').expect(200);
} catch (e) {
expect(400);
}
});
});

describe('Login is', () => {
it('should return 200', async () => {
try {
const app = await getApp();
const response = await supertest(app).post('/login').set('Accept', 'application/json');
expect(response.status).toBe(200);
} catch (e) {
expect(400);
}
});
});
});
});
62 changes: 62 additions & 0 deletions server/__tests__/story.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { Request as ExpressRequest, Response as ExpressResponse } from 'express';
import supertest from 'supertest';

import { getApp } from '../app';
import { appDataSource, fakeUser, loginUser } from './mock';

// Mock connection to database to avoid error message in console.
jest.mock('../utils/database', () => ({
connection: Promise.resolve(),
}));
// Mock nodemailer to avoid message in console.
jest.mock('../emails/nodemailer', () => ({
__esModule: true,
getNodeMailer: async () => ({
t: null,
}),
}));

// Mock frontend NextJS library. We don't need it for testing.
jest.mock('next', () => ({
__esModule: true,
default: () => ({
getRequestHandler: () => (_req: ExpressRequest, res: ExpressResponse) => {
res.sendJSON({ isFrontend: true });
},
prepare: () => Promise.resolve(),
}),
}));

jest.mock('../authentication/login', () => ({
__esModule: true,
login: async () => ({
user: fakeUser,
}),
}));

describe('Story api test', () => {
beforeAll(() => {
return appDataSource.initialize();
});
afterAll(() => {
// return conn.close();
return appDataSource.destroy();
});
describe('Images use in original stories', () => {
const auth = { token: '' };
beforeAll(async () => {
return loginUser(auth);
});

describe('api call images/all', () => {
it('should return ALL images', async () => {
try {
const app = await getApp();
await supertest(app).get(`/api/stories/all`).expect(200);
} catch (e) {
expect(404);
}
});
});
});
});
Loading

0 comments on commit 1b3c3c6

Please sign in to comment.