preferences.test.js 6.04 KB
/*
 *
 * 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();
        });

    });
});