UNPKG

kuzzle-sdk

Version:
81 lines 2.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const cucumber_1 = require("@cucumber/cucumber"); const retry_1 = __importDefault(require("retry")); const should_1 = __importDefault(require("should")); (0, cucumber_1.Given)("I log in as {string}:{string}", async function (username, password) { try { const jwt = await this.kuzzle.auth.login("local", { username, password }); this.jwt = jwt; } catch (error) { this.jwt = "invalid"; this.error = error; } }); (0, cucumber_1.When)("I logout", async function () { await this.kuzzle.auth.logout(); }); (0, cucumber_1.When)("I refresh the JWT", async function () { this.previousJwt = this.jwt; // we have to wait for at least 1s: if we ask Kuzzle to refresh a JWT that // has been generated during the same second, then the new JWT will be // identical to the one being refreshed await new Promise((resolve, reject) => { setTimeout(() => { this.kuzzle.auth .refreshToken() .then((token) => { this.jwt = token.jwt; resolve(); }) .catch((err) => reject(err)); }, 1000); }); }); (0, cucumber_1.Then)("the previous JWT is now invalid", async function () { // prevent false positives, just in case (0, should_1.default)(this.previousJwt).be.a.String().and.not.empty(); (0, should_1.default)(this.previousJwt).not.eql(this.jwt); const op = retry_1.default.operation({ retries: 10, minTimeout: 500, factor: 1 }); await new Promise((resolve, reject) => { op.attempt(() => { this.kuzzle.auth .checkToken(this.previousJwt) .then((response) => { const err = response.valid ? new Error("Unexpected valid token") : null; if (op.retry(err)) { return; } if (err) { reject(err); } else { resolve(); } }) .catch((err) => reject(err)); }); }); }); (0, cucumber_1.Then)(/^the JWT is (in)?valid$/, async function (not) { const response = await this.kuzzle.auth.checkToken(this.jwt); if (not) { (0, should_1.default)(response.valid).be.false(); } else { (0, should_1.default)(response.valid).be.true(); } }); (0, cucumber_1.Given)("I get my rights", async function () { this.rights = await this.kuzzle.auth.getMyRights(); }); (0, cucumber_1.Then)("I have a vector with {int} rights", function (nbRights) { (0, should_1.default)(this.rights).have.length(nbRights); }); //# sourceMappingURL=auth.js.map