-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23acd60
commit 69d5306
Showing
3 changed files
with
132 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as core from '@actions/core' | ||
import { run } from '../src/main' | ||
import { deploy } from '../src/deploy' | ||
import { installCli } from '../src/install-cli' | ||
import { cleanPrEnv } from '../src/clean-pr-env' | ||
|
||
jest.mock('@actions/core') | ||
jest.mock('../src/deploy') | ||
jest.mock('../src/install-cli') | ||
jest.mock('../src/clean-pr-env') | ||
|
||
let getInputMock: jest.SpiedFunction<typeof core.getInput> | ||
|
||
describe('main', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
getInputMock = jest.spyOn(core, 'getInput').mockImplementation() | ||
}) | ||
|
||
it('should call the deploy function when the action is deploy', async () => { | ||
getInputMock.mockImplementation(name => { | ||
switch (name) { | ||
case 'action': | ||
return 'deploy' | ||
default: | ||
return '' | ||
} | ||
}) | ||
await run() | ||
|
||
expect(installCli).toHaveBeenCalled() | ||
expect(deploy).toHaveBeenCalled() | ||
expect(cleanPrEnv).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should call the cleanPrEnv function when the action is clean-pr-env', async () => { | ||
getInputMock.mockImplementation(name => { | ||
switch (name) { | ||
case 'action': | ||
return 'clean-pr-env' | ||
default: | ||
return '' | ||
} | ||
}) | ||
await run() | ||
|
||
expect(installCli).not.toHaveBeenCalled() | ||
expect(deploy).not.toHaveBeenCalled() | ||
expect(cleanPrEnv).toHaveBeenCalled() | ||
}) | ||
|
||
it('should throw an error if the action is invalid', async () => { | ||
getInputMock.mockImplementation(name => { | ||
switch (name) { | ||
case 'action': | ||
return 'invalid-action' | ||
default: | ||
return '' | ||
} | ||
}) | ||
await expect(run()).rejects.toThrow('Invalid action to perform') | ||
|
||
expect(installCli).not.toHaveBeenCalled() | ||
expect(deploy).not.toHaveBeenCalled() | ||
expect(cleanPrEnv).not.toHaveBeenCalled() | ||
}) | ||
}) |
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,64 @@ | ||
import { getAccessToken } from '../src/utils' | ||
|
||
describe('getAccessToken', () => { | ||
it('should return an access token when the request is successful', async () => { | ||
const mockResponse = { | ||
ok: true, | ||
json: jest.fn().mockResolvedValue({ access_token: 'my-access-token' }) | ||
} | ||
global.fetch = jest.fn().mockResolvedValue(mockResponse) | ||
|
||
const accessToken = await getAccessToken('my-cli-token') | ||
const basicAuth = Buffer.from('platform-cli:', 'latin1').toString('base64') | ||
|
||
expect(accessToken).toBe('my-access-token') | ||
expect(fetch).toHaveBeenCalledWith( | ||
'https://accounts.platform.sh/oauth2/token', | ||
{ | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Basic ${basicAuth}`, | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
grant_type: 'api_token', | ||
api_token: 'my-cli-token' | ||
}) | ||
} | ||
) | ||
}) | ||
|
||
it('should throw an error if the request fails', async () => { | ||
const mockResponse = { | ||
ok: false, | ||
status: 401, | ||
statusText: 'Unauthorized' | ||
} | ||
global.fetch = jest.fn().mockResolvedValue(mockResponse) | ||
|
||
await expect(getAccessToken('my-cli-token')).rejects.toThrow( | ||
'Unable to authenticate: 401' | ||
) | ||
}) | ||
|
||
it('should throw an error if the response does not contain an access token', async () => { | ||
const mockResponse = { | ||
ok: true, | ||
json: jest.fn().mockResolvedValue({}) | ||
} | ||
global.fetch = jest.fn().mockResolvedValue(mockResponse) | ||
|
||
await expect(getAccessToken('my-cli-token')).rejects.toThrow( | ||
'No access token found in the response' | ||
) | ||
}) | ||
|
||
it('should throw an error if the fetch call throws an error', async () => { | ||
const error = new Error('Network error') | ||
global.fetch = jest.fn().mockRejectedValue(error) | ||
|
||
await expect(getAccessToken('my-cli-token')).rejects.toThrow( | ||
'Unable to authenticate: Network error' | ||
) | ||
}) | ||
}) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.