Skip to content

Commit 071fdd6

Browse files
committed
chore: update tests
1 parent ca0e9c2 commit 071fdd6

6 files changed

Lines changed: 92 additions & 73 deletions

File tree

.cursor/rules/api-rules.mdc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ alwaysApply: false
88
* always run scripts from api/package.json unless requested
99
* prefer adding new files to the nest repo located at api/src/unraid-api/ instead of the legacy code
1010
* Test suite is VITEST, do not use jest
11+
pnpm --filter ./api test
1112
* Prefer to not mock simple dependencies
1213

api/dev/Unraid.net/myservers.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[api]
2-
version="4.8.0"
2+
version="4.4.1"
33
extraOrigins="https://google.com,https://test.com"
44
[local]
55
sandbox="yes"

api/src/unraid-api/auth/api-key.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ describe('ApiKeyService', () => {
185185

186186
await expect(
187187
apiKeyService.create({ name: 'name', description: 'desc', roles: [] })
188-
).rejects.toThrow('At least one role must be specified');
188+
).rejects.toThrow('At least one role or permission must be specified');
189189

190190
await expect(
191191
apiKeyService.create({
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { newEnforcer } from 'casbin';
2+
import { AuthZService } from 'nest-authz';
3+
import { beforeEach, describe, expect, it, vi } from 'vitest';
4+
5+
import { ApiKeyService } from '@app/unraid-api/auth/api-key.service.js';
6+
import { AuthService } from '@app/unraid-api/auth/auth.service.js';
7+
import { CookieService } from '@app/unraid-api/auth/cookie.service.js';
8+
import { ApiKey, ApiKeyWithSecret } from '@app/unraid-api/graph/resolvers/api-key/api-key.model.js';
9+
import { ApiKeyMutationsResolver } from '@app/unraid-api/graph/resolvers/api-key/api-key.mutation.js';
10+
import { Role } from '@app/unraid-api/graph/resolvers/base.model.js';
11+
12+
describe('ApiKeyMutationsResolver', () => {
13+
let resolver: ApiKeyMutationsResolver;
14+
let authService: AuthService;
15+
let apiKeyService: ApiKeyService;
16+
let authzService: AuthZService;
17+
let cookieService: CookieService;
18+
19+
const mockApiKey: ApiKey = {
20+
id: 'test-api-id',
21+
name: 'Test API Key',
22+
description: 'Test API Key Description',
23+
roles: [Role.GUEST],
24+
createdAt: new Date().toISOString(),
25+
permissions: [],
26+
};
27+
28+
const mockApiKeyWithSecret: ApiKeyWithSecret = {
29+
id: 'test-api-id',
30+
key: 'test-api-key',
31+
name: 'Test API Key',
32+
description: 'Test API Key Description',
33+
roles: [Role.GUEST],
34+
createdAt: new Date().toISOString(),
35+
permissions: [],
36+
};
37+
38+
beforeEach(async () => {
39+
vi.resetAllMocks();
40+
41+
const enforcer = await newEnforcer();
42+
43+
apiKeyService = new ApiKeyService();
44+
authzService = new AuthZService(enforcer);
45+
cookieService = new CookieService();
46+
authService = new AuthService(cookieService, apiKeyService, authzService);
47+
resolver = new ApiKeyMutationsResolver(authService, apiKeyService);
48+
});
49+
50+
describe('create', () => {
51+
it('should create new API key and sync roles', async () => {
52+
const input = {
53+
name: 'New API Key',
54+
description: 'New API Key Description',
55+
roles: [Role.GUEST],
56+
permissions: [],
57+
};
58+
59+
vi.spyOn(apiKeyService, 'create').mockResolvedValue(mockApiKeyWithSecret);
60+
vi.spyOn(authService, 'syncApiKeyRoles').mockResolvedValue();
61+
62+
const result = await resolver.create(input as any);
63+
64+
expect(result).toEqual(mockApiKeyWithSecret);
65+
expect(apiKeyService.create).toHaveBeenCalledWith({
66+
name: input.name,
67+
description: input.description,
68+
overwrite: false,
69+
roles: input.roles,
70+
permissions: [],
71+
});
72+
expect(authService.syncApiKeyRoles).toHaveBeenCalledWith(mockApiKey.id, mockApiKey.roles);
73+
});
74+
});
75+
76+
describe('delete', () => {
77+
it('should delete API keys', async () => {
78+
const input = { ids: [mockApiKey.id] };
79+
vi.spyOn(apiKeyService, 'deleteApiKeys').mockResolvedValue();
80+
81+
const result = await resolver.delete(input as any);
82+
83+
expect(result).toBe(true);
84+
expect(apiKeyService.deleteApiKeys).toHaveBeenCalledWith(input.ids);
85+
});
86+
});
87+
});

api/src/unraid-api/graph/resolvers/api-key/api-key.resolver.spec.ts

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -78,65 +78,4 @@ describe('ApiKeyResolver', () => {
7878
expect(apiKeyService.findById).toHaveBeenCalled();
7979
});
8080
});
81-
82-
describe('createApiKey', () => {
83-
it('should create new API key and sync roles', async () => {
84-
const input = {
85-
name: 'New API Key',
86-
description: 'New API Key Description',
87-
roles: [Role.GUEST],
88-
permissions: [],
89-
};
90-
91-
vi.spyOn(apiKeyService, 'create').mockResolvedValue(mockApiKeyWithSecret);
92-
vi.spyOn(authService, 'syncApiKeyRoles').mockResolvedValue();
93-
94-
const result = await resolver.createApiKey(input);
95-
96-
expect(result).toEqual(mockApiKeyWithSecret);
97-
expect(apiKeyService.create).toHaveBeenCalledWith({
98-
name: input.name,
99-
description: input.description,
100-
overwrite: false,
101-
roles: input.roles,
102-
permissions: [],
103-
});
104-
expect(authService.syncApiKeyRoles).toHaveBeenCalledWith(mockApiKey.id, mockApiKey.roles);
105-
});
106-
});
107-
108-
describe('addRoleForApiKey', () => {
109-
it('should add role to API key', async () => {
110-
const input = {
111-
apiKeyId: mockApiKey.id,
112-
role: Role.ADMIN,
113-
};
114-
115-
vi.spyOn(authService, 'addRoleToApiKey').mockResolvedValue(true);
116-
117-
const result = await resolver.addRoleForApiKey(input);
118-
119-
expect(result).toBe(true);
120-
expect(authService.addRoleToApiKey).toHaveBeenCalledWith(input.apiKeyId, Role[input.role]);
121-
});
122-
});
123-
124-
describe('removeRoleFromApiKey', () => {
125-
it('should remove role from API key', async () => {
126-
const input = {
127-
apiKeyId: mockApiKey.id,
128-
role: Role.ADMIN,
129-
};
130-
131-
vi.spyOn(authService, 'removeRoleFromApiKey').mockResolvedValue(true);
132-
133-
const result = await resolver.removeRoleFromApiKey(input);
134-
135-
expect(result).toBe(true);
136-
expect(authService.removeRoleFromApiKey).toHaveBeenCalledWith(
137-
input.apiKeyId,
138-
Role[input.role]
139-
);
140-
});
141-
});
14281
});

