Skip to content

Commit

Permalink
[7.x] Do not call feature usage APIs on anonymous paths (#77411) (#77510
Browse files Browse the repository at this point in the history
)
  • Loading branch information
joshdover authored Sep 15, 2020
1 parent 08a99d0 commit e85765b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ describe('FeatureUsageService', () => {
}),
});
});

it('does not call endpoint if on anonymous path', async () => {
http.anonymousPaths.isAnonymous.mockReturnValue(true);
const setup = service.setup({ http });
await setup.register('my-feature', 'platinum');
expect(http.post).not.toHaveBeenCalled();
});
});
});

Expand Down Expand Up @@ -64,6 +71,14 @@ describe('FeatureUsageService', () => {
}),
});
});

it('does not call endpoint if on anonymous path', async () => {
http.anonymousPaths.isAnonymous.mockReturnValue(true);
service.setup({ http });
const start = service.start({ http });
await start.notifyUsage('my-feature', 42);
expect(http.post).not.toHaveBeenCalled();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export class FeatureUsageService {
public setup({ http }: SetupDeps): FeatureUsageServiceSetup {
return {
register: async (featureName, licenseType) => {
// Skip registration if on logged-out page
// NOTE: this only works because the login page does a full-page refresh after logging in
// If this is ever changed, this code will need to buffer registrations and call them after the user logs in.
if (http.anonymousPaths.isAnonymous(window.location.pathname)) return;

await http.post('/internal/licensing/feature_usage/register', {
body: JSON.stringify({
featureName,
Expand All @@ -55,6 +60,9 @@ export class FeatureUsageService {
public start({ http }: StartDeps): FeatureUsageServiceStart {
return {
notifyUsage: async (featureName, usedAt = Date.now()) => {
// Skip notification if on logged-out page
if (http.anonymousPaths.isAnonymous(window.location.pathname)) return;

const lastUsed = isDate(usedAt) ? usedAt.getTime() : usedAt;
await http.post('/internal/licensing/feature_usage/notify', {
body: JSON.stringify({
Expand Down

0 comments on commit e85765b

Please sign in to comment.