Skip to content

Commit

Permalink
Change logic to preserve token
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos committed Oct 7, 2024
1 parent 81ac5ed commit 5a48e2c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
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
58 changes: 57 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', () => {
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', () => {

Check failure on line 582 in test/facade/sdkFacade.test.ts

View workflow job for this annotation

GitHub Actions / lint

Test title is used multiple times in the same describe block
const context = createContextMock();

Expand Down

0 comments on commit 5a48e2c

Please sign in to comment.