From fd5e2fb441084e21301461d7bc6883d06c3fb45d Mon Sep 17 00:00:00 2001 From: louis Date: Fri, 31 May 2024 17:19:18 +0900 Subject: [PATCH 1/2] test: add test codes --- src/core/aws-web.core.spec.ts | 37 +++++++++++++ src/core/azure-web.core.spec.ts | 37 +++++++++++++ src/http/aws-http-request.builder.spec.ts | 53 +++++++++++++++++++ src/token-storage/aws-storage.service.ts | 2 +- src/token-storage/azure-storage.service.ts | 2 +- .../token-storage.service.spec.ts | 47 ++++++++++++++++ 6 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 src/core/aws-web.core.spec.ts create mode 100644 src/core/azure-web.core.spec.ts create mode 100644 src/http/aws-http-request.builder.spec.ts create mode 100644 src/token-storage/token-storage.service.spec.ts diff --git a/src/core/aws-web.core.spec.ts b/src/core/aws-web.core.spec.ts new file mode 100644 index 0000000..36e39b0 --- /dev/null +++ b/src/core/aws-web.core.spec.ts @@ -0,0 +1,37 @@ +import { AWSWebCore } from '../core'; +import { WebCoreConfig } from '../types'; + +describe('AWSWebCore', () => { + let config: WebCoreConfig<'aws'>; + let awsWebCore: AWSWebCore; + + beforeEach(() => { + config = { + project: 'test-project-aws', + cloud: 'aws', + oAuthEndpoint: 'http://localhost/oauth', + }; + awsWebCore = new AWSWebCore(config); + }); + + it('should initialize correctly', async () => { + const state = await awsWebCore.init(); + expect(state).toBe('no-token'); + }); + + it('should build signed request correctly', async () => { + const requestConfig = { method: 'GET', baseURL: 'http://localhost' }; + const builder = awsWebCore.buildSignedRequest(requestConfig); + expect(builder).toBeDefined(); + }); + + it('should get saved token correctly', async () => { + const tokens = await awsWebCore.getSavedToken(); + expect(tokens).toEqual({}); + }); + + it('should check authentication correctly', async () => { + const isAuthenticated = await awsWebCore.isAuthenticated(); + expect(isAuthenticated).toBe(false); + }); +}); diff --git a/src/core/azure-web.core.spec.ts b/src/core/azure-web.core.spec.ts new file mode 100644 index 0000000..22ac4c3 --- /dev/null +++ b/src/core/azure-web.core.spec.ts @@ -0,0 +1,37 @@ +import { AzureWebCore } from '../core'; +import { WebCoreConfig } from '../types'; + +describe('AzureWebCore', () => { + let config: WebCoreConfig<'azure'>; + let azureWebCore: AzureWebCore; + + beforeEach(() => { + config = { + project: 'test-project-azure', + cloud: 'azure', + oAuthEndpoint: 'http://localhost/oauth', + }; + azureWebCore = new AzureWebCore(config); + }); + + it('should initialize correctly', async () => { + const state = await azureWebCore.init(); + expect(state).toBe('no-token'); + }); + + it('should build signed request correctly', async () => { + const requestConfig = { method: 'GET', baseURL: 'http://localhost' }; + const builder = azureWebCore.buildSignedRequest(requestConfig); + expect(builder).toBeDefined(); + }); + + it('should get saved token correctly', async () => { + const tokens = await azureWebCore.getSavedToken(); + expect(tokens).toEqual({}); + }); + + it('should check authentication correctly', async () => { + const isAuthenticated = await azureWebCore.isAuthenticated(); + expect(isAuthenticated).toBe(false); + }); +}); diff --git a/src/http/aws-http-request.builder.spec.ts b/src/http/aws-http-request.builder.spec.ts new file mode 100644 index 0000000..dada12f --- /dev/null +++ b/src/http/aws-http-request.builder.spec.ts @@ -0,0 +1,53 @@ +import axios, { AxiosRequestConfig } from 'axios'; +import { AWSHttpRequestBuilder } from '../http'; +import { AWSStorageService } from '../token-storage'; + +jest.mock('axios', () => { + return { + create: jest.fn(() => axios), + request: jest.fn(() => Promise.resolve()), + }; +}); + +describe('AWSHttpRequestBuilder', () => { + let tokenStorage: AWSStorageService; + let config: AxiosRequestConfig; + + beforeEach(() => { + tokenStorage = new AWSStorageService({ project: 'test', cloud: 'aws', oAuthEndpoint: 'http://localhost' }); + config = { + method: 'GET', + baseURL: 'http://localhost', + }; + }); + + it('should set headers correctly', async () => { + const builder = new AWSHttpRequestBuilder(tokenStorage, config); + builder.setHeaders({ 'Content-Type': 'application/json' }); + expect(builder['config'].headers).toEqual({ 'Content-Type': 'application/json' }); + }); + + it('should set params correctly', async () => { + const builder = new AWSHttpRequestBuilder(tokenStorage, config); + builder.setParams({ page: 1 }); + expect(builder['config'].params).toEqual({ page: 1 }); + }); + + it('should set body correctly', async () => { + const builder = new AWSHttpRequestBuilder(tokenStorage, config); + builder.setBody({ data: 'test' }); + expect(builder['config'].data).toEqual({ data: 'test' }); + }); + + it('should execute request correctly', async () => { + const builder = new AWSHttpRequestBuilder(tokenStorage, config); + const mockedAxios = axios.create as jest.Mock; + mockedAxios.mockResolvedValue({ data: 'response' }); + expect(mockedAxios).toHaveBeenCalledWith(expect.objectContaining(config)); + + const mockedRequest = axios.request as jest.Mock; + mockedRequest.mockResolvedValue({ data: 'response' }); + const response = await builder.execute(); + expect(response.data).toBe('response'); + }); +}); diff --git a/src/token-storage/aws-storage.service.ts b/src/token-storage/aws-storage.service.ts index cd4d32e..7f427a5 100644 --- a/src/token-storage/aws-storage.service.ts +++ b/src/token-storage/aws-storage.service.ts @@ -38,7 +38,7 @@ export class AWSStorageService extends TokenStorageService { const accessKeyId = await this.storage.getItem(`${this.prefix}.accessKeyId`); const secretKey = await this.storage.getItem(`${this.prefix}.secretKey`); - return accessKeyId !== null && secretKey !== null && expiredTime !== null; + return !!accessKeyId && !!secretKey && !!expiredTime; } async shouldRefreshToken(): Promise { diff --git a/src/token-storage/azure-storage.service.ts b/src/token-storage/azure-storage.service.ts index bb9e9ef..984d8f9 100644 --- a/src/token-storage/azure-storage.service.ts +++ b/src/token-storage/azure-storage.service.ts @@ -38,7 +38,7 @@ export class AzureStorageService extends TokenStorageService { const accessToken = await this.storage.getItem(`${this.prefix}.accessToken`); const hostKey = await this.storage.getItem(`${this.prefix}.hostKey`); - return identityToken !== null && accessToken !== null && hostKey !== null && expiredTime !== null; + return !!identityToken && !!accessToken && !!hostKey && !!expiredTime; } /** diff --git a/src/token-storage/token-storage.service.spec.ts b/src/token-storage/token-storage.service.spec.ts new file mode 100644 index 0000000..8f4de31 --- /dev/null +++ b/src/token-storage/token-storage.service.spec.ts @@ -0,0 +1,47 @@ +import { TokenStorageService } from '../token-storage'; +import { WebCoreConfig } from '../types'; + +class MockTokenStorageService extends TokenStorageService { + async getAllItems(): Promise<{ [key: string]: string }> { + return {}; + } + + async hasCachedToken(): Promise { + return false; + } + + async shouldRefreshToken(): Promise { + return false; + } +} + +describe('TokenStorageService', () => { + let config: WebCoreConfig<'aws'>; + let tokenStorage: TokenStorageService; + + beforeEach(() => { + config = { + project: 'test-project', + cloud: 'aws', + oAuthEndpoint: 'http://localhost/oauth', + }; + tokenStorage = new MockTokenStorageService(config); + }); + + it('should set item correctly', async () => { + await tokenStorage.setItem('key', 'value'); + const item = await tokenStorage.getItem('key'); + expect(item).toBe('value'); + }); + + it('should get item correctly', async () => { + await tokenStorage.setItem('key', 'value'); + const item = await tokenStorage.getItem('key'); + expect(item).toBe('value'); + }); + + it('should update prefix correctly', () => { + tokenStorage.updatePrefix('new-prefix'); + expect(tokenStorage['prefix']).toBe('@new-prefix'); + }); +}); From 6a22eb56270e663c1d237b75d4776bf5bb65896c Mon Sep 17 00:00:00 2001 From: louis Date: Fri, 31 May 2024 17:20:50 +0900 Subject: [PATCH 2/2] docs: add documentation link --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8aab19e..546dc89 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ npm install @lemoncloud/lemon-web-core ## Usage The LemonWebCore Library allows you to create and use instances for different cloud providers. Below are the usage examples for WebCoreFactory, AWSWebCore, and AzureWebCore. +Please see [documentation](https://tech.lemoncloud.io/lemon-web-core/) for more information. ### WebCoreFactory