Commit 41836f7a authored by Phil Hughes's avatar Phil Hughes

Merge branch 'tor/defect/unauthenticated-whitespace-toggle' into 'master'

Skip saving the diffs whitespace setting if the user isn't logged in

See merge request gitlab-org/gitlab!65094
parents 809d8f6c e07458f0
......@@ -580,7 +580,7 @@ export const setShowWhitespace = async (
{ state, commit },
{ url, showWhitespace, updateDatabase = true },
) => {
if (updateDatabase) {
if (updateDatabase && Boolean(window.gon?.current_user_id)) {
await axios.put(url || state.endpointUpdateUser, { show_whitespace_in_diffs: showWhitespace });
}
......
......@@ -1019,10 +1019,12 @@ describe('DiffsStoreActions', () => {
const endpointUpdateUser = 'user/prefs';
let putSpy;
let mock;
let gon;
beforeEach(() => {
mock = new MockAdapter(axios);
putSpy = jest.spyOn(axios, 'put');
gon = window.gon;
mock.onPut(endpointUpdateUser).reply(200, {});
jest.spyOn(eventHub, '$emit').mockImplementation();
......@@ -1030,6 +1032,7 @@ describe('DiffsStoreActions', () => {
afterEach(() => {
mock.restore();
window.gon = gon;
});
it('commits SET_SHOW_WHITESPACE', (done) => {
......@@ -1043,7 +1046,9 @@ describe('DiffsStoreActions', () => {
);
});
it('saves to the database', async () => {
it('saves to the database when the user is logged in', async () => {
window.gon = { current_user_id: 12345 };
await setShowWhitespace(
{ state: { endpointUpdateUser }, commit() {} },
{ showWhitespace: true, updateDatabase: true },
......@@ -1052,6 +1057,17 @@ describe('DiffsStoreActions', () => {
expect(putSpy).toHaveBeenCalledWith(endpointUpdateUser, { show_whitespace_in_diffs: true });
});
it('does not try to save to the API if the user is not logged in', async () => {
window.gon = {};
await setShowWhitespace(
{ state: { endpointUpdateUser }, commit() {} },
{ showWhitespace: true, updateDatabase: true },
);
expect(putSpy).not.toHaveBeenCalled();
});
it('emits eventHub event', async () => {
await setShowWhitespace(
{ state: {}, commit() {} },
......
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