first commit

This commit is contained in:
Bastian Wagner
2026-07-31 21:02:47 +02:00
commit 6bea4f766a
512 changed files with 64459 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
import * as request from 'supertest';
import { ADMIN_EMAIL, ADMIN_PASSWORD, APP_URL } from '../utils/constants';
describe('Auth admin (e2e)', () => {
const app = APP_URL;
it('Login: /api/v1/auth/admin/email/login (POST)', () => {
return request(app)
.post('/api/v1/auth/admin/email/login')
.send({ email: ADMIN_EMAIL, password: ADMIN_PASSWORD })
.expect(200)
.expect(({ body }) => {
expect(body.token).toBeDefined();
});
});
it('Login via user endpoint: /api/v1/auth/email/login (POST)', () => {
return request(app)
.post('/api/v1/auth/email/login')
.send({ email: ADMIN_EMAIL, password: ADMIN_PASSWORD })
.expect(422);
});
});

View File

@@ -0,0 +1,104 @@
import { APP_URL, ADMIN_EMAIL, ADMIN_PASSWORD } from '../utils/constants';
import * as request from 'supertest';
import { RoleEnum } from '../../src/roles/roles.enum';
import { StatusEnum } from '../../src/statuses/statuses.enum';
describe('Users admin (e2e)', () => {
const app = APP_URL;
let newUserFirst;
const newUserEmailFirst = `user-first.${Date.now()}@example.com`;
const newUserPasswordFirst = `secret`;
const newUserChangedPasswordFirst = `new-secret`;
const newUserByAdminEmailFirst = `user-created-by-admin.${Date.now()}@example.com`;
const newUserByAdminPasswordFirst = `secret`;
let apiToken;
beforeAll(async () => {
await request(app)
.post('/api/v1/auth/admin/email/login')
.send({ email: ADMIN_EMAIL, password: ADMIN_PASSWORD })
.then(({ body }) => {
apiToken = body.token;
});
await request(app)
.post('/api/v1/auth/email/register')
.send({
email: newUserEmailFirst,
password: newUserPasswordFirst,
firstName: `First${Date.now()}`,
lastName: 'E2E',
});
await request(app)
.post('/api/v1/auth/email/login')
.send({ email: newUserEmailFirst, password: newUserPasswordFirst })
.then(({ body }) => {
newUserFirst = body.user;
});
});
it('Change password for new user: /api/v1/users/:id (PATCH)', () => {
return request(app)
.patch(`/api/v1/users/${newUserFirst.id}`)
.auth(apiToken, {
type: 'bearer',
})
.send({ password: newUserChangedPasswordFirst })
.expect(200);
});
it('Login via registered user: /api/v1/auth/email/login (GET)', () => {
return request(app)
.post('/api/v1/auth/email/login')
.send({ email: newUserEmailFirst, password: newUserChangedPasswordFirst })
.expect(200)
.expect(({ body }) => {
expect(body.token).toBeDefined();
});
});
it('Fail create new user by admin: /api/v1/users (POST)', () => {
return request(app)
.post(`/api/v1/users`)
.auth(apiToken, {
type: 'bearer',
})
.send({ email: 'fail-data' })
.expect(422);
});
it('Success create new user by admin: /api/v1/users (POST)', () => {
return request(app)
.post(`/api/v1/users`)
.auth(apiToken, {
type: 'bearer',
})
.send({
email: newUserByAdminEmailFirst,
password: newUserByAdminPasswordFirst,
firstName: `UserByAdmin${Date.now()}`,
lastName: 'E2E',
role: {
id: RoleEnum.user,
},
status: {
id: StatusEnum.active,
},
})
.expect(201);
});
it('Login via created by admin user: /api/v1/auth/email/login (GET)', () => {
return request(app)
.post('/api/v1/auth/email/login')
.send({
email: newUserByAdminEmailFirst,
password: newUserByAdminPasswordFirst,
})
.expect(200)
.expect(({ body }) => {
expect(body.token).toBeDefined();
});
});
});

View File

@@ -0,0 +1,9 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}

View File

