Spaces:
Sleeping
Sleeping
File size: 1,727 Bytes
a5d1c1b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { authService } from '../src/services/auth-service.js';
import { GLOBAL_AUTH_FILE } from '../src/config/constants.js';
describe('AuthService', () => {
afterEach(() => {
vi.restoreAllMocks();
if (fs.existsSync(GLOBAL_AUTH_FILE)) {
try {
fs.unlinkSync(GLOBAL_AUTH_FILE);
} catch {}
}
});
it('validates a GitHub token via GitHub API mock', async () => {
vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce({
ok: true,
status: 200,
headers: new Headers({
'x-oauth-scopes': 'repo, read:user',
}),
json: async () => ({ login: 'octocat' }),
} as unknown as Response);
const result = await authService.validateToken('mock_token_123');
expect(result.username).toBe('octocat');
expect(result.scopes).toEqual(['repo', 'read:user']);
});
it('determines collaborator repo permission level', async () => {
vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ permission: 'admin' }),
} as unknown as Response);
const perm = await authService.getRepoPermission('acme', 'project', 'octocat', 'mock_token');
expect(perm).toBe('admin');
});
it('saves and clears global auth profiles', () => {
authService.saveGlobalToken('test_pat_token', 'johndoe', 'pat');
const token = authService.resolveToken();
expect(token).toBe('test_pat_token');
authService.clearGlobalToken();
const tokenAfterClear = authService.resolveToken();
expect(tokenAfterClear).toBeNull();
});
});
|