Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aiji42 committed Nov 22, 2021
1 parent 94ba935 commit 8aa8ceb
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 27 deletions.
30 changes: 28 additions & 2 deletions src/__tests__/auth0.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ jest.mock('../handle-fallback', () => ({

fetchMock
.get('/api/auth/me', {
status: 200
status: 200,
body: {
email_verified: true
}
})
.get('/api/auth/failed/me', {
status: 401
})
.get('https://authed.com/api/auth/me', {
status: 200
status: 200,
body: {
email_verified: true
}
})
.get('https://not.authed.com/api/auth/me', {
status: 401
Expand Down Expand Up @@ -59,6 +65,26 @@ describe('makeAuth0Inspector', () => {

expect(handleFallback).not.toBeCalled()
})

test('the domain of api endpoint is specified', async () => {
const req = { headers, nextUrl: { origin: '' } } as unknown as NextRequest
await makeAuth0Inspector(
fallback,
'https://not.authed.com/api/auth/me'
)(req)

expect(handleFallback).toBeCalledWith(fallback, req, undefined)
})

test('logged in and passed custom handler', async () => {
await makeAuth0Inspector(
fallback,
'/api/auth/me',
(res) => !!res.email_verified
)({ headers, nextUrl: { origin: '' } } as unknown as NextRequest)

expect(handleFallback).not.toBeCalled()
})
})

describe('has nextUrl origin', () => {
Expand Down
27 changes: 27 additions & 0 deletions src/__tests__/cognito.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ describe('makeCognitoInspector', () => {
expect(handleFallback).not.toBeCalled()
})

test('has the firebase cookie and passed custom handler', async () => {
;(decodeProtectedHeader as jest.Mock).mockReturnValue({
kid: 'kid1'
})
;(jwtVerify as jest.Mock).mockReturnValue(
new Promise((resolve) =>
resolve({
payload: {
email_verified: true
}
})
)
)
await makeCognitoInspector(
fallback,
'ap-northeast-1',
'xxx',
(res) => !!res.email_verified
)({
cookies: {
'CognitoIdentityServiceProvider.xxx.idToken': 'x.x.x'
}
} as unknown as NextRequest)

expect(handleFallback).not.toBeCalled()
})

test("has the cognito cookie, but it's not valid.", async () => {
;(decodeProtectedHeader as jest.Mock).mockReturnValue({
kid: 'kid1'
Expand Down
122 changes: 97 additions & 25 deletions src/__tests__/firebase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,51 @@ import { handleFallback } from '../handle-fallback'
import { Fallback } from '../types'
import fetchMock from 'fetch-mock'
import { decodeProtectedHeader, jwtVerify } from 'jose'
import { toJwk } from 'js-x509-utils'

jest.mock('jose', () => ({
importJWK: jest.fn(),
decodeProtectedHeader: jest.fn(),
jwtVerify: jest.fn()
}))

fetchMock.get(
'https://www.googleapis.com/service_accounts/v1/jwk/[email protected]',
{
status: 200,
body: {
keys: [
{
kid: 'kid1',
n: 'n2',
kty: 'RSA',
use: 'sig',
e: 'AQAB',
alg: 'RS256'
},
{
kid: 'kid2',
n: 'n2',
kty: 'RSA',
use: 'sig',
e: 'AQAB',
alg: 'RS256'
}
]
jest.mock('js-x509-utils', () => ({
toJwk: jest.fn()
}))

fetchMock
.get(
'https://www.googleapis.com/robot/v1/metadata/x509/[email protected]',
{
status: 200,
body: {
kid1: 'xxxxxxxxxx',
kid2: 'yyyyyyyyyy'
}
}
}
)
)
.get(
'https://www.googleapis.com/identitytoolkit/v3/relyingparty/publicKeys',
{
status: 200,
body: {
kid3: 'zzzzzzzzzz'
}
}
)

jest.mock('../handle-fallback', () => ({
handleFallback: jest.fn()
}))

const fallback: Fallback = { type: 'redirect', destination: '/foo' }

const originalEnv = { ...process.env }

describe('makeFirebaseInspector', () => {
beforeEach(() => {
jest.resetAllMocks()
process.env = originalEnv
})

test('has no cookies', async () => {
Expand All @@ -71,6 +73,53 @@ describe('makeFirebaseInspector', () => {
expect(handleFallback).not.toBeCalled()
})

test('has the firebase cookie by custom key', async () => {
process.env = {
...process.env,
FORTRESS_FIREBASE_COOKIE_KEY: 'session'
}
;(decodeProtectedHeader as jest.Mock).mockReturnValue({
kid: 'kid1'
})
;(jwtVerify as jest.Mock).mockReturnValue(
new Promise((resolve) => resolve(true))
)
await makeFirebaseInspector(fallback)({
cookies: {
session: 'x.x.x'
}
} as unknown as NextRequest)

expect(handleFallback).not.toBeCalled()
})

test('has the firebase cookie and passed custom handler', async () => {
;(decodeProtectedHeader as jest.Mock).mockReturnValue({
kid: 'kid1'
})
;(jwtVerify as jest.Mock).mockReturnValue(
new Promise((resolve) =>
resolve({
payload: {
firebase: {
sign_in_provider: 'google.com'
}
}
})
)
)
await makeFirebaseInspector(
fallback,
(res) => res.firebase.sign_in_provider === 'google.com'
)({
cookies: {
__fortressFirebaseSession: 'x.x.x'
}
} as unknown as NextRequest)

expect(handleFallback).not.toBeCalled()
})

test("has the firebase cookie, but it's not valid.", async () => {
;(decodeProtectedHeader as jest.Mock).mockReturnValue({
kid: 'kid1'
Expand Down Expand Up @@ -116,5 +165,28 @@ describe('makeFirebaseInspector', () => {
},
undefined
)
expect(toJwk).toBeCalledWith(undefined, 'pem')
})

test('session cookie mode', async () => {
process.env = {
...process.env,
FORTRESS_FIREBASE_MODE: 'session'
}
;(decodeProtectedHeader as jest.Mock).mockReturnValue({
kid: 'kid3'
})
;(jwtVerify as jest.Mock).mockReturnValue(
new Promise((resolve) => resolve(true))
)
const token = 'x.y.z'
await makeFirebaseInspector(fallback)({
cookies: {
__fortressFirebaseSession: token
}
} as unknown as NextRequest)

expect(handleFallback).not.toBeCalled()
expect(toJwk).toBeCalledWith('zzzzzzzzzz', 'pem')
})
})

0 comments on commit 8aa8ceb

Please sign in to comment.