mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
make sure cookie is never shared among multiple test users
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import superagent from 'superagent';
|
import superagent from 'superagent';
|
||||||
import nconf from 'nconf';
|
import nconf from 'nconf';
|
||||||
|
import { isEmpty, cloneDeep } from 'lodash';
|
||||||
|
|
||||||
const API_TEST_SERVER_PORT = nconf.get('PORT');
|
const API_TEST_SERVER_PORT = nconf.get('PORT');
|
||||||
let apiVersion;
|
let apiVersion;
|
||||||
@@ -9,7 +10,9 @@ let apiVersion;
|
|||||||
// Sets up an abject that can make all REST requests
|
// Sets up an abject that can make all REST requests
|
||||||
// If a user is passed in, the uuid and api token of
|
// If a user is passed in, the uuid and api token of
|
||||||
// the user are used to make the requests
|
// the user are used to make the requests
|
||||||
export function requester (user = {}, additionalSets) {
|
export function requester (user = {}, additionalSets = {}) {
|
||||||
|
additionalSets = cloneDeep(additionalSets); // cloning because it could be modified later to set cookie
|
||||||
|
|
||||||
return {
|
return {
|
||||||
get: _requestMaker(user, 'get', additionalSets),
|
get: _requestMaker(user, 'get', additionalSets),
|
||||||
post: _requestMaker(user, 'post', additionalSets),
|
post: _requestMaker(user, 'post', additionalSets),
|
||||||
@@ -22,11 +25,7 @@ requester.setApiVersion = (version) => {
|
|||||||
apiVersion = version;
|
apiVersion = version;
|
||||||
};
|
};
|
||||||
|
|
||||||
// save the last cookie so that it's resent with every request
|
function _requestMaker (user, method, additionalSets = {}) {
|
||||||
// should be safe since every time a user is generated this will be overwritten
|
|
||||||
let cookie;
|
|
||||||
|
|
||||||
function _requestMaker (user, method, additionalSets) {
|
|
||||||
if (!apiVersion) throw new Error('apiVersion not set');
|
if (!apiVersion) throw new Error('apiVersion not set');
|
||||||
|
|
||||||
return (route, send, query) => {
|
return (route, send, query) => {
|
||||||
@@ -40,12 +39,7 @@ function _requestMaker (user, method, additionalSets) {
|
|||||||
.set('x-api-key', user.apiToken);
|
.set('x-api-key', user.apiToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we previously saved a cookie, send it along the request
|
if (!isEmpty(additionalSets)) {
|
||||||
if (cookie) {
|
|
||||||
request.set('Cookie', cookie);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (additionalSets) {
|
|
||||||
request.set(additionalSets);
|
request.set(additionalSets);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +57,7 @@ function _requestMaker (user, method, additionalSets) {
|
|||||||
|
|
||||||
// if any cookies was sent, save it for the next request
|
// if any cookies was sent, save it for the next request
|
||||||
if (response.headers['set-cookie']) {
|
if (response.headers['set-cookie']) {
|
||||||
cookie = response.headers['set-cookie'].map(cookieString => {
|
additionalSets.cookie = response.headers['set-cookie'].map(cookieString => {
|
||||||
return cookieString.split(';')[0];
|
return cookieString.split(';')[0];
|
||||||
}).join('; ');
|
}).join('; ');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user