Skip to content

Commit

Permalink
add main and utils test
Browse files Browse the repository at this point in the history
  • Loading branch information
zeshanziya committed Jun 6, 2024
1 parent 23acd60 commit 69d5306
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
67 changes: 67 additions & 0 deletions __tests__/main.test.ts
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()
})
})
64 changes: 64 additions & 0 deletions __tests__/utils.test.ts
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'
)
})
})
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 69d5306

Please sign in to comment.