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 9, 2021
1 parent eca7ae6 commit 28d6b3a
Showing 1 changed file with 51 additions and 20 deletions.
71 changes: 51 additions & 20 deletions src/__tests__/auth0.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ fetchMock
status: 200
})
.get('/api/auth/failed/me', {
status: 403
status: 401
})
.get('https://authed.com/api/auth/me', {
status: 200
})
.get('https://not.authed.com/api/auth/me', {
status: 401
})

const fallback: Fallback = { type: 'redirect', destination: '/foo' }
Expand All @@ -27,30 +33,55 @@ describe('makeAuth0Inspector', () => {
jest.resetAllMocks()
})

test('not logged in', async () => {
await makeAuth0Inspector(
fallback,
'/api/auth/failed/me'
)({ headers } as unknown as NextRequest)
describe('dose not have nextUrl origin', () => {
test('not logged in', async () => {
const req = { headers, nextUrl: { origin: '' } } as unknown as NextRequest
await makeAuth0Inspector(fallback, '/api/auth/failed/me')(req)

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

test('does not have cookie', async () => {
const noCookieReq = {
headers: { get: () => undefined }
} as unknown as NextRequest
await makeAuth0Inspector(fallback, '/api/auth/failed/me')(noCookieReq)
test('does not have cookie', async () => {
const noCookieReq = {
headers: { get: () => undefined },
nextUrl: { origin: '' }
} as unknown as NextRequest
await makeAuth0Inspector(fallback, '/api/auth/failed/me')(noCookieReq)

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

test('logged in', async () => {
await makeAuth0Inspector(
fallback,
'/api/auth/me'
)({ headers, nextUrl: { origin: '' } } as unknown as NextRequest)

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

test('logged in', async () => {
await makeAuth0Inspector(
fallback,
'/api/auth/me'
)({ headers } as unknown as NextRequest)
describe('has nextUrl origin', () => {
test('not logged in', async () => {
const req = {
headers,
nextUrl: { origin: 'https://not.authed.com' }
} as unknown as NextRequest
await makeAuth0Inspector(fallback, '/api/auth/me')(req)

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

test('logged in', async () => {
await makeAuth0Inspector(
fallback,
'/api/auth/me'
)({
headers,
nextUrl: { origin: 'https://authed.com' }
} as unknown as NextRequest)

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

0 comments on commit 28d6b3a

Please sign in to comment.