Skip to content

Commit a10df41

Browse files
committed
test fixes and linting
1 parent 2f490e3 commit a10df41

32 files changed

Lines changed: 224 additions & 143 deletions

apps/backend-services/src/actor/actor.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Module } from "@nestjs/common";
22
import { ApiKeyController } from "./api-key.controller";
33
import { ApiKeyService } from "./api-key.service";
44
import { ApiKeyDbService } from "./api-key-db.service";
5-
import { UserDbService } from "./user-db.service";
65
import { UserService } from "./user.service";
6+
import { UserDbService } from "./user-db.service";
77

88
@Module({
99
imports: [],

apps/backend-services/src/actor/api-key.controller.spec.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ describe("ApiKeyController", () => {
2323
roles: ["admin", "editor"],
2424
},
2525
resolvedIdentity: {
26-
actorId: "testuser",
26+
userId: "testuser",
2727
isSystemAdmin: false,
2828
groupRoles: { group123: GroupRole.ADMIN },
29+
actorId: "actor-1",
2930
},
3031
};
3132

@@ -102,7 +103,7 @@ describe("ApiKeyController", () => {
102103
{
103104
user: { sub: "testuser" },
104105
resolvedIdentity: {
105-
actorId: "testuser",
106+
userId: "testuser",
106107
isSystemAdmin: false,
107108
groupRoles: { group123: GroupRole.ADMIN },
108109
},
@@ -142,7 +143,7 @@ describe("ApiKeyController", () => {
142143
{
143144
...mockRequest,
144145
resolvedIdentity: {
145-
actorId: "testuser",
146+
userId: "testuser",
146147
isSystemAdmin: false,
147148
groupRoles: {},
148149
},
@@ -161,7 +162,7 @@ describe("ApiKeyController", () => {
161162
{
162163
user: { sub: "testuser" },
163164
resolvedIdentity: {
164-
actorId: "testuser",
165+
userId: "testuser",
165166
isSystemAdmin: false,
166167
groupRoles: { group123: GroupRole.ADMIN },
167168
},
@@ -241,9 +242,10 @@ describe("ApiKeyController", () => {
241242
{
242243
...mockRequest,
243244
resolvedIdentity: {
244-
actorId: "testuser",
245+
userId: "testuser",
245246
isSystemAdmin: false,
246247
groupRoles: {},
248+
actorId: "actor-1",
247249
},
248250
} as any,
249251
{
@@ -273,6 +275,7 @@ describe("ApiKeyController", () => {
273275
actorId: newUserId,
274276
isSystemAdmin: false,
275277
groupRoles: { group123: GroupRole.ADMIN },
278+
userId: newUserId,
276279
},
277280
};
278281

apps/backend-services/src/actor/user-db.service.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { PrismaService } from "@/database/prisma.service";
44
import { ApiKeyDbService, type CreateApiKeyData } from "./api-key-db.service";
55
import { UserDbService } from "./user-db.service";
66

7-
87
describe("UserDbService", () => {
98
let service: UserDbService;
109
let mockUserPrisma: {
@@ -47,7 +46,7 @@ describe("UserDbService", () => {
4746
expect(service).toBeDefined();
4847
});
4948

50-
describe("isUserSystemAdmin", () => {
49+
describe("isUserSystemAdmin", () => {
5150
it("returns true when admin (no tx)", async () => {
5251
mockPrisma.user.findUnique.mockResolvedValue({ is_system_admin: true });
5352
expect(await service.isUserSystemAdmin("user-1")).toBe(true);

apps/backend-services/src/actor/user-db.service.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { PrismaService } from "@/database/prisma.service";
77
*/
88
@Injectable()
99
export class UserDbService {
10-
constructor(private readonly prismaService: PrismaService) { }
10+
constructor(private readonly prismaService: PrismaService) {}
1111

1212
private get prisma(): PrismaClient {
1313
return this.prismaService.prisma;
@@ -45,10 +45,10 @@ export class UserDbService {
4545
: await this.prisma.$transaction(async (tx) => await userHelper(tx));
4646
}
4747
/**
48-
* Checks whether a user is a system admin.
49-
* @param userId - The ID of the user to check.
50-
* @returns `true` when the user has `is_system_admin` set to `true`, `false` otherwise.
51-
*/
48+
* Checks whether a user is a system admin.
49+
* @param userId - The ID of the user to check.
50+
* @returns `true` when the user has `is_system_admin` set to `true`, `false` otherwise.
51+
*/
5252
async isUserSystemAdmin(
5353
userId: string,
5454
tx?: Prisma.TransactionClient,
@@ -61,11 +61,15 @@ export class UserDbService {
6161
return user?.is_system_admin ?? false;
6262
}
6363

64-
async findUser(userId: string, includeGroups: boolean = false, tx?: Prisma.TransactionClient,) {
64+
async findUser(
65+
userId: string,
66+
includeGroups: boolean = false,
67+
tx?: Prisma.TransactionClient,
68+
) {
6569
const client = tx ?? this.prisma;
6670
return await client.user.findUnique({
6771
where: { id: userId },
68-
include: { userGroups: includeGroups }
69-
})
72+
include: { userGroups: includeGroups },
73+
});
7074
}
7175
}
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
import { Injectable } from "@nestjs/common";
2-
import { AppLoggerService } from "@/logging/app-logger.service";
32
import { UserDbService } from "./user-db.service";
43

54
@Injectable()
65
export class UserService {
7-
constructor(
8-
private readonly userDb: UserDbService,
9-
private readonly logger: AppLoggerService,
10-
) {}
6+
constructor(private readonly userDb: UserDbService) {}
117

128
/**
139
* Returns true/false depending no user's system admin status.
1410
* @param userId A user's id
1511
* @returns A boolean value indicating user's system admin status
1612
*/
17-
async isUserAdmin(userId){
13+
async isUserAdmin(userId) {
1814
return await this.userDb.isUserSystemAdmin(userId);
1915
}
2016

2117
async upsertUser(sub: string, email: string) {
2218
return await this.userDb.upsertUser(sub, email);
2319
}
2420

25-
async findUserWithGroups(userId){
26-
return await this.userDb.findUser(userId, true)
21+
async findUserWithGroups(userId) {
22+
return await this.userDb.findUser(userId, true);
2723
}
2824
}

apps/backend-services/src/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ScheduleModule } from "@nestjs/schedule";
55
import { ThrottlerGuard, ThrottlerModule } from "@nestjs/throttler";
66
import { AuditModule } from "@/audit/audit.module";
77
import { AzureModule } from "@/azure/azure.module";
8+
import { ActorModule } from "./actor/actor.module";
89
import { AuthModule } from "./auth/auth.module";
910
import { BenchmarkModule } from "./benchmark/benchmark.module";
1011
import { BlobStorageModule } from "./blob-storage/blob-storage.module";
@@ -21,7 +22,6 @@ import { TemporalModule } from "./temporal/temporal.module";
2122
import { TrainingModule } from "./training/training.module";
2223
import { UploadModule } from "./upload/upload.module";
2324
import { WorkflowModule } from "./workflow/workflow.module";
24-
import { ActorModule } from "./actor/actor.module";
2525

2626
@Module({
2727
imports: [

apps/backend-services/src/auth/api-key-auth.guard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import {
99
} from "@nestjs/common";
1010
import { Reflector } from "@nestjs/core";
1111
import { Request } from "express";
12+
import { ApiKeyService } from "@/actor/api-key.service";
1213
import {
1314
API_KEY_FAILED_WINDOW_MS,
1415
API_KEY_MAX_FAILED_ATTEMPTS,
1516
API_KEY_SWEEP_INTERVAL_MS,
1617
} from "./auth.config";
1718
import { IDENTITY_KEY, IdentityOptions } from "./identity.decorator";
18-
import { ApiKeyService } from "@/actor/api-key.service";
1919

2020
/**
2121
* Tracks failed API key validation attempts per IP within a time window.

apps/backend-services/src/auth/auth.controller.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ import {
3939
setAuthCookies,
4040
} from "./cookie-auth.utils";
4141
import { MeResponseDto, OAuthCallbackQueryDto, RefreshReturnDto } from "./dto";
42+
import { Identity } from "./identity.decorator";
4243
import { Public } from "./public.decorator";
4344
import { User } from "./types";
44-
import { Identity } from "./identity.decorator";
4545

4646
/**
4747
* Thin HTTP layer that exposes the OAuth entrypoints to the frontend.
@@ -280,7 +280,7 @@ export class AuthController {
280280
})
281281
@ApiUnauthorizedResponse({ description: "Not authenticated" })
282282
@ApiForbiddenResponse({ description: "Invalid token" })
283-
@Identity({allowApiKey: false})
283+
@Identity({ allowApiKey: false })
284284
async getMe(@Req() req: Request): Promise<MeResponseDto> {
285285
const user = req.user as User;
286286
const now = Math.floor(Date.now() / 1000);
@@ -303,4 +303,9 @@ export class AuthController {
303303
groups,
304304
};
305305
}
306+
307+
@Get("test")
308+
async test() {
309+
return "hi";
310+
}
306311
}

apps/backend-services/src/auth/auth.module.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Module } from "@nestjs/common";
22
import { ConfigModule } from "@nestjs/config";
33
import { APP_GUARD } from "@nestjs/core";
44
import { PassportModule } from "@nestjs/passport";
5+
import { ActorModule } from "@/actor/actor.module";
6+
import { UserService } from "@/actor/user.service";
57
import { GroupModule } from "../group/group.module";
68
import { ApiKeyAuthGuard } from "./api-key-auth.guard";
79
import { AuthController } from "./auth.controller";
@@ -10,16 +12,13 @@ import { CsrfGuard } from "./csrf.guard";
1012
import { IdentityGuard } from "./identity.guard";
1113
import { JwtAuthGuard } from "./jwt-auth.guard";
1214
import { KeycloakJwtStrategy } from "./keycloak-jwt.strategy";
13-
import { ActorModule } from "@/actor/actor.module";
14-
import { UserService } from "@/actor/user.service";
1515

1616
@Module({
1717
imports: [
1818
ConfigModule,
1919
PassportModule.register({ defaultStrategy: "jwt" }),
2020
ActorModule,
2121
GroupModule,
22-
UserService
2322
],
2423
controllers: [AuthController],
2524
providers: [

apps/backend-services/src/auth/guard-composition.spec.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import { Identity } from "@/auth/identity.decorator";
1313
import { ApiKeyService } from "../actor/api-key.service";
1414
import { UserService } from "../actor/user.service";
1515
import { ApiKeyAuthGuard } from "./api-key-auth.guard";
16+
import { AuthService } from "./auth.service";
1617
import { CsrfGuard } from "./csrf.guard";
1718
import { IdentityGuard } from "./identity.guard";
1819
import { JwtAuthGuard } from "./jwt-auth.guard";
20+
import { KeycloakJwtStrategy } from "./keycloak-jwt.strategy";
1921
import { Public } from "./public.decorator";
2022

2123
/**
@@ -140,24 +142,32 @@ describe("Guard Composition Integration", () => {
140142
};
141143

142144
const mockUserService = {
143-
getUserWithGroups: jest.fn().mockResolvedValue({
145+
findUserWithGroups: jest.fn().mockResolvedValue({
146+
user_id: "user-1",
144147
userGroups: [],
145148
is_system_admin: false,
146149
actor_id: "actor-1",
147150
}),
148151
};
149152

153+
const mockAuthService = {};
154+
155+
const mockKeycloakJwtStrategy = {};
156+
150157
beforeAll(async () => {
151158
const module: TestingModule = await Test.createTestingModule({
152159
controllers: [TestGuardController],
153160
providers: [
154161
Reflector,
162+
// Register guards in the correct order, only once each, using the stub for JwtAuthGuard
155163
{ provide: APP_GUARD, useClass: StubJwtAuthGuard },
156164
{ provide: APP_GUARD, useClass: ApiKeyAuthGuard },
157165
{ provide: APP_GUARD, useClass: IdentityGuard },
158166
{ provide: APP_GUARD, useClass: CsrfGuard },
167+
{ provide: KeycloakJwtStrategy, useValue: mockKeycloakJwtStrategy },
159168
{ provide: ApiKeyService, useValue: mockApiKeyService },
160169
{ provide: UserService, useValue: mockUserService },
170+
{ provide: AuthService, useValue: mockAuthService },
161171
],
162172
}).compile();
163173

@@ -181,7 +191,7 @@ describe("Guard Composition Integration", () => {
181191
},
182192
);
183193

184-
mockUserService.getUserWithGroups.mockResolvedValue({
194+
mockUserService.findUserWithGroups.mockResolvedValue({
185195
userGroups: [],
186196
is_system_admin: false,
187197
actor_id: "actor-1",

0 commit comments

Comments
 (0)