'use strict';

var _stringify = require('babel-runtime/core-js/json/stringify');

var _stringify2 = _interopRequireDefault(_stringify);

require('source-map-support/register');

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _mocha = require('mocha');

var _assert = require('assert');

var _assert2 = _interopRequireDefault(_assert);

var _Event = require('../../src/agent/Event');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

(0, _mocha.describe)('Event', function () {
    var temp = void 0;
    (0, _mocha.before)(function () {
        temp = process.env.GTM_GITHUB_WEBHOOK_SECRET;
        process.env.GTM_GITHUB_WEBHOOK_SECRET = 'squirrel';
    });

    (0, _mocha.after)(function () {
        process.env.GTM_GITHUB_WEBHOOK_SECRET = temp;
    });

    var message = void 0;
    (0, _mocha.beforeEach)(function () {
        message = JSON.parse(_fs2.default.readFileSync(__dirname + '/../fixtures/githubMessage.json', 'utf-8'));
    });

    (0, _mocha.describe)('requiredAttributes', function () {
        (0, _mocha.it)('should contain expected values', function () {
            var expected = ['ghEventId', 'ghEventType', 'ghAgentGroup', 'ghTaskConfig', 'ghEventSignature'];

            var actual = _Event.Event.requiredAttributes;

            for (var i = 1; i < 5; i++) {
                _assert2.default.equal(actual[i], expected[i]);
            }
        });
    });

    (0, _mocha.describe)('validateMessage', function () {
        (0, _mocha.beforeEach)(function () {
            message = JSON.parse(_fs2.default.readFileSync(__dirname + '/../fixtures/githubMessage.json', 'utf-8'));
        });

        (0, _mocha.it)('should throw when message has missing attribute', function () {
            try {
                delete message.MessageAttributes.ghEventId;
                _Event.Event.validateMessage(message);
            } catch (e) {
                _assert2.default.equal(e.message, 'No Message Attribute \'ghEventId\' in Message - discarding Event!');
            }
        });

        (0, _mocha.it)('should return message attributes', function () {
            var expected = JSON.parse(_fs2.default.readFileSync(__dirname + '/../fixtures/githubEventAttributes.json', 'utf-8'));

            var actual = _Event.Event.validateMessage(message);

            _assert2.default.equal((0, _stringify2.default)(actual), (0, _stringify2.default)(expected));
        });
    });

    (0, _mocha.describe)('checkEventSignature', function () {
        (0, _mocha.it)('should sign message with github webhook secret', function () {
            var signature = message.MessageAttributes.ghEventSignature.StringValue;
            var result = _Event.Event.checkEventSignature(signature, message);

            _assert2.default.equal(result, true);
        });
    });

    (0, _mocha.describe)('buildCheckObject', function () {
        (0, _mocha.it)('should create the expected check object from message', function () {
            var expected = JSON.parse(_fs2.default.readFileSync(__dirname + '/../fixtures/githubMessageCheck.json', 'utf-8'));

            var actual = _Event.Event.buildCheckObject(message);

            _assert2.default.equal((0, _stringify2.default)(actual), (0, _stringify2.default)(expected));
        });
    });

    (0, _mocha.describe)('prepareEventPayload', function () {
        (0, _mocha.it)('should create the expected payload from message and attributes', function () {
            var expected = JSON.parse(_fs2.default.readFileSync(__dirname + '/../fixtures/githubEventPayload.json', 'utf-8'));

            var attrs = _Event.Event.validateMessage(message);
            var actual = _Event.Event.prepareEventPayload(message, attrs);

            _assert2.default.equal((0, _stringify2.default)(actual), (0, _stringify2.default)(expected));
        });
    });
});