logs.test.js 4.5 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/test-helper/command-handler',
    'screens/player/shared/csrf',
    'screens/player/store/store',
    'screens/player/firmware/log/logger',
    'screens/player/command/command',
    'screens/player/command/logs'
], function(ServiceAdmin, stubServiceInterface, serviceTestHelper, commandHandlerTestHelper, CSRFSupport, StoreSvc, LoggerSvc, PlayerCommand, logsCmd) {
    'use strict';

    describe('screens/player/command/logs', function() {

        var stubCsrf, stubStoreService, stubLoggerService;

        beforeEach(function() {
            stubCsrf = sinon.stub(CSRFSupport.prototype);
            stubStoreService = stubServiceInterface(ServiceAdmin, StoreSvc);
            stubLoggerService = stubServiceInterface(ServiceAdmin, LoggerSvc);
        });

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

        serviceTestHelper.runDefaultTests(logsCmd, PlayerCommand.serviceName);
        commandHandlerTestHelper.runDefaultTests(logsCmd, 'logs');

        it('should collect the logs and send them to the server on the "logs" command', function(done) {
            var ajaxPromise = Promise.resolve();
            var listLogsPromise = Promise.resolve(['/log/file/foo', '/log/file/bar']);
            var file1Promise = Promise.resolve({});
            var file2Promise = Promise.resolve({});

            stubCsrf.$ajax.returns(ajaxPromise);
            stubStoreService.api.getState.returns({ statusmodel: { devicePostUrl: '/foo/bar/baz' } });
            stubLoggerService.api.listLogs.returns(listLogsPromise);
            stubLoggerService.api.getLogBlob.onFirstCall().returns(file1Promise);
            stubLoggerService.api.getLogBlob.onSecondCall().returns(file2Promise);

            logsCmd.handleCommand('logs');
            listLogsPromise
                .then(function() {
                    expect(stubLoggerService.api.listLogs).to.have.been.calledWith();
                })
                .then(Promise.all([file1Promise, file2Promise]))
                .then(function() {
                    expect(stubLoggerService.api.getLogBlob.firstCall).to.have.been.calledWith('/log/file/foo');
                    expect(stubLoggerService.api.getLogBlob.secondCall).to.have.been.calledWith('/log/file/bar');
                })
                .then(ajaxPromise)
                .then(function() {
                    expect(stubCsrf.$ajax).to.have.been.called;
                    done();
                });
        });

        it('throws an error if the Store service is not available', function() {
            ServiceAdmin.getService.withArgs(StoreSvc.serviceName).returns(null);
            stubStoreService.api.getState.returns({ statusmodel: { devicePostUrl: '/foo/bar/baz' } });
            expect(function() { logsCmd.handleCommand('logs'); }).to.throw();
            ServiceAdmin.getService.restore();
        });

        it('throws an error if the Logger service is not available', function() {
            ServiceAdmin.getService.withArgs(LoggerSvc.serviceName).returns(null);
            stubStoreService.api.getState.returns({ statusmodel: { devicePostUrl: '/foo/bar/baz' } });
            expect(function() { logsCmd.handleCommand('logs'); }).to.throw();
            ServiceAdmin.getService.restore();
        });

        it('throws an error if the devicePostUrl is missing in the state', function() {
            stubStoreService.api.getState.returns({});
            expect(function() { logsCmd.handleCommand('logs'); }).to.throw();
        });

    });
});