2021-10-14 14:17:28 -04:00
|
|
|
/*
|
2022-01-30 09:48:59 -05:00
|
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
2021-10-14 14:17:28 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
2022-09-24 10:02:37 -04:00
|
|
|
import { EventEmitterModule } from '@nestjs/event-emitter';
|
2021-10-14 15:01:29 -04:00
|
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
2022-01-06 15:41:36 -05:00
|
|
|
import { Test, TestingModule, TestingModuleBuilder } from '@nestjs/testing';
|
2022-03-04 07:13:24 -05:00
|
|
|
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
|
2021-10-15 10:44:43 -04:00
|
|
|
import { RouterModule, Routes } from 'nest-router';
|
2022-03-04 07:13:24 -05:00
|
|
|
import { Connection, createConnection } from 'typeorm';
|
2021-10-14 14:17:28 -04:00
|
|
|
|
|
|
|
import { PrivateApiModule } from '../src/api/private/private-api.module';
|
2021-10-14 14:52:57 -04:00
|
|
|
import { PublicApiModule } from '../src/api/public/public-api.module';
|
2022-06-19 15:23:56 -04:00
|
|
|
import { setupApp } from '../src/app-init';
|
2022-03-04 07:51:50 -05:00
|
|
|
import { AuthTokenWithSecretDto } from '../src/auth/auth-token.dto';
|
2021-10-14 14:17:28 -04:00
|
|
|
import { AuthModule } from '../src/auth/auth.module';
|
2021-12-09 12:59:00 -05:00
|
|
|
import { AuthService } from '../src/auth/auth.service';
|
2021-10-14 14:17:28 -04:00
|
|
|
import { MockAuthGuard } from '../src/auth/mock-auth.guard';
|
|
|
|
import { TokenAuthGuard } from '../src/auth/token.strategy';
|
2021-10-21 13:51:23 -04:00
|
|
|
import { AuthorsModule } from '../src/authors/authors.module';
|
2023-02-12 13:31:32 -05:00
|
|
|
import { AppConfig } from '../src/config/app.config';
|
2021-10-21 13:51:23 -04:00
|
|
|
import { AuthConfig } from '../src/config/auth.config';
|
2022-10-23 14:30:35 -04:00
|
|
|
import { CustomizationConfig } from '../src/config/customization.config';
|
|
|
|
import { DatabaseConfig } from '../src/config/database.config';
|
|
|
|
import { ExternalServicesConfig } from '../src/config/external-services.config';
|
2022-06-19 15:23:56 -04:00
|
|
|
import { MediaConfig } from '../src/config/media.config';
|
2022-10-23 14:30:35 -04:00
|
|
|
import {
|
|
|
|
createDefaultMockAppConfig,
|
|
|
|
registerAppConfig,
|
|
|
|
} from '../src/config/mock/app.config.mock';
|
|
|
|
import {
|
|
|
|
createDefaultMockAuthConfig,
|
|
|
|
registerAuthConfig,
|
|
|
|
} from '../src/config/mock/auth.config.mock';
|
|
|
|
import {
|
|
|
|
createDefaultMockCustomizationConfig,
|
|
|
|
registerCustomizationConfig,
|
|
|
|
} from '../src/config/mock/customization.config.mock';
|
|
|
|
import {
|
|
|
|
createDefaultMockDatabaseConfig,
|
|
|
|
registerDatabaseConfig,
|
|
|
|
} from '../src/config/mock/database.config.mock';
|
|
|
|
import {
|
|
|
|
createDefaultMockExternalServicesConfig,
|
|
|
|
registerExternalServiceConfig,
|
|
|
|
} from '../src/config/mock/external-services.config.mock';
|
|
|
|
import {
|
|
|
|
createDefaultMockMediaConfig,
|
|
|
|
registerMediaConfig,
|
|
|
|
} from '../src/config/mock/media.config.mock';
|
|
|
|
import {
|
|
|
|
createDefaultMockNoteConfig,
|
|
|
|
registerNoteConfig,
|
|
|
|
} from '../src/config/mock/note.config.mock';
|
|
|
|
import { NoteConfig } from '../src/config/note.config';
|
2022-01-23 16:23:00 -05:00
|
|
|
import { ErrorExceptionMapping } from '../src/errors/error-mapping';
|
2022-09-24 10:02:37 -04:00
|
|
|
import { eventModuleConfig } from '../src/events';
|
2021-10-21 13:51:23 -04:00
|
|
|
import { FrontendConfigModule } from '../src/frontend-config/frontend-config.module';
|
2021-10-14 14:17:28 -04:00
|
|
|
import { GroupsModule } from '../src/groups/groups.module';
|
2022-02-27 13:02:28 -05:00
|
|
|
import { GroupsService } from '../src/groups/groups.service';
|
2021-10-14 14:17:28 -04:00
|
|
|
import { HistoryModule } from '../src/history/history.module';
|
2021-10-14 15:17:42 -04:00
|
|
|
import { HistoryService } from '../src/history/history.service';
|
2021-10-21 13:51:23 -04:00
|
|
|
import { IdentityModule } from '../src/identity/identity.module';
|
2021-10-14 14:17:28 -04:00
|
|
|
import { IdentityService } from '../src/identity/identity.service';
|
2022-03-04 07:13:24 -05:00
|
|
|
import { ConsoleLoggerService } from '../src/logger/console-logger.service';
|
2021-10-14 14:17:28 -04:00
|
|
|
import { LoggerModule } from '../src/logger/logger.module';
|
|
|
|
import { MediaModule } from '../src/media/media.module';
|
2021-10-14 14:52:57 -04:00
|
|
|
import { MediaService } from '../src/media/media.service';
|
2021-10-21 13:51:23 -04:00
|
|
|
import { MonitoringModule } from '../src/monitoring/monitoring.module';
|
2021-10-14 15:25:17 -04:00
|
|
|
import { AliasService } from '../src/notes/alias.service';
|
2022-01-06 15:41:36 -05:00
|
|
|
import { Note } from '../src/notes/note.entity';
|
2021-10-14 14:17:28 -04:00
|
|
|
import { NotesModule } from '../src/notes/notes.module';
|
2021-10-14 14:52:57 -04:00
|
|
|
import { NotesService } from '../src/notes/notes.service';
|
2021-10-14 14:17:28 -04:00
|
|
|
import { PermissionsModule } from '../src/permissions/permissions.module';
|
2022-02-06 17:20:05 -05:00
|
|
|
import { PermissionsService } from '../src/permissions/permissions.service';
|
2021-10-21 13:51:23 -04:00
|
|
|
import { RevisionsModule } from '../src/revisions/revisions.module';
|
2022-06-21 10:16:40 -04:00
|
|
|
import { RevisionsService } from '../src/revisions/revisions.service';
|
|
|
|
import { SessionModule } from '../src/session/session.module';
|
|
|
|
import { SessionService } from '../src/session/session.service';
|
2021-12-07 10:39:46 -05:00
|
|
|
import { User } from '../src/users/user.entity';
|
2021-10-14 14:17:28 -04:00
|
|
|
import { UsersModule } from '../src/users/users.module';
|
|
|
|
import { UsersService } from '../src/users/users.service';
|
|
|
|
|
2022-10-23 14:30:35 -04:00
|
|
|
interface CreateTestSetupParameters {
|
|
|
|
appConfigMock?: AppConfig;
|
|
|
|
authConfigMock?: AuthConfig;
|
|
|
|
customizationConfigMock?: CustomizationConfig;
|
|
|
|
databaseConfigMock?: DatabaseConfig;
|
|
|
|
externalServicesConfigMock?: ExternalServicesConfig;
|
|
|
|
mediaConfigMock?: MediaConfig;
|
|
|
|
noteConfigMock?: NoteConfig;
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:17:28 -04:00
|
|
|
export class TestSetup {
|
|
|
|
moduleRef: TestingModule;
|
2021-10-14 15:01:29 -04:00
|
|
|
app: NestExpressApplication;
|
2021-10-14 14:17:28 -04:00
|
|
|
|
|
|
|
userService: UsersService;
|
2022-02-27 13:02:28 -05:00
|
|
|
groupService: GroupsService;
|
2021-10-14 14:17:28 -04:00
|
|
|
configService: ConfigService;
|
|
|
|
identityService: IdentityService;
|
2021-10-14 14:52:57 -04:00
|
|
|
notesService: NotesService;
|
|
|
|
mediaService: MediaService;
|
2021-10-14 15:17:42 -04:00
|
|
|
historyService: HistoryService;
|
2021-10-14 15:25:17 -04:00
|
|
|
aliasService: AliasService;
|
2021-12-09 12:59:00 -05:00
|
|
|
authService: AuthService;
|
2022-06-21 10:16:40 -04:00
|
|
|
sessionService: SessionService;
|
|
|
|
revisionsService: RevisionsService;
|
2021-10-14 14:17:28 -04:00
|
|
|
|
2021-12-07 10:39:46 -05:00
|
|
|
users: User[] = [];
|
2021-12-09 12:59:00 -05:00
|
|
|
authTokens: AuthTokenWithSecretDto[] = [];
|
2022-01-06 17:06:38 -05:00
|
|
|
anonymousNotes: Note[] = [];
|
|
|
|
ownedNotes: Note[] = [];
|
2022-02-06 17:20:05 -05:00
|
|
|
permissionsService: PermissionsService;
|
2022-03-04 07:13:24 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans up remnants from a test run from the database
|
|
|
|
*/
|
|
|
|
public async cleanup() {
|
|
|
|
const appConnection = this.app.get<Connection>(Connection);
|
|
|
|
const connectionOptions = appConnection.options;
|
|
|
|
if (!connectionOptions.database) {
|
|
|
|
throw new Error('Database name not set in connection options');
|
|
|
|
}
|
|
|
|
if (connectionOptions.type === 'sqlite') {
|
|
|
|
// Bail out early, as SQLite runs from memory anyway
|
2022-08-07 16:31:20 -04:00
|
|
|
await this.app.close();
|
2022-03-04 07:13:24 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (appConnection.isConnected) {
|
|
|
|
await appConnection.close();
|
|
|
|
}
|
|
|
|
switch (connectionOptions.type) {
|
2022-03-05 12:45:53 -05:00
|
|
|
case 'postgres':
|
|
|
|
case 'mariadb': {
|
2022-03-04 07:13:24 -05:00
|
|
|
const connection = await createConnection({
|
2022-03-05 12:45:53 -05:00
|
|
|
type: connectionOptions.type,
|
2022-03-04 07:13:24 -05:00
|
|
|
username: 'hedgedoc',
|
|
|
|
password: 'hedgedoc',
|
|
|
|
});
|
|
|
|
await connection.query(`DROP DATABASE ${connectionOptions.database}`);
|
|
|
|
await connection.close();
|
|
|
|
}
|
|
|
|
}
|
2022-08-07 16:31:20 -04:00
|
|
|
await this.app.close();
|
2022-03-04 07:13:24 -05:00
|
|
|
}
|
2022-01-06 15:41:36 -05:00
|
|
|
}
|
2021-12-07 10:39:46 -05:00
|
|
|
|
2022-01-06 15:41:36 -05:00
|
|
|
/**
|
|
|
|
* Builder class for TestSetup
|
|
|
|
* Should be instantiated with the create() method
|
|
|
|
* The useable TestSetup is genereated using build()
|
|
|
|
*/
|
|
|
|
export class TestSetupBuilder {
|
|
|
|
// list of functions that should be executed before or after builing the TestingModule
|
|
|
|
private setupPreCompile: (() => Promise<void>)[] = [];
|
|
|
|
private setupPostCompile: (() => Promise<void>)[] = [];
|
|
|
|
|
|
|
|
private testingModuleBuilder: TestingModuleBuilder;
|
|
|
|
private testSetup = new TestSetup();
|
|
|
|
|
2022-03-04 07:13:24 -05:00
|
|
|
private testId: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares a test database
|
|
|
|
* @param dbName The name of the database to use
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private static async setupTestDB(dbName: string) {
|
2022-03-05 12:45:53 -05:00
|
|
|
const dbType = process.env.HEDGEDOC_TEST_DB_TYPE;
|
|
|
|
if (!dbType || dbType === 'sqlite') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!['postgres', 'mariadb'].includes(dbType)) {
|
|
|
|
throw new Error('Unknown database type in HEDGEDOC_TEST_DB_TYPE');
|
|
|
|
}
|
|
|
|
|
|
|
|
const connection = await createConnection({
|
|
|
|
type: dbType as 'postgres' | 'mariadb',
|
|
|
|
username: dbType === 'mariadb' ? 'root' : 'hedgedoc',
|
|
|
|
password: 'hedgedoc',
|
|
|
|
});
|
|
|
|
|
|
|
|
await connection.query(`CREATE DATABASE ${dbName}`);
|
|
|
|
if (dbType === 'mariadb') {
|
|
|
|
await connection.query(
|
|
|
|
`GRANT ALL PRIVILEGES ON ${dbName}.* TO 'hedgedoc'@'%'`,
|
|
|
|
);
|
2022-03-04 07:13:24 -05:00
|
|
|
}
|
2022-03-05 12:45:53 -05:00
|
|
|
await connection.close();
|
2022-03-04 07:13:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private static getTestDBConf(dbName: string): TypeOrmModuleOptions {
|
|
|
|
switch (process.env.HEDGEDOC_TEST_DB_TYPE || 'sqlite') {
|
|
|
|
case 'sqlite':
|
|
|
|
return {
|
|
|
|
type: 'sqlite',
|
|
|
|
database: ':memory:',
|
|
|
|
autoLoadEntities: true,
|
|
|
|
synchronize: true,
|
|
|
|
dropSchema: true,
|
|
|
|
};
|
|
|
|
case 'postgres':
|
2022-03-05 12:45:53 -05:00
|
|
|
case 'mariadb':
|
2022-03-04 07:13:24 -05:00
|
|
|
return {
|
2022-03-05 12:45:53 -05:00
|
|
|
type: process.env.HEDGEDOC_TEST_DB_TYPE as 'postgres' | 'mariadb',
|
2022-03-04 07:13:24 -05:00
|
|
|
database: dbName,
|
|
|
|
username: 'hedgedoc',
|
|
|
|
password: 'hedgedoc',
|
|
|
|
autoLoadEntities: true,
|
|
|
|
synchronize: true,
|
|
|
|
dropSchema: true,
|
|
|
|
};
|
|
|
|
default:
|
|
|
|
throw new Error('Unknown database type in HEDGEDOC_TEST_DB_TYPE');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 15:41:36 -05:00
|
|
|
/**
|
|
|
|
* Creates a new instance of TestSetupBuilder
|
|
|
|
*/
|
2022-10-23 14:30:35 -04:00
|
|
|
public static create(mocks?: CreateTestSetupParameters): TestSetupBuilder {
|
2022-01-06 15:41:36 -05:00
|
|
|
const testSetupBuilder = new TestSetupBuilder();
|
2022-03-04 07:13:24 -05:00
|
|
|
testSetupBuilder.testId =
|
|
|
|
'hedgedoc_test_' + Math.random().toString(36).substring(2, 15);
|
2021-10-15 10:44:43 -04:00
|
|
|
const routes: Routes = [
|
|
|
|
{
|
|
|
|
path: '/api/v2',
|
|
|
|
module: PublicApiModule,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '/api/private',
|
|
|
|
module: PrivateApiModule,
|
|
|
|
},
|
|
|
|
];
|
2023-02-12 16:04:33 -05:00
|
|
|
process.env.HD_BASE_URL =
|
2022-12-30 06:47:23 -05:00
|
|
|
'https://md-' + testSetupBuilder.testId + '.example.com';
|
2022-01-06 15:41:36 -05:00
|
|
|
testSetupBuilder.testingModuleBuilder = Test.createTestingModule({
|
2021-10-14 14:17:28 -04:00
|
|
|
imports: [
|
2021-10-15 10:44:43 -04:00
|
|
|
RouterModule.forRoutes(routes),
|
2022-03-04 07:13:24 -05:00
|
|
|
TypeOrmModule.forRoot(
|
|
|
|
TestSetupBuilder.getTestDBConf(testSetupBuilder.testId),
|
|
|
|
),
|
2021-10-14 14:17:28 -04:00
|
|
|
ConfigModule.forRoot({
|
|
|
|
isGlobal: true,
|
|
|
|
load: [
|
2022-10-23 14:30:35 -04:00
|
|
|
registerAppConfig(
|
|
|
|
mocks?.appConfigMock ?? createDefaultMockAppConfig(),
|
|
|
|
),
|
|
|
|
registerAuthConfig(
|
|
|
|
mocks?.authConfigMock ?? createDefaultMockAuthConfig(),
|
|
|
|
),
|
|
|
|
registerCustomizationConfig(
|
|
|
|
mocks?.customizationConfigMock ??
|
|
|
|
createDefaultMockCustomizationConfig(),
|
|
|
|
),
|
|
|
|
registerDatabaseConfig(
|
|
|
|
mocks?.databaseConfigMock ?? createDefaultMockDatabaseConfig(),
|
|
|
|
),
|
|
|
|
registerExternalServiceConfig(
|
|
|
|
mocks?.externalServicesConfigMock ??
|
|
|
|
createDefaultMockExternalServicesConfig(),
|
|
|
|
),
|
|
|
|
registerMediaConfig(
|
|
|
|
mocks?.mediaConfigMock ?? createDefaultMockMediaConfig(),
|
|
|
|
),
|
|
|
|
registerNoteConfig(
|
|
|
|
mocks?.noteConfigMock ?? createDefaultMockNoteConfig(),
|
|
|
|
),
|
2021-10-14 14:17:28 -04:00
|
|
|
],
|
|
|
|
}),
|
2021-10-21 13:51:23 -04:00
|
|
|
NotesModule,
|
|
|
|
UsersModule,
|
|
|
|
RevisionsModule,
|
|
|
|
AuthorsModule,
|
2021-10-14 14:52:57 -04:00
|
|
|
PublicApiModule,
|
2021-10-14 14:17:28 -04:00
|
|
|
PrivateApiModule,
|
2021-10-21 13:51:23 -04:00
|
|
|
HistoryModule,
|
|
|
|
MonitoringModule,
|
2021-10-14 14:17:28 -04:00
|
|
|
PermissionsModule,
|
|
|
|
GroupsModule,
|
|
|
|
LoggerModule,
|
|
|
|
MediaModule,
|
2021-10-21 13:51:23 -04:00
|
|
|
AuthModule,
|
|
|
|
FrontendConfigModule,
|
|
|
|
IdentityModule,
|
2022-06-21 10:16:40 -04:00
|
|
|
SessionModule,
|
2022-09-24 10:02:37 -04:00
|
|
|
EventEmitterModule.forRoot(eventModuleConfig),
|
2021-10-14 14:17:28 -04:00
|
|
|
],
|
2022-01-23 16:23:00 -05:00
|
|
|
providers: [
|
|
|
|
{
|
|
|
|
provide: 'APP_FILTER',
|
|
|
|
useClass: ErrorExceptionMapping,
|
|
|
|
},
|
|
|
|
],
|
2021-12-09 12:41:55 -05:00
|
|
|
});
|
2022-01-06 15:41:36 -05:00
|
|
|
return testSetupBuilder;
|
|
|
|
}
|
2021-12-09 12:41:55 -05:00
|
|
|
|
2022-01-06 15:41:36 -05:00
|
|
|
/**
|
|
|
|
* Builds the final TestSetup from the configured builder
|
|
|
|
*/
|
|
|
|
public async build(): Promise<TestSetup> {
|
2022-03-04 07:13:24 -05:00
|
|
|
await TestSetupBuilder.setupTestDB(this.testId);
|
|
|
|
|
2022-01-06 15:41:36 -05:00
|
|
|
for (const setupFunction of this.setupPreCompile) {
|
|
|
|
await setupFunction();
|
2021-12-09 12:41:55 -05:00
|
|
|
}
|
|
|
|
|
2022-01-06 15:41:36 -05:00
|
|
|
this.testSetup.moduleRef = await this.testingModuleBuilder.compile();
|
|
|
|
|
|
|
|
this.testSetup.userService =
|
|
|
|
this.testSetup.moduleRef.get<UsersService>(UsersService);
|
2022-02-27 13:02:28 -05:00
|
|
|
this.testSetup.groupService =
|
|
|
|
this.testSetup.moduleRef.get<GroupsService>(GroupsService);
|
2022-01-06 15:41:36 -05:00
|
|
|
this.testSetup.configService =
|
|
|
|
this.testSetup.moduleRef.get<ConfigService>(ConfigService);
|
|
|
|
this.testSetup.identityService =
|
|
|
|
this.testSetup.moduleRef.get<IdentityService>(IdentityService);
|
|
|
|
this.testSetup.notesService =
|
|
|
|
this.testSetup.moduleRef.get<NotesService>(NotesService);
|
|
|
|
this.testSetup.mediaService =
|
|
|
|
this.testSetup.moduleRef.get<MediaService>(MediaService);
|
|
|
|
this.testSetup.historyService =
|
|
|
|
this.testSetup.moduleRef.get<HistoryService>(HistoryService);
|
|
|
|
this.testSetup.aliasService =
|
|
|
|
this.testSetup.moduleRef.get<AliasService>(AliasService);
|
|
|
|
this.testSetup.authService =
|
|
|
|
this.testSetup.moduleRef.get<AuthService>(AuthService);
|
2022-02-06 17:20:05 -05:00
|
|
|
this.testSetup.permissionsService =
|
|
|
|
this.testSetup.moduleRef.get<PermissionsService>(PermissionsService);
|
2022-06-21 10:16:40 -04:00
|
|
|
this.testSetup.sessionService =
|
|
|
|
this.testSetup.moduleRef.get<SessionService>(SessionService);
|
|
|
|
this.testSetup.revisionsService =
|
|
|
|
this.testSetup.moduleRef.get<RevisionsService>(RevisionsService);
|
2022-01-06 15:41:36 -05:00
|
|
|
|
|
|
|
this.testSetup.app = this.testSetup.moduleRef.createNestApplication();
|
2021-10-14 14:17:28 -04:00
|
|
|
|
2022-06-19 15:23:56 -04:00
|
|
|
await setupApp(
|
2022-01-06 15:41:36 -05:00
|
|
|
this.testSetup.app,
|
2022-06-19 15:23:56 -04:00
|
|
|
this.testSetup.configService.get<AppConfig>('appConfig'),
|
2022-01-06 15:41:36 -05:00
|
|
|
this.testSetup.configService.get<AuthConfig>('authConfig'),
|
2022-06-19 15:23:56 -04:00
|
|
|
this.testSetup.configService.get<MediaConfig>('mediaConfig'),
|
|
|
|
await this.testSetup.app.resolve(ConsoleLoggerService),
|
2022-03-04 11:54:20 -05:00
|
|
|
);
|
2021-10-21 13:51:23 -04:00
|
|
|
|
2022-01-06 15:41:36 -05:00
|
|
|
for (const setupFunction of this.setupPostCompile) {
|
|
|
|
await setupFunction();
|
|
|
|
}
|
|
|
|
return this.testSetup;
|
2021-10-14 14:17:28 -04:00
|
|
|
}
|
2021-12-07 10:39:46 -05:00
|
|
|
|
2022-01-06 15:41:36 -05:00
|
|
|
/**
|
|
|
|
* Enable mock authentication for the public API
|
|
|
|
*/
|
|
|
|
public withMockAuth() {
|
|
|
|
this.setupPreCompile.push(async () => {
|
|
|
|
this.testingModuleBuilder
|
|
|
|
.overrideGuard(TokenAuthGuard)
|
|
|
|
.useClass(MockAuthGuard);
|
|
|
|
return await Promise.resolve();
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
2021-12-07 10:39:46 -05:00
|
|
|
|
2022-01-06 15:41:36 -05:00
|
|
|
/**
|
|
|
|
* Generate a few users, identities and auth tokens for testing
|
|
|
|
*/
|
|
|
|
public withUsers() {
|
|
|
|
this.setupPostCompile.push(async () => {
|
|
|
|
// Create users
|
|
|
|
this.testSetup.users.push(
|
2022-09-24 20:05:30 -04:00
|
|
|
await this.testSetup.userService.createUser(username1, 'Test User 1'),
|
2022-01-06 15:41:36 -05:00
|
|
|
);
|
|
|
|
this.testSetup.users.push(
|
2022-09-24 20:05:30 -04:00
|
|
|
await this.testSetup.userService.createUser(username2, 'Test User 2'),
|
2022-01-06 15:41:36 -05:00
|
|
|
);
|
|
|
|
this.testSetup.users.push(
|
2022-09-24 20:05:30 -04:00
|
|
|
await this.testSetup.userService.createUser(username3, 'Test User 3'),
|
2022-01-06 15:41:36 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Create identities for login
|
|
|
|
await this.testSetup.identityService.createLocalIdentity(
|
|
|
|
this.testSetup.users[0],
|
2022-09-24 20:05:30 -04:00
|
|
|
password1,
|
2022-01-06 15:41:36 -05:00
|
|
|
);
|
|
|
|
await this.testSetup.identityService.createLocalIdentity(
|
|
|
|
this.testSetup.users[1],
|
2022-09-24 20:05:30 -04:00
|
|
|
password2,
|
2022-01-06 15:41:36 -05:00
|
|
|
);
|
|
|
|
await this.testSetup.identityService.createLocalIdentity(
|
|
|
|
this.testSetup.users[2],
|
2022-09-24 20:05:30 -04:00
|
|
|
password3,
|
2022-01-06 15:41:36 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// create auth tokens
|
|
|
|
this.testSetup.authTokens = await Promise.all(
|
|
|
|
this.testSetup.users.map(async (user) => {
|
2022-09-11 15:05:04 -04:00
|
|
|
return await this.testSetup.authService.addToken(
|
2022-01-06 15:41:36 -05:00
|
|
|
user,
|
|
|
|
'test',
|
|
|
|
new Date().getTime() + 60 * 60 * 1000,
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
);
|
2022-01-06 17:06:38 -05:00
|
|
|
|
|
|
|
// create notes with owner
|
|
|
|
this.testSetup.ownedNotes.push(
|
|
|
|
await this.testSetup.notesService.createNote(
|
|
|
|
'Test Note 1',
|
|
|
|
this.testSetup.users[0],
|
|
|
|
'testAlias1',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
this.testSetup.ownedNotes.push(
|
|
|
|
await this.testSetup.notesService.createNote(
|
|
|
|
'Test Note 2',
|
|
|
|
this.testSetup.users[1],
|
|
|
|
'testAlias2',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
this.testSetup.ownedNotes.push(
|
|
|
|
await this.testSetup.notesService.createNote(
|
|
|
|
'Test Note 3',
|
|
|
|
this.testSetup.users[2],
|
|
|
|
'testAlias3',
|
|
|
|
),
|
|
|
|
);
|
2022-01-06 15:41:36 -05:00
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
2021-12-09 12:59:00 -05:00
|
|
|
|
2022-01-06 15:41:36 -05:00
|
|
|
/**
|
2022-01-06 17:06:38 -05:00
|
|
|
* Generate a few anonymousNotes for testing
|
2022-01-06 15:41:36 -05:00
|
|
|
*/
|
|
|
|
public withNotes(): TestSetupBuilder {
|
|
|
|
this.setupPostCompile.push(async () => {
|
2022-01-06 17:06:38 -05:00
|
|
|
this.testSetup.anonymousNotes.push(
|
|
|
|
await this.testSetup.notesService.createNote(
|
|
|
|
'Anonymous Note 1',
|
|
|
|
null,
|
|
|
|
'anonAlias1',
|
|
|
|
),
|
2022-01-06 15:41:36 -05:00
|
|
|
);
|
2022-01-06 17:06:38 -05:00
|
|
|
this.testSetup.anonymousNotes.push(
|
|
|
|
await this.testSetup.notesService.createNote(
|
|
|
|
'Anonymous Note 2',
|
|
|
|
null,
|
|
|
|
'anonAlias2',
|
|
|
|
),
|
2022-01-06 15:41:36 -05:00
|
|
|
);
|
2022-01-06 17:06:38 -05:00
|
|
|
this.testSetup.anonymousNotes.push(
|
|
|
|
await this.testSetup.notesService.createNote(
|
|
|
|
'Anonymous Note 3',
|
|
|
|
null,
|
|
|
|
'anonAlias3',
|
|
|
|
),
|
2022-01-06 15:41:36 -05:00
|
|
|
);
|
|
|
|
});
|
2021-12-07 10:39:46 -05:00
|
|
|
return this;
|
|
|
|
}
|
2021-10-14 14:17:28 -04:00
|
|
|
}
|
2022-09-24 20:05:30 -04:00
|
|
|
|
|
|
|
export const username1 = 'testuser1';
|
|
|
|
export const password1 = 'AStrongP@sswordForUser1';
|
|
|
|
export const username2 = 'testuser2';
|
|
|
|
export const password2 = 'AStrongP@sswordForUser2';
|
|
|
|
export const username3 = 'testuser3';
|
|
|
|
export const password3 = 'AStrongP@sswordForUser3';
|