'use strict';

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

var _regenerator2 = _interopRequireDefault(_regenerator);

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

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _chai = require('chai');

var _assert = require('assert');

var _assert2 = _interopRequireDefault(_assert);

var _mocha = require('mocha');

var _nock = require('nock');

var _nock2 = _interopRequireDefault(_nock);

var _LaunchDarklyUtils = require('../src/LaunchDarklyUtils');

var _LaunchDarklyLogger = require('../src/LaunchDarklyLogger');

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

var log = _LaunchDarklyLogger.LaunchDarklyLogger.logger();

(0, _mocha.describe)('LaunchDarklyUtilsMembers', function () {
    var _this = this;

    var ldutils = void 0;
    (0, _mocha.beforeEach)((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.next = 2;
                        return new _LaunchDarklyUtils.LaunchDarklyUtils().create('MOCK', log);

                    case 2:
                        ldutils = _context.sent;

                    case 3:
                    case 'end':
                        return _context.stop();
                }
            }
        }, _callee, _this);
    })));

    (0, _mocha.describe)('getTeamMembers', function () {
        (0, _mocha.before)(function (done) {
            var scope = (0, _nock2.default)('https://app.launchdarkly.com').get('/api/v2/members').replyWithFile(200, __dirname + '/fixtures/team-members-list.json', {
                'Content-Type': 'application/json'
            });
            (0, _assert2.default)(scope);
            done();
        });

        (0, _mocha.it)('should make expected api call and return results', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
            var expected;
            return _regenerator2.default.wrap(function _callee2$(_context2) {
                while (1) {
                    switch (_context2.prev = _context2.next) {
                        case 0:
                            expected = JSON.parse(_fs2.default.readFileSync(__dirname + '/fixtures/team-members-list.json', 'utf-8'));
                            return _context2.abrupt('return', ldutils.members.getTeamMembers().then(function (actual) {
                                (0, _chai.expect)(actual).to.deep.equal(expected);
                            }));

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

    (0, _mocha.describe)('getTeamMember', function () {
        (0, _mocha.before)(function (done) {
            var scope = (0, _nock2.default)('https://app.launchdarkly.com').get('/api/v2/members/5a3ad672761af020881a8814').replyWithFile(200, __dirname + '/fixtures/team-members-get.json', {
                'Content-Type': 'application/json'
            });
            (0, _assert2.default)(scope);
            done();
        });

        (0, _mocha.it)('should make expected api call and return results', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() {
            var expected;
            return _regenerator2.default.wrap(function _callee3$(_context3) {
                while (1) {
                    switch (_context3.prev = _context3.next) {
                        case 0:
                            expected = JSON.parse(_fs2.default.readFileSync(__dirname + '/fixtures/team-members-get.json', 'utf-8'));
                            return _context3.abrupt('return', ldutils.members.getTeamMember('5a3ad672761af020881a8814').then(function (actual) {
                                (0, _chai.expect)(actual).to.deep.equal(expected);
                            }));

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

    (0, _mocha.describe)('getTeamMemberByEmail', function () {
        (0, _mocha.before)(function (done) {
            var scope = (0, _nock2.default)('https://app.launchdarkly.com').get('/api/v2/members').replyWithFile(200, __dirname + '/fixtures/team-members-list.json', {
                'Content-Type': 'application/json'
            });
            (0, _assert2.default)(scope);
            done();
        });

        (0, _mocha.it)('should make expected api call and return results', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
            var memberList, expected;
            return _regenerator2.default.wrap(function _callee4$(_context4) {
                while (1) {
                    switch (_context4.prev = _context4.next) {
                        case 0:
                            memberList = JSON.parse(_fs2.default.readFileSync(__dirname + '/fixtures/team-members-list.json', 'utf-8'));
                            expected = _lodash2.default.filter(memberList.items, { email: 'Member.TheSecond@example.com' })[0];
                            return _context4.abrupt('return', ldutils.members.getTeamMemberByEmail('Member.TheSecond@example.com').then(function (actual) {
                                (0, _chai.expect)(actual).to.deep.equal(expected);
                            }));

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

    (0, _mocha.describe)('getTeamMemberCustomRoles', function () {
        (0, _mocha.before)(function (done) {
            var scope = (0, _nock2.default)('https://app.launchdarkly.com').get('/api/v2/members').replyWithFile(200, __dirname + '/fixtures/team-members-list.json', {
                'Content-Type': 'application/json'
            }).get('/api/v2/roles').replyWithFile(200, __dirname + '/fixtures/custom-roles-list.json', {
                'Content-Type': 'application/json'
            });
            (0, _assert2.default)(scope);
            done();
        });

        (0, _mocha.it)('should make expected api call and return results', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5() {
            var teamMembers, member, expected;
            return _regenerator2.default.wrap(function _callee5$(_context5) {
                while (1) {
                    switch (_context5.prev = _context5.next) {
                        case 0:
                            teamMembers = JSON.parse(_fs2.default.readFileSync(__dirname + '/fixtures/team-members-list.json', 'utf-8'));
                            member = _lodash2.default.filter(teamMembers.items, { email: 'Member.TheSecond@example.com' })[0];
                            expected = _lodash2.default.cloneDeep(member);

                            expected.customRoleKeys = ['example-role'];
                            return _context5.abrupt('return', ldutils.members.getTeamMemberCustomRoles('Member.TheSecond@example.com').then(function (actual) {
                                (0, _chai.expect)(actual).to.deep.equal(expected);
                            }));

                        case 5:
                        case 'end':
                            return _context5.stop();
                    }
                }
            }, _callee5, this);
        })));
    });
});