-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from lemoncloud-io/feature/louis-add-example-test
Feature/louis add example test
- Loading branch information
Showing
7 changed files
with
177 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |