Skip to content

Commit d0696a9

Browse files
committed
fix: update tests
1 parent 6b1ab7b commit d0696a9

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

api/src/__test__/graphql/resolvers/subscription/network.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ test('getUrlForServer - field exists, ssl yes, port empty', () => {
5858
expect(result).toMatchInlineSnapshot('"https://192.168.1.1/"');
5959
});
6060

61-
test('getUrlForServer - field exists, ssl auto', () => {
61+
test('getUrlForServer - field exists, ssl auto', async () => {
6262
const getResult = async () => getUrlForServer({
6363
nginx: { lanIp: '192.168.1.1', sslEnabled: true, sslMode: 'auto', httpPort: 123, httpsPort: 445 } as const as Nginx,
6464
field: 'lanIp',
6565
});
66-
void expect(getResult).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: Cannot get IP Based URL for field: "lanIp" SSL mode auto]`);
66+
await expect(getResult).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: Cannot get IP Based URL for field: "lanIp" SSL mode auto]`);
6767
});
6868

69-
test('getUrlForServer - field does not exist, ssl disabled', () => {
69+
test('getUrlForServer - field does not exist, ssl disabled', async () => {
7070
const getResult = async () => getUrlForServer(
7171
{
7272
nginx: { lanIp: '192.168.1.1', sslEnabled: false, sslMode: 'no' } as const as Nginx,
@@ -76,7 +76,7 @@ test('getUrlForServer - field does not exist, ssl disabled', () => {
7676
// @ts-expect-error Field doesn't exist
7777
field: 'idontexist',
7878
});
79-
void expect(getResult).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: IP URL Resolver: Could not resolve any access URL for field: "idontexist", is FQDN?: false]`);
79+
await expect(getResult).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: IP URL Resolver: Could not resolve any access URL for field: "idontexist", is FQDN?: false]`);
8080
});
8181

8282
test('getUrlForServer - FQDN - field exists, port non-empty', () => {
@@ -104,13 +104,13 @@ test.each([
104104
expect(result.toString()).toBe('https://my-fqdn.unraid.net/');
105105
});
106106

107-
test('getUrlForServer - field does not exist, ssl disabled', () => {
107+
test('getUrlForServer - field does not exist, ssl disabled', async () => {
108108
const getResult = async () => getUrlForServer({ nginx:
109109
{ lanFqdn: 'my-fqdn.unraid.net' } as const as Nginx,
110110
ports: { portSsl: '', port: '', defaultUrl: new URL('https://my-default-url.unraid.net') },
111111
// @ts-expect-error Field doesn't exist
112112
field: 'idontexist' });
113-
void expect(getResult).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: IP URL Resolver: Could not resolve any access URL for field: "idontexist", is FQDN?: false]`);
113+
await expect(getResult).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: IP URL Resolver: Could not resolve any access URL for field: "idontexist", is FQDN?: false]`);
114114
});
115115

116116
test('integration test, loading nginx ini and generating all URLs', async () => {

api/src/__test__/store/modules/config.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { expect, test } from 'vitest';
22

3+
4+
35
import { store } from '@app/store';
46

7+
8+
9+
10+
511
test('Before init returns default values for all fields', async () => {
612
const state = store.getState().config;
713
expect(state).toMatchInlineSnapshot(`
@@ -74,7 +80,7 @@ test('After init returns values from cfg file for all fields', async () => {
7480
dynamicRemoteAccessType: 'DISABLED',
7581
email: 'test@example.com',
7682
idtoken: '',
77-
localApiKey: '',
83+
localApiKey: '426b62b4d51e441fa97a93dfa1259920390a6eb61bd8675db0caa18dd0e414e9',
7884
refreshtoken: '',
7985
regWizTime: '1611175408732_0951-1653-3509-FBA155FA23C0',
8086
upnpEnabled: 'no',
@@ -127,7 +133,7 @@ test('updateUserConfig merges in changes to current state', async () => {
127133
dynamicRemoteAccessType: 'DISABLED',
128134
email: 'test@example.com',
129135
idtoken: '',
130-
localApiKey: '',
136+
localApiKey: '426b62b4d51e441fa97a93dfa1259920390a6eb61bd8675db0caa18dd0e414e9',
131137
refreshtoken: '',
132138
regWizTime: '1611175408732_0951-1653-3509-FBA155FA23C0',
133139
upnpEnabled: 'no',
@@ -141,4 +147,4 @@ test('updateUserConfig merges in changes to current state', async () => {
141147
},
142148
})
143149
);
144-
});
150+
});

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@ import { Logger } from '@nestjs/common';
22
import { readdir, readFile, writeFile } from 'fs/promises';
33
import { join } from 'path';
44

5+
6+
57
import { ensureDir } from 'fs-extra';
68
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
79
import { ZodError } from 'zod';
810

11+
12+
913
import type { ApiKey, ApiKeyWithSecret } from '@app/graphql/generated/api/types';
1014
import { ApiKeySchema, ApiKeyWithSecretSchema } from '@app/graphql/generated/api/operations';
1115
import { Role } from '@app/graphql/generated/api/types';
1216
import { getters } from '@app/store';
1317

18+
19+
1420
import { ApiKeyService } from './api-key.service';
1521

22+
1623
vi.mock('fs/promises', async () => ({
1724
readdir: vi.fn(),
1825
readFile: vi.fn(),
@@ -127,15 +134,14 @@ describe('ApiKeyService', () => {
127134
it('should create ApiKeyWithSecret with generated key', async () => {
128135
const saveSpy = vi.spyOn(apiKeyService, 'saveApiKey').mockResolvedValue();
129136
const { key, id, description, roles } = mockApiKeyWithSecret;
130-
const inputName = 'Test API Key';
131-
const expectedName = 'TEST_API_KEY';
137+
const name = 'Test API Key';
132138

133-
const result = await apiKeyService.create(inputName, description ?? '', roles);
139+
const result = await apiKeyService.create(name, description ?? '', roles);
134140

135141
expect(result).toMatchObject({
136142
id,
137143
key,
138-
name: expectedName,
144+
name: name,
139145
description,
140146
roles,
141147
createdAt: expect.any(String),
@@ -148,7 +154,7 @@ describe('ApiKeyService', () => {
148154
const saveSpy = vi.spyOn(apiKeyService, 'saveApiKey');
149155

150156
await expect(apiKeyService.create('', 'desc', [Role.GUEST])).rejects.toThrow(
151-
'API key name is required'
157+
'API key name must be alphanumeric + spaces'
152158
);
153159

154160
await expect(apiKeyService.create('name', 'desc', [])).rejects.toThrow(
@@ -300,13 +306,11 @@ describe('ApiKeyService', () => {
300306
expect(readFile).toHaveBeenCalledTimes(2);
301307
});
302308

303-
it('should throw authentication error when file read fails', async () => {
309+
it('Should return null if an API key is invalid', async () => {
304310
vi.mocked(readdir).mockResolvedValue(['key1.json'] as any);
305311
vi.mocked(readFile).mockRejectedValue(new Error('Read error'));
306312

307-
await expect(apiKeyService.findByKey(mockApiKeyWithSecret.key)).rejects.toThrow(
308-
'Authentication system error'
309-
);
313+
await expect(apiKeyService.findByKey(mockApiKeyWithSecret.key)).resolves.toBeNull();
310314
});
311315

312316
it('should throw specific error for corrupted JSON', async () => {
@@ -465,4 +469,4 @@ describe('ApiKeyService', () => {
465469
);
466470
});
467471
});
468-
});
472+
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class ApiKeyService implements OnModuleInit {
5555
if (/^[\p{L}\p{N} ]+$/u.test(name)) {
5656
return name;
5757
} else {
58-
throw new GraphQLError('API key name must be alphanumeric and can only contain spaces');
58+
throw new GraphQLError('API key name must be alphanumeric + spaces');
5959
}
6060
}
6161

@@ -189,7 +189,7 @@ export class ApiKeyService implements OnModuleInit {
189189
try {
190190
const files = await readdir(this.basePath);
191191

192-
for (const file of files) {
192+
for (const file of (files ?? [])) {
193193
if (!file.endsWith('.json')) continue;
194194

195195
try {
@@ -233,7 +233,7 @@ export class ApiKeyService implements OnModuleInit {
233233
}
234234

235235
this.logger.error(`Failed to read API key storage: ${error}`);
236-
throw new GraphQLError('Authentication system unavailable');
236+
throw new GraphQLError('Authentication system unavailable - please see logs');
237237
}
238238
}
239239

0 commit comments

Comments
 (0)