Unit tests
This commit is contained in:
65
api/src/modules/system/system.service.spec.ts
Normal file
65
api/src/modules/system/system.service.spec.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { TestingModule, Test } from "@nestjs/testing"
|
||||
import { SystemService } from "./system.service";
|
||||
import { ActivityHelperService } from "src/shared/service/activity.logger.service";
|
||||
import { ActivityHelperMockService } from "src/shared/service/activity.logger.service.mock";
|
||||
import { MockUserRepository, MockKeySystemRepository, MockMailService } from "../../../mocks";
|
||||
import { KeySystemRepository, UserRepository } from "src/model/repositories";
|
||||
import { MailService } from "../mail/mail.service";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { KeySystem } from "src/model/entitites/system.entity";
|
||||
import { User } from "src/model/entitites";
|
||||
|
||||
describe('KeySystemServce', () => {
|
||||
let service: SystemService;
|
||||
let user: User;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
SystemService,
|
||||
{ provide: KeySystemRepository, useClass: MockKeySystemRepository },
|
||||
{ provide: MailService, useClass: MockMailService },
|
||||
{ provide: UserRepository, useClass: MockUserRepository },
|
||||
{ provide: ActivityHelperService, useClass: ActivityHelperMockService },
|
||||
ConfigService
|
||||
],
|
||||
imports: []
|
||||
|
||||
}).compile();
|
||||
|
||||
service = module.get<SystemService>(SystemService);
|
||||
user = await service['userRepo'].findByUsername('mockuser@test.de', { settings: false })
|
||||
});
|
||||
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
it('should create a system', async () => {
|
||||
const name = 'TestSystem0123'
|
||||
const s = await service.create(user, { name });
|
||||
expect(s).not.toBeNull();
|
||||
expect(s.id).not.toBeNull();
|
||||
expect(s.createdAt).not.toBeNull();
|
||||
expect(s.name).toBe(name);
|
||||
expect(s.managers).toContain(user);
|
||||
expect(service['systemRepo'].create).toHaveBeenCalled()
|
||||
expect(service['systemRepo'].save).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should delete systems', async () => {
|
||||
const repo = service['systemRepo'];
|
||||
const system = await service.remove('abc');
|
||||
expect(repo.softRemove).toHaveBeenCalled();
|
||||
expect(system.deletedAt).not.toBeNull();
|
||||
})
|
||||
|
||||
it('should restore systems', async () => {
|
||||
const repo = service['systemRepo'];
|
||||
|
||||
const system = await service.restore(user, 'abc');
|
||||
expect(repo.save).toHaveBeenCalled();
|
||||
expect(system.deletedAt).toBeNull();
|
||||
})
|
||||
})
|
||||
@@ -1,9 +1,8 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { CreateSystemDto } from './dto/create-system.dto';
|
||||
import { UpdateSystemDto } from './dto/update-system.dto';
|
||||
import { ActivityRepository, KeySystemRepository, UserRepository } from 'src/model/repositories';
|
||||
import { KeySystemRepository, UserRepository } from 'src/model/repositories';
|
||||
import { User } from 'src/model/entitites';
|
||||
import { IUser } from 'src/model/interface';
|
||||
import { MailService } from '../mail/mail.service';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { ActivityHelperService } from 'src/shared/service/activity.logger.service';
|
||||
@@ -14,7 +13,7 @@ export class SystemService {
|
||||
constructor(
|
||||
private systemRepo: KeySystemRepository,
|
||||
private userRepo: UserRepository,
|
||||
private systemActivityRepo: ActivityRepository,
|
||||
// private systemActivityRepo: ActivityRepository,
|
||||
private mailService: MailService,
|
||||
private readonly configService: ConfigService,
|
||||
private readonly activityService: ActivityHelperService
|
||||
@@ -28,15 +27,7 @@ export class SystemService {
|
||||
const sys = this.systemRepo.create(createSystemDto);
|
||||
sys.managers = [user];
|
||||
try {
|
||||
const res = await this.systemRepo.save(sys);
|
||||
|
||||
this.systemActivityRepo.save({
|
||||
message: `Schließanlage ${(res as any).name} angelegt`,
|
||||
user: user,
|
||||
system: res
|
||||
});
|
||||
|
||||
|
||||
const res = await this.systemRepo.save(sys);
|
||||
return res;
|
||||
} catch (e) {
|
||||
throw new HttpException(e.code, HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
@@ -78,7 +69,6 @@ export class SystemService {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async update(user: User, updateSystemDto: UpdateSystemDto) {
|
||||
if (!user || !user.id || !updateSystemDto.id) { throw new HttpException('forbidden', HttpStatus.FORBIDDEN); }
|
||||
console.log(updateSystemDto);
|
||||
const system = await this.systemRepo.findOne({ where: { id: updateSystemDto.id, managers: { id: user.id } }, withDeleted: true });
|
||||
|
||||
if (!system) { throw new HttpException('forbidden', HttpStatus.FORBIDDEN); }
|
||||
|
||||
Reference in New Issue
Block a user