Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
29d31e9
Add OAuth2 token management and storage implementation
anatolyshipitz Jul 30, 2025
25a6345
Refactor OAuth2TokenManager and remove unused types
anatolyshipitz Jul 31, 2025
6e28da6
Add OAuth2Error class and integrate into token management
anatolyshipitz Jul 31, 2025
2c5cc80
Enhance OAuth2TokenManager tests for token retrieval and error handling
anatolyshipitz Jul 31, 2025
e871c4e
Refactor OAuth2TokenManager and enhance error handling
anatolyshipitz Jul 31, 2025
d4d0f2e
Merge branch 'main' into feature/65031_oauth_service
anatolyshipitz Aug 1, 2025
60c0b1c
Enhance OAuth2TokenManager with improved error handling and constants
anatolyshipitz Aug 1, 2025
74ac3c3
Enhance OAuth2TokenManager with token validation and error handling i…
anatolyshipitz Aug 1, 2025
e356bd6
Enhance OAuth2TokenManager with additional token validation checks
anatolyshipitz Aug 1, 2025
449abb4
Refactor OAuth2 token management interfaces and consolidate types
anatolyshipitz Aug 1, 2025
7092850
Refactor OAuth2 token loading to support asynchronous operations
anatolyshipitz Aug 1, 2025
cf78db5
Enhance OAuth2Error class with error code functionality
anatolyshipitz Aug 1, 2025
a939883
Enhance setTokenDataForTesting method in OAuth2TokenManager
anatolyshipitz Aug 1, 2025
6379c43
Add unit tests for FileTokenStorage and OAuth2TokenRefreshProvider
anatolyshipitz Aug 1, 2025
8881bea
Add unit tests for OAuth2TokenManager and OAuth2TokenRefreshProvider
anatolyshipitz Aug 1, 2025
f6845a9
Add edge case unit tests for OAuth2TokenManager
anatolyshipitz Aug 1, 2025
3abdcc5
Add unit tests for token refresh handling in OAuth2TokenManager
anatolyshipitz Aug 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions workers/main/src/common/errors/OAuth2Error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, it } from 'vitest';

import { OAuth2Error } from './OAuth2Error';

describe('OAuth2Error', () => {
it('should set the message, name, and default error code', () => {
const err = new OAuth2Error('test message');

expect(err.message).toBe('test message');
expect(err.name).toBe('OAuth2Error');
expect(err.code).toBe('UNKNOWN_OAUTH2_ERROR');
});

it('should set the message, name, and custom error code', () => {
const err = new OAuth2Error('test message', 'CUSTOM_ERROR');

expect(err.message).toBe('test message');
expect(err.name).toBe('OAuth2Error');
expect(err.code).toBe('CUSTOM_ERROR');
});
});
10 changes: 10 additions & 0 deletions workers/main/src/common/errors/OAuth2Error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AppError } from './AppError';

export class OAuth2Error extends AppError {
public readonly code: string;

constructor(message: string, code: string = 'UNKNOWN_OAUTH2_ERROR') {
super(message, 'OAuth2Error');
this.code = code;
}
}
1 change: 1 addition & 0 deletions workers/main/src/common/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './AppError';
export * from './FileUtilsError';
export * from './FinAppRepositoryError';
export * from './OAuth2Error';
export * from './QuickBooksRepositoryError';
export * from './SlackRepositoryError';
export * from './TargetUnitRepositoryError';
99 changes: 99 additions & 0 deletions workers/main/src/services/OAuth2/FileTokenStorage.basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { promises as fs } from 'fs';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { OAuth2Error } from '../../common/errors';
import { FileTokenStorage } from './FileTokenStorage';
import { TokenData } from './types';

vi.mock('fs', () => ({
promises: {
mkdir: vi.fn(),
writeFile: vi.fn(),
readFile: vi.fn(),
unlink: vi.fn(),
},
}));

