Skip to content

Commit

Permalink
draft changed to status
Browse files Browse the repository at this point in the history
  • Loading branch information
farzaneh-haghani committed Jun 1, 2024
1 parent dcc9e7c commit 549ff1c
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 135 deletions.
6 changes: 3 additions & 3 deletions client/setupTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ global.fetch = whatwgFetch;
export const resourceStub = (overrides = {}) => ({
accession: new Date(),
description: null,
draft: false,
status: "published",
id: randomUUID(),
publication: null,
publisher: null,
statusChangedDate: null,
statusChangedBy: null,
source: randomUUID(),
title: "",
topic: null,
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/Drafts/Drafts.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("Drafts", () => {
}),
http.patch("/api/resources/:id", ({ request: req }) => {
patchRequest = req;
return HttpResponse.json({ ...resource, draft: false });
return HttpResponse.json({ ...resource, status: "published" });
})
);
render(<Drafts />);
Expand Down
4 changes: 2 additions & 2 deletions client/src/pages/Drafts/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function Drafts() {

const publish = useCallback(
async (id) => {
await resourceService.publish(id);
await resourceService.action(id, "published");
await refreshDrafts();
},
[refreshDrafts, resourceService]
Expand All @@ -22,7 +22,7 @@ export default function Drafts() {
const reject = useCallback(
async (id) => {
if (window.confirm("Do you really want to reject?")) {
await resourceService.reject(id);
await resourceService.action(id, "rejected");
await refreshDrafts();
}
},
Expand Down
21 changes: 5 additions & 16 deletions client/src/services/resourceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class ResourceService {

async getDrafts() {
const res = await this.fetch(
`${ResourceService.ENDPOINT}?${new URLSearchParams({ draft: true })}`
`${ResourceService.ENDPOINT}?${new URLSearchParams({ status: "drafted" })}`
);
if (res.ok) {
const { resources } = await res.json();
Expand All @@ -30,9 +30,9 @@ export default class ResourceService {
}
}

async publish(id) {
async action(id, status) {
const res = await this.fetch(`${ResourceService.ENDPOINT}/${id}`, {
body: JSON.stringify({ draft: false }),
body: JSON.stringify({ status }),
headers: { "Content-Type": "application/json" },
method: "PATCH",
});
Expand All @@ -41,17 +41,6 @@ export default class ResourceService {
}
}

async reject(id) {
const res = await this.fetch(`${ResourceService.ENDPOINT}/${id}`, {
body: JSON.stringify({ draft: false }),
headers: { "Content-Type": "application/json" },
method: "DELETE",
});
if (res.ok) {
return this._revive(await res.json());
}
}

async suggest(resource) {
const res = await this.fetch(ResourceService.ENDPOINT, {
body: JSON.stringify(resource),
Expand All @@ -68,11 +57,11 @@ export default class ResourceService {
}
}

_revive({ accession, publication, ...resource }) {
_revive({ accession, statusChangedDate, ...resource }) {
return {
...resource,
accession: accession && new Date(accession),
publication: publication && new Date(publication),
statusChangedDate: statusChangedDate && new Date(statusChangedDate),
};
}
}
14 changes: 7 additions & 7 deletions client/src/services/resourceService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ describe("ResourceService", () => {
})
);
await service.getDrafts();
expect(new URL(request.url).searchParams.get("draft")).toBe("true");
expect(new URL(request.url).searchParams.get("status")).toBe("drafted");
});

it("returns resources on success", async () => {
const resources = [true, true, true].map((draft) =>
resourceStub({ draft })
const resources = ["drafted", "drafted", "drafted"].map((status) =>
resourceStub({ status })
);
server.use(
http.get("/api/resources", () => {
Expand Down Expand Up @@ -87,12 +87,12 @@ describe("ResourceService", () => {
server.use(
http.patch("/api/resources/:id", async ({ request: req }) => {
request = req;
return HttpResponse.json({ draft: false });
return HttpResponse.json({ status: "published" });
})
);
await service.publish(id);
await service.action(id, "published");
expect(new URL(request.url).pathname).toMatch(new RegExp(`/${id}$`));
await expect(request.json()).resolves.toEqual({ draft: false });
await expect(request.json()).resolves.toEqual({ status: "published" });
});
});

Expand All @@ -102,7 +102,7 @@ describe("ResourceService", () => {
const submitted = { title: "foo bar", url: "https://example.com" };
const created = resourceStub({
...submitted,
draft: true,
status: "drafted",
});
server.use(
http.post("/api/resources", ({ request: req }) => {
Expand Down
2 changes: 1 addition & 1 deletion e2e/fixtures/oneDraftResource.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"resources": [
{
"draft": true,
"status": "drafted",
"source": "2a8e4bb3-6f9c-4602-b084-af343fcbe6f0",
"title": "Joi documentation",
"url": "https://joi.dev/"
Expand Down
2 changes: 1 addition & 1 deletion e2e/fixtures/onePublishedResource.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"resources": [
{
"description": "Part 4 of an ongoing series on test-driven development (TDD) in JavaScript.",
"draft": false,
"status": "published",
"source": "2a8e4bb3-6f9c-4602-b084-af343fcbe6f0",
"title": "JS TDD Ohm",
"url": "https://blog.jonrshar.pe/2023/May/23/js-tdd-ohm.html"
Expand Down
8 changes: 4 additions & 4 deletions e2e/integration/pagination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ function createResource(index) {
date.setHours(12, 13, 14);
const accession = new Date(date.getTime());
date.setHours(14, 15, 16);
const publication = new Date(date.getTime());
const status_changed_date = new Date(date.getTime());
return {
accession,
draft: false,
publication,
publisher: admin.id,
status: "published",
status_changed_date,
status_changed_by: admin.id,
source: user.id,
title: `Resource ${id}`,
url: `https://example.com/resources/${id}`,
Expand Down
2 changes: 1 addition & 1 deletion e2e/integration/suggest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ it("lets an authenticated user suggest a resource", () => {
expect(submitted).to.have.property("title", title);
expect(submitted).to.have.property("url", url);
cy.request({
body: { draft: false },
body: { status: "published" },
headers: { Authorization: `Bearer ${Cypress.env("SUDO_TOKEN")}` },
method: "PATCH",
url: `/api/resources/${created.id}`,
Expand Down
4 changes: 0 additions & 4 deletions server/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,3 @@ export function updateQuery(tableName, columns) {
.join(", ")
);
}

export function deleteQuery(tableName) {
return format("DELETE FROM %I WHERE id = $1 RETURNING *;", tableName);
}
10 changes: 5 additions & 5 deletions server/docs/resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
**Note**: drafts are only available to admin users or with SUDO token.
schema:
type: boolean
type: string
- in: query
name: page
description: Which page of resources to fetch.
Expand Down Expand Up @@ -87,11 +87,11 @@
application/json:
schema:
type: object
required: [draft]
required: [status]
properties:
draft:
type: boolean
example: false
status:
type: string
example: "published"
responses:
200:
content:
Expand Down
20 changes: 10 additions & 10 deletions server/docs/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ components:
allOf:
- $ref: "#/components/schemas/BaseResource"
- type: object
required: [draft]
required: [status]
properties:
draft:
type: boolean
example: true
publication:
status:
type: string
example: "drafted"
statusChangedDate:
type: string
example: null
nullable: true
Expand All @@ -102,15 +102,15 @@ components:
allOf:
- $ref: "#/components/schemas/BaseResource"
- type: object
required: [draft, publication]
required: [status, statusChangedDate]
properties:
draft:
example: false
publication:
status:
example: "published"
statusChangedDate:
type: string
format: date-time
nullable: false
publisher:
statusChangedBy:
description: |
The ID of the user who published the resource (or `null`, if the SUDO token was used).
type: string
Expand Down
41 changes: 22 additions & 19 deletions server/resources/resources.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("/api/resources", () => {
expect(body).toMatchObject({
accession: expect.stringMatching(patterns.DATETIME),
description: null,
draft: true,
status: "drafted",
id: expect.stringMatching(patterns.UUID),
source: id,
title: resource.title,
Expand Down Expand Up @@ -160,7 +160,7 @@ describe("/api/resources", () => {
const { agent: adminAgent } = await authenticateAs("admin");
const { body: envelope } = await adminAgent
.get("/api/resources")
.query({ draft: true })
.query({ status: "drafted" })
.set("User-Agent", "supertest")
.expect(200);
expect(envelope).toEqual({
Expand All @@ -179,7 +179,7 @@ describe("/api/resources", () => {
const { agent: adminAgent } = await authenticateAs("admin");
const { body: envelope } = await adminAgent
.get("/api/resources")
.query({ draft: true, page: 2, perPage: 10 })
.query({ status: "drafted", page: 2, perPage: 10 })
.set("User-Agent", "supertest")
.expect(200);
expect(envelope).toEqual({
Expand All @@ -198,7 +198,7 @@ describe("/api/resources", () => {
const { agent } = await authenticateAs("admin");
await agent
.get("/api/resources")
.query({ draft: true, page: 0, perPage: "foo" })
.query({ status: "drafted", page: 0, perPage: "foo" })
.set("User-Agent", "supertest")
.expect(400, {
page: '"page" must be greater than or equal to 1',
Expand All @@ -221,7 +221,7 @@ describe("/api/resources", () => {
body: { resources },
} = await anonAgent
.get("/api/resources")
.query({ draft: true })
.query({ status: "drafted" })
.set("Authorization", `Bearer ${sudoToken}`)
.set("User-Agent", "supertest")
.expect(200);
Expand All @@ -242,7 +242,7 @@ describe("/api/resources", () => {

await anonAgent
.get("/api/resources")
.query({ draft: true })
.query({ status: "drafted" })
.set("User-Agent", "supertest")
.expect(403, "Forbidden");
});
Expand All @@ -269,15 +269,15 @@ describe("/api/resources", () => {

const {
body: {
resources: [draft],
resources: [status],
},
} = await anonAgent
.get("/api/resources")
.query({ draft: true })
.query({ status: "drafted" })
.set("Authorization", `Bearer ${sudoToken}`)
.set("User-Agent", "supertest")
.expect(200);
expect(draft).toHaveProperty("topic_name", topic.name);
expect(status).toHaveProperty("topic_name", topic.name);
});
});

Expand All @@ -296,16 +296,16 @@ describe("/api/resources", () => {

const { body: updated } = await anonAgent
.patch(`/api/resources/${resource.id}`)
.send({ draft: false })
.send({ status: "published" })
.set("Authorization", `Bearer ${sudoToken}`)
.set("User-Agent", "supertest")
.expect(200);

expect(updated).toEqual({
...resource,
draft: false,
publication: expect.stringMatching(patterns.DATETIME),
publisher: null,
status: "published",
status_changed_date: expect.stringMatching(patterns.DATETIME),
status_changed_by: null,
});

const {
Expand All @@ -328,11 +328,11 @@ describe("/api/resources", () => {

await anonAgent
.patch(`/api/resources/${resource.id}`)
.send({ draft: true, title: "Something else" })
.send({ status: "drafted", title: "Something else" })
.set("Authorization", `Bearer ${sudoToken}`)
.set("User-Agent", "supertest")
.expect(400, {
draft: '"draft" must be [false]',
status: '"status" must be one of [rejected, published]',
title: '"title" is not allowed',
});
});
Expand All @@ -341,7 +341,7 @@ describe("/api/resources", () => {
const { agent } = await authenticateAs("anonymous");
await agent
.patch(`/api/resources/${randomUUID()}`)
.send({ draft: false })
.send({ status: "published" })
.set("Authorization", `Bearer ${sudoToken}`)
.set("User-Agent", "supertest")
.expect(404);
Expand All @@ -361,7 +361,7 @@ describe("/api/resources", () => {

await anonAgent
.patch(`/api/resources/${resource.id}`)
.send({ draft: false })
.send({ status: "published" })
.set("User-Agent", "supertest")
.expect(401);
});
Expand All @@ -378,11 +378,14 @@ describe("/api/resources", () => {

const { body: published } = await adminAgent
.patch(`/api/resources/${created.id}`)
.send({ draft: false })
.send({ status: "published" })
.set("User-Agent", "supertest")
.expect(200);

expect(published).toMatchObject({ publisher: admin.id, source: user.id });
expect(published).toMatchObject({
status_changed_by: admin.id,
source: user.id,
});
});
});
});
Loading

0 comments on commit 549ff1c

Please sign in to comment.