Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change logic to preserve token #433

Merged
merged 3 commits into from
Oct 7, 2024
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
11 changes: 6 additions & 5 deletions src/facade/sdkFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ export class SdkFacade {
);

if (userId !== undefined) {
if (userId === null) {
sdk.unsetToken();
} else {
const currentToken = sdk.context.getToken();
const currentToken = sdk.context.getToken();
const currentSubject = currentToken?.getSubject() ?? null;

if (currentToken?.getSubject() !== userId) {
if (currentSubject !== userId) {
if (userId === null) {
sdk.unsetToken();
} else {
sdk.identify(userId);
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/evaluator.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as fetchMock from 'fetch-mock';
import {MockOptions} from 'fetch-mock';
import {
Evaluator,
ErrorResponse,
EvaluationContext,
EvaluationError,
EvaluationErrorType,
EvaluationOptions,
Evaluator,
QueryError,
QueryErrorResponse,
EvaluationOptions,
} from '../src/evaluator';
import {Token} from '../src/token';
import {BASE_ENDPOINT_URL, CLIENT_LIBRARY} from '../src/constants';
Expand Down
59 changes: 58 additions & 1 deletion test/facade/sdkFacade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ describe('A SDK facade', () => {
expect(context.setToken).not.toHaveBeenCalled();
});

it('should allow unidentifying a user', () => {
it('should allow anonymizing a user passing a null user ID', () => {
const context = createContextMock();

jest.spyOn(context, 'getToken')
Expand Down Expand Up @@ -523,6 +523,62 @@ describe('A SDK facade', () => {
expect(context.setToken).toHaveBeenCalledTimes(1);
});

it('should preserve the current token if the user is already anonymous', () => {
const context = createContextMock();

jest.spyOn(context, 'getToken')
.mockImplementation(() => Token.issue(appId, null));

jest.spyOn(context, 'setToken')
.mockImplementation();

jest.spyOn(Sdk, 'init')
.mockImplementationOnce(config => {
const sdk = Sdk.init(config);

jest.spyOn(sdk, 'appId', 'get').mockReturnValue(appId);
jest.spyOn(sdk, 'context', 'get').mockReturnValue(context);

return sdk;
});

SdkFacade.init({
appId: appId,
track: false,
userId: null,
});

expect(context.setToken).not.toHaveBeenCalled();
});

it('should preserve the current token if the user ID is the same', () => {
const context = createContextMock();

jest.spyOn(context, 'getToken')
.mockImplementation(() => Token.issue(appId, 'c4r0l'));

jest.spyOn(context, 'setToken')
.mockImplementation();

jest.spyOn(Sdk, 'init')
.mockImplementationOnce(config => {
const sdk = Sdk.init(config);

jest.spyOn(sdk, 'appId', 'get').mockReturnValue(appId);
jest.spyOn(sdk, 'context', 'get').mockReturnValue(context);

return sdk;
});

SdkFacade.init({
appId: appId,
track: false,
userId: 'c4r0l',
});

expect(context.setToken).not.toHaveBeenCalled();
});

it('should allow anonymizing a user', () => {
const context = createContextMock();

Expand All @@ -531,6 +587,7 @@ describe('A SDK facade', () => {
jest.spyOn(context, 'getToken')
.mockImplementation()
.mockImplementation(() => token);

jest.spyOn(context, 'setToken')
.mockImplementation()
.mockImplementation(newToken => {
Expand Down
Loading