Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
fix: ignore invalid userToken (#802)
Browse files Browse the repository at this point in the history
* fix: ignore invalid userToken

* fix build error

* remove unnecessary validation

* console warn directly

* update warning message

* update test cases
  • Loading branch information
Eunjae Lee committed Dec 4, 2020
1 parent 24f88fd commit a2876c5
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/SearchParameters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var find = require('../functions/find');
var valToNumber = require('../functions/valToNumber');
var omit = require('../functions/omit');
var objectHasKeys = require('../functions/objectHasKeys');
var isValidUserToken = require('../utils/isValidUserToken');

var RefinementList = require('./RefinementList');

Expand Down Expand Up @@ -93,6 +94,9 @@ function findArray(array, searchedValue) {
function SearchParameters(newParameters) {
var params = newParameters ? SearchParameters._parseNumbers(newParameters) : {};

if (params.userToken !== undefined && !isValidUserToken(params.userToken)) {
console.warn('[algoliasearch-helper] The `userToken` parameter is invalid. This can lead to wrong analytics.\n - Format: [a-zA-Z0-9_-]{1,64}');
}
/**
* This attribute contains the list of all the conjunctive facets
* used. This list will be added to requested facets in the
Expand Down
8 changes: 8 additions & 0 deletions src/utils/isValidUserToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = function isValidUserToken(userToken) {
if (userToken === null) {
return false;
}
return /^[a-zA-Z0-9_-]{1,64}$/.test(userToken);
};
39 changes: 39 additions & 0 deletions test/spec/SearchParameters/constructorFn.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ test('Constructor should ignore keys with undefined values', function() {
expect(state).not.toHaveProperty('page');
});

test('Constructor should warn about invalid userToken', function() {
const message = '[algoliasearch-helper] The `userToken` parameter is invalid. This can lead to wrong analytics.\n - Format: [a-zA-Z0-9_-]{1,64}';
console.warn = jest.fn();
expect(new SearchParameters({
userToken: ''
}));
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenLastCalledWith(message);

expect(new SearchParameters({
userToken: null
}));
expect(console.warn).toHaveBeenCalledTimes(2);
expect(console.warn).toHaveBeenLastCalledWith(message);

expect(new SearchParameters({
userToken: 'wrong user token!'
}));
expect(console.warn).toHaveBeenCalledTimes(3);
expect(console.warn).toHaveBeenLastCalledWith(message);
});

test('Factory should accept an object with known keys', function() {
var legitConfig = {
'query': '',
Expand Down Expand Up @@ -116,3 +138,20 @@ test('Factory should ignore keys with undefined values', function() {

expect(state).not.toHaveProperty('page');
});

test('Factory should warn about invalid userToken', function() {
const message = '[algoliasearch-helper] The `userToken` parameter is invalid. This can lead to wrong analytics.\n - Format: [a-zA-Z0-9_-]{1,64}';
console.warn = jest.fn();

SearchParameters.make({userToken: null});
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenLastCalledWith(message);

SearchParameters.make({userToken: ''});
expect(console.warn).toHaveBeenCalledTimes(2);
expect(console.warn).toHaveBeenLastCalledWith(message);

SearchParameters.make({userToken: 'my invalid token!'});
expect(console.warn).toHaveBeenCalledTimes(3);
expect(console.warn).toHaveBeenLastCalledWith(message);
});
18 changes: 18 additions & 0 deletions test/spec/SearchParameters/setQueryParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,21 @@ test(
expect(state1.betaParameter).toEqual('configValue');
}
);

test('setQueryParameter should warn about invalid userToken', function() {
const message = '[algoliasearch-helper] The `userToken` parameter is invalid. This can lead to wrong analytics.\n - Format: [a-zA-Z0-9_-]{1,64}';
console.warn = jest.fn();

var state = new SearchParameters();
state.setQueryParameter('userToken', null);
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenLastCalledWith(message);

state.setQueryParameter('userToken', '');
expect(console.warn).toHaveBeenCalledTimes(2);
expect(console.warn).toHaveBeenLastCalledWith(message);

state.setQueryParameter('userToken', 'my invalid token!');
expect(console.warn).toHaveBeenCalledTimes(3);
expect(console.warn).toHaveBeenLastCalledWith(message);
});
18 changes: 18 additions & 0 deletions test/spec/SearchParameters/setQueryParameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,21 @@ test('setQueryParameters should omit defined parameters with next values of unde
expect(state1).not.toHaveProperty('query');
expect(state1).not.toHaveProperty('page');
});

test('setQueryParameters should warn about invalid userToken', function() {
const message = '[algoliasearch-helper] The `userToken` parameter is invalid. This can lead to wrong analytics.\n - Format: [a-zA-Z0-9_-]{1,64}';
console.warn = jest.fn();

var state = new SearchParameters();
state.setQueryParameters({userToken: null});
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenLastCalledWith(message);

state.setQueryParameters({userToken: ''});
expect(console.warn).toHaveBeenCalledTimes(2);
expect(console.warn).toHaveBeenLastCalledWith(message);

state.setQueryParameters({userToken: 'my invalid token!'});
expect(console.warn).toHaveBeenCalledTimes(3);
expect(console.warn).toHaveBeenLastCalledWith(message);
});
18 changes: 18 additions & 0 deletions test/spec/algoliasearch.helper/setQueryParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,21 @@ test('setChange should not change the current state: no real modification', func
expect(changed).toBe(false);
expect(helper.state).toBe(initialState);
});

test('setQueryParameter should warn about invalid userToken', function() {
const message = '[algoliasearch-helper] The `userToken` parameter is invalid. This can lead to wrong analytics.\n - Format: [a-zA-Z0-9_-]{1,64}';
console.warn = jest.fn();

var helper = algoliasearchHelper(fakeClient, null, {});
helper.setQueryParameter('userToken', null);
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenLastCalledWith(message);

helper.setQueryParameter('userToken', '');
expect(console.warn).toHaveBeenCalledTimes(2);
expect(console.warn).toHaveBeenLastCalledWith(message);

helper.setQueryParameter('userToken', 'my invalid token!');
expect(console.warn).toHaveBeenCalledTimes(3);
expect(console.warn).toHaveBeenLastCalledWith(message);
});
18 changes: 18 additions & 0 deletions test/spec/algoliasearch.helper/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,21 @@ test('setState should set a default hierarchicalFacetRefinement when a rootPath
'hierarchicalCategories.lvl0': ['Cameras & Camcorders']
});
});

test('setState should warn about invalid userToken', function() {
const message = '[algoliasearch-helper] The `userToken` parameter is invalid. This can lead to wrong analytics.\n - Format: [a-zA-Z0-9_-]{1,64}';
console.warn = jest.fn();

var helper = algoliasearchHelper(fakeClient, null, {});
helper.setState({userToken: null});
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenLastCalledWith(message);

helper.setState({userToken: ''});
expect(console.warn).toHaveBeenCalledTimes(2);
expect(console.warn).toHaveBeenLastCalledWith(message);

helper.setState({userToken: 'my invalid token!'});
expect(console.warn).toHaveBeenCalledTimes(3);
expect(console.warn).toHaveBeenLastCalledWith(message);
});
15 changes: 15 additions & 0 deletions test/spec/utils/isValidUserToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

var isValidUserToken = require('../../../src/utils/isValidUserToken');

test('returns true with valid user token', function() {
expect(isValidUserToken('abc')).toEqual(true);
expect(isValidUserToken('abc-def')).toEqual(true);
expect(isValidUserToken('abc-def_ghi012')).toEqual(true);
});

test('returns false with invalid user token', function() {
expect(isValidUserToken(null)).toEqual(false);
expect(isValidUserToken('')).toEqual(false);
expect(isValidUserToken('my token')).toEqual(false);
});

0 comments on commit a2876c5

Please sign in to comment.