Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
69 changes: 69 additions & 0 deletions src/client/eppo-precomputed-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
import { decodeBase64, encodeBase64, getMD5Hash } from '../obfuscation';
import PrecomputedRequestor from '../precomputed-requestor';

import EppoPrecomputedClient, {

Check warning on line 29 in src/client/eppo-precomputed-client.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (18)

There should be at least one empty line between import groups

Check warning on line 29 in src/client/eppo-precomputed-client.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (20)

There should be at least one empty line between import groups

Check warning on line 29 in src/client/eppo-precomputed-client.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (22)

There should be at least one empty line between import groups

Check warning on line 29 in src/client/eppo-precomputed-client.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (23)

There should be at least one empty line between import groups
PrecomputedFlagsRequestParameters,
Subject,
} from './eppo-precomputed-client';
import { applicationLogger } from '..';

Check warning on line 33 in src/client/eppo-precomputed-client.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (18)

`..` import should occur before import of `../../test/testHelpers`

Check warning on line 33 in src/client/eppo-precomputed-client.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (20)

`..` import should occur before import of `../../test/testHelpers`

Check warning on line 33 in src/client/eppo-precomputed-client.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (22)

`..` import should occur before import of `../../test/testHelpers`

Check warning on line 33 in src/client/eppo-precomputed-client.spec.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (23)

`..` import should occur before import of `../../test/testHelpers`

describe('EppoPrecomputedClient E2E test', () => {
const precomputedConfigurationWire = readMockConfigurationWireResponse(
Expand All @@ -52,6 +53,7 @@
subjectKey: 'test-subject',
subjectAttributes: { attr1: 'value1' },
};

beforeEach(async () => {
storage = new MemoryOnlyConfigurationStore<PrecomputedFlag>();
storage.setFormat(FormatEnum.PRECOMPUTED);
Expand Down Expand Up @@ -110,6 +112,73 @@
});
});

describe('store initialization logged errors', () => {
let mockError: jest.SpyInstance;

beforeEach(() => {
mockError = jest.spyOn(applicationLogger, 'error');
});

afterEach(() => {
mockError.mockRestore();
});

it('logs error when initialized with store without salt', () => {
const emptyStore = new MemoryOnlyConfigurationStore<PrecomputedFlag>();
new EppoPrecomputedClient({
precomputedFlagStore: emptyStore,
subject: {
subjectKey: '',
subjectAttributes: {},
},
});
expect(mockError).not.toHaveBeenCalledWith(
'EppoPrecomputedClient requires a precomputedFlagStore with a salt if requestParameters are not provided',
);
});

it('logs errors when constructor receives an uninitialized store without a salt', () => {
const nonemptyStore = new MemoryOnlyConfigurationStore<PrecomputedFlag>();
// Incorrectly initialized: no salt, not set to initialized
jest.spyOn(nonemptyStore, 'getKeys').mockReturnValue(['some-key']);

new EppoPrecomputedClient({
precomputedFlagStore: nonemptyStore,
subject: {
subjectKey: '',
subjectAttributes: {},
},
});
expect(mockError).toHaveBeenCalledWith(
'[Eppo SDK] EppoPrecomputedClient requires an initialized precomputedFlagStore if requestParameters are not provided',
);
expect(mockError).toHaveBeenCalledWith(
'[Eppo SDK] EppoPrecomputedClient requires a precomputedFlagStore with a salt if requestParameters are not provided',
);
});

it('only logs initialization error when constructor receives an uninitialized store with salt', () => {
const nonemptyStore = new MemoryOnlyConfigurationStore<PrecomputedFlag>();
nonemptyStore.salt = 'nacl';
// Incorrectly initialized: no salt, not set to initialized
jest.spyOn(nonemptyStore, 'getKeys').mockReturnValue(['some-key']);

new EppoPrecomputedClient({
precomputedFlagStore: nonemptyStore,
subject: {
subjectKey: '',
subjectAttributes: {},
},
});
expect(mockError).toHaveBeenCalledWith(
'[Eppo SDK] EppoPrecomputedClient requires an initialized precomputedFlagStore if requestParameters are not provided',
);
expect(mockError).not.toHaveBeenCalledWith(
'[Eppo SDK] EppoPrecomputedClient requires a precomputedFlagStore with a salt if requestParameters are not provided',
);
});
});

describe('setLogger', () => {
let flagStorage: IConfigurationStore<PrecomputedFlag>;
let subject: Subject;
Expand Down
27 changes: 15 additions & 12 deletions src/client/eppo-precomputed-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,21 @@
// Online-mode
this.requestParameters = options.requestParameters;
} else {
// Offline-mode

// Offline mode depends on pre-populated IConfigurationStores (flags and bandits) to source configuration.
if (!this.precomputedFlagStore.isInitialized()) {
logger.error(
`${loggerPrefix} EppoPrecomputedClient requires an initialized precomputedFlagStore if requestParameters are not provided`,
);
// Offline-mode -- depends on pre-populated IConfigurationStores (flags and bandits) to source configuration.

// Allow an empty precomputedFlagStore to be passed in, but if it has items, ensure it was initialized properly.
if (this.precomputedFlagStore.getKeys().length > 0) {
if (!this.precomputedFlagStore.isInitialized()) {
logger.error(
`${loggerPrefix} EppoPrecomputedClient requires an initialized precomputedFlagStore if requestParameters are not provided`,
);
}

if (!this.precomputedFlagStore.salt) {
logger.error(
`${loggerPrefix} EppoPrecomputedClient requires a precomputedFlagStore with a salt if requestParameters are not provided`,
);
}
}

if (this.precomputedBanditStore && !this.precomputedBanditStore.isInitialized()) {
Expand All @@ -115,12 +123,7 @@
);
}

if (!this.precomputedFlagStore.salt) {
logger.error(
`${loggerPrefix} EppoPrecomputedClient requires a precomputedFlagStore with a salt if requestParameters are not provided`,
);
}

Check warning on line 126 in src/client/eppo-precomputed-client.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (18)

Delete `⏎`

Check warning on line 126 in src/client/eppo-precomputed-client.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (20)

Delete `⏎`

Check warning on line 126 in src/client/eppo-precomputed-client.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (22)

Delete `⏎`

Check warning on line 126 in src/client/eppo-precomputed-client.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (23)

Delete `⏎`
if (this.precomputedBanditStore && !this.precomputedBanditStore.salt) {
logger.warn(
`${loggerPrefix} EppoPrecomputedClient missing or empty salt for precomputedBanditStore`,
Expand Down
Loading