Skip to content

Commit

Permalink
Improve our approach for testing auth (part 1) (#9681)
Browse files Browse the repository at this point in the history
* improve stackexchange auth testing

Change auth tests to include all shields of the base class.
The code is formated to be used in more general cases and increases code reuseability.

* Remove dummy auth test

We already test all existing classes, no need for a dummy

* Add getBadgeExampleCall to test-helpers

Add getBadgeExampleCall to extract the first OpenAPI example then reformat it for service invoke function.

* Use getBadgeExampleCall in stackexchange-base tests

* Fix getBadgeExampleCall Errors

* Add testAuth to test-helpers

Add the testAuth function which tests auth of a service (badge) using a provided dummy response.

* Refactor stackexchange-base.spec.js to use testAuth from test-helpers

* Split stackexchange-base.spec into per service test file

* Add all auth methods to testAuth

Add all auth methods used to testAuth to be generic and used by all services.
Add helper functions to make testAuth more readable

* Handle non-default bearer and api headers

* Add discord.spec.js as first attempt for bearer auth

* Fix basic auth user

* Add dynamic authorizedOrigins

* Add header optional argument

* Add obs as basicAuth example

* Use apiHeaderKey and bearerHeaderKey function params

Use apiHeaderKey & bearerHeaderKey as function params rather then extracting them with regex from function strings.

Those options are now part of an options object param joined with the contentType that replaces header.

header was originaly added for setting content type of the reply, so it makes more sense to directly set the content type

* Remove old comment

* Allow any pass & user key for QueryStringAuth

Before this commit the QueryStringAuth would only work for the key of stackexchange.
This commit makes the testAuth function generic and allows passing user and pass keys.

* Add auth test for PepyDownloads

* Fix wrong header for jwt login

Might set wrong header for jwt login request.
This commit fixes that.

* Support multiple authOrigins in testAuth

Some services might have more then one authOrigin.
This commit makes sure we test for redundent authOrigins as well as support requests to them if needed.

* Add docker-automated auth test

* Fix JwtAuth testing by introducing mandatory jwtLoginEndpoint

Prior to this change, JwtAuth testing would lead to erros due to the absence of a specified login endpoint,
Nock would be dumplicated for both login and non login hosts and indicate a missing request.

This commit enforces the requirement for a new jwtLoginEndpoint argument when testing JwtAuth.
The argument seperates the endpoint nock scope from the behavior of the request nock.

* Fix type test in generateFakeConfig
  • Loading branch information
jNullj authored Feb 21, 2024
1 parent 503764e commit 8ab9dfa
Show file tree
Hide file tree
Showing 9 changed files with 413 additions and 71 deletions.
41 changes: 9 additions & 32 deletions services/discord/discord.spec.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
import { expect } from 'chai'
import nock from 'nock'
import { cleanUpNockAfterEach, defaultContext } from '../test-helpers.js'
import { testAuth } from '../test-helpers.js'
import Discord from './discord.service.js'

describe('Discord', function () {
cleanUpNockAfterEach()

it('sends the auth information as configured', async function () {
const pass = 'password'
const config = {
private: {
discord_bot_token: pass,
},
}

const scope = nock('https://discord.com', {
// This ensures that the expected credential is actually being sent with the HTTP request.
// Without this the request wouldn't match and the test would fail.
reqheaders: { Authorization: 'Bot password' },
describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
Discord,
'BearerAuthHeader',
{ presence_count: 125 },
{ bearerHeaderKey: 'Bot' },
)
})
.get('/api/v6/guilds/12345/widget.json')
.reply(200, {
presence_count: 125,
})

expect(
await Discord.invoke(defaultContext, config, {
serverId: '12345',
}),
).to.deep.equal({
message: '125 online',
color: 'brightgreen',
})

scope.done()
})
})
15 changes: 15 additions & 0 deletions services/docker/docker-automated.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { testAuth } from '../test-helpers.js'
import DockerAutomatedBuild from './docker-automated.service.js'

describe('DockerAutomatedBuild', function () {
describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
DockerAutomatedBuild,
'JwtAuth',
{ is_automated: true },
{ jwtLoginEndpoint: 'https://hub.docker.com/v2/users/login/' },
)
})
})
})
16 changes: 16 additions & 0 deletions services/obs/obs.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { testAuth } from '../test-helpers.js'
import ObsService from './obs.service.js'

describe('ObsService', function () {
describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
ObsService,
'BasicAuth',
`<?xml version="1.0" encoding="UTF-8"?>
<status package="example" code="passed"></status>`,
{ contentType: 'application/xml' },
)
})
})
})
10 changes: 10 additions & 0 deletions services/pepy/pepy-downloads.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { testAuth } from '../test-helpers.js'
import PepyDownloads from './pepy-downloads.service.js'

describe('PepyDownloads', function () {
describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(PepyDownloads, 'ApiKeyHeader', { total_downloads: 42 })
})
})
})
38 changes: 0 additions & 38 deletions services/stackexchange/stackexchange-base.spec.js

This file was deleted.

17 changes: 17 additions & 0 deletions services/stackexchange/stackexchange-monthlyquestions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { testAuth } from '../test-helpers.js'
import StackExchangeMonthlyQuestions from './stackexchange-monthlyquestions.service.js'

describe('StackExchangeMonthlyQuestions', function () {
describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
StackExchangeMonthlyQuestions,
'QueryStringAuth',
{
total: 8,
},
{ queryPassKey: 'key' },
)
})
})
})
17 changes: 17 additions & 0 deletions services/stackexchange/stackexchange-reputation.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { testAuth } from '../test-helpers.js'
import StackExchangeReputation from './stackexchange-reputation.service.js'

describe('StackExchangeReputation', function () {
describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
StackExchangeReputation,
'QueryStringAuth',
{
items: [{ reputation: 8 }],
},
{ queryPassKey: 'key' },
)
})
})
})
17 changes: 17 additions & 0 deletions services/stackexchange/stackexchange-taginfo.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { testAuth } from '../test-helpers.js'
import StackExchangeQuestions from './stackexchange-taginfo.service.js'

describe('StackExchangeQuestions', function () {
describe('auth', function () {
it('sends the auth information as configured', async function () {
return testAuth(
StackExchangeQuestions,
'QueryStringAuth',
{
items: [{ count: 8 }],
},
{ queryPassKey: 'key' },
)
})
})
})
Loading

0 comments on commit 8ab9dfa

Please sign in to comment.