Skip to content

Commit 51db2d5

Browse files
committed
add ldap user service
1 parent ecc5726 commit 51db2d5

File tree

10 files changed

+481
-19
lines changed

10 files changed

+481
-19
lines changed

.env

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ AWS_REGION=
2121
AWS_S3_BUCKET_NAME=
2222

2323
# optional
24+
#LDAP_ENABLED=true
25+
#LDAP_TLS_NO_VERIFY=false
26+
#LDAP_URL='ldaps://ldap.example.com:636/'
27+
#LDAP_BIND_USER='CN=searchuser,OU=systemaccounts,DC=example,DC=com'
28+
#LDAP_BIND_PASSWORD='searchuserpassword'
29+
#LDAP_SEARCH_DN='OU=users,DC=example,DC=com'
30+
#LDAP_USERS_SEARCH_FILTER='(&(objectClass=Person)(objectCategory=Person)(|(mail={{email}})(sAMAccountName={{email}})))'
31+
#LDAP_ATTRIBUTE_LAST_NAME='sn'
32+
#LDAP_ATTRIBUTE_FIRST_NAME='givenName'
33+
#LDAP_ATTRIBUTE_MAIL='mail'
34+
2435
#HTTPS_KEY_PATH='./secrets/ssl.key'
2536
#HTTPS_CERT_PATH='./secrets/ssl.cert'
2637
#SERVER_TIMEOUT=120000

package-lock.json

Lines changed: 95 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"class-transformer": "^0.5.1",
4848
"class-validator": "^0.14.0",
4949
"fs-extra": "^11.1.1",
50+
"ldapts": "^7.1.0",
5051
"looks-same": "^9.0.0",
5152
"odiff-bin": "^2.6.1",
5253
"passport": "^0.6.0",

src/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { HealthController } from './health/health.controller';
2222
CacheModule.register(),
2323
ScheduleModule.forRoot(),
2424
AuthModule,
25-
UsersModule,
25+
UsersModule.register(),
2626
BuildsModule,
2727
ProjectsModule,
2828
TestRunsModule,

src/builds/builds.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { AuthModule } from '../auth/auth.module';
99
import { ProjectsModule } from '../projects/projects.module';
1010

1111
@Module({
12-
imports: [SharedModule, UsersModule, forwardRef(() => TestRunsModule), AuthModule, forwardRef(() => ProjectsModule)],
12+
imports: [SharedModule, UsersModule.register(), forwardRef(() => TestRunsModule), AuthModule, forwardRef(() => ProjectsModule)],
1313
providers: [BuildsService, PrismaService],
1414
controllers: [BuildsController],
1515
exports: [BuildsService],

src/users/dbusers.service.spec.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { Test, TestingModule } from '@nestjs/testing';
22
import { DbUsersService } from './dbusers.service';
33
import { AuthService } from '../auth/auth.service';
44
import { PrismaService } from '../prisma/prisma.service';
5+
import { UserLoginResponseDto } from './dto/user-login-response.dto';
6+
import { Role } from '@prisma/client';
57

6-
describe('UsersService', () => {
8+
describe('DbUsersService', () => {
79
let service: DbUsersService;
10+
let prismaService: PrismaService;
811

912
beforeEach(async () => {
1013
const module: TestingModule = await Test.createTestingModule({
@@ -15,17 +18,64 @@ describe('UsersService', () => {
1518
useValue: {
1619
user: {
1720
findMany: jest.fn().mockResolvedValueOnce([]),
21+
create: jest.fn(),
1822
},
1923
},
2024
},
21-
{ provide: AuthService, useValue: {} },
25+
{
26+
provide: AuthService,
27+
useValue: {
28+
generateApiKey: jest.fn((..._: any[]) => 'generatedApiKey'),
29+
encryptPassword: jest.fn((..._: any[]) => 'encryptedPassword'),
30+
},
31+
},
2232
],
2333
}).compile();
2434

2535
service = module.get<DbUsersService>(DbUsersService);
36+
prismaService = module.get<PrismaService>(PrismaService);
2637
});
2738

2839
it('should be defined', () => {
2940
expect(service).toBeDefined();
3041
});
42+
43+
it('should create a new user', async () => {
44+
// Arrange
45+
const createUserDto = {
46+
47+
firstName: 'John',
48+
lastName: 'Doe',
49+
password: 'password123',
50+
};
51+
52+
const prismaUserCreateMock = jest.spyOn(prismaService.user, 'create');
53+
prismaUserCreateMock.mockResolvedValueOnce({
54+
id: '1',
55+
56+
firstName: 'John',
57+
lastName: 'Doe',
58+
apiKey: 'generatedApiKey',
59+
password: 'encryptedPassword',
60+
isActive: true,
61+
role: Role.editor,
62+
updatedAt: new Date(),
63+
createdAt: new Date(),
64+
});
65+
66+
// Act
67+
const result = await service.create(createUserDto);
68+
69+
// Assert
70+
expect(prismaUserCreateMock).toHaveBeenCalledWith({
71+
data: {
72+
73+
firstName: 'John',
74+
lastName: 'Doe',
75+
apiKey: 'generatedApiKey',
76+
password: 'encryptedPassword',
77+
},
78+
});
79+
expect(result).toEqual(expect.any(UserLoginResponseDto));
80+
});
3181
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 { UserLoginResponseDto } from './dto/user-login-response.dto';
6+
7+
describe('LdapUsersService', () => {
8+
let service: LdapUsersService;
9+
10+
beforeEach(async () => {
11+
const module: TestingModule = await Test.createTestingModule({
12+
providers: [
13+
LdapUsersService,
14+
{
15+
provide: PrismaService,
16+
useValue: {
17+
user: {
18+
findMany: jest.fn().mockResolvedValueOnce([]),
19+
},
20+
},
21+
},
22+
{ provide: AuthService, useValue: {} },
23+
],
24+
}).compile();
25+
26+
service = module.get<LdapUsersService>(LdapUsersService);
27+
});
28+
29+
it('should be defined', () => {
30+
expect(service).toBeDefined();
31+
});
32+
33+
xit('login using ldap', async () => {
34+
// Act
35+
const result = await service.login({ email: '[email protected]', password: 'example' });
36+
// Assert
37+
expect(result).toEqual(expect.any(UserLoginResponseDto));
38+
});
39+
});

0 commit comments

Comments
 (0)