Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve our approach for testing auth (part 1) #9681

Merged
merged 29 commits into from
Feb 21, 2024
Merged
Changes from 4 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
764582e
improve stackexchange auth testing
jNullj Oct 23, 2023
2e913a7
Merge branch 'master' into feat/9493/improve-auth-testing
jNullj Oct 23, 2023
ae1a231
Merge branch 'badges:master' into feat/9493/improve-auth-testing
jNullj Jan 6, 2024
b2c2a18
Merge branch 'badges:master' into feat/9493/improve-auth-testing
jNullj Jan 6, 2024
9dd597d
Remove dummy auth test
jNullj Jan 16, 2024
7bc3cc0
Add getBadgeExampleCall to test-helpers
jNullj Jan 20, 2024
f6da3af
Use getBadgeExampleCall in stackexchange-base tests
jNullj Jan 20, 2024
31c3f94
Fix getBadgeExampleCall Errors
jNullj Jan 20, 2024
18ec387
Add testAuth to test-helpers
jNullj Jan 20, 2024
1688e58
Refactor stackexchange-base.spec.js to use testAuth from test-helpers
jNullj Jan 20, 2024
609c017
Split stackexchange-base.spec into per service test file
jNullj Jan 20, 2024
ffc7800
Add all auth methods to testAuth
jNullj Feb 10, 2024
3e5c98d
Handle non-default bearer and api headers
jNullj Feb 11, 2024
876708f
Add discord.spec.js as first attempt for bearer auth
jNullj Feb 11, 2024
1ddd577
Merge branch 'badges:master' into feat/9493/improve-auth-testing
jNullj Feb 11, 2024
c41f60f
Fix basic auth user
jNullj Feb 11, 2024
f4cc1af
Add dynamic authorizedOrigins
jNullj Feb 11, 2024
b471c5c
Add header optional argument
jNullj Feb 11, 2024
7aadc10
Add obs as basicAuth example
jNullj Feb 11, 2024
79dc536
Use apiHeaderKey and bearerHeaderKey function params
jNullj Feb 13, 2024
d1435c2
Remove old comment
jNullj Feb 13, 2024
a53f716
Allow any pass & user key for QueryStringAuth
jNullj Feb 16, 2024
14d0789
Add auth test for PepyDownloads
jNullj Feb 16, 2024
d22de8a
Fix wrong header for jwt login
jNullj Feb 16, 2024
50f4144
Support multiple authOrigins in testAuth
jNullj Feb 16, 2024
2d310bd
Add docker-automated auth test
jNullj Feb 16, 2024
1b79b4c
Fix JwtAuth testing by introducing mandatory jwtLoginEndpoint
jNullj Feb 17, 2024
419bd01
Merge branch 'badges:master' into feat/9493/improve-auth-testing
jNullj Feb 17, 2024
a2b838c
Fix type test in generateFakeConfig
jNullj Feb 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 53 additions & 14 deletions services/stackexchange/stackexchange-base.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import Joi from 'joi'
import { expect } from 'chai'
import nock from 'nock'
import { pathParams } from '../index.js'
import { cleanUpNockAfterEach, defaultContext } from '../test-helpers.js'
import { StackExchangeBase } from './stackexchange-base.js'
import StackExchangeMonthlyQuestions from './stackexchange-monthlyquestions.service.js'
import StackExchangeReputation from './stackexchange-reputation.service.js'
import StackExchangeQuestions from './stackexchange-taginfo.service.js'

class DummyStackExchangeService extends StackExchangeBase {
jNullj marked this conversation as resolved.
Show resolved Hide resolved
static route = { base: 'fake-base' }

static openApi = {
'/fake-base': {
get: {
parameters: pathParams({
name: 'fakeparam',
example: 'fakeparam',
}),
},
},
}

async handle() {
const data = await this.fetch({
schema: Joi.any(),
Expand All @@ -16,23 +31,47 @@ class DummyStackExchangeService extends StackExchangeBase {
}
}

describe('StackExchangeBase', function () {
describe('auth', function () {
cleanUpNockAfterEach()
// format is [class, example response from server]
const testClasses = [
[DummyStackExchangeService, { message: 'fake message' }],
[StackExchangeMonthlyQuestions, { total: 8 }],
[StackExchangeReputation, { items: [{ reputation: 8 }] }],
[StackExchangeQuestions, { items: [{ count: 8 }] }],
]

const config = { private: { stackapps_api_key: 'fake-key' } }
for (const [serviceClass, dummyResponse] of testClasses) {
testAuth(serviceClass, dummyResponse)
}

it('sends the auth information as configured', async function () {
const scope = nock('https://api.stackexchange.com')
.get('/2.2/tags/python/info')
.query({ key: 'fake-key' })
.reply(200, { message: 'fake message' })
function testAuth(serviceClass, dummyResponse) {
describe(serviceClass.name, function () {
describe('auth', function () {
jNullj marked this conversation as resolved.
Show resolved Hide resolved
cleanUpNockAfterEach()

expect(
await DummyStackExchangeService.invoke(defaultContext, config, {}),
).to.deep.equal({ message: 'fake message' })
const config = { private: { stackapps_api_key: 'fake-key' } }
const firstOpenapiPath = Object.keys(serviceClass.openApi)[0]
const exampleInvokeParams = serviceClass.openApi[
firstOpenapiPath
].get.parameters.reduce((acc, obj) => {
acc[obj.name] = obj.example
return acc
}, {})
jNullj marked this conversation as resolved.
Show resolved Hide resolved

scope.done()
it('sends the auth information as configured', async function () {
const scope = nock('https://api.stackexchange.com')
.get(/.*/)
.query(queryObject => queryObject.key === 'fake-key')
.reply(200, dummyResponse)
expect(
await serviceClass.invoke(
defaultContext,
config,
exampleInvokeParams,
),
).to.not.have.property('isError')

scope.done()
})
})
})
})
}
Loading