|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-present, Parse, LLC |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the license found in the LICENSE file in |
| 6 | + * the root directory of this source tree. |
| 7 | + */ |
| 8 | +jest.dontMock('../../../Parse-Dashboard/Authentication.js'); |
| 9 | +jest.dontMock('../../../Parse-Dashboard/app.js'); |
| 10 | + |
| 11 | +const express = require('express'); |
| 12 | +const session = require('express-session'); |
| 13 | +const Authentication = require('../../../Parse-Dashboard/Authentication'); |
| 14 | + |
| 15 | +describe('SessionStore Integration', () => { |
| 16 | + it('uses default in-memory store when cookieSessionStore is not provided', () => { |
| 17 | + const app = express(); |
| 18 | + const users = [{ user: 'test', pass: 'password' }]; |
| 19 | + const auth = new Authentication(users, false, '/'); |
| 20 | + |
| 21 | + // Mock app.use to capture session configuration |
| 22 | + const useSpy = jest.fn(); |
| 23 | + app.use = useSpy; |
| 24 | + |
| 25 | + auth.initialize(app, {}); |
| 26 | + |
| 27 | + // Find the call that sets up express-session |
| 28 | + const sessionCall = useSpy.mock.calls.find(call => |
| 29 | + call[0] && call[0].name === 'session' |
| 30 | + ); |
| 31 | + |
| 32 | + expect(sessionCall).toBeDefined(); |
| 33 | + // When no store is provided, express-session uses MemoryStore by default |
| 34 | + // The session function should be called without a custom store |
| 35 | + }); |
| 36 | + |
| 37 | + it('uses custom session store when cookieSessionStore is provided', () => { |
| 38 | + const app = express(); |
| 39 | + const users = [{ user: 'test', pass: 'password' }]; |
| 40 | + const auth = new Authentication(users, false, '/'); |
| 41 | + |
| 42 | + // Create a mock session store that implements the Store interface |
| 43 | + const Store = session.Store; |
| 44 | + class MockStore extends Store { |
| 45 | + constructor() { |
| 46 | + super(); |
| 47 | + } |
| 48 | + get(sid, callback) { |
| 49 | + callback(null, {}); |
| 50 | + } |
| 51 | + set(sid, session, callback) { |
| 52 | + callback(null); |
| 53 | + } |
| 54 | + destroy(sid, callback) { |
| 55 | + callback(null); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + const mockStore = new MockStore(); |
| 60 | + |
| 61 | + // Mock app.use to capture session configuration |
| 62 | + const useSpy = jest.fn(); |
| 63 | + app.use = useSpy; |
| 64 | + |
| 65 | + auth.initialize(app, { cookieSessionStore: mockStore }); |
| 66 | + |
| 67 | + // The session middleware should have been configured |
| 68 | + expect(useSpy).toHaveBeenCalled(); |
| 69 | + |
| 70 | + // Find the call that sets up express-session |
| 71 | + const sessionCall = useSpy.mock.calls.find(call => |
| 72 | + call[0] && call[0].name === 'session' |
| 73 | + ); |
| 74 | + |
| 75 | + expect(sessionCall).toBeDefined(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('passes cookieSessionStore through app.js to Authentication', () => { |
| 79 | + const parseDashboard = require('../../../Parse-Dashboard/app.js'); |
| 80 | + |
| 81 | + // Create a mock session store that implements the Store interface |
| 82 | + const Store = session.Store; |
| 83 | + class MockStore extends Store { |
| 84 | + constructor() { |
| 85 | + super(); |
| 86 | + } |
| 87 | + get(sid, callback) { |
| 88 | + callback(null, {}); |
| 89 | + } |
| 90 | + set(sid, session, callback) { |
| 91 | + callback(null); |
| 92 | + } |
| 93 | + destroy(sid, callback) { |
| 94 | + callback(null); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + const mockStore = new MockStore(); |
| 99 | + |
| 100 | + const config = { |
| 101 | + apps: [ |
| 102 | + { |
| 103 | + serverURL: 'http://localhost:1337/parse', |
| 104 | + appId: 'testAppId', |
| 105 | + masterKey: 'testMasterKey', |
| 106 | + appName: 'TestApp', |
| 107 | + }, |
| 108 | + ], |
| 109 | + users: [ |
| 110 | + { |
| 111 | + user: 'testuser', |
| 112 | + pass: 'testpass', |
| 113 | + }, |
| 114 | + ], |
| 115 | + }; |
| 116 | + |
| 117 | + const options = { |
| 118 | + cookieSessionStore: mockStore, |
| 119 | + cookieSessionSecret: 'test-secret', |
| 120 | + }; |
| 121 | + |
| 122 | + // Create dashboard app |
| 123 | + const dashboardApp = parseDashboard(config, options); |
| 124 | + |
| 125 | + // The app should be created successfully with the session store |
| 126 | + expect(dashboardApp).toBeDefined(); |
| 127 | + expect(typeof dashboardApp).toBe('function'); // Express app is a function |
| 128 | + }); |
| 129 | + |
| 130 | + it('maintains backward compatibility without cookieSessionStore option', () => { |
| 131 | + const parseDashboard = require('../../../Parse-Dashboard/app.js'); |
| 132 | + |
| 133 | + const config = { |
| 134 | + apps: [ |
| 135 | + { |
| 136 | + serverURL: 'http://localhost:1337/parse', |
| 137 | + appId: 'testAppId', |
| 138 | + masterKey: 'testMasterKey', |
| 139 | + appName: 'TestApp', |
| 140 | + }, |
| 141 | + ], |
| 142 | + users: [ |
| 143 | + { |
| 144 | + user: 'testuser', |
| 145 | + pass: 'testpass', |
| 146 | + }, |
| 147 | + ], |
| 148 | + }; |
| 149 | + |
| 150 | + const options = { |
| 151 | + cookieSessionSecret: 'test-secret', |
| 152 | + }; |
| 153 | + |
| 154 | + // Create dashboard app without cookieSessionStore option |
| 155 | + const dashboardApp = parseDashboard(config, options); |
| 156 | + |
| 157 | + // The app should be created successfully even without session store |
| 158 | + expect(dashboardApp).toBeDefined(); |
| 159 | + expect(typeof dashboardApp).toBe('function'); |
| 160 | + }); |
| 161 | +}); |
0 commit comments