|
| 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 | +}); |
0 commit comments