describe('FileTokenStorage - Basic', () => {
let fileTokenStorage: FileTokenStorage;
let mockMkdir: ReturnType<typeof vi.fn>;
let mockWriteFile: ReturnType<typeof vi.fn>;

const mockTokenData: TokenData = {
access_token: 'test-access-token',
refresh_token: 'test-refresh-token',
expires_at: Date.now() + 3600000,
token_type: 'Bearer',
};

beforeEach(() => {
mockMkdir = vi.mocked(fs.mkdir);
mockWriteFile = vi.mocked(fs.writeFile);

mockMkdir.mockResolvedValue(undefined);
mockWriteFile.mockResolvedValue(undefined);

fileTokenStorage = new FileTokenStorage('test-service');
});

afterEach(() => {
vi.clearAllMocks();
});

describe('constructor', () => {
it('should create instance with default service name', () => {
const storage = new FileTokenStorage();

expect(storage).toBeInstanceOf(FileTokenStorage);
});

it('should create instance with custom service name', () => {
const storage = new FileTokenStorage('custom-service');

expect(storage).toBeInstanceOf(FileTokenStorage);
});

it('should create instance with custom token file path', () => {
const customPath = '/custom/path/token.json';
const storage = new FileTokenStorage('test-service', customPath);

expect(storage).toBeInstanceOf(FileTokenStorage);
});
});

describe('save', () => {
it('should save token data successfully', async () => {
await fileTokenStorage.save(mockTokenData);

expect(mockMkdir).toHaveBeenCalledWith(expect.any(String), {
recursive: true,
});
expect(mockWriteFile).toHaveBeenCalledWith(
expect.any(String),
JSON.stringify(mockTokenData, null, 2),
);
});

it('should throw OAuth2Error when save fails', async () => {
mockWriteFile.mockRejectedValue(new Error('Write failed'));

await expect(fileTokenStorage.save(mockTokenData)).rejects.toThrow(
OAuth2Error,
);
await expect(fileTokenStorage.save(mockTokenData)).rejects.toThrow(
'Failed to save token data to file',
);
});

it('should throw OAuth2Error when mkdir fails', async () => {
mockMkdir.mockRejectedValue(new Error('Mkdir failed'));

await expect(fileTokenStorage.save(mockTokenData)).rejects.toThrow(
OAuth2Error,
);
await expect(fileTokenStorage.save(mockTokenData)).rejects.toThrow(
'Failed to save token data to file',
);
});
});
});
65 changes: 65 additions & 0 deletions workers/main/src/services/OAuth2/FileTokenStorage.clear.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { promises as fs } from 'fs';
import { join } from 'path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { OAuth2Error } from '../../common/errors';
import { FileTokenStorage } from './FileTokenStorage';

vi.mock('fs', () => ({
promises: {
mkdir: vi.fn(),
writeFile: vi.fn(),
readFile: vi.fn(),
unlink: vi.fn(),
},
}));

vi.mock('path', () => ({
join: vi.fn(),
}));

describe('FileTokenStorage - Clear', () => {
let fileTokenStorage: FileTokenStorage;
let mockJoin: ReturnType<typeof vi.fn>;
let mockUnlink: ReturnType<typeof vi.fn>;

beforeEach(() => {
mockJoin = vi.mocked(join);
mockUnlink = vi.mocked(fs.unlink);

mockJoin.mockReturnValue('/test/path/token.json');
mockUnlink.mockResolvedValue(undefined);

fileTokenStorage = new FileTokenStorage('test-service');
});

afterEach(() => {
vi.clearAllMocks();
});

describe('clear', () => {
it('should clear token data successfully', async () => {
await fileTokenStorage.clear();

expect(mockUnlink).toHaveBeenCalledWith('/test/path/token.json');
});

it('should not throw error when file does not exist', async () => {
const error = new Error('File not found') as NodeJS.ErrnoException;

error.code = 'ENOENT';
mockUnlink.mockRejectedValue(error);

await expect(fileTokenStorage.clear()).resolves.toBeUndefined();
});

it('should throw OAuth2Error when clear fails with other error', async () => {
mockUnlink.mockRejectedValue(new Error('Delete failed'));

await expect(fileTokenStorage.clear()).rejects.toThrow(OAuth2Error);
await expect(fileTokenStorage.clear()).rejects.toThrow(
'Failed to clear token data from file',
);
});
});
});
117 changes: 117 additions & 0 deletions workers/main/src/services/OAuth2/FileTokenStorage.load.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { promises as fs } from 'fs';
import { join } from 'path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { FileTokenStorage } from './FileTokenStorage';
import { TokenData } from './types';

vi.mock('fs', () => ({
promises: {
mkdir: vi.fn(),
writeFile: vi.fn(),
readFile: vi.fn(),
unlink: vi.fn(),
},
}));

