statusinfo-service.test.js 3.42 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([
    'screens/player/shared/serviceadmin',
    'screens/player/shared/test-helper/stub-service-interface',
    'screens/player/shared/test-helper/service',
    'screens/player/shared/csrf',
    'screens/player/firmware/statusinfo/statusinfo',
    'screens/player/store/store',
    'screens/player/firmware/storage/storage',
    'screens/player/firmware/statusinfo/impl/statusinfo-service'
], function(ServiceAdmin, stubServiceInterface, serviceTestHelper, CSRFSupport, StatusInfo, Store, Storage, StatusInfoService) {
    'use strict';

    describe('StatusInfo', function() {

        var stubCsrf, stubStoreService, stubStorageService;

        beforeEach(function() {
            stubCsrf = sinon.stub(CSRFSupport.prototype);
            stubStoreService = stubServiceInterface(ServiceAdmin, Store);
            stubStorageService = stubServiceInterface(ServiceAdmin, Storage);
            stubStoreService.api.getState.returns({ statusmodel: { devicePostUrl: '/foo/bar/baz', version: '1.1.1' } });
            stubStorageService.api.refresh.returns(Promise.resolve({some: 'storage'}));
        });

        afterEach(function() {
            Object.keys(stubCsrf).forEach(function(k) {
                stubCsrf[k].restore();
            });
            stubStoreService.restore();
            stubStorageService.restore();
        });

        serviceTestHelper.runDefaultTests(StatusInfoService, StatusInfo.serviceName);

        describe('send action', function() {

            it('should send the status information', function() {
                var dateStub = new Date();
                var clock = sinon.useFakeTimers(dateStub.getTime()); // there is some bug in Sinon that turns the timestamp to 0
                stubCsrf.$ajax.yieldsTo('success');
                return StatusInfoService.send().then(function() {
                    expect(stubCsrf.$ajax).calledWithMatch(sinon.match.defined, {
                        url: '/foo/bar/baz.statusinfo.json',
                        type: 'POST',
                        data: { data: JSON.stringify({
                            usage: {},
                            timestamp: 0,
                            startTimestamp: 0,
                            firmwareVersion: '1.1.1'
                        }) }
                    });
                    clock.restore();
                });
            });

            it('rejects if the devicePostUrl is missing in the state', function() {
                stubStoreService.api.getState.returns({});
                return StatusInfoService.send().catch(function(error) {
                    expect(error.message).to.equal('unable to send status information. no device url.');
                });
            });

        });

    });
});