'use strict';

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

var _regenerator2 = _interopRequireDefault(_regenerator);

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

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

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

var _stringify2 = _interopRequireDefault(_stringify);

var _promise = require('babel-runtime/core-js/promise');

var _promise2 = _interopRequireDefault(_promise);

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

var _sinon = require('sinon');

var _sinon2 = _interopRequireDefault(_sinon);

var _assert = require('assert');

var _assert2 = _interopRequireDefault(_assert);

var _mocha = require('mocha');

var _crypto = require('crypto');

var _crypto2 = _interopRequireDefault(_crypto);

var _gtmGithubHook = require('../../../src/serverless/gtmGithubHook/gtmGithubHook.js');

var _gtmGithubHook2 = _interopRequireDefault(_gtmGithubHook);

var _gtmGithubUtils = require('../../../src/serverless/gtmGithubUtils.js');

var _gtmGithubUtils2 = _interopRequireDefault(_gtmGithubUtils);

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

(0, _mocha.describe)('gtmGithubHook', function () {
    (0, _mocha.describe)('decodeEventBody', function () {
        (0, _mocha.it)('should remove prefix and parse body', function (done) {
            var expected = { action: 'test' };
            var event = {};
            event.body = 'payload=%7B%22action%22%3A%20%22test%22%7D';

            var actual = _gtmGithubHook2.default.decodeEventBody(event);
            _assert2.default.equal(actual.action, expected.action);
            done();
        });
    });
    (0, _mocha.describe)('listener', function () {
        (0, _mocha.it)('should run', function (done) {
            var event = {};
            event.type = 'pull_request';
            event.body = 'payload=%7B%22action%22%3A%20%22test%22%7D';

            var key = 'abc';
            process.env.GTM_GITHUB_WEBHOOK_SECRET = key;
            var sig = 'sha1=' + _crypto2.default.createHmac('sha1', key).update(event.body, 'utf-8').digest('hex');

            event.headers = {
                'X-Hub-Signature': sig,
                'X-GitHub-Event': 'test',
                'X-GitHub-Delivery': 'test'
            };

            _gtmGithubHook2.default.listener(event, null, function () {});
            _assert2.default.equal('1', '1'); //todo
            done();
        });
    });
    (0, _mocha.describe)('handleEvent', function () {
        (0, _mocha.it)('should fire', function (done) {
            var type = 'pull_request';
            var body = {
                pull_request: {
                    ref: 'sha123',
                    head: {
                        repo: {
                            name: 'code',
                            owner: {
                                login: 'bob'
                            }
                        }
                    }
                }
            };

            _gtmGithubHook2.default.handleEvent(type, body);
            _assert2.default.equal(1, 1); //todo
            done();
        });
    });

    (0, _mocha.describe)('getTaskConfig', function () {
        var config = {
            pull_request: {
                tasks: [{
                    executor: 'jenkins',
                    context: 'functional',
                    options: {
                        tags: ['@smoke'],
                        browsers: ['chrome']
                    }
                }]
            }
        };

        (0, _mocha.before)(function () {
            _sinon2.default.stub(_gtmGithubUtils2.default, 'getFile').callsFake(function () {
                return _promise2.default.resolve({
                    data: {
                        content: Buffer.from((0, _stringify2.default)(config)).toString('base64')
                    }
                });
            });
        });

        (0, _mocha.after)(function () {
            _gtmGithubUtils2.default.getFile.restore();
        });

        (0, _mocha.it)('should extract json from github file response', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
            var body, actual;
            return _regenerator2.default.wrap(function _callee$(_context) {
                while (1) {
                    switch (_context.prev = _context.next) {
                        case 0:
                            body = {
                                pull_request: {
                                    ref: 'sha123',
                                    head: {
                                        repo: {
                                            name: 'code',
                                            owner: {
                                                login: 'bob'
                                            }
                                        }
                                    }
                                }
                            };

                            //gtmGithubHook.setUtils(githubUtils);

                            _context.next = 3;
                            return _gtmGithubHook2.default.getTaskConfig('pull_request', body);

                        case 3:
                            actual = _context.sent;
                            return _context.abrupt('return', _assert2.default.equal(actual.pull_request.tasks[0].executor, 'jenkins'));

                        case 5:
                        case 'end':
                            return _context.stop();
                    }
                }
            }, _callee, this);
        })));
    });
});