|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { LdapUsersService } from './ldapusers.service'; |
| 3 | +import { AuthService } from '../../auth/auth.service'; |
| 4 | +import { PrismaService } from '../../prisma/prisma.service'; |
| 5 | +import { ConfigService } from '@nestjs/config'; |
| 6 | +import { Role, User } from '@prisma/client'; |
| 7 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 8 | +import { Entry as LdapEntry, Client as LdapClient, SearchOptions, SearchResult } from 'ldapts'; |
| 9 | + |
| 10 | +jest.mock('ldapts', () => { |
| 11 | + const mockLdapClient = { |
| 12 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 13 | + search: jest.fn((_searchDN, searchOptions: SearchOptions): Promise<SearchResult> => { |
| 14 | + if (searchOptions.filter.toString().includes('[email protected]')) { |
| 15 | + return Promise.resolve({ searchEntries: [{dn: 'dn',LDAP_ATTRIBUTE_MAIL: '[email protected]',LDAP_ATTRIBUTE_FIRST_NAME: 'first',LDAP_ATTRIBUTE_LAST_NAME: 'last'}], searchReferences: [] }); |
| 16 | + } else { |
| 17 | + return Promise.resolve({ searchEntries: [], searchReferences: [] }); |
| 18 | + } |
| 19 | + }), |
| 20 | + bind: jest.fn(), |
| 21 | + unbind: jest.fn(), |
| 22 | + }; |
| 23 | + |
| 24 | + return { |
| 25 | + Client: jest.fn(() => mockLdapClient), |
| 26 | + }; |
| 27 | +}); |
| 28 | + |
| 29 | +const user: User = { |
| 30 | + id: '1', |
| 31 | + |
| 32 | + firstName: 'John', |
| 33 | + lastName: 'Doe', |
| 34 | + apiKey: 'generatedApiKey', |
| 35 | + password: 'encryptedPassword', |
| 36 | + isActive: true, |
| 37 | + role: Role.editor, |
| 38 | + updatedAt: new Date(), |
| 39 | + createdAt: new Date(), |
| 40 | +}; |
| 41 | + |
| 42 | +describe('LdapUsersService match from ldap', () => { |
| 43 | + let service: LdapUsersService; |
| 44 | + let prismaService: PrismaService; |
| 45 | + let configService: ConfigService; |
| 46 | + let authService: AuthService; |
| 47 | + |
| 48 | + beforeEach(async () => { |
| 49 | + const module: TestingModule = await Test.createTestingModule({ |
| 50 | + providers: [ |
| 51 | + { |
| 52 | + provide: ConfigService, |
| 53 | + useValue: { |
| 54 | + get: jest.fn().mockReturnValue('true'), |
| 55 | + getOrThrow: jest.fn((string) => (string === 'LDAP_USERS_SEARCH_FILTER' ? '(&(mail={{email}}))' : string)), |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + provide: PrismaService, |
| 60 | + useValue: { |
| 61 | + user: { |
| 62 | + findMany: jest.fn().mockResolvedValueOnce([]), |
| 63 | + findUnique: jest.fn(), |
| 64 | + create: jest.fn(), |
| 65 | + update: jest.fn(), |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + provide: AuthService, |
| 71 | + useValue: { |
| 72 | + generateApiKey: jest.fn(() => 'generatedApiKey'), |
| 73 | + encryptPassword: jest.fn(() => 'encryptedPassword'), |
| 74 | + signToken: jest.fn(() => 'token'), |
| 75 | + }, |
| 76 | + }, |
| 77 | + ], |
| 78 | + }).compile(); |
| 79 | + configService = module.get<ConfigService>(ConfigService); |
| 80 | + prismaService = module.get<PrismaService>(PrismaService); |
| 81 | + authService = module.get<AuthService>(AuthService); |
| 82 | + service = new LdapUsersService(configService, prismaService, authService); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should be defined', () => { |
| 86 | + expect(service).toBeDefined(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should create new user on login', async () => { |
| 90 | + jest.spyOn(prismaService.user, 'findUnique').mockResolvedValue(undefined); |
| 91 | + const prismaUserCreateMock = jest.spyOn(prismaService.user, 'create').mockResolvedValue(user); |
| 92 | + await service.login({ email: '[email protected]', password: 'password' }); |
| 93 | + expect(prismaUserCreateMock).toBeCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should login with user already in db', async () => { |
| 97 | + jest.spyOn(prismaService.user, 'findUnique').mockResolvedValue(user); |
| 98 | + const prismaUserCreateMock = jest.spyOn(prismaService.user, 'create'); |
| 99 | + await service.login({ email: '[email protected]', password: 'password' }); |
| 100 | + expect(prismaUserCreateMock).not.toBeCalled(); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe('LdapUsersService with no results from ldap', () => { |
| 105 | + let service: LdapUsersService; |
| 106 | + let prismaService: PrismaService; |
| 107 | + let configService: ConfigService; |
| 108 | + let authService: AuthService; |
| 109 | + |
| 110 | + beforeEach(async () => { |
| 111 | + const module: TestingModule = await Test.createTestingModule({ |
| 112 | + providers: [ |
| 113 | + { |
| 114 | + provide: ConfigService, |
| 115 | + useValue: { |
| 116 | + get: jest.fn().mockReturnValue('true'), |
| 117 | + getOrThrow: jest.fn().mockReturnValue('string'), |
| 118 | + }, |
| 119 | + }, |
| 120 | + { |
| 121 | + provide: PrismaService, |
| 122 | + useValue: { |
| 123 | + user: { |
| 124 | + findMany: jest.fn().mockResolvedValueOnce([]), |
| 125 | + create: jest.fn(), |
| 126 | + update: jest.fn(), |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + { |
| 131 | + provide: AuthService, |
| 132 | + useValue: { |
| 133 | + generateApiKey: jest.fn(() => 'generatedApiKey'), |
| 134 | + encryptPassword: jest.fn(() => 'encryptedPassword'), |
| 135 | + }, |
| 136 | + }, |
| 137 | + ], |
| 138 | + }).compile(); |
| 139 | + configService = module.get<ConfigService>(ConfigService); |
| 140 | + prismaService = module.get<PrismaService>(PrismaService); |
| 141 | + authService = module.get<AuthService>(AuthService); |
| 142 | + service = new LdapUsersService(configService, prismaService, authService); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should be defined', () => { |
| 146 | + expect(service).toBeDefined(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should never change a password', async () => { |
| 150 | + const prismaUserCreateMock = jest.spyOn(prismaService.user, 'create'); |
| 151 | + const prismaUserUpdateMock = jest.spyOn(prismaService.user, 'update'); |
| 152 | + const result = await service.changePassword(user, 'newPassword'); |
| 153 | + expect(prismaUserCreateMock).not.toBeCalled(); |
| 154 | + expect(prismaUserUpdateMock).not.toBeCalled(); |
| 155 | + expect(result).toBe(true); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should not login when ldap search results are empty', async () => { |
| 159 | + expect.assertions(1); |
| 160 | + try { |
| 161 | + await service.login({ email: 'testÜ*()\\\[email protected]', password: 'password' }); |
| 162 | + } catch (e) { |
| 163 | + expect(e.message).toBe('Invalid email or password.'); |
| 164 | + } |
| 165 | + }); |
| 166 | +}); |
0 commit comments