api/src/unraid-api/graph/resolvers/api-key/api-key.resolver.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
1+
import { Args, Query, Resolver } from '@nestjs/graphql';
22

33
import { ApiKeyService } from '@app/unraid-api/auth/api-key.service.js';
44
import { AuthService } from '@app/unraid-api/auth/auth.service.js';
@@ -7,16 +7,8 @@ import {
77
AuthPossession,
88
UsePermissions,
99
} from '@app/unraid-api/graph/directives/use-permissions.directive.js';
10-
import {
11-
AddRoleForApiKeyInput,
12-
ApiKey,
13-
ApiKeyWithSecret,
14-
CreateApiKeyInput,
15-
Permission,
16-
RemoveRoleFromApiKeyInput,
17-
} from '@app/unraid-api/graph/resolvers/api-key/api-key.model.js';
10+
import { ApiKey, Permission } from '@app/unraid-api/graph/resolvers/api-key/api-key.model.js';
1811
import { Resource, Role } from '@app/unraid-api/graph/resolvers/base.model.js';
19-
import { validateObject } from '@app/unraid-api/graph/resolvers/validation.utils.js';
2012
import { PrefixedID } from '@app/unraid-api/graph/scalars/graphql-type-prefixed-id.js';
2113

2214
@Resolver(() => ApiKey)

0 commit comments

Comments
 (0)