recruter/test/team.test.ts

80 lines
4.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, it } from "vitest";
import { buildApp } from "../src/app.js";
import { saveApplication } from "../src/applications.js";
import { openDb } from "../src/db.js";
function setup(apiToken = "t0ken", bot?: string) {
const db = openDb();
saveApplication(db, { tgUserId: 1, tgUsername: "aigerim", fullName: "Айгерим Сапарова", phone: "77011234567", city: "kokshetau", source: "hh", lang: "ru" });
saveApplication(db, { tgUserId: 2, fullName: "Ерлан Ахметов", phone: "77019998877", city: "taldykorgan", source: "enbek", lang: "ru" });
return buildApp({ db, apiToken, botUsername: () => bot });
}
const login = async (a: ReturnType<typeof setup>, key = "t0ken") =>
a.inject({ method: "POST", url: "/team/login", headers: { "content-type": "application/x-www-form-urlencoded" }, payload: `key=${encodeURIComponent(key)}` });
const cookieOf = (res: { headers: Record<string, unknown> }) => String(res.headers["set-cookie"]).split(";")[0]!;
describe("team-applications", () => {
it("Scenario: Рекрутер видит отклик и источник", async () => {
const a = setup();
const res = await login(a);
expect(res.statusCode).toBe(303);
expect(String(res.headers["set-cookie"])).toContain("HttpOnly");
expect(String(res.headers["set-cookie"])).not.toContain("t0ken");
const page = await a.inject({ method: "GET", url: "/team", headers: { cookie: cookieOf(res) } });
expect(page.statusCode).toBe(200);
for (const s of ["Айгерим Сапарова", "+77011234567", "Кокшетау", "Астана", "hh", "@aigerim"]) expect(page.body).toContain(s);
});
it("Scenario: Без входа", async () => {
const page = await setup().inject({ method: "GET", url: "/team" });
expect(page.body).toContain("Ключ доступа");
expect(page.body).not.toContain("Айгерим Сапарова");
const forged = await setup().inject({ method: "GET", url: "/team", headers: { cookie: "team_session=abc" } });
expect(forged.body).not.toContain("Айгерим Сапарова");
});
it("Scenario: Неверный ключ", async () => {
const res = await login(setup(), "wrong");
expect(res.statusCode).toBe(401);
expect(res.body).toContain("Ключ не подошёл. Проверьте его и попробуйте ещё раз.");
});
it("без ключа доступа вход невозможен", async () => {
const a = setup("");
expect((await a.inject({ method: "GET", url: "/team" })).body).toContain("ключ доступа ещё не выдан");
expect((await login(a, "")).statusCode).toBe(401);
});
it("Scenario: Отклики через API с фильтром по офису", async () => {
const a = setup();
const res = await a.inject({ method: "GET", url: "/api/applications?office=almaty", headers: { authorization: "Bearer t0ken" } });
expect(res.json()).toMatchObject([{ fullName: "Ерлан Ахметов", office: "almaty", source: "enbek" }]);
expect(res.json()).toHaveLength(1);
expect((await a.inject({ method: "GET", url: "/api/applications" })).statusCode).toBe(401);
});
it("фильтр по офису на странице команды", async () => {
const a = setup();
const page = await a.inject({ method: "GET", url: "/team?office=astana", headers: { cookie: cookieOf(await login(a)) } });
expect(page.body).toContain("Айгерим Сапарова");
expect(page.body).not.toContain("Ерлан Ахметов");
});
it("Scenario: Ссылки с источником", async () => {
const a = setup("t0ken", "kt_intern_bot");
const page = await a.inject({ method: "GET", url: "/team", headers: { cookie: cookieOf(await login(a)) } });
for (const s of ["qsamruk", "enbek", "hh"]) expect(page.body).toContain(`https://t.me/kt_intern_bot?start=${s}`);
});
it("Scenario: Бот не подключён", async () => {
const a = setup();
const page = await a.inject({ method: "GET", url: "/team", headers: { cookie: cookieOf(await login(a)) } });
expect(page.body).toContain("Ссылки появятся, когда бот будет подключён");
});
it("выход сбрасывает вход", async () => {
const res = await setup().inject({ method: "POST", url: "/team/logout" });
expect(String(res.headers["set-cookie"])).toContain("Max-Age=0");
});
});