mirror of
https://github.com/immich-app/immich.git
synced 2025-01-03 13:09:27 +02:00
chore(web,server): run code coverage reports (#1313)
* chore(web,server): run code coverage reports * chore(tests): fail test check if coverage drops * chore: disable e2e until they are fixed * chore(web): coverage threshold
This commit is contained in:
parent
6db541c89b
commit
755a1331da
16
.github/workflows/test.yml
vendored
16
.github/workflows/test.yml
vendored
@ -6,17 +6,17 @@ on:
|
|||||||
branches: [main]
|
branches: [main]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
e2e-tests:
|
# e2e-tests:
|
||||||
name: Run end-to-end test suites
|
# name: Run end-to-end test suites
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
# runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
# steps:
|
||||||
- name: Checkout code
|
# - name: Checkout code
|
||||||
uses: actions/checkout@v3
|
# uses: actions/checkout@v3
|
||||||
|
|
||||||
- name: Run Immich Server E2E Test
|
# - name: Run Immich Server E2E Test
|
||||||
run: docker-compose -f ./docker/docker-compose.test.yml --env-file ./docker/.env.test up --abort-on-container-exit --exit-code-from immich-server-test
|
# run: docker-compose -f ./docker/docker-compose.test.yml --env-file ./docker/.env.test up --abort-on-container-exit --exit-code-from immich-server-test
|
||||||
|
|
||||||
server-unit-tests:
|
server-unit-tests:
|
||||||
name: Run server unit test suites and checks
|
name: Run server unit test suites and checks
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@
|
|||||||
.idea
|
.idea
|
||||||
|
|
||||||
docker/upload
|
docker/upload
|
||||||
|
coverage
|
||||||
|
@ -6,23 +6,19 @@ import { when } from 'jest-when';
|
|||||||
import { UserService } from './user.service';
|
import { UserService } from './user.service';
|
||||||
import { UpdateUserDto } from './dto/update-user.dto';
|
import { UpdateUserDto } from './dto/update-user.dto';
|
||||||
|
|
||||||
describe('UserService', () => {
|
const adminUserAuth: AuthUserDto = Object.freeze({
|
||||||
let sut: UserService;
|
|
||||||
let userRepositoryMock: jest.Mocked<IUserRepository>;
|
|
||||||
|
|
||||||
const adminUserAuth: AuthUserDto = Object.freeze({
|
|
||||||
id: 'admin_id',
|
id: 'admin_id',
|
||||||
email: 'admin@test.com',
|
email: 'admin@test.com',
|
||||||
isAdmin: true,
|
isAdmin: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const immichUserAuth: AuthUserDto = Object.freeze({
|
const immichUserAuth: AuthUserDto = Object.freeze({
|
||||||
id: 'immich_id',
|
id: 'immich_id',
|
||||||
email: 'immich@test.com',
|
email: 'immich@test.com',
|
||||||
isAdmin: false,
|
isAdmin: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const adminUser: UserEntity = Object.freeze({
|
const adminUser: UserEntity = Object.freeze({
|
||||||
id: adminUserAuth.id,
|
id: adminUserAuth.id,
|
||||||
email: 'admin@test.com',
|
email: 'admin@test.com',
|
||||||
password: 'admin_password',
|
password: 'admin_password',
|
||||||
@ -34,9 +30,9 @@ describe('UserService', () => {
|
|||||||
profileImagePath: '',
|
profileImagePath: '',
|
||||||
createdAt: '2021-01-01',
|
createdAt: '2021-01-01',
|
||||||
tags: [],
|
tags: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
const immichUser: UserEntity = Object.freeze({
|
const immichUser: UserEntity = Object.freeze({
|
||||||
id: immichUserAuth.id,
|
id: immichUserAuth.id,
|
||||||
email: 'immich@test.com',
|
email: 'immich@test.com',
|
||||||
password: 'immich_password',
|
password: 'immich_password',
|
||||||
@ -48,9 +44,9 @@ describe('UserService', () => {
|
|||||||
profileImagePath: '',
|
profileImagePath: '',
|
||||||
createdAt: '2021-01-01',
|
createdAt: '2021-01-01',
|
||||||
tags: [],
|
tags: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
const updatedImmichUser: UserEntity = Object.freeze({
|
const updatedImmichUser: UserEntity = Object.freeze({
|
||||||
id: immichUserAuth.id,
|
id: immichUserAuth.id,
|
||||||
email: 'immich@test.com',
|
email: 'immich@test.com',
|
||||||
password: 'immich_password',
|
password: 'immich_password',
|
||||||
@ -62,7 +58,24 @@ describe('UserService', () => {
|
|||||||
profileImagePath: '',
|
profileImagePath: '',
|
||||||
createdAt: '2021-01-01',
|
createdAt: '2021-01-01',
|
||||||
tags: [],
|
tags: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const adminUserResponse = Object.freeze({
|
||||||
|
id: adminUserAuth.id,
|
||||||
|
email: 'admin@test.com',
|
||||||
|
deletedAt: undefined,
|
||||||
|
firstName: 'admin_first_name',
|
||||||
|
lastName: 'admin_last_name',
|
||||||
|
isAdmin: true,
|
||||||
|
oauthId: '',
|
||||||
|
shouldChangePassword: false,
|
||||||
|
profileImagePath: '',
|
||||||
|
createdAt: '2021-01-01',
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('UserService', () => {
|
||||||
|
let sut: UserService;
|
||||||
|
let userRepositoryMock: jest.Mocked<IUserRepository>;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
userRepositoryMock = {
|
userRepositoryMock = {
|
||||||
@ -78,12 +91,86 @@ describe('UserService', () => {
|
|||||||
};
|
};
|
||||||
when(userRepositoryMock.get).calledWith(adminUser.id).mockResolvedValue(adminUser);
|
when(userRepositoryMock.get).calledWith(adminUser.id).mockResolvedValue(adminUser);
|
||||||
when(userRepositoryMock.get).calledWith(adminUser.id, undefined).mockResolvedValue(adminUser);
|
when(userRepositoryMock.get).calledWith(adminUser.id, undefined).mockResolvedValue(adminUser);
|
||||||
|
when(userRepositoryMock.get).calledWith(immichUser.id).mockResolvedValue(immichUser);
|
||||||
when(userRepositoryMock.get).calledWith(immichUser.id, undefined).mockResolvedValue(immichUser);
|
when(userRepositoryMock.get).calledWith(immichUser.id, undefined).mockResolvedValue(immichUser);
|
||||||
|
|
||||||
sut = new UserService(userRepositoryMock);
|
sut = new UserService(userRepositoryMock);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Update user', () => {
|
describe('getAllUsers', () => {
|
||||||
|
it('should get all users', async () => {
|
||||||
|
userRepositoryMock.getList.mockResolvedValue([adminUser]);
|
||||||
|
|
||||||
|
const response = await sut.getAllUsers(adminUserAuth, false);
|
||||||
|
|
||||||
|
expect(userRepositoryMock.getList).toHaveBeenCalledWith({ excludeId: adminUser.id });
|
||||||
|
expect(response).toEqual([
|
||||||
|
{
|
||||||
|
id: adminUserAuth.id,
|
||||||
|
email: 'admin@test.com',
|
||||||
|
deletedAt: undefined,
|
||||||
|
firstName: 'admin_first_name',
|
||||||
|
lastName: 'admin_last_name',
|
||||||
|
isAdmin: true,
|
||||||
|
oauthId: '',
|
||||||
|
shouldChangePassword: false,
|
||||||
|
profileImagePath: '',
|
||||||
|
createdAt: '2021-01-01',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getUserById', () => {
|
||||||
|
it('should get a user by id', async () => {
|
||||||
|
userRepositoryMock.get.mockResolvedValue(adminUser);
|
||||||
|
|
||||||
|
const response = await sut.getUserById(adminUser.id);
|
||||||
|
|
||||||
|
expect(userRepositoryMock.get).toHaveBeenCalledWith(adminUser.id, false);
|
||||||
|
expect(response).toEqual(adminUserResponse);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error if a user is not found', async () => {
|
||||||
|
userRepositoryMock.get.mockResolvedValue(null);
|
||||||
|
|
||||||
|
await expect(sut.getUserById(adminUser.id)).rejects.toBeInstanceOf(NotFoundException);
|
||||||
|
|
||||||
|
expect(userRepositoryMock.get).toHaveBeenCalledWith(adminUser.id, false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getUserInfo', () => {
|
||||||
|
it("should get the auth user's info", async () => {
|
||||||
|
userRepositoryMock.get.mockResolvedValue(adminUser);
|
||||||
|
|
||||||
|
const response = await sut.getUserInfo(adminUser);
|
||||||
|
|
||||||
|
expect(userRepositoryMock.get).toHaveBeenCalledWith(adminUser.id, undefined);
|
||||||
|
expect(response).toEqual(adminUserResponse);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error if a user is not found', async () => {
|
||||||
|
userRepositoryMock.get.mockResolvedValue(null);
|
||||||
|
|
||||||
|
await expect(sut.getUserInfo(adminUser)).rejects.toBeInstanceOf(BadRequestException);
|
||||||
|
|
||||||
|
expect(userRepositoryMock.get).toHaveBeenCalledWith(adminUser.id, undefined);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getUserCount', () => {
|
||||||
|
it('should get the user count', async () => {
|
||||||
|
userRepositoryMock.getList.mockResolvedValue([adminUser]);
|
||||||
|
|
||||||
|
const response = await sut.getUserCount({});
|
||||||
|
|
||||||
|
expect(userRepositoryMock.getList).toHaveBeenCalled();
|
||||||
|
expect(response).toEqual({ userCount: 1 });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('update', () => {
|
||||||
it('should update user', async () => {
|
it('should update user', async () => {
|
||||||
const update: UpdateUserDto = {
|
const update: UpdateUserDto = {
|
||||||
id: immichUser.id,
|
id: immichUser.id,
|
||||||
@ -161,17 +248,7 @@ describe('UserService', () => {
|
|||||||
|
|
||||||
await expect(result).rejects.toBeInstanceOf(NotFoundException);
|
await expect(result).rejects.toBeInstanceOf(NotFoundException);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
describe('Delete user', () => {
|
|
||||||
it('cannot delete admin user', async () => {
|
|
||||||
const result = sut.deleteUser(adminUserAuth, adminUserAuth.id);
|
|
||||||
|
|
||||||
await expect(result).rejects.toBeInstanceOf(ForbiddenException);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Create user', () => {
|
|
||||||
it('should let the admin update himself', async () => {
|
it('should let the admin update himself', async () => {
|
||||||
const dto = { id: adminUser.id, shouldChangePassword: true, isAdmin: true };
|
const dto = { id: adminUser.id, shouldChangePassword: true, isAdmin: true };
|
||||||
|
|
||||||
@ -190,7 +267,37 @@ describe('UserService', () => {
|
|||||||
|
|
||||||
await expect(sut.updateUser(adminUser, dto)).rejects.toBeInstanceOf(BadRequestException);
|
await expect(sut.updateUser(adminUser, dto)).rejects.toBeInstanceOf(BadRequestException);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('restoreUser', () => {
|
||||||
|
it('should require an admin', async () => {
|
||||||
|
when(userRepositoryMock.get).calledWith(adminUser.id, true).mockResolvedValue(adminUser);
|
||||||
|
await expect(sut.restoreUser(immichUserAuth, adminUser.id)).rejects.toBeInstanceOf(ForbiddenException);
|
||||||
|
expect(userRepositoryMock.get).toHaveBeenCalledWith(adminUser.id, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should require the auth user be an admin', async () => {
|
||||||
|
await expect(sut.deleteUser(immichUserAuth, adminUserAuth.id)).rejects.toBeInstanceOf(ForbiddenException);
|
||||||
|
|
||||||
|
expect(userRepositoryMock.delete).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('deleteUser', () => {
|
||||||
|
it('cannot delete admin user', async () => {
|
||||||
|
const result = sut.deleteUser(adminUserAuth, adminUserAuth.id);
|
||||||
|
|
||||||
|
await expect(result).rejects.toBeInstanceOf(ForbiddenException);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should require the auth user be an admin', async () => {
|
||||||
|
await expect(sut.deleteUser(immichUserAuth, adminUserAuth.id)).rejects.toBeInstanceOf(ForbiddenException);
|
||||||
|
|
||||||
|
expect(userRepositoryMock.delete).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('update', () => {
|
||||||
it('should not create a user if there is no local admin account', async () => {
|
it('should not create a user if there is no local admin account', async () => {
|
||||||
when(userRepositoryMock.getAdmin).calledWith().mockResolvedValueOnce(null);
|
when(userRepositoryMock.getAdmin).calledWith().mockResolvedValueOnce(null);
|
||||||
|
|
||||||
@ -204,4 +311,33 @@ describe('UserService', () => {
|
|||||||
).rejects.toBeInstanceOf(BadRequestException);
|
).rejects.toBeInstanceOf(BadRequestException);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('createProfileImage', () => {
|
||||||
|
it('should throw an error if the user does not exist', async () => {
|
||||||
|
const file = { path: '/profile/path' } as Express.Multer.File;
|
||||||
|
userRepositoryMock.update.mockResolvedValue({ ...adminUser, profileImagePath: file.path });
|
||||||
|
|
||||||
|
await sut.createProfileImage(adminUserAuth, file);
|
||||||
|
|
||||||
|
expect(userRepositoryMock.update).toHaveBeenCalledWith(adminUserAuth.id, { profileImagePath: file.path });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getUserProfileImage', () => {
|
||||||
|
it('should throw an error if the user does not exist', async () => {
|
||||||
|
userRepositoryMock.get.mockResolvedValue(null);
|
||||||
|
|
||||||
|
await expect(sut.getUserProfileImage(adminUserAuth.id)).rejects.toBeInstanceOf(NotFoundException);
|
||||||
|
|
||||||
|
expect(userRepositoryMock.get).toHaveBeenCalledWith(adminUserAuth.id, undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error if the user does not have a picture', async () => {
|
||||||
|
userRepositoryMock.get.mockResolvedValue(adminUser);
|
||||||
|
|
||||||
|
await expect(sut.getUserProfileImage(adminUserAuth.id)).rejects.toBeInstanceOf(NotFoundException);
|
||||||
|
|
||||||
|
expect(userRepositoryMock.get).toHaveBeenCalledWith(adminUserAuth.id, undefined);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
"lint:fix": "npm run lint -- --fix",
|
"lint:fix": "npm run lint -- --fix",
|
||||||
"check": "tsc --noEmit",
|
"check": "tsc --noEmit",
|
||||||
"check:code": "npm run format && npm run lint && npm run check",
|
"check:code": "npm run format && npm run lint && npm run check",
|
||||||
"check:all": "npm run check:code && npm run test",
|
"check:all": "npm run check:code && npm run test:cov",
|
||||||
"test": "jest",
|
"test": "jest",
|
||||||
"test:watch": "jest --watch",
|
"test:watch": "jest --watch",
|
||||||
"test:cov": "jest --coverage",
|
"test:cov": "jest --coverage",
|
||||||
@ -139,6 +139,18 @@
|
|||||||
"**/*.(t|j)s"
|
"**/*.(t|j)s"
|
||||||
],
|
],
|
||||||
"coverageDirectory": "./coverage",
|
"coverageDirectory": "./coverage",
|
||||||
|
"coverageThreshold": {
|
||||||
|
"global": {
|
||||||
|
"lines": 20,
|
||||||
|
"statements": 20
|
||||||
|
},
|
||||||
|
"./libs/domain/": {
|
||||||
|
"branches": 60,
|
||||||
|
"functions": 80,
|
||||||
|
"lines": 80,
|
||||||
|
"statements": 80
|
||||||
|
}
|
||||||
|
},
|
||||||
"testEnvironment": "node",
|
"testEnvironment": "node",
|
||||||
"roots": [
|
"roots": [
|
||||||
"<rootDir>/apps/",
|
"<rootDir>/apps/",
|
||||||
|
@ -20,10 +20,16 @@ export default {
|
|||||||
// collectCoverage: false,
|
// collectCoverage: false,
|
||||||
|
|
||||||
// An array of glob patterns indicating a set of files for which coverage information should be collected
|
// An array of glob patterns indicating a set of files for which coverage information should be collected
|
||||||
// collectCoverageFrom: undefined,
|
collectCoverageFrom: ['src/**/*.*', '!src/api/open-api/**'],
|
||||||
|
|
||||||
// The directory where Jest should output its coverage files
|
// The directory where Jest should output its coverage files
|
||||||
// coverageDirectory: undefined,
|
// coverageDirectory: undefined,
|
||||||
|
coverageThreshold: {
|
||||||
|
global: {
|
||||||
|
lines: 4,
|
||||||
|
statements: 4
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// An array of regexp pattern strings used to skip coverage collection
|
// An array of regexp pattern strings used to skip coverage collection
|
||||||
// coveragePathIgnorePatterns: [
|
// coveragePathIgnorePatterns: [
|
||||||
@ -76,16 +82,7 @@ export default {
|
|||||||
// ],
|
// ],
|
||||||
|
|
||||||
// An array of file extensions your modules use
|
// An array of file extensions your modules use
|
||||||
// moduleFileExtensions: [
|
moduleFileExtensions: ['svelte', 'js', 'ts'],
|
||||||
// "js",
|
|
||||||
// "mjs",
|
|
||||||
// "cjs",
|
|
||||||
// "jsx",
|
|
||||||
// "ts",
|
|
||||||
// "tsx",
|
|
||||||
// "json",
|
|
||||||
// "node"
|
|
||||||
// ],
|
|
||||||
|
|
||||||
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
|
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
|
||||||
moduleNameMapper: {
|
moduleNameMapper: {
|
||||||
|
@ -9,12 +9,13 @@
|
|||||||
"check": "svelte-check --no-tsconfig --fail-on-warnings --ignore \"src/api/open-api\"",
|
"check": "svelte-check --no-tsconfig --fail-on-warnings --ignore \"src/api/open-api\"",
|
||||||
"check:watch": "npm run check -- --watch",
|
"check:watch": "npm run check -- --watch",
|
||||||
"check:code": "npm run format && npm run lint && npm run check",
|
"check:code": "npm run format && npm run lint && npm run check",
|
||||||
"check:all": "npm run check:code && npm test",
|
"check:all": "npm run check:code && npm run test:cov",
|
||||||
"lint": "eslint . --max-warnings 0",
|
"lint": "eslint . --max-warnings 0",
|
||||||
"lint:fix": "npm run lint -- --fix",
|
"lint:fix": "npm run lint -- --fix",
|
||||||
"format": "prettier --check --plugin-search-dir=. .",
|
"format": "prettier --check --plugin-search-dir=. .",
|
||||||
"format:fix": "prettier --write --plugin-search-dir=. .",
|
"format:fix": "prettier --write --plugin-search-dir=. .",
|
||||||
"test": "jest",
|
"test": "jest",
|
||||||
|
"test:cov": "jest --coverage",
|
||||||
"test:watch": "npm test -- --watch"
|
"test:watch": "npm test -- --watch"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
Loading…
Reference in New Issue
Block a user