forked from cypress-io/cypress-realworld-app
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi-comments.spec.ts
More file actions
131 lines (113 loc) · 4.03 KB
/
Copy pathapi-comments.spec.ts
File metadata and controls
131 lines (113 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// check this file using TypeScript if available
// @ts-check
import { User, Comment } from "../../../src/models";
const apiComments = `${Cypress.env("apiUrl")}/comments`;
type TestCommentsCtx = {
authenticatedUser?: User;
transactionId?: string;
};
describe("Comments API", function () {
let ctx: TestCommentsCtx = {};
before(() => {
// Hacky workaround to have the e2e tests pass when cy.visit('http://localhost:3000') is called
cy.request("GET", "/");
});
beforeEach(function () {
cy.task("db:seed");
cy.database("filter", "users").then((users: User[]) => {
ctx.authenticatedUser = users[0];
return cy.loginByApi(ctx.authenticatedUser.username);
});
cy.database("find", "comments").then((comment: Comment) => {
ctx.transactionId = comment.transactionId;
});
});
context("GET /comments/:transactionId", function () {
it("gets a list of comments for a transaction", function () {
const transactionId = ctx.transactionId!;
cy.request("GET", `${apiComments}/${transactionId}`).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.comments).to.be.an("array").that.has.length(1);
const comment = response.body.comments[0];
expect(comment).to.have.property("id");
expect(comment).to.have.property("uuid");
expect(comment).to.have.property("content");
expect(comment).to.have.property("userId");
expect(comment).to.have.property("transactionId");
expect(comment).to.have.property("createdAt");
expect(comment).to.have.property("modifiedAt");
});
});
it("errors when invalid transactionId", function () {
cy.request({
method: "GET",
url: `${apiComments}/1234`,
failOnStatusCode: false,
}).then((response) => {
expect(response.status).to.eq(422);
expect(response.body.errors).to.be.an("array").that.has.length(1);
});
});
});
context("POST /comments/:transactionId", function () {
it("creates a new comment for a transaction", function () {
const transactionId = ctx.transactionId!;
cy.request("POST", `${apiComments}/${transactionId}`, {
content: "This is my comment",
}).then((response) => {
expect(response.status).to.eq(200);
});
// Verify persistence
cy.request("GET", `${apiComments}/${ctx.transactionId}`).then((response) => {
expect(response.status).to.eq(200);
const contents = response.body.comments.map((c: Comment) => c.content);
expect(contents).to.include("This is my comment");
});
});
it("errors when content is missing from POST body", function () {
cy.request({
method: "POST",
url: `${apiComments}/${ctx.transactionId}`,
failOnStatusCode: false,
body: {},
}).then((response) => {
expect(response.status).to.eq(422);
expect(response.body.errors).to.be.an("array");
});
});
it("errors when invalid transactionId on POST", function () {
cy.request({
method: "POST",
url: `${apiComments}/1234`,
failOnStatusCode: false,
body: { content: "test" },
}).then((response) => {
expect(response.status).to.eq(422);
expect(response.body.errors).to.be.an("array");
});
});
});
context("Unauthenticated", function () {
it("is unauthorized to GET comments without login", function () {
cy.clearCookies();
cy.request({
method: "GET",
url: `${apiComments}/${ctx.transactionId}`,
failOnStatusCode: false,
}).then((response) => {
expect(response.status).to.eq(401);
});
});
it("is unauthorized to POST comments without login", function () {
cy.clearCookies();
cy.request({
method: "POST",
url: `${apiComments}/${ctx.transactionId}`,
failOnStatusCode: false,
body: { content: "This is my comment" },
}).then((response) => {
expect(response.status).to.eq(401);
});
});
});
});