-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve our approach for testing auth (part 1) (#9681)
* 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
Showing
9 changed files
with
413 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/' }, | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
services/stackexchange/stackexchange-monthlyquestions.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.