'use strict';

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 _mocha = require('mocha');

var _assert = require('assert');

var _assert2 = _interopRequireDefault(_assert);

var _crypto = require('crypto');

var _crypto2 = _interopRequireDefault(_crypto);

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

var _gtmGithubUtils2 = _interopRequireDefault(_gtmGithubUtils);

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

process.env.GTM_GITHUB_TOKEN = '';

(0, _mocha.describe)('gtmGithubUtils', function () {
    (0, _mocha.describe)('connect', function () {
        (0, _mocha.it)('should throw without creds', function (done) {
            _assert2.default.throws(_gtmGithubUtils2.default.connect, Error);
            done();
        });
    });

    (0, _mocha.describe)('signRequestBody', function () {
        (0, _mocha.it)('should encrypt correctly', function (done) {
            var key = 'abc';
            var body = 'def';

            var expected = 'sha1=' + _crypto2.default.createHmac('sha1', key).update(body, 'utf-8').digest('hex');

            var actual = _gtmGithubUtils2.default.signRequestBody(key, body);
            _assert2.default.equal(actual, expected);
            done();
        });
    });

    (0, _mocha.describe)('invalidHook', function () {
        (0, _mocha.it)('should return error if event header missing', function (done) {
            process.env.GTM_GITHUB_WEBHOOK_SECRET = 'abc';

            var event = {
                body: 'testing',
                headers: {
                    'X-Hub-Signature': 'test'
                }
            };

            var expected = 'Error: No X-Github-Event found on request';
            var actual = _gtmGithubUtils2.default.invalidHook(event);
            _assert2.default.equal(actual, expected);
            done();
        });
    });

    (0, _mocha.describe)('decodeFileResponse', function () {
        (0, _mocha.it)('should base64 decode', function (done) {
            var fileResponse = {
                data: {
                    content: 'eyJhYmMiOiAidGhpcyBpcyBhIHRlc3QifQo=' //base64 { abc: 'this is a test' }
                }
            };

            var actual = _gtmGithubUtils2.default.decodeFileResponse(fileResponse);
            _assert2.default.equal(actual.abc, 'this is a test');
            done();
        });
    });

    (0, _mocha.describe)('updateGitHubPullRequest', function () {
        (0, _mocha.it)('should throw exception when not logged in', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
            var message, actual;
            return _regenerator2.default.wrap(function _callee$(_context) {
                while (1) {
                    switch (_context.prev = _context.next) {
                        case 0:
                            message = {
                                Body: '{"context": "test", "eventData": { "ghEventId": "abc", "ghEventType": "pull_request"}}'
                            };
                            actual = void 0;
                            _context.prev = 2;
                            _context.next = 5;
                            return _gtmGithubUtils2.default.handleEventTaskResult(message);

                        case 5:
                            actual = _context.sent;

                            console.log(actual);
                            _context.next = 12;
                            break;

                        case 9:
                            _context.prev = 9;
                            _context.t0 = _context['catch'](2);
                            return _context.abrupt('return', _assert2.default.equal(_context.t0.message, 'OAuth2 authentication requires a token or key & secret to be set'));

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

    (0, _mocha.describe)('getFile', function () {
        (0, _mocha.it)('should throw exception when not logged in', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
            var actual;
            return _regenerator2.default.wrap(function _callee2$(_context2) {
                while (1) {
                    switch (_context2.prev = _context2.next) {
                        case 0:
                            actual = void 0;
                            _context2.prev = 1;
                            _context2.next = 4;
                            return _gtmGithubUtils2.default.getFile();

                        case 4:
                            actual = _context2.sent;

                            console.log(actual);
                            _context2.next = 11;
                            break;

                        case 8:
                            _context2.prev = 8;
                            _context2.t0 = _context2['catch'](1);
                            return _context2.abrupt('return', _assert2.default.equal('OAuth2 authentication requires a token or key & secret to be set', _context2.t0.message));

                        case 11:
                        case 'end':
                            return _context2.stop();
                    }
                }
            }, _callee2, this, [[1, 8]]);
        })));
    });
});