2020-01-18 19:28:37 +00:00
|
|
|
import * as http from 'http'
|
2023-09-15 13:54:28 +00:00
|
|
|
import { proxy } from 'proxy'
|
2020-06-03 01:39:46 +00:00
|
|
|
import {getOctokit} from '../src/github'
|
|
|
|
import {GitHub, getOctokitOptions} from '../src/utils'
|
2020-01-18 19:28:37 +00:00
|
|
|
|
|
|
|
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
|
2023-09-15 13:54:28 +00:00
|
|
|
proxyServer = proxy()
|
2020-01-18 19:28:37 +00:00
|
|
|
await new Promise(resolve => {
|
|
|
|
const port = Number(proxyUrl.split(':')[2])
|
2021-05-04 20:20:38 +00:00
|
|
|
proxyServer.listen(port, () => resolve(null))
|
2020-01-18 19:28:37 +00:00
|
|
|
})
|
|
|
|
proxyServer.on('connect', req => {
|
2022-12-14 00:28:46 +00:00
|
|
|
proxyConnects.push(req.url ?? '')
|
2020-01-18 19:28:37 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
delete process.env['https_proxy']
|
|
|
|
proxyConnects = []
|
|
|
|
})
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
// Stop proxy server
|
|
|
|
await new Promise(resolve => {
|
2021-05-04 20:20:38 +00:00
|
|
|
proxyServer.once('close', () => resolve(null))
|
2020-01-18 19:28:37 +00:00
|
|
|
proxyServer.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
if (originalProxyUrl) {
|
|
|
|
process.env['https_proxy'] = originalProxyUrl
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('basic REST client', async () => {
|
|
|
|
const token = getToken()
|
|
|
|
if (!token) {
|
|
|
|
return
|
|
|
|
}
|
2020-06-03 01:39:46 +00:00
|
|
|
const octokit = new GitHub(getOctokitOptions(token))
|
2021-05-04 20:20:38 +00:00
|
|
|
const branch = await octokit.rest.repos.getBranch({
|
2020-06-03 01:39:46 +00:00
|
|
|
owner: 'actions',
|
|
|
|
repo: 'toolkit',
|
2020-07-21 15:33:05 +00:00
|
|
|
branch: 'main'
|
2020-06-03 01:39:46 +00:00
|
|
|
})
|
2020-07-21 15:33:05 +00:00
|
|
|
expect(branch.data.name).toBe('main')
|
2020-06-03 01:39:46 +00:00
|
|
|
expect(proxyConnects).toHaveLength(0)
|
|
|
|
})
|
2020-01-18 19:28:37 +00:00
|
|
|
|
2020-06-03 01:39:46 +00:00
|
|
|
it('basic getOctokit client', async () => {
|
|
|
|
const token = getToken()
|
|
|
|
if (!token) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const octokit = getOctokit(token)
|
2021-05-04 20:20:38 +00:00
|
|
|
const branch = await octokit.rest.repos.getBranch({
|
2020-01-18 19:28:37 +00:00
|
|
|
owner: 'actions',
|
|
|
|
repo: 'toolkit',
|
2020-07-21 15:33:05 +00:00
|
|
|
branch: 'main'
|
2020-01-18 19:28:37 +00:00
|
|
|
})
|
2020-07-21 15:33:05 +00:00
|
|
|
expect(branch.data.name).toBe('main')
|
2020-01-18 19:28:37 +00:00
|
|
|
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}`})
|
2021-05-04 20:20:38 +00:00
|
|
|
const branch = await octokit.rest.repos.getBranch({
|
2020-01-18 19:28:37 +00:00
|
|
|
owner: 'actions',
|
|
|
|
repo: 'toolkit',
|
2020-07-21 15:33:05 +00:00
|
|
|
branch: 'main'
|
2020-01-18 19:28:37 +00:00
|
|
|
})
|
2020-07-21 15:33:05 +00:00
|
|
|
expect(branch.data.name).toBe('main')
|
2020-01-18 19:28:37 +00:00
|
|
|
expect(proxyConnects).toHaveLength(0)
|
|
|
|
|
|
|
|
// Invalid token
|
|
|
|
octokit = new GitHub({auth: `token asdf`})
|
|
|
|
let failed = false
|
|
|
|
try {
|
2021-05-04 20:20:38 +00:00
|
|
|
await octokit.rest.repos.getBranch({
|
2020-01-18 19:28:37 +00:00
|
|
|
owner: 'actions',
|
|
|
|
repo: 'toolkit',
|
2020-07-21 15:33:05 +00:00
|
|
|
branch: 'main'
|
2020-01-18 19:28:37 +00:00
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
failed = true
|
|
|
|
}
|
|
|
|
expect(failed).toBeTruthy()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('basic GraphQL client', async () => {
|
|
|
|
const token = getToken()
|
|
|
|
if (!token) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-03 01:39:46 +00:00
|
|
|
const octokit = getOctokit(token)
|
2020-01-18 19:28:37 +00:00
|
|
|
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
|
2020-06-03 01:39:46 +00:00
|
|
|
let octokit = getOctokit(token)
|
2020-01-18 19:28:37 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|