-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
222 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { DynamicModule, Global, Module, ModuleMetadata } from "@nestjs/common"; | ||
import { ConfigModule } from "@nestjs/config"; | ||
import { LoggerModule } from "nestjs-pino"; | ||
|
||
/** | ||
* Baseline module for any Bridge nest applications. | ||
* | ||
* - `@nestjs/config`, nestjs ConfigModule | ||
* - `nestjs-pino`, the Pino logger for NestJS | ||
* - `joi`, for validation of environment variables | ||
*/ | ||
@Global() | ||
@Module({ | ||
imports: [ | ||
LoggerModule.forRoot({ | ||
exclude: ["/health", "/version", "/settings"], | ||
}), | ||
ConfigModule.forRoot({ | ||
isGlobal: true, | ||
cache: true, | ||
}), | ||
], | ||
}) | ||
export class BaseModule { | ||
static with(metadata: ModuleMetadata): DynamicModule { | ||
return { | ||
module: BaseModule, | ||
global: true, | ||
...metadata, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,75 @@ | ||
import { PostgreSqlContainer, StartedPostgreSqlContainer } from '@stickyjs/testcontainers'; | ||
import { UserController} from "./UserController"; | ||
import { UserService} from "./UserService"; | ||
import {PrismaService} from "../PrismaService"; | ||
|
||
import {buildTestConfig, TestingModule } from "../../test/TestingModule"; | ||
import {MarbleFiLsdServerTestingApp} from "../../dist/test-i9n/MarbleFiLsdServerTestingApp"; | ||
|
||
describe('UserController', () => { | ||
let testing: MarbleFiLsdServerTestingApp | ||
let userController: UserController; | ||
let userService: UserService; | ||
let prismaService: PrismaService | ||
let startedPostgresContainer: StartedPostgreSqlContainer; | ||
|
||
beforeAll(async () => { | ||
startedPostgresContainer = await new PostgreSqlContainer().start(); | ||
testing = new MarbleFiLsdServerTestingApp( | ||
TestingModule.register( | ||
buildTestConfig({startedPostgresContainer}), | ||
), | ||
); | ||
const app = await testing.start(); | ||
import { HttpStatus } from "@nestjs/common"; | ||
import { | ||
PostgreSqlContainer, | ||
StartedPostgreSqlContainer, | ||
} from "@stickyjs/testcontainers"; | ||
import { UserController } from "./UserController"; | ||
import { UserService } from "./UserService"; | ||
import { PrismaService } from "../PrismaService"; | ||
|
||
// init postgres database | ||
prismaService = app.get<PrismaService>(PrismaService); | ||
userService = new UserService(prismaService); | ||
userController = new UserController(userService); | ||
}); | ||
import { buildTestConfig, TestingModule } from "../../test/TestingModule"; | ||
import { MarbleFiLsdServerTestingApp } from "../../test/MarbleFiLsdServerTestingApp"; | ||
|
||
describe("UserController", () => { | ||
let testing: MarbleFiLsdServerTestingApp; | ||
let userController: UserController; | ||
let userService: UserService; | ||
let prismaService: PrismaService; | ||
let startedPostgresContainer: StartedPostgreSqlContainer; | ||
|
||
beforeAll(async () => { | ||
startedPostgresContainer = await new PostgreSqlContainer().start(); | ||
testing = new MarbleFiLsdServerTestingApp( | ||
TestingModule.register(buildTestConfig({ startedPostgresContainer })), | ||
); | ||
const app = await testing.start(); | ||
|
||
describe('create user', () => { | ||
it('should create an active user in db', async () => { | ||
const res = await userController.create("[email protected]", 'ACTIVE'); | ||
// init postgres database | ||
prismaService = app.get<PrismaService>(PrismaService); | ||
userService = new UserService(prismaService); | ||
userController = new UserController(userService); | ||
}); | ||
|
||
expect(res).toEqual({ id: 1, email: '[email protected]', status: 'ACTIVE' }); | ||
}); | ||
describe("create user", () => { | ||
it("should create an active user in db", async () => { | ||
const res = await userController.create("[email protected]", "ACTIVE"); | ||
|
||
it('should create an inactive user in db', async () => { | ||
const res = await userController.create("[email protected]", 'INACTIVE'); | ||
expect(res).toEqual({ | ||
id: 1, | ||
email: "[email protected]", | ||
status: "ACTIVE", | ||
}); | ||
}); | ||
|
||
expect(res).toEqual({ id: 2, email: '[email protected]', status: 'INACTIVE' }); | ||
}); | ||
it("should create an inactive user in db", async () => { | ||
const res = await userController.create("[email protected]", "INACTIVE"); | ||
|
||
it('should create an active user by default', async () => { | ||
const res = await userController.create("[email protected]"); | ||
expect(res).toEqual({ | ||
id: 2, | ||
email: "[email protected]", | ||
status: "INACTIVE", | ||
}); | ||
}); | ||
|
||
expect(res).toEqual({ id: 3, email: '[email protected]', status: 'ACTIVE' }); | ||
}); | ||
it("should create an active user by default", async () => { | ||
const res = await userController.create("[email protected]"); | ||
|
||
it('should not create a user with same email', async () => { | ||
const res = await userController.create("[email protected]"); | ||
expect(res).toEqual({ | ||
id: 3, | ||
email: "[email protected]", | ||
status: "ACTIVE", | ||
}); | ||
}); | ||
|
||
console.log(res) | ||
// expect(res).toEqual({ id: 3, email: 'test@example.com', status: 'ACTIVE' }); | ||
}); | ||
it("should not create a user with same email", async () => { | ||
try { | ||
await userController.create("[email protected]"); | ||
} catch (e) { | ||
expect(e.response.statusCode).toStrictEqual(HttpStatus.BAD_REQUEST); | ||
expect(e.response.message).toStrictEqual( | ||
`Duplicate email '[email protected]' found in database`, | ||
); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { NestFastifyApplication } from "@nestjs/platform-fastify"; | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { | ||
Chain as LightMyRequestChain, | ||
InjectOptions, | ||
Response as LightMyRequestResponse, | ||
} from "light-my-request"; | ||
|
||
import { MarbleFiLsdServerApp } from "../src/MarbleFiLsdServerApp"; | ||
import { BaseModule } from "../src/modules/BaseModule"; | ||
|
||
/** | ||
* Testing app used for testing MarbleFi Server App behaviour through integration tests | ||
*/ | ||
export class MarbleFiLsdServerTestingApp extends MarbleFiLsdServerApp<NestFastifyApplication> { | ||
async createTestingModule(): Promise<TestingModule> { | ||
return Test.createTestingModule({ | ||
imports: [ | ||
BaseModule.with({ | ||
imports: [this.module], | ||
}), | ||
], | ||
}).compile(); | ||
} | ||
|
||
override async createNestApp(): Promise<NestFastifyApplication> { | ||
const module = await this.createTestingModule(); | ||
return module.createNestApplication<NestFastifyApplication>( | ||
this.fastifyAdapter, | ||
this.nestApplicationOptions, | ||
); | ||
} | ||
|
||
async start(): Promise<NestFastifyApplication> { | ||
return this.init(); | ||
} | ||
|
||
/** | ||
* A wrapper function around native `fastify.inject()` method. | ||
* @returns {void} | ||
*/ | ||
inject(): LightMyRequestChain; | ||
inject(opts: InjectOptions | string): Promise<LightMyRequestResponse>; | ||
inject( | ||
opts?: InjectOptions | string, | ||
): LightMyRequestChain | Promise<LightMyRequestResponse> { | ||
if (opts === undefined) { | ||
return this.app!.inject(); | ||
} | ||
return this.app!.inject(opts); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,41 @@ | ||
import * as child_process from 'node:child_process'; | ||
import * as child_process from "node:child_process"; | ||
|
||
import { DynamicModule, Module } from '@nestjs/common'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { StartedPostgreSqlContainer } from '@stickyjs/testcontainers'; | ||
import { DynamicModule, Module } from "@nestjs/common"; | ||
import { ConfigModule } from "@nestjs/config"; | ||
import { StartedPostgreSqlContainer } from "@stickyjs/testcontainers"; | ||
|
||
import { AppConfig, DeepPartial} from "../src/AppConfig"; | ||
import { AppModule} from "../src/AppModule"; | ||
import { AppConfig, DeepPartial } from "../src/AppConfig"; | ||
import { AppModule } from "../src/AppModule"; | ||
|
||
@Module({}) | ||
export class TestingModule { | ||
static register(config: AppConfig): DynamicModule { | ||
return { | ||
module: TestingModule, | ||
imports: [AppModule, ConfigModule.forFeature(() => config)], | ||
}; | ||
} | ||
static register(config: AppConfig): DynamicModule { | ||
return { | ||
module: TestingModule, | ||
imports: [AppModule, ConfigModule.forFeature(() => config)], | ||
}; | ||
} | ||
} | ||
|
||
export function buildTestConfig({ | ||
startedPostgresContainer, | ||
}: BuildTestConfigParams) { | ||
if (startedPostgresContainer === undefined) { | ||
throw Error('Must pass in StartedPostgresContainer'); | ||
} | ||
const dbUrl = `postgres://${startedPostgresContainer.getUsername()}:${startedPostgresContainer.getPassword()}@${startedPostgresContainer.getHost()}:${startedPostgresContainer.getPort()}`; | ||
child_process.execSync(`export DATABASE_URL=${dbUrl} && pnpm prisma migrate deploy`); | ||
return { | ||
dbUrl: dbUrl ?? '' | ||
}; | ||
startedPostgresContainer, | ||
}: BuildTestConfigParams) { | ||
if (startedPostgresContainer === undefined) { | ||
throw Error("Must pass in StartedPostgresContainer"); | ||
} | ||
const dbUrl = `postgres://${startedPostgresContainer.getUsername()}:${startedPostgresContainer.getPassword()}@${startedPostgresContainer.getHost()}:${startedPostgresContainer.getPort()}`; | ||
child_process.execSync( | ||
`export DATABASE_URL=${dbUrl} && pnpm prisma migrate deploy`, | ||
); | ||
return { | ||
dbUrl: dbUrl ?? "", | ||
}; | ||
} | ||
|
||
type BuildTestConfigParams = DeepPartial<OptionalBuildTestConfigParams> & { | ||
startedPostgresContainer: StartedPostgreSqlContainer; | ||
startedPostgresContainer: StartedPostgreSqlContainer; | ||
}; | ||
|
||
type OptionalBuildTestConfigParams = { | ||
dbUrl: string; | ||
dbUrl: string; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.