"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _fs = _interopRequireDefault(require("fs"));

var _lodash = _interopRequireDefault(require("lodash"));

var _chai = require("chai");

var _assert = _interopRequireDefault(require("assert"));

var _mocha = require("mocha");

var _nock = _interopRequireDefault(require("nock"));

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

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

var log = _LaunchDarklyLogger.LaunchDarklyLogger.logger();

(0, _mocha.describe)('LaunchDarklyUtilsMembers', function () {
  var ldutils;
  (0, _mocha.beforeEach)( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
    return _regenerator["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);
  })));
  (0, _mocha.describe)('getTeamMembers', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["default"])('https://app.launchdarkly.com').get('/api/v2/members').replyWithFile(200, __dirname + '/fixtures/team-members-list.json', {
        'Content-Type': 'application/json'
      });
      (0, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should make expected api call and return results', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
      var expected;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              expected = JSON.parse(_fs["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);
    })));
  });
  (0, _mocha.describe)('getTeamMember', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["default"])('https://app.launchdarkly.com').get('/api/v2/members/5a3ad672761af020881a8814').replyWithFile(200, __dirname + '/fixtures/team-members-get.json', {
        'Content-Type': 'application/json'
      });
      (0, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should make expected api call and return results', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
      var expected;
      return _regenerator["default"].wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              expected = JSON.parse(_fs["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);
    })));
  });
  (0, _mocha.describe)('getTeamMemberByEmail', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["default"])('https://app.launchdarkly.com').get('/api/v2/members').replyWithFile(200, __dirname + '/fixtures/team-members-list.json', {
        'Content-Type': 'application/json'
      });
      (0, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should make expected api call and return results', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4() {
      var memberList, expected;
      return _regenerator["default"].wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              memberList = JSON.parse(_fs["default"].readFileSync(__dirname + '/fixtures/team-members-list.json', 'utf-8'));
              expected = _lodash["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);
    })));
  });
  (0, _mocha.describe)('getTeamMemberCustomRoles', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["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, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should make expected api call and return results', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5() {
      var teamMembers, member, expected;
      return _regenerator["default"].wrap(function _callee5$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              teamMembers = JSON.parse(_fs["default"].readFileSync(__dirname + '/fixtures/team-members-list.json', 'utf-8'));
              member = _lodash["default"].filter(teamMembers.items, {
                email: 'Member.TheSecond@example.com'
              })[0];
              expected = _lodash["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);
    })));
  });
  (0, _mocha.describe)('inviteTeamMember', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["default"])('https://app.launchdarkly.com').post('/api/v2/members').replyWithFile(201, __dirname + '/fixtures/team-members-get.json', {
        'Content-Type': 'application/json'
      });
      (0, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should make expected api call and return results', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6() {
      var expected;
      return _regenerator["default"].wrap(function _callee6$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              expected = JSON.parse(_fs["default"].readFileSync(__dirname + '/fixtures/team-members-get.json', 'utf-8'));
              return _context6.abrupt("return", ldutils.members.inviteTeamMember('owner-sample-account@launchdarkly.com', 'owner').then(function (actual) {
                (0, _chai.expect)(actual).to.deep.equal(expected);
              }));

            case 2:
            case "end":
              return _context6.stop();
          }
        }
      }, _callee6);
    })));
  });
});