From d03f0ae2c0a32e68ace455af05cf8ed0a54a84b0 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Wed, 12 Jul 2023 10:57:57 +0200 Subject: [PATCH] Add missing tests to project and session routes Signed-off-by: Sebastian Martinez --- httpd-client/tests/project.test.ts | 219 +++++++++++++++++++++++++++-- httpd-client/tests/session.test.ts | 51 ++++++- 2 files changed, 251 insertions(+), 19 deletions(-) diff --git a/httpd-client/tests/project.test.ts b/httpd-client/tests/project.test.ts index 28e274c52..09a48ad8c 100644 --- a/httpd-client/tests/project.test.ts +++ b/httpd-client/tests/project.test.ts @@ -4,16 +4,26 @@ import { HttpdClient } from "@httpd-client"; import { aliceMainHead, aliceRemote, + bobRemote, + cobRid, sourceBrowsingRid, -} from "@tests/support/fixtures"; - -const api = new HttpdClient({ - hostname: "127.0.0.1", - port: 8080, - scheme: "http", -}); +} from "@tests/support/fixtures.js"; +import { + assertIssue, + assertPatch, + createIssueToBeModified, + createPatchToBeModified, +} from "@httpd-client/tests/support/support"; +import { authenticate } from "@httpd-client/tests/support/httpd.js"; +import { testFixture as testWithAPI } from "@httpd-client/tests/support/fixtures.js"; describe("project", () => { + const api = new HttpdClient({ + hostname: "127.0.0.1", + port: 8080, + scheme: "http", + }); + test("#getByDelegate(delegateId)", async () => { await api.project.getByDelegate(aliceRemote); }); @@ -76,12 +86,191 @@ describe("project", () => { await api.project.getCommitBySha(sourceBrowsingRid, aliceMainHead); }); - test.todo("#getDiff(id, revisionBase, revisionOid)"); - test.todo("#getIssueById(id, issueId)"); - test.todo("#getAllIssues(id)"); - test.todo("#createIssue(id, { title, description, assignees, tags })"); - test.todo("#updateIssue(id, issueId, issueUpdateAction, authToken)"); - test.todo("#getPatchById(id, patchId)"); - test.todo("#getAllPatches(id)"); - test.todo("#updatePatch(id, patchId, patchUpdateAction, authToken)"); + test("#getDiff(id, revisionBase, revisionOid)", async () => { + await api.project.getDiff( + sourceBrowsingRid, + "90f6d058ece12f75f349bc7bbe88142187fe0379", + aliceMainHead, + ); + }); + + test("#getIssueById(id, issueId)", async () => { + await api.project.getIssueById( + cobRid, + "4fc727e722d3979fd2073d9b56b2751658a4ae79", + ); + }); + + test("#getAllIssues(id)", async () => { + await api.project.getAllIssues(cobRid, { + page: 0, + perPage: 5, + state: "open", + }); + }); + + testWithAPI( + "#createIssue(id, { title, description, assignees, tags })", + async ({ httpd: { api, peer } }) => { + const sessionId = await authenticate(api, peer); + const { id: issueId } = await api.project.createIssue( + cobRid, + { + title: "aaa", + description: "bbb", + assignees: [], + tags: ["bug", "documentation"], + }, + sessionId, + ); + await assertIssue( + issueId, + { + title: "aaa", + discussion: [{ body: "bbb" }], + assignees: [], + tags: ["bug", "documentation"], + }, + api, + ); + }, + ); + + testWithAPI( + "#updateIssue(id, issueId, { type: 'edit' }, authToken)", + async ({ httpd: { api, peer } }) => { + const sessionId = await authenticate(api, peer); + const issueId = await createIssueToBeModified(api, sessionId); + await api.project.updateIssue( + cobRid, + issueId, + { type: "edit", title: "ccc" }, + sessionId, + ); + await assertIssue(issueId, { title: "ccc" }, api); + }, + ); + + testWithAPI( + "#updateIssue(id, issueId, { type: 'tag' }, authToken)", + async ({ httpd: { api, peer } }) => { + const sessionId = await authenticate(api, peer); + const issueId = await createIssueToBeModified(api, sessionId); + await api.project.updateIssue( + cobRid, + issueId, + { type: "tag", add: ["bug"], remove: [] }, + sessionId, + ); + await assertIssue(issueId, { tags: ["bug"] }, api); + }, + ); + + testWithAPI( + "#updateIssue(id, issueId, { type: 'assign' }, authToken)", + async ({ httpd: { api, peer } }) => { + const sessionId = await authenticate(api, peer); + const issueId = await createIssueToBeModified(api, sessionId); + const assignee = bobRemote.replace("did:key:", ""); + await api.project.updateIssue( + cobRid, + issueId, + { + type: "assign", + add: [assignee], + remove: [], + }, + sessionId, + ); + await assertIssue(issueId, { assignees: [`did:key:${assignee}`] }, api); + }, + ); + + testWithAPI( + "#updateIssue(id, issueId, { type: 'lifecycle' }, authToken)", + async ({ httpd: { api, peer } }) => { + const sessionId = await authenticate(api, peer); + const issueId = await createIssueToBeModified(api, sessionId); + await api.project.updateIssue( + cobRid, + issueId, + { type: "lifecycle", state: { status: "closed", reason: "solved" } }, + sessionId, + ); + await assertIssue( + issueId, + { + state: { status: "closed", reason: "solved" }, + }, + api, + ); + }, + ); + + test("#getPatchById(id, patchId)", async () => { + await api.project.getPatchById( + cobRid, + "013f8b2734df1840b2e33d52ff5632c8d66b199a", + ); + }); + + test("#getAllPatches(id)", async () => { + await api.project.getAllPatches(cobRid); + }); + + testWithAPI( + "#createPatch(id, patchCreate, authToken)", + async ({ httpd: { api, peer } }) => { + const sessionId = await authenticate(api, peer); + const { id: oid } = await api.project.createPatch( + cobRid, + { + title: "ppp", + description: "qqq", + target: "d7dd8cecae16b1108234e09dbdb5d64ae394bc25", + oid: "38c225e2a0b47ba59def211f4e4825c31d9463ec", + tags: [], + }, + sessionId, + ); + await assertPatch( + oid, + { + title: "ppp", + state: { status: "open" }, + target: "delegates", + tags: [], + revisions: [ + { + description: "qqq", + base: "d7dd8cecae16b1108234e09dbdb5d64ae394bc25", + oid: "38c225e2a0b47ba59def211f4e4825c31d9463ec", + }, + ], + }, + api, + ); + }, + ); + + testWithAPI( + "#updatePatch(id, patchId, { type: 'edit' }, authToken)", + async ({ httpd: { api, peer } }) => { + const sessionId = await authenticate(api, peer); + const patchId = await createPatchToBeModified(api, sessionId); + await api.project.updatePatch( + cobRid, + patchId, + { type: "tag", add: ["bug"], remove: [] }, + sessionId, + ); + await assertPatch( + patchId, + { + tags: ["bug"], + }, + api, + ); + }, + ); }); diff --git a/httpd-client/tests/session.test.ts b/httpd-client/tests/session.test.ts index a129391b4..1eb1c57d3 100644 --- a/httpd-client/tests/session.test.ts +++ b/httpd-client/tests/session.test.ts @@ -1,7 +1,50 @@ +import * as FsSync from "node:fs"; +import * as Path from "node:path"; import { describe, test } from "vitest"; -describe("session", () => { - test.todo("#getById(id)"); - test.todo("#update(id, {sig, pk})"); - test.todo("#delete(id)"); +import { HttpdClient } from "@httpd-client"; +import { authenticate } from "@httpd-client/tests/support/httpd.js"; +import { createPeerManager } from "@tests/support/peerManager.js"; +import { gitOptions } from "@tests/support/fixtures.js"; +import { sessionPayloadSchema } from "@httpd-client/lib/session"; +import { tmpDir } from "@tests/support/support.js"; + +describe("session", async () => { + const peerManager = await createPeerManager({ + dataDir: Path.resolve(Path.join(tmpDir, "peers")), + outputLog: FsSync.createWriteStream( + Path.join(tmpDir, "peerManager.log"), + ).setMaxListeners(16), + }); + const peer = await peerManager.startPeer({ + name: "session", + gitOptions: gitOptions["alice"], + }); + await peer.startHttpd(); + const api = new HttpdClient(peer.httpdBaseUrl); + + test("#getById(id)", async () => { + const id = await authenticate(api, peer); + await api.session.getById(id); + }); + + test("#update(id, {sig, pk})", async () => { + const { stdout } = await peer.rad(["web", "--backend", api.url, "--json"]); + const session = sessionPayloadSchema.safeParse(JSON.parse(stdout)); + + if (!session.success) { + throw new Error("Failed to parse session payload"); + } + + const { sessionId, signature, publicKey } = session.data; + await api.session.update(sessionId, { + sig: signature, + pk: publicKey, + }); + }); + + test("#delete(id)", async () => { + const id = await authenticate(api, peer); + await api.session.delete(id); + }); });