Skip to content

Commit 5b1c4c9

Browse files
committed
wip tests
1 parent 227aff7 commit 5b1c4c9

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

src/__tests__/request-builder.spec.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
import { buildUrlHeaders } from '../utils'
2-
1+
// import { buildUrlHeaders } from '../utils'
2+
import * as utils from '../utils'
33

44
describe('request builder test', () => {
5-
it('not clean CF_', async () => { //todo: change tests
6-
const
7-
{ url, headers } = await buildUrlHeaders({
5+
beforeEach(() => {
6+
jest.spyOn(utils, 'getRuntimeIngressHost').mockImplementation(async (runtimeName: string, headers: Record<string, string>) => { return 'fjfjfj'})
7+
})
8+
it('support CF_HOST', async () => {
9+
const { url, headers } = await utils.buildUrlHeaders({
810
'CF_API_KEY': 'the-token',
911
'CF_HOST': 'https://g.codefresh.io',
10-
// 'CF_RUNTIME_NAME': 'codefresh-hosted',
12+
'CF_IMAGE': 'testImage'
13+
})
14+
expect(url).toEqual('https://g.codefresh.io/app-proxy/api/image-report?CF_IMAGE=testImage')
15+
expect(headers).toEqual({ 'authorization': 'the-token' })
16+
})
17+
18+
it('support CF_RUNTIME_NAME', async () => {
19+
const { url, headers } = await utils.buildUrlHeaders({
20+
'CF_API_KEY': 'the-token',
21+
'CF_RUNTIME_NAME': 'runtime',
1122
'CF_IMAGE': 'testImage'
1223
})
1324
expect(url).toEqual('https://g.codefresh.io/app-proxy/api/image-report?CF_IMAGE=testImage')

src/__tests__/validate.spec.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,48 @@ import { validate } from '../validate'
44
describe('client report-image validation', () => {
55
it('Must have', async () => {
66
try {
7-
validate({ ENV1: '1', someVal: 'ignored', CF_RUNTIME_NAME: 'eti', CF_HOST: 'fff' })
7+
validate({ ENV1: '1', someVal: 'ignored' })
88
} catch (error) {
9-
console.log(error)
109
const expectedErrorMsg = 'Validation Error: ["CF_API_KEY must be provided as environment variable.","CF_IMAGE must be provided as environment variable.","CF_RUNTIME_NAME must be provided as environment variable."]'
11-
// expect(error.message).toBe(expectedErrorMsg)
10+
expect(error.message).toBe(expectedErrorMsg)
1211
return
1312
}
1413
fail(`should have thrown Validation Error`)
1514
})
16-
it('All OK', async () => { //todo: change tests
15+
it('not support both CF_HOST and CF_RUNTIME_NAME', async () => {
16+
try {
17+
validate({ CF_API_KEY: '1', alsoIgnored: 'ignored', IGNORED: 'ignored too', CF_IMAGE: 'testImage', CF_HOST: `host.io`, CF_RUNTIME_NAME: 'runtime' })
18+
} catch (error) {
19+
const expectedErrorMsg = 'Validation Error: ["You can only specify CF_RUNTIME_NAME or CF_HOST. please delete one of them."]'
20+
expect(error.message).toBe(expectedErrorMsg)
21+
return
22+
}
23+
fail(`should have thrown Validation Error`)
24+
})
25+
it('support CF_HOST', async () => {
1726
let res
1827
try {
1928
res = validate({ CF_API_KEY: '1', alsoIgnored: 'ignored', IGNORED: 'ignored too', CF_IMAGE: 'testImage', CF_HOST: `host.io` })
2029
} catch (error) {
2130
fail(`should have not thrown an error ${JSON.stringify(error)}`)
2231
}
23-
// expect(res).toEqual({
24-
// 'CF_API_KEY': '1',
25-
// 'CF_HOST': 'host.io',
26-
// 'CF_IMAGE': 'testImage'
27-
// })
32+
expect(res).toEqual({
33+
'CF_API_KEY': '1',
34+
'CF_HOST': 'host.io',
35+
'CF_IMAGE': 'testImage'
36+
})
37+
})
38+
it('support CF_RUNTIME_NAME', async () => {
39+
let res
40+
try {
41+
res = validate({ CF_API_KEY: '1', alsoIgnored: 'ignored', IGNORED: 'ignored too', CF_IMAGE: 'testImage', CF_RUNTIME_NAME: `runtime` })
42+
} catch (error) {
43+
fail(`should have not thrown an error ${JSON.stringify(error)}`)
44+
}
45+
expect(res).toEqual({
46+
'CF_API_KEY': '1',
47+
'CF_RUNTIME_NAME': 'runtime',
48+
'CF_IMAGE': 'testImage'
49+
})
2850
})
2951
})

src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as utils from './utils'
12
import { GraphQLClient, gql } from 'graphql-request'
23
import { get } from 'lodash'
34
export const tryParseJson = (str: string) => {
@@ -36,7 +37,7 @@ export const tryParseJson = (str: string) => {
3637
}
3738

3839

39-
async function getRuntimeIngressHost(runtimeName: string, headers: Record<string, string>, platformHost = 'https://g.codefresh.io'): Promise<string> {
40+
export async function getRuntimeIngressHost(runtimeName: string, headers: Record<string, string>, platformHost = 'https://g.codefresh.io'): Promise<string> {
4041
const graphQLClient = new GraphQLClient(`${platformHost}/2.0/api/graphql`, {
4142
headers
4243
})

0 commit comments

Comments
 (0)