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
19 changes: 19 additions & 0 deletions src/OidcAuthStateStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import EncryptedStorage from 'react-native-encrypted-storage';
import OidcAuthStateStorage, {
type OidcAuthState,
} from './OidcAuthStateStorage';
import * as SentryIntegrationModule from './sentryIntegration';

describe('OidcAuthStateStorage', () => {
const CLIENT_ID = 'test-client-id';
const EXPECTED_STORAGE_KEY = `de.contentpass.${CLIENT_ID}-OIDCAuthState`;

let storage: OidcAuthStateStorage;
let reportErrorSpy: jest.SpyInstance;
const mockAuthState: OidcAuthState = {
accessToken: 'test-access-token',
accessTokenExpirationDate: '2023-12-31T23:59:59Z',
Expand All @@ -18,6 +20,9 @@ describe('OidcAuthStateStorage', () => {

beforeEach(() => {
storage = new OidcAuthStateStorage(CLIENT_ID);
reportErrorSpy = jest
.spyOn(SentryIntegrationModule, 'reportError')
.mockReturnValue(undefined);
});

afterEach(() => {
Expand Down Expand Up @@ -59,4 +64,18 @@ describe('OidcAuthStateStorage', () => {
EXPECTED_STORAGE_KEY
);
});

it('should report error if clearing OIDC auth state fails', async () => {
const error = new Error('test-error');
(EncryptedStorage.removeItem as jest.Mock).mockRejectedValue(error);

await storage.clearOidcAuthState();

expect(EncryptedStorage.removeItem).toHaveBeenCalledWith(
EXPECTED_STORAGE_KEY
);
expect(reportErrorSpy).toHaveBeenCalledWith(error, {
msg: 'Failed to clear OIDC auth state. Most probably we tried to remove item which does not exist',
});
});
});
9 changes: 8 additions & 1 deletion src/OidcAuthStateStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import EncryptedStorage from 'react-native-encrypted-storage';
import { reportError } from './sentryIntegration';

const AUTH_STATE_KEY = 'OIDCAuthState';

Expand Down Expand Up @@ -28,6 +29,12 @@ export default class OidcAuthStateStorage {
}

public async clearOidcAuthState() {
await EncryptedStorage.removeItem(this.key);
try {
await EncryptedStorage.removeItem(this.key);
} catch (err: any) {
reportError(err, {
msg: 'Failed to clear OIDC auth state. Most probably we tried to remove item which does not exist',
});
}
}
}