vi.mock('path', () => ({
join: vi.fn(),
}));

describe('FileTokenStorage - Load', () => {
let fileTokenStorage: FileTokenStorage;
let mockJoin: ReturnType<typeof vi.fn>;
let mockReadFile: ReturnType<typeof vi.fn>;

const mockTokenData: TokenData = {
access_token: 'test-access-token',
refresh_token: 'test-refresh-token',
expires_at: Date.now() + 3600000,
token_type: 'Bearer',
};

beforeEach(() => {
mockJoin = vi.mocked(join);
mockReadFile = vi.mocked(fs.readFile);

mockJoin.mockReturnValue('/test/path/token.json');
mockReadFile.mockResolvedValue(JSON.stringify(mockTokenData));

fileTokenStorage = new FileTokenStorage('test-service');
});

afterEach(() => {
vi.clearAllMocks();
});

describe('load', () => {
it('should load valid token data successfully', async () => {
const result = await fileTokenStorage.load();

expect(mockReadFile).toHaveBeenCalledWith(
'/test/path/token.json',
'utf8',
);
expect(result).toEqual(mockTokenData);
});

it('should return null when file does not exist', async () => {
const error = new Error('File not found') as NodeJS.ErrnoException;

error.code = 'ENOENT';
mockReadFile.mockRejectedValue(error);

const result = await fileTokenStorage.load();

expect(result).toBeNull();
});

it('should return null when file read fails with other error', async () => {
mockReadFile.mockRejectedValue(new Error('Read failed'));

const result = await fileTokenStorage.load();

expect(result).toBeNull();
});

it('should return null when token data is invalid', async () => {
const invalidData = { invalid: 'data' };

mockReadFile.mockResolvedValue(JSON.stringify(invalidData));

const result = await fileTokenStorage.load();

expect(result).toBeNull();
});

it('should return null when token data has missing fields', async () => {
const invalidData = {
refresh_token: 'test-refresh-token',
expires_at: Date.now() + 3600000,
token_type: 'Bearer',
};

mockReadFile.mockResolvedValue(JSON.stringify(invalidData));

const result = await fileTokenStorage.load();

expect(result).toBeNull();
});

it('should return null when token data is null', async () => {
mockReadFile.mockResolvedValue('null');

const result = await fileTokenStorage.load();

expect(result).toBeNull();
});

it('should return null when token data is not an object', async () => {
mockReadFile.mockResolvedValue('"string"');

const result = await fileTokenStorage.load();

expect(result).toBeNull();
});
});
});
72 changes: 72 additions & 0 deletions workers/main/src/services/OAuth2/FileTokenStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { promises as fs } from 'fs';
import { join } from 'path';

import { OAuth2Error } from '../../common/errors';
import { TokenStorageProvider } from './types';
import { TokenData } from './types';

export class FileTokenStorage implements TokenStorageProvider {
private readonly tokenFilePath: string;
private readonly serviceName: string;

constructor(serviceName: string = 'qbo', tokenFilePath?: string) {
this.serviceName = serviceName;
this.tokenFilePath =
tokenFilePath ||
join(process.cwd(), 'data', 'oauth2_tokens', `${serviceName}.json`);
}

async save(tokenData: TokenData): Promise<void> {
try {
const dir = join(this.tokenFilePath, '..');

await fs.mkdir(dir, { recursive: true });
await fs.writeFile(
this.tokenFilePath,
JSON.stringify(tokenData, null, 2),
);
} catch {
throw new OAuth2Error('Failed to save token data to file');
}
}

async load(): Promise<TokenData | null> {
try {
const data = await fs.readFile(this.tokenFilePath, 'utf8');
const tokenData = JSON.parse(data) as TokenData;

if (!this.isValidTokenData(tokenData)) {
return null;
}

return tokenData;
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
return null;
}

return null;
}
}

async clear(): Promise<void> {
try {
await fs.unlink(this.tokenFilePath);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
throw new OAuth2Error('Failed to clear token data from file');
}
}
}

private isValidTokenData(data: unknown): data is TokenData {
return (
typeof data === 'object' &&
data !== null &&
typeof (data as TokenData).access_token === 'string' &&
typeof (data as TokenData).refresh_token === 'string' &&
typeof (data as TokenData).expires_at === 'number' &&
typeof (data as TokenData).token_type === 'string'
);
}
}
Loading