-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathgithub.test.ts
156 lines (139 loc) · 3.83 KB
/
github.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import * as http from 'http'
import {createProxy} from 'proxy'
import {getOctokit} from '../src/github'
import {GitHub, getOctokitOptions} from '../src/utils'
describe('@actions/github', () => {
const proxyUrl = 'http://127.0.0.1:8080'
const originalProxyUrl = process.env['https_proxy']
let proxyConnects: string[]
let proxyServer: http.Server
let first = true
beforeAll(async () => {
// Start proxy server
proxyServer = createProxy()
await new Promise<void>(resolve => {
const port = Number(proxyUrl.split(':')[2])
proxyServer.listen(port, () => resolve())
})
proxyServer.on('connect', req => {
proxyConnects.push(req.url ?? '')
})
})
beforeEach(() => {
delete process.env['https_proxy']
proxyConnects = []
})
afterAll(async () => {
// Stop proxy server
await new Promise<void>(resolve => {
proxyServer.once('close', () => resolve())
proxyServer.close()
})
if (originalProxyUrl) {
process.env['https_proxy'] = originalProxyUrl
}
})
it('basic REST client', async () => {
const token = getToken()
if (!token) {
return
}
const octokit = new GitHub(getOctokitOptions(token))
const branch = await octokit.rest.repos.getBranch({
owner: 'actions',
repo: 'toolkit',
branch: 'main'
})
expect(branch.data.name).toBe('main')
expect(proxyConnects).toHaveLength(0)
})
it('basic getOctokit client', async () => {
const token = getToken()
if (!token) {
return
}
const octokit = getOctokit(token)
const branch = await octokit.rest.repos.getBranch({
owner: 'actions',
repo: 'toolkit',
branch: 'main'
})
expect(branch.data.name).toBe('main')
expect(proxyConnects).toHaveLength(0)
})
it('basic REST client with custom auth', async () => {
const token = getToken()
if (!token) {
return
}
// Valid token
let octokit = new GitHub({auth: `token ${token}`})
const branch = await octokit.rest.repos.getBranch({
owner: 'actions',
repo: 'toolkit',
branch: 'main'
})
expect(branch.data.name).toBe('main')
expect(proxyConnects).toHaveLength(0)
// Invalid token
octokit = new GitHub({auth: `token asdf`})
let failed = false
try {
await octokit.rest.repos.getBranch({
owner: 'actions',
repo: 'toolkit',
branch: 'main'
})
} catch (err) {
failed = true
}
expect(failed).toBeTruthy()
})
it('basic GraphQL client', async () => {
const token = getToken()
if (!token) {
return
}
const octokit = getOctokit(token)
const repository = await octokit.graphql(
'{repository(owner:"actions", name:"toolkit"){name}}'
)
expect(repository).toEqual({repository: {name: 'toolkit'}})
expect(proxyConnects).toHaveLength(0)
})
it('basic GraphQL client with custom auth', async () => {
const token = getToken()
if (!token) {
return
}
// Valid token
let octokit = getOctokit(token)
const repository = await octokit.graphql(
'{repository(owner:"actions", name:"toolkit"){name}}'
)
expect(repository).toEqual({repository: {name: 'toolkit'}})
expect(proxyConnects).toHaveLength(0)
// Invalid token
octokit = new GitHub({auth: `token asdf`})
let failed = false
try {
await octokit.graphql(
'{repository(owner:"actions", name:"toolkit"){name}}'
)
} catch (err) {
failed = true
}
expect(failed).toBeTruthy()
})
function getToken(): string {
const token = process.env['GITHUB_TOKEN'] || ''
if (!token && first) {
/* eslint-disable-next-line no-console */
console.warn(
'Skipping GitHub tests. Set $GITHUB_TOKEN to run REST client and GraphQL client tests'
)
first = false
}
return token
}
})