preferences.test.js
6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2016 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*/
/* globals Promise, sinon, describe, it, beforeEach, afterEach, expect */
/* eslint max-nested-callbacks: 0 */
define([
'underscore',
'screens/player/shared/serviceadmin',
'screens/player/shared/test-helper/stub-service-interface',
'screens/player/shared/test-helper/service',
'screens/player/shared/test-helper/command-handler',
'screens/player/shared/csrf',
'screens/player/store/store',
'screens/player/firmware/core/firmware/firmware',
'screens/player/firmware/preferences/preferences',
'screens/player/command/command',
'screens/player/command/preferences'
], function(_, ServiceAdmin, stubServiceInterface, serviceTestHelper, commandHandlerTestHelper, CSRFSupport, Store, Firmware, Preferences, PlayerCommand, preferencesCmd) {
'use strict';
describe('screens/player/command/preferences', function() {
var stubCsrf, stubStoreService, stubFirmwareService, stubPreferencesService;
beforeEach(function() {
stubCsrf = sinon.stub(CSRFSupport.prototype);
stubStoreService = stubServiceInterface(ServiceAdmin, Store);
stubFirmwareService = stubServiceInterface(ServiceAdmin, Firmware);
stubPreferencesService = stubServiceInterface(ServiceAdmin, Preferences);
});
afterEach(function() {
Object.keys(stubCsrf).forEach(function(k) {
stubCsrf[k].restore();
});
stubStoreService.restore();
stubFirmwareService.restore();
stubPreferencesService.restore();
});
serviceTestHelper.runDefaultTests(preferencesCmd, PlayerCommand.serviceName);
commandHandlerTestHelper.runDefaultTests(preferencesCmd, ['preferences-send', 'preferences-update']);
it('should send the preferences on the "preferences-send" command', function(done) {
var preferences = { 'foo': true, 'bar': false };
var ajaxPromise = Promise.resolve();
stubCsrf.$ajax.returns(ajaxPromise);
stubStoreService.api.getState.returns({ statusmodel: { server: '/foo/bar/baz' }, preferences: preferences });
preferencesCmd.handleCommand('preferences-send');
expect(stubCsrf.$ajax).to.be.calledWithMatch(sinon.match.defined, sinon.match(function(options) {
return options.type === 'POST' && options.data.data === JSON.stringify(preferences);
}));
ajaxPromise.then(function() {
done();
});
});
it('should not send sensitive preferences on the "preferences-send" command', function() {
var preferences = { device: 'foo', user: 'bar', password: 'baz' };
stubCsrf.$ajax.returns(Promise.resolve());
stubStoreService.api.getState.returns({ statusmodel: { server: '/foo/bar/baz' }, preferences: preferences });
preferencesCmd.handleCommand('preferences-send');
expect(stubCsrf.$ajax).to.be.calledWithMatch(sinon.match.defined, sinon.match(function(options) {
var prefs = JSON.parse(options.data.data);
return !prefs.device && !prefs.user && !prefs.password;
}));
});
it('should not modify state after sensitive preferences removal', function() {
var preferences = { device: 'foo', user: 'bar', password: 'baz' };
stubCsrf.$ajax.returns(Promise.resolve());
var state = { statusmodel: { server: '/foo/bar/baz' }, preferences: preferences };
var initialPreferences = _.assign({}, state.preferences);
stubStoreService.api.getState.returns(state);
preferencesCmd.handleCommand('preferences-send');
expect(initialPreferences).to.deep.equal(state.preferences);
});
it('should get the preferences from the server, then update them on the "preferences-update" command', function(done) {
var ajaxPromise = Promise.resolve({status: 'ok'});
stubCsrf.$ajax.returns(ajaxPromise);
stubStoreService.api.getState.returns({ statusmodel: { server: '/foo/bar/baz' } });
preferencesCmd.handleCommand('preferences-update');
expect(stubCsrf.$ajax).to.be.calledWithMatch(sinon.match.defined, sinon.match(function(options) {
return options.type === 'GET';
}));
ajaxPromise.then(function() {
expect(stubPreferencesService.api.save).to.be.called;
done();
});
});
it('should throw an error if the Store service is not available', function() {
ServiceAdmin.getService.withArgs(Store.serviceName).returns(null);
expect(function() { preferencesCmd.handleCommand('preferences-update'); }).to.throw();
ServiceAdmin.getService.restore();
});
it('should throw an error if the Firmware service is not available', function() {
ServiceAdmin.getService.withArgs(Firmware.serviceName).returns(null);
expect(function() { preferencesCmd.handleCommand('preferences-update'); }).to.throw();
ServiceAdmin.getService.restore();
});
it('should throw an error if the Preferences service is not available', function() {
ServiceAdmin.getService.withArgs(Preferences.serviceName).returns(null);
expect(function() { preferencesCmd.handleCommand('preferences-update'); }).to.throw();
ServiceAdmin.getService.restore();
});
});
});