From 2bef25550d9d29b27315c5857665c331ec57e75a Mon Sep 17 00:00:00 2001 From: Marcus Eide Date: Mon, 24 Jan 2022 10:59:56 +0100 Subject: [PATCH] Add tests Signed-off-by: Marcus Eide --- .../src/lib/oauth/OAuthAdapter.test.ts | 81 +++++++++++++++++++ .../src/lib/oauth/helpers.test.ts | 34 +++++++- 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/plugins/auth-backend/src/lib/oauth/OAuthAdapter.test.ts b/plugins/auth-backend/src/lib/oauth/OAuthAdapter.test.ts index a7463bb7dc..5d5d7e3a87 100644 --- a/plugins/auth-backend/src/lib/oauth/OAuthAdapter.test.ts +++ b/plugins/auth-backend/src/lib/oauth/OAuthAdapter.test.ts @@ -347,4 +347,85 @@ describe('OAuthAdapter', () => { }, }); }); + + it('sets the correct cookie configuration using the base url', async () => { + const config = { + baseUrl: 'http://domain.org/auth', + appUrl: 'http://domain.org', + isOriginAllowed: () => false, + }; + + const oauthProvider = OAuthAdapter.fromConfig( + config, + providerInstance, + oAuthProviderOptions, + ); + + const mockRequest = { + query: { + scope: 'user', + env: 'development', + }, + } as unknown as express.Request; + + const mockResponse = { + cookie: jest.fn().mockReturnThis(), + end: jest.fn().mockReturnThis(), + setHeader: jest.fn().mockReturnThis(), + statusCode: jest.fn().mockReturnThis(), + } as unknown as express.Response; + + await oauthProvider.start(mockRequest, mockResponse); + + expect(mockResponse.cookie).toBeCalledTimes(1); + expect(mockResponse.cookie).toBeCalledWith( + `${oAuthProviderOptions.providerId}-nonce`, + expect.any(String), + expect.objectContaining({ + domain: 'domain.org', + path: '/auth/test-provider', + secure: false, + }), + ); + }); + + it('sets the correct cookie configuration using a callbackUrl', async () => { + const config = { + baseUrl: 'http://domain.org/auth', + appUrl: 'http://domain.org', + isOriginAllowed: () => false, + }; + + const oauthProvider = OAuthAdapter.fromConfig(config, providerInstance, { + ...oAuthProviderOptions, + callbackUrl: 'https://authdomain.org/auth/test-provider/handler/frame', + }); + + const mockRequest = { + query: { + scope: 'user', + env: 'development', + }, + } as unknown as express.Request; + + const mockResponse = { + cookie: jest.fn().mockReturnThis(), + end: jest.fn().mockReturnThis(), + setHeader: jest.fn().mockReturnThis(), + statusCode: jest.fn().mockReturnThis(), + } as unknown as express.Response; + + await oauthProvider.start(mockRequest, mockResponse); + + expect(mockResponse.cookie).toBeCalledTimes(1); + expect(mockResponse.cookie).toBeCalledWith( + `${oAuthProviderOptions.providerId}-nonce`, + expect.any(String), + expect.objectContaining({ + domain: 'authdomain.org', + path: '/auth/test-provider', + secure: true, + }), + ); + }); }); diff --git a/plugins/auth-backend/src/lib/oauth/helpers.test.ts b/plugins/auth-backend/src/lib/oauth/helpers.test.ts index 8af1d2f2ad..9145f73370 100644 --- a/plugins/auth-backend/src/lib/oauth/helpers.test.ts +++ b/plugins/auth-backend/src/lib/oauth/helpers.test.ts @@ -15,7 +15,12 @@ */ import express from 'express'; -import { verifyNonce, encodeState, readState } from './helpers'; +import { + verifyNonce, + encodeState, + readState, + getCookieConfig, +} from './helpers'; describe('OAuthProvider Utils', () => { describe('encodeState', () => { @@ -104,4 +109,31 @@ describe('OAuthProvider Utils', () => { }).not.toThrow(); }); }); + + describe('getCookieConfig', () => { + it('should set the correct domain and path for a base url', () => { + const mockAuthUrl = new URL('http://domain.org/auth'); + expect(getCookieConfig(mockAuthUrl, 'test-provider')).toMatchObject({ + cookieDomain: 'domain.org', + cookiePath: '/auth/test-provider', + }); + }); + + it('should set the correct domain and path for a url containing a frame handler', () => { + const mockAuthUrl = new URL( + 'http://domain.org/auth/test-provider/handler/frame', + ); + expect(getCookieConfig(mockAuthUrl, 'test-provider')).toMatchObject({ + cookieDomain: 'domain.org', + cookiePath: '/auth/test-provider', + }); + }); + + it('should set the secure flag if url is using https', () => { + const mockAuthUrl = new URL('https://domain.org/auth'); + expect(getCookieConfig(mockAuthUrl, 'test-provider')).toMatchObject({ + secure: true, + }); + }); + }); });