@@ -0,0 +1,198 @@
import * as request from 'supertest';
import {
APP_URL,
TESTER_EMAIL,
TESTER_PASSWORD,
MAIL_HOST,
MAIL_PORT,
} from '../utils/constants';
describe('Auth user (e2e)', () => {
const app = APP_URL;
const mail = `http://${MAIL_HOST}:${MAIL_PORT}`;
const newUserFirstName = `Tester${Date.now()}`;
const newUserLastName = `E2E`;
const newUserEmail = `User.${Date.now()}@example.com`;
const newUserPassword = `secret`;
it('Login: /api/v1/auth/email/login (POST)', () => {
return request(app)
.post('/api/v1/auth/email/login')
.send({ email: TESTER_EMAIL, password: TESTER_PASSWORD })
.expect(200)
.expect(({ body }) => {
expect(body.token).toBeDefined();
expect(body.user.provider).not.toBeDefined();
expect(body.user.hash).not.toBeDefined();
expect(body.user.password).not.toBeDefined();
expect(body.user.previousPassword).not.toBeDefined();
});
});
it('Login via admin endpoint: /api/v1/auth/admin/email/login (POST)', () => {
return request(app)
.post('/api/v1/auth/admin/email/login')
.send({ email: TESTER_EMAIL, password: TESTER_PASSWORD })
.expect(422);
});
it('Login via admin endpoint with extra spaced: /api/v1/auth/admin/email/login (POST)', () => {
return request(app)
.post('/api/v1/auth/admin/email/login')
.send({ email: TESTER_EMAIL + ' ', password: TESTER_PASSWORD })
.expect(422);
});
it('Do not allow register user with exists email: /api/v1/auth/email/register (POST)', () => {
return request(app)
.post('/api/v1/auth/email/register')
.send({
email: TESTER_EMAIL,
password: TESTER_PASSWORD,
firstName: 'Tester',
lastName: 'E2E',
})
.expect(422)
.expect(({ body }) => {
expect(body.errors.email).toBeDefined();
});
});
it('Register new user: /api/v1/auth/email/register (POST)', async () => {
return request(app)
.post('/api/v1/auth/email/register')
.send({
email: newUserEmail,
password: newUserPassword,
firstName: newUserFirstName,
lastName: newUserLastName,
})
.expect(201);
});
it('Login unconfirmed user: /api/v1/auth/email/login (POST)', () => {
return request(app)
.post('/api/v1/auth/email/login')
.send({ email: newUserEmail, password: newUserPassword })
.expect(200)
.expect(({ body }) => {
expect(body.token).toBeDefined();
});
});
it('Confirm email: /api/v1/auth/email/confirm (POST)', async () => {
const hash = await request(mail)
.get('/email')
.then(({ body }) =>
body
.find(
(letter) =>
letter.to[0].address.toLowerCase() ===
newUserEmail.toLowerCase() &&
/.*confirm\-email\/(\w+).*/g.test(letter.text),
)
?.text.replace(/.*confirm\-email\/(\w+).*/g, '$1'),
);
return request(app)
.post('/api/v1/auth/email/confirm')
.send({
hash,
})
.expect(200);
});
it('Login confirmed user: /api/v1/auth/email/login (POST)', () => {
return request(app)
.post('/api/v1/auth/email/login')
.send({ email: newUserEmail, password: newUserPassword })
.expect(200)
.expect(({ body }) => {
expect(body.token).toBeDefined();
});
});
it('Confirmed user retrieve profile: /api/v1/auth/me (GET)', async () => {
const newUserApiToken = await request(app)
.post('/api/v1/auth/email/login')
.send({ email: newUserEmail, password: newUserPassword })
.then(({ body }) => body.token);
await request(app)
.get('/api/v1/auth/me')
.auth(newUserApiToken, {
type: 'bearer',
})
.send()
.expect(({ body }) => {
expect(body.provider).toBeDefined();
expect(body.hash).not.toBeDefined();
expect(body.password).not.toBeDefined();
expect(body.previousPassword).not.toBeDefined();
});
});
it('New user update profile: /api/v1/auth/me (PATCH)', async () => {
const newUserNewName = Date.now();
const newUserNewPassword = 'new-secret';
const newUserApiToken = await request(app)
.post('/api/v1/auth/email/login')
.send({ email: newUserEmail, password: newUserPassword })
.then(({ body }) => body.token);
await request(app)
.patch('/api/v1/auth/me')
.auth(newUserApiToken, {
type: 'bearer',
})
.send({
firstName: newUserNewName,
password: newUserNewPassword,
})
.expect(422);
await request(app)
.patch('/api/v1/auth/me')
.auth(newUserApiToken, {
type: 'bearer',
})
.send({
firstName: newUserNewName,
password: newUserNewPassword,
oldPassword: newUserPassword,
})
.expect(200);
await request(app)
.post('/api/v1/auth/email/login')
.send({ email: newUserEmail, password: newUserNewPassword })
.expect(200)
.expect(({ body }) => {
expect(body.token).toBeDefined();
});
await request(app)
.patch('/api/v1/auth/me')
.auth(newUserApiToken, {
type: 'bearer',
})
.send({ password: newUserPassword, oldPassword: newUserNewPassword })
.expect(200);
});
it('New user delete profile: /api/v1/auth/me (DELETE)', async () => {
const newUserApiToken = await request(app)
.post('/api/v1/auth/email/login')
.send({ email: newUserEmail, password: newUserPassword })
.then(({ body }) => body.token);
await request(app).delete('/api/v1/auth/me').auth(newUserApiToken, {
type: 'bearer',
});
return request(app)
.post('/api/v1/auth/email/login')
.send({ email: newUserEmail, password: newUserPassword })
.expect(422);
});
});

View File

@@ -0,0 +1,7 @@
export const APP_URL = `http://localhost:${process.env.APP_PORT}`;
export const TESTER_EMAIL = 'john.doe@example.com';
export const TESTER_PASSWORD = 'secret';
export const ADMIN_EMAIL = 'admin@example.com';
export const ADMIN_PASSWORD = 'secret';
export const MAIL_HOST = process.env.MAIL_HOST;
export const MAIL_PORT = process.env.MAIL_CLIENT_PORT;