Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ import { redactBreadcrumb, redactEvent } from './util/sentryRedaction';

Sentry.init({
dsn: "https://8a606d82aa68850021fbfac2ffda30b5@o4509293310967808.ingest.de.sentry.io/4509293314113617",
// The dev server (`npm start`, also used by the Cypress runs in CI)
// must not report: its errors would eat the reserved Sentry volume
enabled: !__DEV__,
// Our DPA rules out personal data in Sentry. Sentry v11 collects user
// info (incl. IP address), cookies, headers and bodies by default, so
// switch every data collection category off explicitly.
Expand Down
30 changes: 28 additions & 2 deletions src/modules/settings/watchSettingWhileAdmin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {runSaga, stdChannel} from 'redux-saga';
import {onValue} from 'firebase/database';
import firebase from '../../util/firebase';
import firebase, {isSignedIn} from '../../util/firebase';
import {error} from '../../util/log';
import {FIREBASE_AUTHENTICATION_EVENT} from '../auth/actions';
import {watchSettingWhileAdmin} from './watchSettingWhileAdmin';
Expand All @@ -23,6 +23,11 @@ const authEvent = (authData: any) => ({
const adminLogin = () => authEvent({uid: 'admin-uid', admin: true});
const userLogin = () => authEvent({uid: 'user-uid', admin: false});
const logout = () => authEvent(null);
// Shaped like the errors Firebase passes to a cancelled listener
const permissionDenied = () => Object.assign(
new Error(`permission_denied at ${PATH}: Client doesn't have permission to access the desired data.`),
{code: 'PERMISSION_DENIED'}
);

describe('modules', () => {
describe('settings', () => {
Expand All @@ -42,6 +47,7 @@ describe('modules', () => {
beforeEach(() => {
jest.clearAllMocks();
(firebase as jest.Mock).mockImplementation((path: string) => ({path}));
(isSignedIn as jest.Mock).mockReturnValue(true);
unsubscribe = jest.fn();
(onValue as jest.Mock).mockReturnValue(unsubscribe);

Expand Down Expand Up @@ -129,14 +135,34 @@ describe('modules', () => {

it('logs and allows a new subscription when Firebase cancels the listener', () => {
emit(adminLogin());
const denied = new Error('permission_denied');
const denied = permissionDenied();
lastListener().onCancel(denied);

expect(error).toHaveBeenCalledWith(`Listener for ${PATH} was cancelled`, denied);

emit(adminLogin());
expect(onValue).toHaveBeenCalledTimes(2);
});

it('does not log a permission_denied cancel after the sign-out', () => {
emit(adminLogin());
(isSignedIn as jest.Mock).mockReturnValue(false);
lastListener().onCancel(permissionDenied());

expect(error).not.toHaveBeenCalled();

emit(adminLogin());
expect(onValue).toHaveBeenCalledTimes(2);
});

it('logs other cancels after the sign-out', () => {
emit(adminLogin());
(isSignedIn as jest.Mock).mockReturnValue(false);
const unavailable = Object.assign(new Error('unavailable'), {code: 'UNAVAILABLE'});
lastListener().onCancel(unavailable);

expect(error).toHaveBeenCalledWith(`Listener for ${PATH} was cancelled`, unavailable);
});
});
});
});
13 changes: 11 additions & 2 deletions src/modules/settings/watchSettingWhileAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import {END, eventChannel, Task} from 'redux-saga';
import {cancel, fork, put, take, takeEvery} from 'redux-saga/effects';
import {onValue} from 'firebase/database';
import {FIREBASE_AUTHENTICATION_EVENT} from '../auth/actions';
import firebase from '../../util/firebase';
import firebase, {isSignedIn} from '../../util/firebase';
import {error} from '../../util/log';

type LoadedActionCreator = (value: any) => { type: string };

// Signing out makes Firebase cancel the admin-only listeners with
// permission_denied, often before the sign-out reaches the sagas that
// would remove them. Such a cancel is expected and not worth reporting.
function isDeniedAfterSignOut(e: unknown) {
return (e as any)?.code === 'PERMISSION_DENIED' && !isSignedIn();
}

// Emits `loaded(value)` for every change at `path`. Closing the channel
// removes the Firebase listener. If Firebase cancels the listener (e.g.
// permission denied), the error is logged and the channel ends.
Expand All @@ -15,7 +22,9 @@ export function createSettingChannel(path: string, loaded: LoadedActionCreator)
firebase(path),
snapshot => emit(loaded(snapshot.val())),
e => {
error(`Listener for ${path} was cancelled`, e);
if (!isDeniedAfterSignOut(e)) {
error(`Listener for ${path} was cancelled`, e);
}
emit(END);
}
));
Expand Down
12 changes: 12 additions & 0 deletions src/util/firebase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import firebaseDefault, {
unauth,
loadValue,
getIdToken,
isSignedIn,
} from './firebase';

describe('util/firebase', () => {
Expand Down Expand Up @@ -307,4 +308,15 @@ describe('util/firebase', () => {
expect(result).toBe(mockToken);
});
});

describe('isSignedIn', () => {
it('returns true when a user is signed in', () => {
expect(isSignedIn()).toBe(true);
});

it('returns false when nobody is signed in', () => {
mockAuth.currentUser = null;
expect(isSignedIn()).toBe(false);
});
});
});
5 changes: 5 additions & 0 deletions src/util/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ export function getIdToken() {
return getAuth().currentUser!.getIdToken();
}

export function isSignedIn() {
initialize();
return !!getAuth().currentUser;
}

export default firebase;

if ((window as any).Cypress) {
Expand Down
Loading