"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 _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)('LaunchDarklyUtilsRoles', 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)('getCustomRoles', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["default"])('https://app.launchdarkly.com').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 _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/custom-roles-list.json', 'utf-8'));
              return _context2.abrupt("return", ldutils.roles.getCustomRoles().then(function (actual) {
                (0, _chai.expect)(actual).to.deep.equal(expected);
              }));

            case 2:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
  });
  (0, _mocha.describe)('getCustomRole', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["default"])('https://app.launchdarkly.com').get('/api/v2/roles/example-role').replyWithFile(200, __dirname + '/fixtures/custom-roles-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/custom-roles-get.json', 'utf-8'));
              return _context3.abrupt("return", ldutils.roles.getCustomRole('example-role').then(function (actual) {
                (0, _chai.expect)(actual).to.deep.equal(expected);
              }));

            case 2:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    })));
  });
  (0, _mocha.describe)('getCustomRoleById', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["default"])('https://app.launchdarkly.com').get('/api/v2/roles').replyWithFile(200, __dirname + '/fixtures/custom-roles-list.json', {
        'Content-Type': 'application/json'
      }).get('/api/v2/roles/example-role').replyWithFile(200, __dirname + '/fixtures/custom-roles-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 _callee4() {
      var expected;
      return _regenerator["default"].wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              expected = JSON.parse(_fs["default"].readFileSync(__dirname + '/fixtures/custom-roles-get.json', 'utf-8'));
              return _context4.abrupt("return", ldutils.roles.getCustomRoleById('5a593f890z875421af55d96e').then(function (actual) {
                (0, _chai.expect)(actual).to.deep.equal(expected);
              }));

            case 2:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    })));
  });
  (0, _mocha.describe)('createCustomRole', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["default"])('https://app.launchdarkly.com').post('/api/v2/roles').replyWithFile(200, __dirname + '/fixtures/custom-roles-create.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 expected;
      return _regenerator["default"].wrap(function _callee5$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              expected = JSON.parse(_fs["default"].readFileSync(__dirname + '/fixtures/custom-roles-create.json', 'utf-8'));
              return _context5.abrupt("return", ldutils.roles.createCustomRole('test-role', 'test role', [{
                resources: ['proj/*:env/production'],
                actions: ['*'],
                effect: 'allow'
              }], 'Allow access to production').then(function (actual) {
                (0, _chai.expect)(actual).to.deep.equal(expected);
              }));

            case 2:
            case "end":
              return _context5.stop();
          }
        }
      }, _callee5);
    })));
  });
  (0, _mocha.describe)('updateCustomRole', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["default"])('https://app.launchdarkly.com').get('/api/v2/roles/sample-role').replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
        'Content-Type': 'application/json'
      }).patch('/api/v2/roles/sample-role').replyWithFile(200, __dirname + '/fixtures/custom-roles-update.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/custom-roles-update.json', 'utf-8'));
              return _context6.abrupt("return", ldutils.roles.updateCustomRole('sample-role', 'sample-role', [{
                resources: ['proj/*:env/production'],
                actions: ['*'],
                effect: 'allow'
              }], 'Allow access to production').then(function (actual) {
                (0, _chai.expect)(actual).to.deep.equal(expected);
              }));

            case 2:
            case "end":
              return _context6.stop();
          }
        }
      }, _callee6);
    })));
  });
  (0, _mocha.describe)('upsertCustomRole', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["default"])('https://app.launchdarkly.com') // update
      .get('/api/v2/roles/example-role').replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
        'Content-Type': 'application/json'
      }).get('/api/v2/roles/example-role').replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
        'Content-Type': 'application/json'
      }).patch('/api/v2/roles/example-role').replyWithFile(200, __dirname + '/fixtures/custom-roles-update.json', {
        'Content-Type': 'application/json'
      }) // create
      .get('/api/v2/roles/example-role').reply(404, {
        error: 'Not Found'
      }, {
        'Content-Type': 'application/json'
      }).post('/api/v2/roles').replyWithFile(200, __dirname + '/fixtures/custom-roles-create.json', {
        'Content-Type': 'application/json'
      });
      (0, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should patch existing role if found', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7() {
      var expected;
      return _regenerator["default"].wrap(function _callee7$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              expected = JSON.parse(_fs["default"].readFileSync(__dirname + '/fixtures/custom-roles-update.json', 'utf-8'));
              return _context7.abrupt("return", ldutils.roles.upsertCustomRole('example-role', 'example role', [{
                resources: ['proj/*:env/production'],
                actions: ['*'],
                effect: 'allow'
              }], 'Allow access to production').then(function (actual) {
                (0, _chai.expect)(actual).to.deep.equal(expected);
              }));

            case 2:
            case "end":
              return _context7.stop();
          }
        }
      }, _callee7);
    })));
    (0, _mocha.it)('should create new role if not found', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8() {
      var expected;
      return _regenerator["default"].wrap(function _callee8$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              expected = JSON.parse(_fs["default"].readFileSync(__dirname + '/fixtures/custom-roles-create.json', 'utf-8'));
              return _context8.abrupt("return", ldutils.roles.upsertCustomRole('example-role', 'example role', [{
                resources: ['proj/*:env/production'],
                actions: ['*'],
                effect: 'allow'
              }], 'Allow access to production').then(function (actual) {
                (0, _chai.expect)(actual).to.deep.equal(expected);
              }));

            case 2:
            case "end":
              return _context8.stop();
          }
        }
      }, _callee8);
    })));
  });
  (0, _mocha.describe)('bulkUpsertCustomRoles', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["default"])('https://app.launchdarkly.com') // update
      .get('/api/v2/roles/test-role-one').replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
        'Content-Type': 'application/json'
      }).get('/api/v2/roles/test-role-one').replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
        'Content-Type': 'application/json'
      }).patch('/api/v2/roles/test-role-one').replyWithFile(200, __dirname + '/fixtures/custom-roles-update.json', {
        'Content-Type': 'application/json'
      }).get('/api/v2/roles/test-role-two').replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
        'Content-Type': 'application/json'
      }).get('/api/v2/roles/test-role-two').replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
        'Content-Type': 'application/json'
      }).patch('/api/v2/roles/test-role-two').replyWithFile(200, __dirname + '/fixtures/custom-roles-update.json', {
        'Content-Type': 'application/json'
      }) // create
      .get('/api/v2/roles/test-role-three').reply(404, {
        error: 'Not Found'
      }, {
        'Content-Type': 'application/json'
      }).post('/api/v2/roles').replyWithFile(200, __dirname + '/fixtures/custom-roles-create.json', {
        'Content-Type': 'application/json'
      });
      (0, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should operate on json bulkload file', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9() {
      var expected;
      return _regenerator["default"].wrap(function _callee9$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              expected = JSON.parse(_fs["default"].readFileSync(__dirname + '/fixtures/custom-roles-bulk-load-result.json', 'utf-8'));
              return _context9.abrupt("return", ldutils.roles.bulkUpsertCustomRoles(__dirname + '/fixtures/custom-roles-bulk-load-file.json').then(function (actual) {
                (0, _chai.expect)(actual).to.deep.equal(expected);
              }));

            case 2:
            case "end":
              return _context9.stop();
          }
        }
      }, _callee9);
    })));
  });
  (0, _mocha.describe)('bulkUpsertCustomRoleFolder', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["default"])('https://app.launchdarkly.com') // update
      .get('/api/v2/roles/test-role-one').replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
        'Content-Type': 'application/json'
      }).get('/api/v2/roles/test-role-one').replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
        'Content-Type': 'application/json'
      }).patch('/api/v2/roles/test-role-one').replyWithFile(200, __dirname + '/fixtures/custom-roles-update.json', {
        'Content-Type': 'application/json'
      }).get('/api/v2/roles/test-role-two').replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
        'Content-Type': 'application/json'
      }).get('/api/v2/roles/test-role-two').replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
        'Content-Type': 'application/json'
      }).patch('/api/v2/roles/test-role-two').replyWithFile(200, __dirname + '/fixtures/custom-roles-update.json', {
        'Content-Type': 'application/json'
      }) // create
      .get('/api/v2/roles/test-role-three').reply(404, {
        error: 'Not Found'
      }, {
        'Content-Type': 'application/json'
      }).post('/api/v2/roles').replyWithFile(200, __dirname + '/fixtures/custom-roles-create.json', {
        'Content-Type': 'application/json'
      });
      (0, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should operate on json bulkload file', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee10() {
      var expected;
      return _regenerator["default"].wrap(function _callee10$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              expected = JSON.parse(_fs["default"].readFileSync(__dirname + '/fixtures/custom-roles-bulk-load-result.json', 'utf-8'));
              return _context10.abrupt("return", ldutils.roles.bulkUpsertCustomRoleFolder(__dirname + '/fixtures/bulkroles').then(function (actual) {
                (0, _chai.expect)(actual).to.deep.equal(expected);
              }));

            case 2:
            case "end":
              return _context10.stop();
          }
        }
      }, _callee10);
    })));
  });
});