forked from cypress-io/cypress-realworld-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi-contacts.spec.ts
77 lines (66 loc) · 2.19 KB
/
api-contacts.spec.ts
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
import { User, Contact } from "../../../src/models";
const apiContacts = `${Cypress.env("apiUrl")}/contacts`;
type TestContactsCtx = {
allUsers?: User[];
authenticatedUser?: User;
contact?: Contact;
};
describe("Contacts API", function () {
let ctx: TestContactsCtx = {};
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];
ctx.allUsers = users;
return cy.loginByApi(ctx.authenticatedUser.username);
});
cy.database("find", "contacts").then((contact: Contact) => {
ctx.contact = contact;
});
});
context("GET /contacts/:username", function () {
it("gets a list of contacts by username", function () {
const { username } = ctx.authenticatedUser!;
cy.request("GET", `${apiContacts}/${username}`).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.contacts[0]).to.have.property("userId");
});
});
});
context("POST /contacts", function () {
it("creates a new contact", function () {
const { id: userId } = ctx.authenticatedUser!;
cy.request("POST", `${apiContacts}`, {
contactUserId: ctx.contact!.id,
}).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.contact.id).to.be.a("string");
expect(response.body.contact.userId).to.eq(userId);
});
});
it("error when invalid contactUserId", function () {
cy.request({
method: "POST",
url: `${apiContacts}`,
failOnStatusCode: false,
body: {
contactUserId: "1234",
},
}).then((response) => {
expect(response.status).to.eq(422);
expect(response.body.errors.length).to.eq(1);
});
});
});
context("DELETE /contacts/:contactId", function () {
it("deletes a contact", function () {
cy.request("DELETE", `${apiContacts}/${ctx.contact!.id}`).then((response) => {
expect(response.status).to.eq(200);
});
});
});
});