From 28d6b3a83a1feb9a1246f0b469987b95123c40b7 Mon Sep 17 00:00:00 2001 From: Aiji Uejima Date: Tue, 9 Nov 2021 10:48:22 +0900 Subject: [PATCH] test: update tests --- src/__tests__/auth0.spec.ts | 71 ++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/src/__tests__/auth0.spec.ts b/src/__tests__/auth0.spec.ts index c2ef0e6..39a70ec 100644 --- a/src/__tests__/auth0.spec.ts +++ b/src/__tests__/auth0.spec.ts @@ -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' } @@ -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() + }) }) })