mirror of
https://github.com/badges/shields.git
synced 2026-11-21 16:06:35 +03:00
* feat(github): store token scopes Assisted-by: Codex <gpt-5.5> * perf(github): index token scopes Assisted-by: Codex <gpt-5.5> * feat(github): persist accepted token scopes Assisted-by: Codex <gpt-5.5> * docs(github): clarify OAuth scope capture Assisted-by: Codex <gpt-5.5> * docs(github): shorten OAuth scope note Assisted-by: Codex <gpt-5.5> * docs(github): clarify default OAuth scopes Assisted-by: Codex <gpt-5.5> * docs(github): link OAuth scope discussion Assisted-by: Codex <gpt-5.5> * docs(github): mark OAuth scopes as TODO Assisted-by: Codex <gpt-5.5> * fix(token-pool): refresh existing token data Assisted-by: Codex <gpt-5.5> * fix(token-pool): avoid duplicate revalidated tokens Assisted-by: Codex:gpt-5.5 * fix(token-pool): remove revalidated tokens from priority queue Assisted-by: Codex:gpt-5.5 * fix(token-pool): preserve token metadata on re-add Assisted-by: Codex:gpt-5.5 * chore(github): defer token scopes index Assisted-by: Codex:gpt-5.5 * fix(github): trim accepted token scopes Assisted-by: Codex:gpt-5.5 * chore(github): keep token scopes index Assisted-by: Codex:gpt-5.5 * docs(token-pool): document metadata update semantics Assisted-by: Codex:gpt-5.5 * chore(github): store token scopes without index Assisted-by: Codex:gpt-5.5 * chore: rerun ci * test(token-pool): use mixed scope persistence fixture Assisted-by: Codex:gpt-5.5 * fix(token-pool): replace invalid tokens on re-add Keep stale queue references invalid so they can be removed lazily without rebuilding the priority queue. Assisted-by: Codex:gpt-5.6-sol * chore: apply Prettier 3.9 formatting Assisted-by: Codex:gpt-5.6-sol * chore: retry CI Assisted-by: Codex:gpt-5.6-sol --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
141 lines
4.1 KiB
JavaScript
141 lines
4.1 KiB
JavaScript
import { expect } from 'chai'
|
|
import Camp from '@shields_io/camp'
|
|
import sinon from 'sinon'
|
|
import portfinder from 'portfinder'
|
|
import qs from 'qs'
|
|
import nock from 'nock'
|
|
import '../../../core/register-chai-plugins.spec.js'
|
|
import got from '../../../core/got-test-client.js'
|
|
import GithubConstellation from '../github-constellation.js'
|
|
import { setRoutes } from './acceptor.js'
|
|
|
|
const fakeClientId = 'githubdabomb'
|
|
const fakeClientSecret = 'foobar'
|
|
|
|
describe('Github token acceptor', function () {
|
|
const oauthHelper = GithubConstellation._createOauthHelper({
|
|
private: { gh_client_id: fakeClientId, gh_client_secret: fakeClientSecret },
|
|
})
|
|
|
|
let port, baseUrl
|
|
beforeEach(async function () {
|
|
port = await portfinder.getPortPromise()
|
|
baseUrl = `http://127.0.0.1:${port}`
|
|
})
|
|
|
|
let camp
|
|
beforeEach(async function () {
|
|
camp = Camp.start({ port, hostname: '::' })
|
|
await new Promise(resolve => camp.on('listening', () => resolve()))
|
|
})
|
|
afterEach(async function () {
|
|
if (camp) {
|
|
await new Promise(resolve => camp.close(resolve))
|
|
camp = undefined
|
|
}
|
|
})
|
|
|
|
let onTokenAccepted
|
|
beforeEach(function () {
|
|
onTokenAccepted = sinon.stub()
|
|
setRoutes({
|
|
server: camp,
|
|
authHelper: oauthHelper,
|
|
onTokenAccepted,
|
|
})
|
|
})
|
|
|
|
it('should start the OAuth process', async function () {
|
|
const res = await got(`${baseUrl}/github-auth`, { followRedirect: false })
|
|
|
|
expect(res.statusCode).to.equal(302)
|
|
|
|
const queryString = qs.stringify({
|
|
client_id: fakeClientId,
|
|
redirect_uri: 'https://img.shields.io/github-auth/done',
|
|
})
|
|
const expectedLocationHeader = `https://github.com/login/oauth/authorize?${queryString}`
|
|
expect(res.headers.location).to.equal(expectedLocationHeader)
|
|
})
|
|
|
|
describe('Finishing the OAuth process', function () {
|
|
context('no code is provided', function () {
|
|
it('should return an error', async function () {
|
|
const res = await got(`${baseUrl}/github-auth/done`)
|
|
expect(res.body).to.equal(
|
|
'GitHub OAuth authentication failed to provide a code.',
|
|
)
|
|
})
|
|
})
|
|
|
|
const fakeCode = '123456789'
|
|
const fakeAccessToken = 'abcdef'
|
|
|
|
context('a code is provided', function () {
|
|
let scope
|
|
let tokenResponse
|
|
beforeEach(function () {
|
|
nock.enableNetConnect(/127\.0\.0\.1/)
|
|
tokenResponse = { access_token: fakeAccessToken }
|
|
|
|
scope = nock('https://github.com')
|
|
.post('/login/oauth/access_token')
|
|
.reply((url, requestBody) => {
|
|
const parsedBody = qs.parse(requestBody)
|
|
expect(parsedBody.client_id).to.equal(fakeClientId)
|
|
expect(parsedBody.client_secret).to.equal(fakeClientSecret)
|
|
expect(parsedBody.code).to.equal(fakeCode)
|
|
return [200, qs.stringify(tokenResponse)]
|
|
})
|
|
})
|
|
|
|
afterEach(function () {
|
|
// Make sure other tests will make live requests even when this test
|
|
// fails.
|
|
nock.enableNetConnect()
|
|
})
|
|
|
|
afterEach(function () {
|
|
if (scope) {
|
|
scope.done()
|
|
scope = null
|
|
}
|
|
})
|
|
|
|
afterEach(function () {
|
|
nock.cleanAll()
|
|
})
|
|
|
|
it('should finish the OAuth process', async function () {
|
|
const form = new FormData()
|
|
form.append('code', fakeCode)
|
|
|
|
const res = await got.post(`${baseUrl}/github-auth/done`, {
|
|
body: form,
|
|
})
|
|
expect(
|
|
res.body.startsWith(
|
|
'<p>Shields.io has received your app-specific GitHub user token.',
|
|
),
|
|
).to.be.true
|
|
expect(onTokenAccepted).to.have.been.calledWith(fakeAccessToken)
|
|
})
|
|
|
|
it('should pass token scopes from the OAuth response', async function () {
|
|
tokenResponse.scope = 'read:packages, read:user'
|
|
|
|
const form = new FormData()
|
|
form.append('code', fakeCode)
|
|
|
|
await got.post(`${baseUrl}/github-auth/done`, {
|
|
body: form,
|
|
})
|
|
|
|
expect(onTokenAccepted).to.have.been.calledWith(fakeAccessToken, {
|
|
scopes: ['read:packages', 'read:user'],
|
|
})
|
|
})
|
|
})
|
|
})
|
|
})
|