'use strict';

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

var _stringify2 = _interopRequireDefault(_stringify);

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

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_CRYPT_GITHUB_WEBHOOK_SECRET;
        process.env.GTM_CRYPT_GITHUB_WEBHOOK_SECRET = 'squirrel';
    });

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

    var message = void 0;
    (0, _mocha.beforeEach)(function () {
        process.env.GTM_AWS_KMS_KEY_ID = '';
        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', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
            return _regenerator2.default.wrap(function _callee$(_context) {
                while (1) {
                    switch (_context.prev = _context.next) {
                        case 0:
                            _context.prev = 0;

                            delete message.MessageAttributes.ghEventId;
                            _context.next = 4;
                            return _Event.Event.validateMessage(message);

                        case 4:
                            _context.next = 9;
                            break;

                        case 6:
                            _context.prev = 6;
                            _context.t0 = _context['catch'](0);

                            _assert2.default.equal(_context.t0.message, 'No Message Attribute \'ghEventId\' in Message - discarding Event!');

                        case 9:
                        case 'end':
                            return _context.stop();
                    }
                }
            }, _callee, this, [[0, 6]]);
        })));

        (0, _mocha.it)('should return message attributes', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
            var expected, actual;
            return _regenerator2.default.wrap(function _callee2$(_context2) {
                while (1) {
                    switch (_context2.prev = _context2.next) {
                        case 0:
                            expected = JSON.parse(_fs2.default.readFileSync(__dirname + '/../fixtures/githubEventAttributes.json', 'utf-8'));
                            _context2.next = 3;
                            return _Event.Event.validateMessage(message);

                        case 3:
                            actual = _context2.sent;


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

                        case 5:
                        case 'end':
                            return _context2.stop();
                    }
                }
            }, _callee2, this);
        })));
    });

    (0, _mocha.describe)('checkEventSignature', function () {
        (0, _mocha.it)('should sign message with github webhook secret', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() {
            var signature, result;
            return _regenerator2.default.wrap(function _callee3$(_context3) {
                while (1) {
                    switch (_context3.prev = _context3.next) {
                        case 0:
                            signature = message.MessageAttributes.ghEventSignature.StringValue;
                            _context3.next = 3;
                            return _Event.Event.checkEventSignature(signature, message);

                        case 3:
                            result = _context3.sent;


                            _assert2.default.equal(result, true);

                        case 5:
                        case 'end':
                            return _context3.stop();
                    }
                }
            }, _callee3, this);
        })));
    });

    (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', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
            var expected, attrs, actual;
            return _regenerator2.default.wrap(function _callee4$(_context4) {
                while (1) {
                    switch (_context4.prev = _context4.next) {
                        case 0:
                            expected = JSON.parse(_fs2.default.readFileSync(__dirname + '/../fixtures/githubEventPayload.json', 'utf-8'));
                            _context4.next = 3;
                            return _Event.Event.validateMessage(message);

                        case 3:
                            attrs = _context4.sent;
                            actual = _Event.Event.prepareEventPayload(message, attrs);


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

                        case 6:
                        case 'end':
                            return _context4.stop();
                    }
                }
            }, _callee4, this);
        })));
    });
});