2023-07-01 20:27:34 +02:00
|
|
|
import { AuthUserDto } from '@app/domain';
|
|
|
|
import { AppGuard } from '@app/immich/app.guard';
|
2022-05-20 01:30:47 +02:00
|
|
|
import { CanActivate, ExecutionContext } from '@nestjs/common';
|
|
|
|
import { TestingModuleBuilder } from '@nestjs/testing';
|
2022-11-15 04:24:25 +02:00
|
|
|
import { DataSource } from 'typeorm';
|
2022-05-20 01:30:47 +02:00
|
|
|
|
|
|
|
type CustomAuthCallback = () => AuthUserDto;
|
|
|
|
|
2022-07-17 06:43:31 +02:00
|
|
|
export async function clearDb(db: DataSource) {
|
2022-06-29 20:39:58 +02:00
|
|
|
const entities = db.entityMetadatas;
|
2022-05-20 01:30:47 +02:00
|
|
|
for (const entity of entities) {
|
2022-06-29 20:39:58 +02:00
|
|
|
const repository = db.getRepository(entity.name);
|
2022-05-20 01:30:47 +02:00
|
|
|
await repository.query(`TRUNCATE ${entity.tableName} RESTART IDENTITY CASCADE;`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAuthUser(): AuthUserDto {
|
|
|
|
return {
|
|
|
|
id: '3108ac14-8afb-4b7e-87fd-39ebb6b79750',
|
|
|
|
email: 'test@email.com',
|
2022-12-23 22:08:50 +02:00
|
|
|
isAdmin: false,
|
2022-05-20 01:30:47 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function auth(builder: TestingModuleBuilder): TestingModuleBuilder {
|
|
|
|
return authCustom(builder, getAuthUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function authCustom(builder: TestingModuleBuilder, callback: CustomAuthCallback): TestingModuleBuilder {
|
|
|
|
const canActivate: CanActivate = {
|
|
|
|
canActivate: (context: ExecutionContext) => {
|
|
|
|
const req = context.switchToHttp().getRequest();
|
|
|
|
req.user = callback();
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
};
|
2023-07-01 20:27:34 +02:00
|
|
|
return builder.overrideProvider(AppGuard).useValue(canActivate);
|
2022-05-20 01:30:47 +02:00
|
|
|
}
|