UNPKG

@hz-9/a5-authn

Version:

Authentication module for the @hz-9/a5-* series of repositories.

231 lines 8.7 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); require("./__import-reflect-metadata"); const a5_factory_1 = require("@hz-9/a5-core/core/a5-factory"); const common_1 = require("@nestjs/common"); const supertest_1 = __importDefault(require("supertest")); const const_1 = require("../../const"); const pwd_1 = require("../../modules/pwd"); // ==================== Mock Service ==================== let MockPwdService = class MockPwdService { async getTokens(user) { return { accessToken: 'mock-pwd-access-token', refreshToken: 'mock-pwd-refresh-token', tokenType: 'Bearer', expiresIn: 3600, }; } async validateCredentials(username, password) { if (username === 'test' && password === 'password') { return { id: '1', username }; } return null; } }; MockPwdService = __decorate([ (0, common_1.Injectable)() ], MockPwdService); let MockPwdModule = class MockPwdModule { }; MockPwdModule = __decorate([ (0, common_1.Global)(), (0, common_1.Module)({ providers: [ { provide: const_1.A5_AUTHN_SERVICE_TOKEN, useClass: MockPwdService, }, ], exports: [ { provide: const_1.A5_AUTHN_SERVICE_TOKEN, useClass: MockPwdService, }, ], }) ], MockPwdModule); // ==================== Tests ==================== describe('A5AuthnPwdModule Integration Test', () => { // eslint-disable-next-line init-declarations let processStdoutWriteSpy; // eslint-disable-next-line init-declarations let processStderrWriteSpy; beforeAll(() => { processStdoutWriteSpy = jest.spyOn(process.stdout, 'write').mockImplementation(); processStderrWriteSpy = jest.spyOn(process.stderr, 'write').mockImplementation(); jest.clearAllMocks(); }); afterAll(() => { processStdoutWriteSpy.mockRestore(); processStderrWriteSpy.mockRestore(); jest.restoreAllMocks(); }); describe('[[Module]]', () => { it('forRoot', async () => { let TestModule = class TestModule { }; TestModule = __decorate([ (0, common_1.Module)({ imports: [ MockPwdModule, pwd_1.A5AuthnPwdModule.forRoot({ usernameField: 'username', passwordField: 'password', }), ], }) ], TestModule); const app = await a5_factory_1.A5Factory.create(TestModule, { printLogo: false, }); expect(app).toBeDefined(); expect(app.nestApp).toBeDefined(); await app.close(); }); it('forRootAsync(useFactory)', async () => { let TestModule = class TestModule { }; TestModule = __decorate([ (0, common_1.Module)({ imports: [ MockPwdModule, pwd_1.A5AuthnPwdModule.forRootAsync({ useFactory: () => ({ usernameField: 'username', passwordField: 'password', }), }), ], }) ], TestModule); const app = await a5_factory_1.A5Factory.create(TestModule, { printLogo: false, }); expect(app).toBeDefined(); expect(app.nestApp).toBeDefined(); await app.close(); }); it('forRootAsync(async useFactory)', async () => { let TestModule = class TestModule { }; TestModule = __decorate([ (0, common_1.Module)({ imports: [ MockPwdModule, pwd_1.A5AuthnPwdModule.forRootAsync({ useFactory: async () => ({ usernameField: 'username', passwordField: 'password', }), }), ], }) ], TestModule); const app = await a5_factory_1.A5Factory.create(TestModule, { printLogo: false, }); expect(app).toBeDefined(); expect(app.nestApp).toBeDefined(); await app.close(); }); it('forRootAsync(useClass)', async () => { class ModuleFactory { createOptions() { return { usernameField: 'username', passwordField: 'password', }; } } let TestModule = class TestModule { }; TestModule = __decorate([ (0, common_1.Module)({ imports: [ MockPwdModule, pwd_1.A5AuthnPwdModule.forRootAsync({ useClass: ModuleFactory, }), ], }) ], TestModule); const app = await a5_factory_1.A5Factory.create(TestModule, { printLogo: false, }); expect(app).toBeDefined(); expect(app.nestApp).toBeDefined(); await app.close(); }); it('forRootAsync(async useClass)', async () => { class ModuleFactory { async createOptions() { return { usernameField: 'username', passwordField: 'password', }; } } let TestModule = class TestModule { }; TestModule = __decorate([ (0, common_1.Module)({ imports: [ MockPwdModule, pwd_1.A5AuthnPwdModule.forRootAsync({ useClass: ModuleFactory, }), ], }) ], TestModule); const app = await a5_factory_1.A5Factory.create(TestModule, { printLogo: false, }); expect(app).toBeDefined(); expect(app.nestApp).toBeDefined(); await app.close(); }); }); describe('[[API]]', () => { it('POST /auth/pwd/login', async () => { let TestModule = class TestModule { }; TestModule = __decorate([ (0, common_1.Module)({ imports: [ MockPwdModule, pwd_1.A5AuthnPwdModule.forRoot({ usernameField: 'username', passwordField: 'password', }), ], }) ], TestModule); const app = await a5_factory_1.A5Factory.create(TestModule, { printLogo: false, }); await app.nestApp.getHttpAdapter().getInstance().ready(); const response = await (0, supertest_1.default)(app.nestApp.getHttpServer()) .post('/auth/pwd/login') .send({ username: 'test', password: 'password' }) .expect(200); expect(response.body).toMatchObject({ accessToken: 'mock-pwd-access-token', refreshToken: 'mock-pwd-refresh-token', tokenType: 'Bearer', expiresIn: 3600, }); await app.close(); }); }); }); //# sourceMappingURL=a5-authn-pwd-module.integration.spec.js.map