Skip to content

Commit 138e1a9

Browse files
committed
fix tests
1 parent ffe5ffc commit 138e1a9

File tree

4 files changed

+42
-42
lines changed

4 files changed

+42
-42
lines changed

frontend/src/index.test.ts

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { expect, fixture } from "@open-wc/testing";
22
import { html } from "lit";
33
import { restore, stub } from "sinon";
44

5+
import { APIController } from "./controllers/api";
56
import { NavigateController } from "./controllers/navigate";
67
import { NotifyController } from "./controllers/notify";
78
import { type AppSettings } from "./utils/app";
@@ -46,6 +47,7 @@ const mockAppSettings: AppSettings = {
4647
salesEmail: "",
4748
supportEmail: "",
4849
localesEnabled: ["en", "es"],
50+
numBrowsersPerInstance: 1,
4951
};
5052

5153
describe("browsertrix-app", () => {
@@ -55,6 +57,7 @@ describe("browsertrix-app", () => {
5557
window.localStorage.clear();
5658
stub(window.history, "pushState");
5759
stub(NotifyController.prototype, "toast");
60+
stub(APIController.prototype, "fetch");
5861
});
5962

6063
afterEach(() => {
@@ -84,15 +87,12 @@ describe("browsertrix-app", () => {
8487

8588
it("renders 404 when not in org", async () => {
8689
stub(AuthService.prototype, "initSessionStorage").returns(
87-
Promise.resolve({
88-
headers: { Authorization: "_fake_headers_" },
89-
tokenExpiresAt: 0,
90-
username: "[email protected]",
91-
}),
90+
Promise.resolve(mockAuth),
9291
);
9392
// @ts-expect-error checkFreshness is private
9493
stub(AuthService.prototype, "checkFreshness");
9594

95+
AppStateService.updateAuth(mockAuth);
9696
AppStateService.updateUser(
9797
formatAPIUser({
9898
...mockAPIUser,
@@ -116,15 +116,12 @@ describe("browsertrix-app", () => {
116116
};
117117

118118
stub(AuthService.prototype, "initSessionStorage").returns(
119-
Promise.resolve({
120-
headers: { Authorization: "_fake_headers_" },
121-
tokenExpiresAt: 0,
122-
username: "[email protected]",
123-
}),
119+
Promise.resolve(mockAuth),
124120
);
125121
// @ts-expect-error checkFreshness is private
126122
stub(AuthService.prototype, "checkFreshness");
127123

124+
AppStateService.updateAuth(mockAuth);
128125
AppStateService.updateUser(
129126
formatAPIUser({
130127
...mockAPIUser,
@@ -158,27 +155,6 @@ describe("browsertrix-app", () => {
158155
expect(el.shadowRoot?.querySelector("btrix-log-in")).to.exist;
159156
});
160157

161-
// TODO move tests to AuthService
162-
it("sets auth state from session storage", async () => {
163-
stub(AuthService.prototype, "startFreshnessCheck").callsFake(() => {});
164-
stub(window.sessionStorage, "getItem").callsFake((key) => {
165-
if (key === "btrix.auth")
166-
return JSON.stringify({
167-
headers: { Authorization: "_fake_headers_" },
168-
tokenExpiresAt: 0,
169-
username: "[email protected]",
170-
});
171-
return null;
172-
});
173-
const el = await fixture<App>("<browsertrix-app></browsertrix-app>");
174-
175-
expect(el.authService.authState).to.eql({
176-
headers: { Authorization: "_fake_headers_" },
177-
tokenExpiresAt: 0,
178-
username: "[email protected]",
179-
});
180-
});
181-
182158
it("sets user info", async () => {
183159
stub(App.prototype, "getUserInfo").callsFake(async () =>
184160
Promise.resolve(mockAPIUser),
@@ -227,6 +203,7 @@ describe("browsertrix-app", () => {
227203
slug: id,
228204
role: 10,
229205
};
206+
AppStateService.updateAuth(mockAuth);
230207
AppStateService.updateUser(
231208
formatAPIUser({
232209
...mockAPIUser,
@@ -236,11 +213,7 @@ describe("browsertrix-app", () => {
236213
stub(App.prototype, "getLocationPathname").callsFake(() => `/orgs/${id}`);
237214
stub(AuthService.prototype, "startFreshnessCheck").callsFake(() => {});
238215
stub(AuthService.prototype, "initSessionStorage").callsFake(async () =>
239-
Promise.resolve({
240-
headers: { Authorization: "_fake_headers_" },
241-
tokenExpiresAt: 0,
242-
username: "[email protected]",
243-
}),
216+
Promise.resolve(mockAuth),
244217
);
245218

246219
const el = await fixture<App>("<browsertrix-app></browsertrix-app>");

frontend/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export class App extends BtrixElement {
141141
} catch (e) {
142142
console.debug(e);
143143
}
144+
144145
this.syncViewState();
145146

146147
if (authState && !this.userInfo) {

frontend/src/utils/AuthService.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import { restore, stub } from "sinon";
44
import AuthService from "./AuthService";
55
import { AppStateService } from "./state";
66

7+
import { APIController } from "@/controllers/api";
8+
79
describe("AuthService", () => {
810
beforeEach(() => {
911
AppStateService.resetAll();
1012
window.sessionStorage.clear();
1113
window.sessionStorage.clear();
1214
stub(window.history, "pushState");
15+
stub(APIController.prototype, "fetch");
16+
stub(AuthService.prototype, "refresh");
1317
});
1418

1519
afterEach(() => {
@@ -59,6 +63,27 @@ describe("AuthService", () => {
5963
});
6064
otherTabChannel.close();
6165
});
66+
it("saves auth in session storage", async () => {
67+
stub(window.sessionStorage, "getItem");
68+
const otherTabChannel = new BroadcastChannel(AuthService.storageKey);
69+
otherTabChannel.addEventListener("message", () => {
70+
otherTabChannel.postMessage({
71+
name: "responding_auth",
72+
auth: {
73+
headers: { Authorization: "_fake_headers_from_tab_to_save_" },
74+
tokenExpiresAt: 9999,
75+
username: "[email protected]_from_tab_to_save_",
76+
},
77+
});
78+
});
79+
await authService.initSessionStorage();
80+
expect(authService.authState).to.deep.equal({
81+
headers: { Authorization: "_fake_headers_from_tab_to_save_" },
82+
tokenExpiresAt: 9999,
83+
username: "[email protected]_from_tab_to_save_",
84+
});
85+
otherTabChannel.close();
86+
});
6287
it("resolves without stored auth or another tab", async () => {
6388
stub(window.sessionStorage, "getItem");
6489
const result = await authService.initSessionStorage();

frontend/src/utils/AuthService.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,16 @@ export default class AuthService {
179179
* and set up session syncing
180180
*/
181181
async initSessionStorage(): Promise<AuthState> {
182-
const authState =
183-
AuthService.getCurrentTabAuth() || (await this.getSharedSessionAuth());
182+
let authState = AuthService.getCurrentTabAuth();
184183

185-
if (authState) {
186-
if (!this.authState) {
184+
if (!authState) {
185+
authState = await this.getSharedSessionAuth();
186+
187+
if (authState) {
187188
this.saveLogin(authState);
189+
} else {
190+
AppStateService.updateAuth(null);
188191
}
189-
} else {
190-
AppStateService.updateAuth(null);
191192
}
192193

193194
return authState;

0 commit comments

Comments
 (0)