"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const runQuirrel_1 = require("./runQuirrel");
const fastify_1 = __importDefault(require("fastify"));
const delay_1 = __importDefault(require("delay"));
const secure_webhooks_1 = require("secure-webhooks");
const supertest_1 = __importDefault(require("supertest"));
const util_1 = require("../../client/test/util");
const passphrase = "hello";
(0, util_1.describeAcrossBackends)("Authenticated Jobs", (backend) => {
    let quirrel;
    let teardown;
    let server;
    let endpoint;
    let lastBody;
    let lastSignature;
    beforeAll(async () => {
        const res = await (0, runQuirrel_1.run)(backend, {
            passphrases: [passphrase],
        });
        quirrel = res.server;
        teardown = res.teardown;
        await res.redis.flushall();
        server = (0, fastify_1.default)();
        server.post("/", (request, reply) => {
            lastBody = request.body;
            lastSignature = request.headers["x-quirrel-signature"];
            reply.status(200).send("OK");
        });
        endpoint = encodeURIComponent(await server.listen({
            port: 0,
        }));
    });
    afterAll(async () => {
        await Promise.all([teardown(), server.close()]);
    });
    test("post a job", async () => {
        const { text: token } = await (0, supertest_1.default)(quirrel)
            .put("/tokens/testproject")
            .auth("ignored", passphrase)
            .expect(201);
        await (0, supertest_1.default)(quirrel)
            .post("/queues/" + endpoint)
            .send({
            body: JSON.stringify({ foo: "bar" }),
        })
            .expect(401);
        await (0, supertest_1.default)(quirrel)
            .post("/queues/" + endpoint)
            .auth(token, { type: "bearer" })
            .send({
            body: JSON.stringify({ foo: "bar" }),
        })
            .expect(201);
        await (0, delay_1.default)(300);
        expect(lastBody).toEqual('{"foo":"bar"}');
        expect(lastSignature).toMatch(/v=(\d+),d=([\da-f]+)/);
        expect((0, secure_webhooks_1.verify)(lastBody, token, lastSignature)).toBe(true);
        await (0, supertest_1.default)(quirrel)
            .post("/queues/" + endpoint)
            .auth(token, { type: "bearer" })
            .send({
            body: JSON.stringify({ foo: "long-standing" }),
            delay: 1000,
        })
            .expect(201);
        await (0, supertest_1.default)(quirrel)
            .delete("/tokens/non-existant")
            .auth("ignored", passphrase)
            .expect(404);
        await (0, supertest_1.default)(quirrel)
            .delete("/tokens/testproject")
            .auth("ignored", passphrase)
            .expect(204);
        await (0, supertest_1.default)(quirrel)
            .post("/queues/" + endpoint)
            .auth(token, { type: "bearer" })
            .send({
            body: JSON.stringify({ foo: "bar" }),
        })
            .expect(401);
        const { text: newToken } = await (0, supertest_1.default)(quirrel)
            .put("/tokens/testproject")
            .auth("ignored", passphrase)
            .expect(201);
        await (0, supertest_1.default)(quirrel)
            .get("/queues/" + endpoint)
            .auth(newToken, { type: "bearer" })
            .expect(200, { cursor: null, jobs: [] });
        await (0, supertest_1.default)(quirrel)
            .delete("/usage")
            .auth("ignored", passphrase)
            .expect(200, {
            testproject: 4, // one for the initial call, one for execution
        });
        await (0, supertest_1.default)(quirrel)
            .delete("/usage")
            .auth("ignored", passphrase)
            .expect(200, {});
    });
    test("admin impersonation", async () => {
        await (0, supertest_1.default)(quirrel)
            .delete("/usage")
            .auth("ignored", passphrase)
            .expect(200);
        const { text: token } = await (0, supertest_1.default)(quirrel)
            .put("/tokens/this.is.a.project")
            .auth("ignored", passphrase)
            .expect(201);
        await (0, supertest_1.default)(quirrel)
            .post("/queues/" + endpoint)
            .auth("this.is.a.project", passphrase)
            .send({
            body: JSON.stringify({ foo: "bar" }),
        })
            .expect(201);
        await (0, delay_1.default)(300);
        expect(lastBody).toEqual('{"foo":"bar"}');
        expect(lastSignature).toMatch(/v=(\d+),d=([\da-f]+)/);
        expect((0, secure_webhooks_1.verify)(lastBody, token, lastSignature)).toBe(true);
        await (0, supertest_1.default)(quirrel)
            .delete("/usage")
            .auth("ignored", passphrase)
            .expect(200, {
            // only one for execution
            "this.is.a.project": 1,
        });
        await (0, supertest_1.default)(quirrel)
            .post("/queues/" + endpoint)
            .set("x-quirrel-count-usage", "true")
            .auth("this.is.a.project", passphrase)
            .send({
            body: JSON.stringify({ foo: "bar" }),
        })
            .expect(201);
        await (0, delay_1.default)(300);
        await (0, supertest_1.default)(quirrel)
            .delete("/usage")
            .auth("ignored", passphrase)
            .expect(200, {
            // one for enqueueing, one for execution
            "this.is.a.project": 2,
        });
    });
});
//# sourceMappingURL=authenticated_jobs.test.js.map