92 lines
3.5 KiB
TypeScript
92 lines
3.5 KiB
TypeScript
import assert from "node:assert/strict";
|
||
import { after, before, describe, it } from "node:test";
|
||
import { COFFEE, startTestApp, type TestApp } from "./helpers.ts";
|
||
|
||
describe("auth-and-roles", () => {
|
||
let app: TestApp;
|
||
before(async () => {
|
||
app = await startTestApp();
|
||
});
|
||
after(() => app.close());
|
||
|
||
it("Scenario: Вход тестового жителя в среде разработки", async () => {
|
||
const res = await app.request("POST", "/api/auth/dev-login", {
|
||
body: { login: "demo-resident" },
|
||
});
|
||
assert.equal(res.status, 200);
|
||
assert.equal(typeof res.body.token, "string");
|
||
const me = await app.request("GET", "/api/me", { token: res.body.token });
|
||
assert.equal(me.status, 200);
|
||
assert.equal(me.body.role, "resident");
|
||
assert.equal(me.body.status, "verified");
|
||
});
|
||
|
||
it("Scenario: Тестовый вход в продакшене недоступен", async () => {
|
||
for (const appEnv of ["production", "staging"] as const) {
|
||
const prod = await startTestApp({ appEnv });
|
||
try {
|
||
const res = await prod.request("POST", "/api/auth/dev-login", {
|
||
body: { login: "demo-resident" },
|
||
});
|
||
assert.equal(res.status, 404, appEnv);
|
||
assert.equal(res.body.token, undefined);
|
||
} finally {
|
||
await prod.close();
|
||
}
|
||
}
|
||
});
|
||
|
||
it("Scenario: Неизвестный тестовый пользователь", async () => {
|
||
const res = await app.request("POST", "/api/auth/dev-login", { body: { login: "nobody" } });
|
||
assert.equal(res.status, 401);
|
||
assert.equal(res.body.error.code, "invalid_login");
|
||
});
|
||
|
||
it("Scenario: Запрос профиля без токена", async () => {
|
||
const res = await app.request("GET", "/api/me");
|
||
assert.equal(res.status, 401);
|
||
assert.equal(res.body.error.code, "unauthorized");
|
||
});
|
||
|
||
it("Scenario: Запрос профиля с неизвестным токеном", async () => {
|
||
const res = await app.request("GET", "/api/me", { token: "wrong-token" });
|
||
assert.equal(res.status, 401);
|
||
assert.equal(res.body.error.code, "unauthorized");
|
||
});
|
||
|
||
it("Scenario: Партнёр не может обменивать баллы", async () => {
|
||
const token = await app.login("demo-partner");
|
||
const res = await app.request("POST", `/api/rewards/${COFFEE}/redeem`, {
|
||
token,
|
||
headers: { "idempotency-key": "p-1" },
|
||
});
|
||
assert.equal(res.status, 403);
|
||
assert.equal(res.body.error.code, "forbidden");
|
||
});
|
||
|
||
it("Scenario: Житель не может погашать коды партнёра", async () => {
|
||
const token = await app.login("demo-resident");
|
||
const res = await app.request("POST", "/api/partner/redemptions/redeem-code", {
|
||
token,
|
||
body: { code: "TZK-AAAA-AAAA" },
|
||
});
|
||
assert.equal(res.status, 403);
|
||
assert.equal(res.body.error.code, "forbidden");
|
||
});
|
||
|
||
it("Scenario: Профиль жителя содержит код и статус", async () => {
|
||
const token = await app.login("demo-resident");
|
||
const me = await app.request("GET", "/api/me", { token });
|
||
assert.equal(me.body.residentCode, "R-DEMO01");
|
||
assert.equal(me.body.status, "verified");
|
||
});
|
||
|
||
it("Scenario: Заблокированный житель не может войти", async () => {
|
||
const res = await app.request("POST", "/api/auth/dev-login", {
|
||
body: { login: "demo-blocked" },
|
||
});
|
||
assert.equal(res.status, 403);
|
||
assert.equal(res.body.error.code, "user_blocked");
|
||
});
|
||
});
|