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