-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathlogin-browser-integ-test.js
More file actions
314 lines (279 loc) · 12.7 KB
/
login-browser-integ-test.js
File metadata and controls
314 lines (279 loc) · 12.7 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*global describe, it, expect, beforeAll, afterAll, beforeEach, afterEach, awaitsFor, awaitsForDone, awaits*/
define(function (require, exports, module) {
const SpecRunnerUtils = require("spec/SpecRunnerUtils");
const LoginShared = require("./login-shared");
describe("integration: login/logout browser app tests", function () {
if (Phoenix.isNativeApp) {
// Browser login tests are not applicable for native apps
it("This test disabled in native apps as its browser login tests", function () {
expect(1).toEqual(1);
});
return;
}
let testWindow,
LoginServiceExports,
LoginBrowserExports,
ProDialogsExports,
EntitlementsExports,
entitlementsService,
originalOpen,
originalFetch;
let setupTrialState,
setupExpiredTrial,
verifyProBranding,
verifyProfilePopupContent,
cleanupTrialState,
popupToAppear,
performFullLogoutFlow,
verifyProfileIconBlanked,
VIEW_TRIAL_DAYS_LEFT,
VIEW_PHOENIX_PRO,
VIEW_PHOENIX_FREE,
SIGNIN_POPUP,
PROFILE_POPUP;
beforeAll(async function () {
testWindow = await SpecRunnerUtils.createTestWindowAndRun();
// Wait for test exports to be available (KernalModeTrust is sandboxed, use test exports)
await awaitsFor(
function () {
return testWindow._test_login_service_exports &&
testWindow._test_login_browser_exports &&
testWindow._test_pro_dlg_login_exports &&
testWindow._test_entitlements_exports;
},
"Test exports to be available",
5000
);
// Access the login service exports from the test window
LoginServiceExports = testWindow._test_login_service_exports;
LoginBrowserExports = testWindow._test_login_browser_exports;
ProDialogsExports = testWindow._test_pro_dlg_login_exports;
EntitlementsExports = testWindow._test_entitlements_exports;
entitlementsService = EntitlementsExports.EntitlementsService;
entitlementsService.on(entitlementsService.EVENT_ENTITLEMENTS_CHANGED,
LoginShared.entitlmentsChangedHandler);
// Store original functions for restoration
originalOpen = testWindow.open;
originalFetch = testWindow.fetch;
// Wait for profile menu to be initialized
await awaitsFor(
function () {
return testWindow.$("#user-profile-button").length > 0;
},
"Profile button to be available",
3000
);
LoginShared.setup(testWindow, LoginServiceExports, setupProUserMock, performFullLoginFlow,
EntitlementsExports);
VIEW_TRIAL_DAYS_LEFT = LoginShared.VIEW_TRIAL_DAYS_LEFT;
VIEW_PHOENIX_PRO = LoginShared.VIEW_PHOENIX_PRO;
VIEW_PHOENIX_FREE = LoginShared.VIEW_PHOENIX_FREE;
SIGNIN_POPUP = LoginShared.SIGNIN_POPUP;
PROFILE_POPUP = LoginShared.PROFILE_POPUP;
setupTrialState = LoginShared.setupTrialState;
setupExpiredTrial = LoginShared.setupExpiredTrial;
verifyProBranding = LoginShared.verifyProBranding;
verifyProfilePopupContent = LoginShared.verifyProfilePopupContent;
cleanupTrialState = LoginShared.cleanupTrialState;
popupToAppear = LoginShared.popupToAppear;
performFullLogoutFlow = LoginShared.performFullLogoutFlow;
verifyProfileIconBlanked = LoginShared.verifyProfileIconBlanked;
}, 30000);
afterAll(async function () {
// Restore original functions
entitlementsService.off(entitlementsService.EVENT_ENTITLEMENTS_CHANGED,
LoginShared.entitlmentsChangedHandler);
testWindow.open = originalOpen;
// Restore all fetch function overrides
LoginServiceExports.setFetchFn(originalFetch);
LoginBrowserExports.setFetchFn(originalFetch);
ProDialogsExports.setFetchFn(originalFetch);
testWindow = null;
LoginServiceExports = null;
LoginBrowserExports = null;
ProDialogsExports = null;
originalOpen = null;
originalFetch = null;
await SpecRunnerUtils.closeTestWindow();
}, 30000);
beforeEach(function () {
// Ensure we start each test in a logged-out state
// Note: We can't easily reset login state, so tests should handle this
});
function setupProUserMock(hasActiveSubscription = true, expiredEntitlements = false) {
let userSignedOut = false;
// Set fetch mock on both browser and service exports
const fetchMock = (url, options) => {
console.log("llgT: browser promo test fetchFn called with URL:", url);
if (url.includes('/resolveBrowserSession')) {
if (userSignedOut) {
return Promise.resolve({
ok: false,
status: 401,
json: () => Promise.resolve({ isSuccess: false })
});
}
const response = {
isSuccess: true,
email: "prouser@example.com",
firstName: "Pro",
lastName: "User",
customerID: "test-customer-id",
loginTime: Date.now(),
profileIcon: {
initials: "TU",
color: "#14b8a6"
}
};
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve(response)
});
} else if (url.includes('/getAppEntitlements')) {
// Entitlements endpoint - return user's plan and entitlements
console.log("llgT: Handling getAppEntitlements call");
if (userSignedOut) {
return Promise.resolve({
ok: false,
status: 401,
json: () => Promise.resolve({ isSuccess: false })
});
} else {
const entitlementsResponse = {
isSuccess: true,
lang: "en"
};
if (hasActiveSubscription) {
const validTill = expiredEntitlements ?
Date.now() - 86400000 : // expired yesterday
Date.now() + 30 * 24 * 60 * 60 * 1000; // valid for 30 days
entitlementsResponse.plan = {
isSubscriber: true,
paidSubscriber: true,
name: "Phoenix Pro",
fullName: "Phoenix Pro",
validTill: validTill
};
entitlementsResponse.entitlements = {
liveEdit: {
activated: true,
validTill: validTill
}
};
} else {
entitlementsResponse.plan = {
isSubscriber: false,
paidSubscriber: false,
name: "Free Plan",
fullName: "Free Plan"
};
}
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve(entitlementsResponse)
});
}
} else if (url.includes('/signOut')) {
userSignedOut = true;
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve({ isSuccess: true })
});
} else {
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve({ isSuccess: true })
});
}
};
// Apply fetch mock to both browser exports and login service exports
LoginBrowserExports.setFetchFn(fetchMock);
LoginServiceExports.setFetchFn(fetchMock);
ProDialogsExports.setFetchFn(fetchMock);
}
async function performFullLoginFlow() {
// Mock window.open like the original test
let capturedURL = null;
let capturedTarget = null;
testWindow.open = function(url, target) {
capturedURL = url;
capturedTarget = target;
return {
focus: function() {},
close: function() {},
closed: false
};
};
// Click profile button
const $profileButton = testWindow.$("#user-profile-button");
$profileButton.trigger('click');
await popupToAppear(SIGNIN_POPUP);
// Find and click sign in button
let popupContent = testWindow.$('.profile-popup');
const signInButton = popupContent.find('#phoenix-signin-btn');
signInButton.trigger('click');
// Verify window.open was called
expect(capturedURL).toBeDefined();
expect(capturedURL).toContain('phcode.dev');
expect(capturedTarget).toBe('_blank');
// Wait for browser login waiting dialog
await testWindow.__PR.waitForModalDialog(".browser-login-waiting-dialog");
// Click "Check Now" button to verify login
const checkNowButton = testWindow.$('[data-button-id="check"]');
checkNowButton.trigger('click');
// Wait for login to complete
await awaitsFor(
function () {
return LoginServiceExports.LoginService.isLoggedIn();
},
"User to be logged in",
5000
);
// Wait for login dialog to close
await testWindow.__PR.waitForModalDialogClosed(".modal", "Login waiting dialog");
// Wait for profile icon to update with user data
await awaitsFor(
function () {
const $profileIcon = testWindow.$("#user-profile-button");
const profileIconContent = $profileIcon.html();
return profileIconContent && profileIconContent.includes('TU');
},
"profile icon to update with user initials",
3000
);
}
describe("Browser Login Tests", function () {
beforeEach(async function () {
// Ensure clean state before each test
if (LoginServiceExports.LoginService.isLoggedIn()) {
throw new Error("browser login tests require user to be logged out at start. Please log out before running these tests.");
}
await cleanupTrialState();
});
LoginShared.setupSharedTests();
});
});
});