From 1d9f3e8d5f615808bbafe5f60a0531dcff1ce767 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 24 Aug 2025 14:37:14 +0200 Subject: [PATCH] add new tests --- package-lock.json | 12 +- package.json | 5 +- src/guards/token.guard.ts | 2 +- src/services/account.factory.service.spec.ts | 133 ++++++++--- src/services/account.service.spec.ts | 230 +++++++++++++++++++ src/services/account.service.ts | 11 +- src/services/group.service.ts | 11 +- src/services/login.service.ts | 6 +- src/services/organization.service.ts | 7 +- src/services/registration.service.ts | 2 +- src/services/right.service.ts | 4 +- src/token.authentication.module.spec.ts | 40 ---- test/account.repository.mock.ts | 25 +- test/group.repository.mock.ts | 22 +- test/mail.verification.service.mock.ts | 21 +- test/organization.repository.mock.ts | 25 +- test/password.service.mock.ts | 22 +- test/right.repository.mock.ts | 22 +- test/session.service.mock.ts | 30 ++- test/token.service.mock.ts | 31 +-- 20 files changed, 448 insertions(+), 213 deletions(-) create mode 100644 src/services/account.service.spec.ts diff --git a/package-lock.json b/package-lock.json index 316a3f9..b15ef4f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@apihub24/token-authentication", - "version": "1.0.5", + "version": "1.0.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@apihub24/token-authentication", - "version": "1.0.5", + "version": "1.0.6", "license": "MIT", "dependencies": { - "@apihub24/authentication": "^1.0.1", + "@apihub24/authentication": "2.0.0-alpha.0", "@apihub24/repository": "^1.0.3", "@apihub24/translation": "^1.0.1", "@nestjs/common": "^11.1.6", @@ -42,9 +42,9 @@ } }, "node_modules/@apihub24/authentication": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/@apihub24/authentication/-/authentication-1.0.1.tgz", - "integrity": "sha512-nWw75ofQKHxE0dI7PzvNBQNcQrX/HSrzuAJTYNu42BoCROba1NUz8QAodTn5+3dIeQEzw127gtSb6D7yW0B8Jg==", + "version": "2.0.0-alpha.0", + "resolved": "https://registry.npmjs.org/@apihub24/authentication/-/authentication-2.0.0-alpha.0.tgz", + "integrity": "sha512-e7ZGD2fHSo2LJFuu9/6lJ4JMq52anK1jUe5btusVbNRzAP+8lrFcwytlctLUF2Adtlg/DgLGrLzCgSm+g19TVw==", "license": "MIT" }, "node_modules/@apihub24/repository": { diff --git a/package.json b/package.json index 0f07e71..28ecb66 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,13 @@ { "name": "@apihub24/token-authentication", - "version": "1.0.5", + "version": "2.0.0-alpha.0", "description": "", "main": "dist/index.js", "types": "./dist/index.d.ts", "scripts": { "build": "rimraf ./dist && tsc", "test": "jest", + "test:watch": "jest --watch", "test:coverage": "jest --coverage", "pub": "npm publish", "rel": "npm i && npm run build && npm run test:coverage" @@ -15,7 +16,7 @@ "@nestjs/common": "^11.1.6", "@nestjs/config": "^4.0.2", "@nestjs/core": "^11.1.6", - "@apihub24/authentication": "^1.0.1", + "@apihub24/authentication": "2.0.0-alpha.0", "@apihub24/repository": "^1.0.3", "@apihub24/translation": "^1.0.1", "class-validator": "^0.14.2", diff --git a/src/guards/token.guard.ts b/src/guards/token.guard.ts index e5d5ab1..8ead170 100644 --- a/src/guards/token.guard.ts +++ b/src/guards/token.guard.ts @@ -23,7 +23,7 @@ export class TokenGuard implements CanActivate { */ constructor( private reflector: Reflector, - @Inject("@apihub24/token_service") + @Inject(authentication.APIHUB24_TOKEN_SERVICE) private readonly tokenService: authentication.ITokenService ) {} diff --git a/src/services/account.factory.service.spec.ts b/src/services/account.factory.service.spec.ts index 04c88ba..90a190e 100644 --- a/src/services/account.factory.service.spec.ts +++ b/src/services/account.factory.service.spec.ts @@ -6,6 +6,7 @@ import { } from "../"; import { APIHUB24_PASSWORD_SERVICE, + IPasswordService, IRegistration, } from "@apihub24/authentication"; import { MailServiceMockModule } from "../../test/mail.verification.service.mock"; @@ -18,11 +19,51 @@ import { RightRepositoryMockModule } from "../../test/right.repository.mock"; import { OrganizationRepositoryMockModule } from "../../test/organization.repository.mock"; import { Group } from "../models/group"; import { Organization } from "../models/organization"; +import * as validator from "class-validator"; +import * as transformer from "class-transformer"; +import { Account } from "../models/account"; +import { Registration } from "../models/registration"; describe("AccountFactoryService Tests", () => { - let module: TestingModule | null = null; + let service: AccountFactoryService; + let passwordService: IPasswordService; + let groupService: GroupService; + let module: TestingModule; + + const mockedPasswordHash = "hashed_password_123"; + const mockedAdminGroup = { + id: "1", + name: "admin", + organization: { + id: "1", + name: "root", + } as Organization, + rights: [], + } as Group; + const mockedRegistration: IRegistration = { + accountName: "test", + email: "test@example.de", + password: "123", + passwordComparer: "123", + groupIds: [mockedAdminGroup.id], + }; + + jest.spyOn(transformer, "plainToClass").mockImplementation((cls, obj) => { + if (cls.name === "Account") { + const account = new Account(); + Object.assign(account, obj); + return account; + } + if (cls.name === "Registration") { + const registration = new Registration(); + Object.assign(registration, obj); + return registration; + } + return obj; + }); beforeAll(async () => { + // create testing module module = await Test.createTestingModule({ imports: [ MailServiceMockModule.forRoot(), @@ -35,42 +76,72 @@ describe("AccountFactoryService Tests", () => { OrganizationRepositoryMockModule.forRoot(), TokenAuthenticationModule.forRoot(), ], - }).compile(); + }) + .overrideProvider(APIHUB24_PASSWORD_SERVICE) + .useValue({ + hash: jest.fn().mockResolvedValue(mockedPasswordHash), + }) + .overrideProvider(GroupService) + .useValue({ + getBy: jest.fn().mockResolvedValue([]), + }) + .compile(); + + // get services + service = module.get(AccountFactoryService); + passwordService = module.get(APIHUB24_PASSWORD_SERVICE); + groupService = module.get(GroupService); + + jest.clearAllMocks(); }); - it("should create Account from Registration", async () => { + it("should be defined", () => { expect(module).toBeDefined(); - const service = module?.get(AccountFactoryService); - const groupService = module?.get(GroupService); - const passwordService = module?.get(APIHUB24_PASSWORD_SERVICE); expect(service).toBeDefined(); - expect(groupService).toBeDefined(); expect(passwordService).toBeDefined(); + expect(groupService).toBeDefined(); + }); - const adminGroup = new Group(); - adminGroup.id = "1"; - adminGroup.name = "admin"; - adminGroup.organization = new Organization(); - adminGroup.organization.id = "1"; - adminGroup.organization.name = "root"; - adminGroup.rights = []; - const registration: IRegistration = { - accountName: "test", - email: "test@example.de", - password: "123", - passwordComparer: "123", - groupIds: ["1"], - }; - jest - .spyOn(groupService!, "getBy") - .mockReturnValue(Promise.resolve([adminGroup])); - jest.spyOn(passwordService, "hash").mockReturnValue(registration.password); - const user = await service?.createFromRegistration(registration); + it("should create inactive Account from Registration with unverified Email", async () => { + const user = await service.createFromRegistration(mockedRegistration); expect(user).toBeDefined(); - expect(user?.accountName).toBe(registration.accountName); - expect(user?.email).toBe(registration.email); - expect(user?.passwordHash).toBe(registration.password); - expect(user?.emailVerified).toBeFalsy(); - expect(user?.active).toBeFalsy(); + expect(user.accountName).toBe(mockedRegistration.accountName); + expect(user.email).toBe(mockedRegistration.email); + expect(user.passwordHash).toBe(mockedPasswordHash); + expect(user.emailVerified).toBeFalsy(); + expect(user.active).toBeFalsy(); + }); + + it("should throw Error on invalid Registration", async () => { + const errorMessage = "Invalid Registration data"; + jest.spyOn(validator, "validate").mockReturnValueOnce( + Promise.resolve([ + { + toString: () => errorMessage, + } as validator.ValidationError, + ]) + ); + + expect(service.createFromRegistration(mockedRegistration)).rejects.toThrow( + errorMessage + ); + }); + + it("should throw Error on invalid Account", async () => { + const errorMessage = "Invalid Account data"; + jest + .spyOn(validator, "validate") + .mockReturnValueOnce(Promise.resolve([])) + .mockReturnValueOnce( + Promise.resolve([ + { + toString: () => errorMessage, + } as validator.ValidationError, + ]) + ); + + expect(service.createFromRegistration(mockedRegistration)).rejects.toThrow( + errorMessage + ); }); }); diff --git a/src/services/account.service.spec.ts b/src/services/account.service.spec.ts new file mode 100644 index 0000000..5f6551b --- /dev/null +++ b/src/services/account.service.spec.ts @@ -0,0 +1,230 @@ +import { Test, TestingModule } from "@nestjs/testing"; +import { AccountService, TokenAuthenticationModule } from "../"; +import { MailServiceMockModule } from "../../test/mail.verification.service.mock"; +import { PasswordServiceMockModule } from "../../test/password.service.mock"; +import { SessionServiceMockModule } from "../../test/session.service.mock"; +import { TokenServiceMockModule } from "../../test/token.service.mock"; +import { AccountRepositoryMockModule } from "../../test/account.repository.mock"; +import { GroupRepositoryMockModule } from "../../test/group.repository.mock"; +import { RightRepositoryMockModule } from "../../test/right.repository.mock"; +import { OrganizationRepositoryMockModule } from "../../test/organization.repository.mock"; +import { IRepository } from "@apihub24/repository"; +import { + APIHUB24_ACCOUNT_REPOSITORY, + APIHUB24_GROUP_REPOSITORY, + IAccount, + IGroup, +} from "@apihub24/authentication"; + +describe("AccountService Tests", () => { + let module: TestingModule; + let service: AccountService; + let accountRepository: IRepository; + let groupRepository: IRepository; + + const mockedAdminGroup: IGroup = { + id: "1", + name: "admin", + organization: { + id: "1", + name: "root", + }, + rights: [], + }; + const mockedAdminAccount: IAccount = { + id: "1", + accountName: "admin", + email: "admin@example.de", + passwordHash: "123", + active: true, + emailVerified: true, + groups: [mockedAdminGroup], + }; + + beforeAll(async () => { + module = await Test.createTestingModule({ + imports: [ + MailServiceMockModule.forRoot(), + PasswordServiceMockModule.forRoot(), + SessionServiceMockModule.forRoot(), + TokenServiceMockModule.forRoot(), + AccountRepositoryMockModule.forRoot(), + GroupRepositoryMockModule.forRoot(), + RightRepositoryMockModule.forRoot(), + OrganizationRepositoryMockModule.forRoot(), + TokenAuthenticationModule.forRoot(), + ], + }).compile(); + + service = module.get(AccountService); + accountRepository = module.get(APIHUB24_ACCOUNT_REPOSITORY); + groupRepository = module.get(APIHUB24_GROUP_REPOSITORY); + + jest.clearAllMocks(); + }); + + it("should be defined", () => { + expect(service).toBeDefined(); + expect(accountRepository).toBeDefined(); + expect(groupRepository).toBeDefined(); + }); + + describe("getBy", () => { + it("should get accounts by filter", async () => { + jest + .spyOn(accountRepository, "getBy") + .mockResolvedValueOnce([mockedAdminAccount]); + const filter = (x) => x.accountName === "admin"; + const result = await service.getBy(filter); + expect(result).toStrictEqual([mockedAdminAccount]); + expect(accountRepository.getBy).toHaveBeenCalledWith(filter); + }); + }); + + describe("save", () => { + it("should save accounts", async () => { + jest + .spyOn(accountRepository, "save") + .mockReturnValueOnce(Promise.resolve([mockedAdminAccount])); + const result = await service.save(mockedAdminAccount); + expect(result).toBe(mockedAdminAccount); + expect(accountRepository.save).toHaveBeenCalledWith([mockedAdminAccount]); + }); + + it("should return null when save fails", async () => { + jest + .spyOn(accountRepository, "save") + .mockReturnValueOnce(Promise.resolve([])); + const result = await service.save(mockedAdminAccount); + expect(result).toBeNull(); + }); + + it("should return null when save throws an Error", async () => { + const errorMessage = "some Repository Error"; + jest + .spyOn(accountRepository, "save") + .mockRejectedValueOnce(new Error(errorMessage)); + const result = await service.save(mockedAdminAccount); + expect(result).toBeNull(); + }); + }); + + describe("delete", () => { + it("should delete Accounts", async () => { + const filter = (x) => x.accountName === "admin"; + jest + .spyOn(accountRepository, "deleteBy") + .mockReturnValueOnce(Promise.resolve(true)); + const result = await service.delete(filter); + expect(result).toBeTruthy(); + expect(accountRepository.deleteBy).toHaveBeenLastCalledWith(filter); + }); + }); + + describe("addAccountToGroup", () => { + it("should add a Account to a Group", async () => { + jest + .spyOn(accountRepository, "getBy") + .mockReturnValueOnce( + Promise.resolve([{ ...mockedAdminAccount, groups: [] }]) + ); + jest + .spyOn(groupRepository, "getBy") + .mockReturnValueOnce(Promise.resolve([mockedAdminGroup])); + jest + .spyOn(accountRepository, "save") + .mockReturnValue( + Promise.resolve([ + { ...mockedAdminAccount, groups: [mockedAdminGroup] }, + ]) + ); + + const result = await service.addAccountToGroup( + mockedAdminAccount.id, + mockedAdminGroup.id + ); + expect(result.groups).toContainEqual(mockedAdminGroup); + expect(accountRepository.getBy).toHaveBeenCalledWith( + expect.any(Function) + ); + expect(groupRepository.getBy).toHaveBeenCalledWith(expect.any(Function)); + expect(accountRepository.save).toHaveBeenCalled(); + }); + + it("should throw Error when Account not found", async () => { + jest + .spyOn(accountRepository, "getBy") + .mockReturnValueOnce(Promise.resolve([])); + expect( + service.addAccountToGroup(mockedAdminAccount.id, mockedAdminGroup.id) + ).rejects.toThrow(new Error("account with id 1 not found")); + }); + it("should throw Error when Group not found", async () => { + jest + .spyOn(accountRepository, "getBy") + .mockReturnValueOnce(Promise.resolve([mockedAdminAccount])); + jest + .spyOn(groupRepository, "getBy") + .mockReturnValueOnce(Promise.resolve([])); + expect( + service.addAccountToGroup(mockedAdminAccount.id, mockedAdminGroup.id) + ).rejects.toThrow(new Error("group with id 1 not found")); + }); + }); + + describe("removeAccountFromGroup", () => { + it("should remove Account from Group", async () => { + jest + .spyOn(accountRepository, "getBy") + .mockReturnValueOnce( + Promise.resolve([ + { ...mockedAdminAccount, groups: [mockedAdminGroup] }, + ]) + ); + jest + .spyOn(groupRepository, "getBy") + .mockReturnValueOnce(Promise.resolve([mockedAdminGroup])); + jest + .spyOn(accountRepository, "save") + .mockReturnValue( + Promise.resolve([{ ...mockedAdminAccount, groups: [] }]) + ); + + const result = await service.removeAccountFromGroup( + mockedAdminAccount.id, + mockedAdminGroup.id + ); + expect(result.groups).not.toContainEqual(mockedAdminGroup); + expect(accountRepository.getBy).toHaveBeenCalledWith( + expect.any(Function) + ); + expect(groupRepository.getBy).toHaveBeenCalledWith(expect.any(Function)); + expect(accountRepository.save).toHaveBeenCalled(); + }); + it("should throw Error when Account not found", async () => { + jest + .spyOn(accountRepository, "getBy") + .mockReturnValueOnce(Promise.resolve([])); + expect( + service.removeAccountFromGroup( + mockedAdminAccount.id, + mockedAdminGroup.id + ) + ).rejects.toThrow(new Error("account with id 1 not found")); + }); + it("should throw Error when Group not found", async () => { + jest + .spyOn(accountRepository, "getBy") + .mockReturnValueOnce(Promise.resolve([mockedAdminAccount])); + jest + .spyOn(groupRepository, "getBy") + .mockReturnValueOnce(Promise.resolve([])); + expect( + service.removeAccountFromGroup( + mockedAdminAccount.id, + mockedAdminGroup.id + ) + ).rejects.toThrow(new Error("group with id 1 not found")); + }); + }); +}); diff --git a/src/services/account.service.ts b/src/services/account.service.ts index 35fd53c..98ecde3 100644 --- a/src/services/account.service.ts +++ b/src/services/account.service.ts @@ -1,13 +1,18 @@ -import { IAccount, IGroup } from "@apihub24/authentication"; +import { + APIHUB24_ACCOUNT_REPOSITORY, + APIHUB24_GROUP_REPOSITORY, + IAccount, + IGroup, +} from "@apihub24/authentication"; import * as repository from "@apihub24/repository"; import { Inject, Injectable } from "@nestjs/common"; @Injectable() export class AccountService { constructor( - @Inject("@apihub24/account_repository") + @Inject(APIHUB24_ACCOUNT_REPOSITORY) private readonly accountRepository: repository.IRepository, - @Inject("@apihub24/group_repository") + @Inject(APIHUB24_GROUP_REPOSITORY) private readonly groupRepository: repository.IRepository ) {} diff --git a/src/services/group.service.ts b/src/services/group.service.ts index 0b62ba1..3123886 100644 --- a/src/services/group.service.ts +++ b/src/services/group.service.ts @@ -1,13 +1,18 @@ import { Inject, Injectable } from "@nestjs/common"; import * as repository from "@apihub24/repository"; -import { IGroup, IRight } from "@apihub24/authentication"; +import { + APIHUB24_GROUP_REPOSITORY, + APIHUB24_RIGHT_REPOSITORY, + IGroup, + IRight, +} from "@apihub24/authentication"; @Injectable() export class GroupService { constructor( - @Inject("@apihub24/group_repository") + @Inject(APIHUB24_GROUP_REPOSITORY) private readonly groupRepository: repository.IRepository, - @Inject("@apihub24/right_repository") + @Inject(APIHUB24_RIGHT_REPOSITORY) private readonly rightRepository: repository.IRepository ) {} diff --git a/src/services/login.service.ts b/src/services/login.service.ts index 50ea18b..0013628 100644 --- a/src/services/login.service.ts +++ b/src/services/login.service.ts @@ -5,11 +5,11 @@ import * as authentication from "@apihub24/authentication"; @Injectable() export class LoginService { constructor( - @Inject("@apihub24/token_service") + @Inject(authentication.APIHUB24_TOKEN_SERVICE) private readonly tokenService: authentication.ITokenService, - @Inject("@apihub24/password_service") + @Inject(authentication.APIHUB24_PASSWORD_SERVICE) private readonly passwordService: authentication.IPasswordService, - @Inject("@apihub24/session_service") + @Inject(authentication.APIHUB24_SESSION_SERVICE) private readonly sessionService: authentication.ISessionService, @Inject(AccountService) private readonly accountService: AccountService diff --git a/src/services/organization.service.ts b/src/services/organization.service.ts index dda36ea..1c00474 100644 --- a/src/services/organization.service.ts +++ b/src/services/organization.service.ts @@ -1,11 +1,14 @@ import { Inject, Injectable } from "@nestjs/common"; import * as repository from "@apihub24/repository"; -import { IOrganization } from "@apihub24/authentication"; +import { + APIHUB24_ORGANIZATION_REPOSITORY, + IOrganization, +} from "@apihub24/authentication"; @Injectable() export class OrganizationService { constructor( - @Inject("@apihub24/organization_repository") + @Inject(APIHUB24_ORGANIZATION_REPOSITORY) private readonly organizationRepository: repository.IRepository ) {} diff --git a/src/services/registration.service.ts b/src/services/registration.service.ts index 2070347..8931112 100644 --- a/src/services/registration.service.ts +++ b/src/services/registration.service.ts @@ -10,7 +10,7 @@ export class RegistrationService { private readonly accountService: AccountService, @Inject(AccountFactoryService) private readonly accountFactory: AccountFactoryService, - @Inject("@apihub24/mail_verification_service") + @Inject(authentication.APIHUB24_MAIL_SERVICE) private readonly mailVerificationService: authentication.IMailVerificationService ) {} diff --git a/src/services/right.service.ts b/src/services/right.service.ts index bee1562..aa1cef9 100644 --- a/src/services/right.service.ts +++ b/src/services/right.service.ts @@ -1,11 +1,11 @@ import { Inject, Injectable } from "@nestjs/common"; import * as repository from "@apihub24/repository"; -import { IRight } from "@apihub24/authentication"; +import { APIHUB24_RIGHT_REPOSITORY, IRight } from "@apihub24/authentication"; @Injectable() export class RightService { constructor( - @Inject("@apihub24/right_repository") + @Inject(APIHUB24_RIGHT_REPOSITORY) private readonly rightRepository: repository.IRepository ) {} diff --git a/src/token.authentication.module.spec.ts b/src/token.authentication.module.spec.ts index 81e8e55..431f157 100644 --- a/src/token.authentication.module.spec.ts +++ b/src/token.authentication.module.spec.ts @@ -65,44 +65,4 @@ describe("TokenAuthenticationModule Tests", () => { expect(module).toBeDefined(); expect(module?.get(AccountFactoryService)).toBeDefined(); }); - - it("should provide @apihub24/mail_verification_service implementation", () => { - expect(module).toBeDefined(); - expect(module?.get("@apihub24/mail_verification_service")).toBeDefined(); - }); - - it("should provide @apihub24/token_service implementation", () => { - expect(module).toBeDefined(); - expect(module?.get("@apihub24/token_service")).toBeDefined(); - }); - - it("should provide @apihub24/session_service implementation", () => { - expect(module).toBeDefined(); - expect(module?.get("@apihub24/session_service")).toBeDefined(); - }); - - it("should provide @apihub24/password_service implementation", () => { - expect(module).toBeDefined(); - expect(module?.get("@apihub24/password_service")).toBeDefined(); - }); - - it("should provide @apihub24/account_repository implementation", () => { - expect(module).toBeDefined(); - expect(module?.get("@apihub24/account_repository")).toBeDefined(); - }); - - it("should provide @apihub24/group_repository implementation", () => { - expect(module).toBeDefined(); - expect(module?.get("@apihub24/group_repository")).toBeDefined(); - }); - - it("should provide @apihub24/right_repository implementation", () => { - expect(module).toBeDefined(); - expect(module?.get("@apihub24/right_repository")).toBeDefined(); - }); - - it("should provide @apihub24/organization_repository implementation", () => { - expect(module).toBeDefined(); - expect(module?.get("@apihub24/organization_repository")).toBeDefined(); - }); }); diff --git a/test/account.repository.mock.ts b/test/account.repository.mock.ts index e565a09..3504b59 100644 --- a/test/account.repository.mock.ts +++ b/test/account.repository.mock.ts @@ -1,6 +1,9 @@ import { DynamicModule, Global, Module } from "@nestjs/common"; -import { IAccount } from "../src"; import { IRepository } from "@apihub24/repository"; +import { + APIHUB24_ACCOUNT_REPOSITORY, + IAccount, +} from "@apihub24/authentication"; @Global() @Module({}) @@ -8,8 +11,12 @@ export class AccountRepositoryMockModule { static forRoot(): DynamicModule { const providers = [ { - provide: "@apihub24/account_repository", - useClass: AccountRepositoryMock, + provide: APIHUB24_ACCOUNT_REPOSITORY, + useValue: { + deleteBy: jest.fn(), + getBy: jest.fn(), + save: jest.fn(), + } as IRepository, }, ]; return { @@ -19,15 +26,3 @@ export class AccountRepositoryMockModule { }; } } - -class AccountRepositoryMock implements IRepository { - getBy(filter: (model: IAccount) => boolean): Promise { - throw new Error("Method not implemented."); - } - save(models: IAccount[]): Promise { - throw new Error("Method not implemented."); - } - deleteBy(filter: (model: IAccount) => boolean): Promise { - throw new Error("Method not implemented."); - } -} diff --git a/test/group.repository.mock.ts b/test/group.repository.mock.ts index 16fd411..5a73ccb 100644 --- a/test/group.repository.mock.ts +++ b/test/group.repository.mock.ts @@ -1,6 +1,6 @@ import { DynamicModule, Global, Module } from "@nestjs/common"; -import { IGroup } from "../src"; import { IRepository } from "@apihub24/repository"; +import { APIHUB24_GROUP_REPOSITORY, IGroup } from "@apihub24/authentication"; @Global() @Module({}) @@ -8,8 +8,12 @@ export class GroupRepositoryMockModule { static forRoot(): DynamicModule { const providers = [ { - provide: "@apihub24/group_repository", - useClass: GroupRepositoryMock, + provide: APIHUB24_GROUP_REPOSITORY, + useValue: { + deleteBy: jest.fn(), + getBy: jest.fn(), + save: jest.fn(), + } as IRepository, }, ]; return { @@ -19,15 +23,3 @@ export class GroupRepositoryMockModule { }; } } - -class GroupRepositoryMock implements IRepository { - getBy(filter: (model: IGroup) => boolean): Promise { - throw new Error("Method not implemented."); - } - save(models: IGroup[]): Promise { - throw new Error("Method not implemented."); - } - deleteBy(filter: (model: IGroup) => boolean): Promise { - throw new Error("Method not implemented."); - } -} diff --git a/test/mail.verification.service.mock.ts b/test/mail.verification.service.mock.ts index 0824360..fadbb02 100644 --- a/test/mail.verification.service.mock.ts +++ b/test/mail.verification.service.mock.ts @@ -1,5 +1,8 @@ import { DynamicModule, Global, Module } from "@nestjs/common"; -import { IAccount, ISession, IMailVerificationService } from "../src"; +import { + APIHUB24_MAIL_SERVICE, + IMailVerificationService, +} from "@apihub24/authentication"; @Global() @Module({}) @@ -7,8 +10,11 @@ export class MailServiceMockModule { static forRoot(): DynamicModule { const providers = [ { - provide: "@apihub24/mail_verification_service", - useClass: MailServiceMock, + provide: APIHUB24_MAIL_SERVICE, + useValue: { + sendVerificationMail: jest.fn(), + verify: jest.fn(), + } as IMailVerificationService, }, ]; return { @@ -18,12 +24,3 @@ export class MailServiceMockModule { }; } } - -class MailServiceMock implements IMailVerificationService { - sendVerificationMail(account: IAccount): Promise { - throw new Error("Method not implemented."); - } - verify(email: string, code: string): Promise { - throw new Error("Method not implemented."); - } -} diff --git a/test/organization.repository.mock.ts b/test/organization.repository.mock.ts index 8d69f06..3735c34 100644 --- a/test/organization.repository.mock.ts +++ b/test/organization.repository.mock.ts @@ -1,6 +1,9 @@ import { DynamicModule, Global, Module } from "@nestjs/common"; -import { IOrganization } from "../src"; import { IRepository } from "@apihub24/repository"; +import { + APIHUB24_ORGANIZATION_REPOSITORY, + IOrganization, +} from "@apihub24/authentication"; @Global() @Module({}) @@ -8,8 +11,12 @@ export class OrganizationRepositoryMockModule { static forRoot(): DynamicModule { const providers = [ { - provide: "@apihub24/organization_repository", - useClass: OrganizationRepositoryMock, + provide: APIHUB24_ORGANIZATION_REPOSITORY, + useValue: { + deleteBy: jest.fn(), + getBy: jest.fn(), + save: jest.fn(), + } as IRepository, }, ]; return { @@ -19,15 +26,3 @@ export class OrganizationRepositoryMockModule { }; } } - -class OrganizationRepositoryMock implements IRepository { - getBy(filter: (model: IOrganization) => boolean): Promise { - throw new Error("Method not implemented."); - } - save(models: IOrganization[]): Promise { - throw new Error("Method not implemented."); - } - deleteBy(filter: (model: IOrganization) => boolean): Promise { - throw new Error("Method not implemented."); - } -} diff --git a/test/password.service.mock.ts b/test/password.service.mock.ts index 3901885..17f3654 100644 --- a/test/password.service.mock.ts +++ b/test/password.service.mock.ts @@ -1,12 +1,21 @@ import { DynamicModule, Global, Module } from "@nestjs/common"; -import { IPasswordService } from "../src"; +import { + APIHUB24_PASSWORD_SERVICE, + IPasswordService, +} from "@apihub24/authentication"; @Global() @Module({}) export class PasswordServiceMockModule { static forRoot(): DynamicModule { const providers = [ - { provide: "@apihub24/password_service", useClass: PasswordServiceMock }, + { + provide: APIHUB24_PASSWORD_SERVICE, + useValue: { + hash: jest.fn(), + verify: jest.fn(), + } as IPasswordService, + }, ]; return { module: PasswordServiceMockModule, @@ -15,12 +24,3 @@ export class PasswordServiceMockModule { }; } } - -class PasswordServiceMock implements IPasswordService { - hash(plainTextPassword: string): Promise { - throw new Error("Method not implemented."); - } - verify(plainTextPassword: string, passwordHash: string): Promise { - throw new Error("Method not implemented."); - } -} diff --git a/test/right.repository.mock.ts b/test/right.repository.mock.ts index 199b7e7..79522a8 100644 --- a/test/right.repository.mock.ts +++ b/test/right.repository.mock.ts @@ -1,6 +1,6 @@ import { DynamicModule, Global, Module } from "@nestjs/common"; -import { IRight } from "../src"; import { IRepository } from "@apihub24/repository"; +import { APIHUB24_RIGHT_REPOSITORY, IRight } from "@apihub24/authentication"; @Global() @Module({}) @@ -8,8 +8,12 @@ export class RightRepositoryMockModule { static forRoot(): DynamicModule { const providers = [ { - provide: "@apihub24/right_repository", - useClass: RightRepositoryMock, + provide: APIHUB24_RIGHT_REPOSITORY, + useValue: { + getBy: jest.fn(), + deleteBy: jest.fn(), + save: jest.fn(), + } as IRepository, }, ]; return { @@ -19,15 +23,3 @@ export class RightRepositoryMockModule { }; } } - -class RightRepositoryMock implements IRepository { - getBy(filter: (model: IRight) => boolean): Promise { - throw new Error("Method not implemented."); - } - save(models: IRight[]): Promise { - throw new Error("Method not implemented."); - } - deleteBy(filter: (model: IRight) => boolean): Promise { - throw new Error("Method not implemented."); - } -} diff --git a/test/session.service.mock.ts b/test/session.service.mock.ts index 94890e8..32a395e 100644 --- a/test/session.service.mock.ts +++ b/test/session.service.mock.ts @@ -1,12 +1,23 @@ import { DynamicModule, Global, Module } from "@nestjs/common"; -import { IAccount, ISession, ISessionService } from "../src"; +import { + APIHUB24_SESSION_SERVICE, + ISessionService, +} from "@apihub24/authentication"; @Global() @Module({}) export class SessionServiceMockModule { static forRoot(): DynamicModule { const providers = [ - { provide: "@apihub24/session_service", useClass: SessionServiceMock }, + { + provide: APIHUB24_SESSION_SERVICE, + useValue: { + create: jest.fn(), + getBy: jest.fn(), + getById: jest.fn(), + remove: jest.fn(), + } as ISessionService, + }, ]; return { module: SessionServiceMockModule, @@ -15,18 +26,3 @@ export class SessionServiceMockModule { }; } } - -class SessionServiceMock implements ISessionService { - create(account: IAccount): Promise { - throw new Error("Method not implemented."); - } - getBy(filter: (account: IAccount) => boolean): Promise { - throw new Error("Method not implemented."); - } - getById(sessionId: string): Promise { - throw new Error("Method not implemented."); - } - remove(sessionId: string): Promise { - throw new Error("Method not implemented."); - } -} diff --git a/test/token.service.mock.ts b/test/token.service.mock.ts index 397a223..c76c96d 100644 --- a/test/token.service.mock.ts +++ b/test/token.service.mock.ts @@ -1,12 +1,22 @@ import { DynamicModule, Global, Module } from "@nestjs/common"; -import { Algorithm, IAccount, ISession, ITokenService } from "../src"; +import { + APIHUB24_TOKEN_SERVICE, + ITokenService, +} from "@apihub24/authentication"; @Global() @Module({}) export class TokenServiceMockModule { static forRoot(): DynamicModule { const providers = [ - { provide: "@apihub24/token_service", useClass: TokenServiceMock }, + { + provide: APIHUB24_TOKEN_SERVICE, + useValue: { + generate: jest.fn(), + getAccount: jest.fn(), + validate: jest.fn(), + } as ITokenService, + }, ]; return { module: TokenServiceMockModule, @@ -15,20 +25,3 @@ export class TokenServiceMockModule { }; } } - -class TokenServiceMock implements ITokenService { - generate( - session: ISession, - subject: string, - expires?: string, - algorithm?: Algorithm - ): Promise { - throw new Error("Method not implemented."); - } - validate(token: string): Promise { - throw new Error("Method not implemented."); - } - getAccount(token: string): Promise { - throw new Error("Method not implemented."); - } -}