Commit 95b44716 authored by Nicolas Dular's avatar Nicolas Dular

Call RedisHllUserEvent only when a user is set

We're calling the `trackRedisHllUserEvent` function from parts of the
application where we can't guarantee a signed in user. The API however
assumes that there is a user set and would respond with a 401 error.

As a solution, we now only pass events to the endpoint when a user is
signed in.

This MR also fixes a potential false-positiv test by calling the
function before checking if the mocked axios client gets called.

Changelog: fixed
parent f8e1d6e4
...@@ -866,7 +866,7 @@ const Api = { ...@@ -866,7 +866,7 @@ const Api = {
}, },
trackRedisHllUserEvent(event) { trackRedisHllUserEvent(event) {
if (!gon.features?.usageDataApi) { if (!gon.current_user_id || !gon.features?.usageDataApi) {
return null; return null;
} }
......
...@@ -1503,33 +1503,55 @@ describe('Api', () => { ...@@ -1503,33 +1503,55 @@ describe('Api', () => {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}; };
describe('when usage data increment unique users is called with feature flag disabled', () => { describe('when user is set', () => {
beforeEach(() => { beforeEach(() => {
gon.features = { ...gon.features, usageDataApi: false }; window.gon.current_user_id = 1;
}); });
it('returns null', () => { describe('when usage data increment unique users is called with feature flag disabled', () => {
jest.spyOn(axios, 'post'); beforeEach(() => {
mock.onPost(expectedUrl).replyOnce(httpStatus.OK, true); gon.features = { ...gon.features, usageDataApi: false };
});
expect(axios.post).toHaveBeenCalledTimes(0); it('returns null and does not call the endpoint', () => {
expect(Api.trackRedisHllUserEvent(event)).toEqual(null); jest.spyOn(axios, 'post');
const result = Api.trackRedisHllUserEvent(event);
expect(result).toEqual(null);
expect(axios.post).toHaveBeenCalledTimes(0);
});
});
describe('when usage data increment unique users is called', () => {
beforeEach(() => {
gon.features = { ...gon.features, usageDataApi: true };
});
it('resolves the Promise', () => {
jest.spyOn(axios, 'post');
mock.onPost(expectedUrl, { event }).replyOnce(httpStatus.OK, true);
return Api.trackRedisHllUserEvent(event).then(({ data }) => {
expect(data).toEqual(true);
expect(axios.post).toHaveBeenCalledWith(expectedUrl, postData, { headers });
});
});
}); });
}); });
describe('when usage data increment unique users is called', () => { describe('when user is not set and feature flag enabled', () => {
beforeEach(() => { beforeEach(() => {
gon.features = { ...gon.features, usageDataApi: true }; gon.features = { ...gon.features, usageDataApi: true };
}); });
it('resolves the Promise', () => { it('returns null and does not call the endpoint', () => {
jest.spyOn(axios, 'post'); jest.spyOn(axios, 'post');
mock.onPost(expectedUrl, { event }).replyOnce(httpStatus.OK, true);
return Api.trackRedisHllUserEvent(event).then(({ data }) => { const result = Api.trackRedisHllUserEvent(event);
expect(data).toEqual(true);
expect(axios.post).toHaveBeenCalledWith(expectedUrl, postData, { headers }); expect(result).toEqual(null);
}); expect(axios.post).toHaveBeenCalledTimes(0);
}); });
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment