UNPKG

realm-object-server

Version:

Realm Object Server

97 lines 4.59 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const tmp = require("tmp"); const fs = require("fs-extra"); const path = require("path"); const TestServer_1 = require("../TestServer"); const Constants_1 = require("../shared/Constants"); describe("Server Admin.json Tests", function () { const resolveAdminJson = (dataPath) => { return path.resolve(dataPath, "keys", "admin.json"); }; const verifyValidAdminJson = (server) => __awaiter(this, void 0, void 0, function* () { const fileAsString = yield fs.readFile(resolveAdminJson(server.dataPath), "utf8"); const json = JSON.parse(fileAsString); const adminToken = json["ADMIN_TOKEN"]; chai_1.expect(adminToken).to.not.be.undefined; const parsedToken = server.tokenValidator.parse(adminToken); chai_1.expect(parsedToken.identity).to.equal(Constants_1.Constants.AdminUserId); }); const verifyNoAdminJson = (server) => __awaiter(this, void 0, void 0, function* () { const exists = yield fs.pathExists(resolveAdminJson(server.dataPath)); chai_1.expect(exists).to.be.false; }); const writeDummyAdminJson = (dataPath) => __awaiter(this, void 0, void 0, function* () { const adminJsonPath = resolveAdminJson(dataPath); yield fs.mkdirp(path.dirname(adminJsonPath)); yield fs.writeFile(adminJsonPath, "foo:bar"); }); const verifyDoesntOverwrireAdminJson = (server) => __awaiter(this, void 0, void 0, function* () { const fileAsString = yield fs.readFile(resolveAdminJson(server.dataPath), "utf8"); chai_1.expect(fileAsString).to.equal("foo:bar"); }); const runServer = (config, validator) => __awaiter(this, void 0, void 0, function* () { const server = new TestServer_1.TestServer(); try { yield server.start(config); yield validator(server); } finally { yield server.shutdown(); } }); describe("when keys are provided", () => { it("should generate admin.json", () => __awaiter(this, void 0, void 0, function* () { yield runServer({}, verifyValidAdminJson); })); it("should not generate admin.json if disabled", () => __awaiter(this, void 0, void 0, function* () { yield runServer({ writeAdminTokenToJson: false }, verifyNoAdminJson); })); it("should not override existing admin.json", () => __awaiter(this, void 0, void 0, function* () { const tmpDir = tmp.dirSync(); yield writeDummyAdminJson(tmpDir.name); yield runServer({ dataPath: tmpDir.name }, verifyDoesntOverwrireAdminJson); })); }); describe("when keys are not provided", () => { it("should generate admin.json", () => __awaiter(this, void 0, void 0, function* () { yield runServer({ autoKeyGen: true, publicKeyPath: null, privateKeyPath: null }, verifyValidAdminJson); })); it("should not generate admin.json if disabled", () => __awaiter(this, void 0, void 0, function* () { yield runServer({ autoKeyGen: true, publicKeyPath: null, privateKeyPath: null, writeAdminTokenToJson: false }, verifyNoAdminJson); })); it("should not override existing admin.json", () => __awaiter(this, void 0, void 0, function* () { const tmpDir = tmp.dirSync(); yield writeDummyAdminJson(tmpDir.name); yield runServer({ autoKeyGen: true, publicKeyPath: null, privateKeyPath: null, dataPath: tmpDir.name }, verifyDoesntOverwrireAdminJson); })); }); }); //# sourceMappingURL=server-write-admin-token-tests.spec.js.map