Skip to content

Commit

Permalink
Merge pull request #15 from lemoncloud-io/feature/louis-add-example-test
Browse files Browse the repository at this point in the history
Feature/louis add example test
  • Loading branch information
louis-lemon authored May 31, 2024
2 parents f77f185 + 6a22eb5 commit 34ff11c
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 37 additions & 0 deletions src/core/aws-web.core.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
37 changes: 37 additions & 0 deletions src/core/azure-web.core.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
53 changes: 53 additions & 0 deletions src/http/aws-http-request.builder.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
2 changes: 1 addition & 1 deletion src/token-storage/aws-storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
Expand Down
2 changes: 1 addition & 1 deletion src/token-storage/azure-storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
47 changes: 47 additions & 0 deletions src/token-storage/token-storage.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
return false;
}

async shouldRefreshToken(): Promise<boolean> {
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');
});
});

0 comments on commit 34ff11c

Please sign in to comment.