-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add to api GET /user/payment, add AuthorizationTestContext to e…
…ncapsulate magic.link test bypass (#1769) * start user-payment * add GET /user/payment * encapsulate magic.link bypass hack
- Loading branch information
Showing
8 changed files
with
143 additions
and
6 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
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
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
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,25 @@ | ||
export const createMagicTestTokenBypass = ( | ||
requiredVariableName, | ||
requiredTokenValue | ||
) => { | ||
return { | ||
requiredVariableName, | ||
requiredTokenValue, | ||
defaults: { | ||
issuer: 'test-magic-issuer' | ||
}, | ||
summary: [ | ||
'This is a bypass for testing our APIs even though most of them require a valid magic token.', | ||
'When testing, we\'ll use a special-use token.', | ||
'And our token-validating middleware will allow that token,', | ||
`but *only* when env.${requiredVariableName} is truthy.` | ||
].join(' ') | ||
} | ||
} | ||
|
||
export const defaultBypassMagicLinkVariableName = 'DANGEROUSLY_BYPASS_MAGIC_AUTH' | ||
|
||
export const magicLinkBypass = createMagicTestTokenBypass( | ||
defaultBypassMagicLinkVariableName, | ||
'test-magic' | ||
) |
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
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,30 @@ | ||
import * as magic from '../../src/magic.link.js' | ||
|
||
const symbol = Symbol.for('AuthorizationTestContext') | ||
|
||
export class AuthorizationTestContext { | ||
static install (testContext, constructorArgs = []) { | ||
const authzContext = new AuthorizationTestContext(...constructorArgs) | ||
testContext[symbol] = authzContext | ||
} | ||
|
||
static use (testContext) { | ||
if (!(symbol in testContext)) { | ||
throw new Error('cant use AuthorizationTestContext because it hasnt been installed yet') | ||
} | ||
return testContext[symbol] | ||
} | ||
|
||
constructor ( | ||
bypass = magic.magicLinkBypass | ||
) { | ||
this.bypass = bypass | ||
} | ||
|
||
/** | ||
* Create a bearer token that can be used by tests that require one to test something behind basic is-user checks | ||
*/ | ||
createUserToken () { | ||
return this.bypass.requiredTokenValue | ||
} | ||
} |
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
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,51 @@ | ||
/* eslint-env mocha */ | ||
import assert from 'assert' | ||
import fetch, { Request } from '@web-std/fetch' | ||
import { endpoint } from './scripts/constants.js' | ||
import { AuthorizationTestContext } from './contexts/authorization.js' | ||
|
||
function createBearerAuthorization (bearerToken) { | ||
return `Bearer ${bearerToken}` | ||
} | ||
|
||
function createUserPaymentRequest (arg) { | ||
const { path, baseUrl, authorization } = { | ||
authorization: undefined, | ||
path: '/user/payment', | ||
baseUrl: endpoint, | ||
accept: 'application/json', | ||
method: 'get', | ||
...arg | ||
} | ||
return new Request( | ||
new URL(path, baseUrl), | ||
{ | ||
headers: { | ||
accept: 'application/json', | ||
authorization | ||
} | ||
} | ||
) | ||
} | ||
|
||
describe('GET /user/payment', () => { | ||
it('error if no auth header', async () => { | ||
const res = await fetch(createUserPaymentRequest()) | ||
assert(!res.ok) | ||
}) | ||
it('error if bad auth header', async () => { | ||
const createRandomString = () => Math.random().toString().slice(2) | ||
const authorization = createBearerAuthorization(createRandomString()) | ||
const res = await fetch(createUserPaymentRequest({ authorization })) | ||
assert(!res.ok) | ||
}) | ||
it('retrieves user account data', async function () { | ||
const token = AuthorizationTestContext.use(this).createUserToken() | ||
const authorization = createBearerAuthorization(token) | ||
const res = await fetch(createUserPaymentRequest({ authorization })) | ||
assert(res.ok) | ||
const userPaymentSettings = await res.json() | ||
assert.equal(typeof userPaymentSettings, 'object') | ||
assert.ok(!userPaymentSettings.method, 'userPaymentSettings.method is falsy') | ||
}) | ||
}) |