From 3e8f9d6542cad5b96621b0aac5f938859b7848a3 Mon Sep 17 00:00:00 2001 From: Arthur Almeida Date: Thu, 3 Jan 2019 14:05:58 -0200 Subject: [PATCH] added support to configure credentials option for Remote Storage --- src/storage_manager/config/config.js | 4 +- src/storage_manager/model/RemoteStorage.js | 14 +++---- test/specs/storage_manager/model/Models.js | 45 ++++++++++++++++++++++ 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/storage_manager/config/config.js b/src/storage_manager/config/config.js index f77f889d0..4cd583ae6 100644 --- a/src/storage_manager/config/config.js +++ b/src/storage_manager/config/config.js @@ -53,5 +53,7 @@ module.exports = { // set contentType paramater of $.ajax // true: application/json; charset=utf-8' // false: 'x-www-form-urlencoded' - contentTypeJson: false + contentTypeJson: false, + + credentials: 'include' }; diff --git a/src/storage_manager/model/RemoteStorage.js b/src/storage_manager/model/RemoteStorage.js index d8fe918e5..ba78518b6 100644 --- a/src/storage_manager/model/RemoteStorage.js +++ b/src/storage_manager/model/RemoteStorage.js @@ -10,7 +10,8 @@ module.exports = require('backbone').Model.extend({ params: {}, beforeSend() {}, onComplete() {}, - contentTypeJson: false + contentTypeJson: false, + credentials: 'include' }, /** @@ -113,7 +114,7 @@ module.exports = require('backbone').Model.extend({ } fetchOptions = { method: opts.method || 'post', - credentials: 'include', + credentials: this.get('credentials'), headers }; @@ -124,11 +125,10 @@ module.exports = require('backbone').Model.extend({ this.onStart(); this.fetch(url, fetchOptions) - .then( - res => - ((res.status / 200) | 0) == 1 - ? res.text() - : res.text().then(text => Promise.reject(text)) + .then(res => + ((res.status / 200) | 0) == 1 + ? res.text() + : res.text().then(text => Promise.reject(text)) ) .then(text => this.onResponse(text, clb)) .catch(err => this.onError(err, clbErr)); diff --git a/test/specs/storage_manager/model/Models.js b/test/specs/storage_manager/model/Models.js index c5b07a359..6cd4a1184 100644 --- a/test/specs/storage_manager/model/Models.js +++ b/test/specs/storage_manager/model/Models.js @@ -96,6 +96,51 @@ module.exports = { expect(callResult.called).toEqual(true); expect(callResult.firstCall.args[0]).toEqual(endpointLoad); }); + + test("Load data with credentials option as 'include' by default", () => { + obj.load(['item1', 'item2']); + const callResult = obj.fetch; + expect(callResult.called).toEqual(true); + expect(callResult.firstCall.args[1]).toMatchObject({ + credentials: 'include' + }); + }); + + test("Store data with credentials option as 'include' by default", () => { + obj.store(data); + const callResult = obj.fetch; + expect(callResult.called).toEqual(true); + expect(callResult.firstCall.args[1]).toMatchObject({ + credentials: 'include' + }); + }); + + test('Store data with credentials option as false ', () => { + obj = new RemoteStorage({ ...storageOptions, credentials: false }); + sinon + .stub(obj, 'fetch') + .returns(Promise.resolve(mockResponse({ data: 1 }))); + + obj.store(data); + const callResult = obj.fetch; + expect(callResult.called).toEqual(true); + expect(callResult.firstCall.args[1]).toMatchObject({ + credentials: false + }); + }); + + test('Load data with credentials option as false', () => { + obj = new RemoteStorage({ ...storageOptions, credentials: false }); + sinon + .stub(obj, 'fetch') + .returns(Promise.resolve(mockResponse({ data: 1 }))); + obj.load(['item1', 'item2']); + const callResult = obj.fetch; + expect(callResult.called).toEqual(true); + expect(callResult.firstCall.args[1]).toMatchObject({ + credentials: false + }); + }); }); } };