forked from cypress-io/cypress-realworld-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-transactions.spec.ts
164 lines (144 loc) · 5.5 KB
/
api-transactions.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import faker from "faker";
import { isEqual } from "lodash/fp";
import { User, NotificationType, Transaction, BankAccount } from "../../../src/models";
type TestTransactionsCtx = {
receiver?: User;
authenticatedUser?: User;
transactionId?: string;
notificationId?: string;
bankAccountId?: string;
};
const getFakeAmount = () => parseInt(faker.finance.amount(), 10);
const apiTransactions = `${Cypress.env("apiUrl")}/transactions`;
describe("Transactions API", function () {
let ctx: TestTransactionsCtx = {};
const isSenderOrReceiver = ({ senderId, receiverId }: Transaction) =>
isEqual(senderId, ctx.authenticatedUser!.id) || isEqual(receiverId, ctx.authenticatedUser!.id);
beforeEach(function () {
cy.task("db:seed");
cy.database("filter", "users").then((users: User[]) => {
ctx.authenticatedUser = users[0];
ctx.receiver = users[1];
return cy.loginByApi(ctx.authenticatedUser.username);
});
cy.database("find", "transactions").then((transaction: Transaction) => {
ctx.transactionId = transaction.id;
});
cy.database("find", "notifications").then((notification: NotificationType) => {
ctx.notificationId = notification.id;
});
cy.database("find", "bankaccounts").then((bankaccount: BankAccount) => {
ctx.bankAccountId = bankaccount.id;
});
});
context("GET /transactions", function () {
it("gets a list of transactions for user (default)", function () {
cy.request("GET", `${apiTransactions}`).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.results[0]).to.satisfy(isSenderOrReceiver);
});
});
it("gets a list of pending request transactions for user", function () {
cy.request({
method: "GET",
url: `${apiTransactions}`,
qs: {
requestStatus: "pending",
},
}).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.results[0]).to.satisfy(isSenderOrReceiver);
});
});
it("gets a list of pending request transactions for user between a time range", function () {
cy.request({
method: "GET",
url: `${apiTransactions}`,
qs: {
requestStatus: "pending",
dateRangeStart: new Date("Jan 01 2018"),
dateRangeEnd: new Date("Dec 05 2030"),
},
}).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.results[0]).to.satisfy(isSenderOrReceiver);
});
});
});
context("GET /transactions/contacts", function () {
it("gets a list of transactions for users list of contacts, page one", function () {
cy.request("GET", `${apiTransactions}/contacts`).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.results).length.to.be.greaterThan(1);
});
});
it("gets a list of transactions for users list of contacts, page two", function () {
cy.request("GET", `${apiTransactions}/contacts?page=2`).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.results).length.to.be.greaterThan(1);
});
});
});
context("GET /transactions/public", function () {
it("gets a list of public transactions", function () {
cy.request("GET", `${apiTransactions}/public`).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.results).length.to.be.greaterThan(1);
});
});
});
context("POST /transactions", function () {
it("creates a new payment", function () {
cy.request("POST", `${apiTransactions}`, {
transactionType: "payment",
source: ctx.bankAccountId,
receiverId: ctx.receiver!.id,
description: `Payment: ${ctx.authenticatedUser!.id} to ${ctx.receiver!.id}`,
amount: getFakeAmount(),
privacyLevel: "public",
}).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.transaction.id).to.be.a("string");
expect(response.body.transaction.status).to.eq("complete");
expect(response.body.transaction.requestStatus).to.eq(undefined);
});
});
it("creates a new request", function () {
cy.request("POST", `${apiTransactions}`, {
transactionType: "request",
source: ctx.bankAccountId,
receiverId: ctx.receiver!.id,
description: `Request: ${ctx.authenticatedUser!.id} from ${ctx.receiver!.id}`,
amount: getFakeAmount(),
privacyLevel: "public",
}).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.transaction.id).to.be.a("string");
expect(response.body.transaction.status).to.eq("pending");
expect(response.body.transaction.requestStatus).to.eq("pending");
});
});
});
context("PATCH /transactions/:transactionId", function () {
it("updates a transaction", function () {
cy.request("PATCH", `${apiTransactions}/${ctx.transactionId}`, {
requestStatus: "rejected",
}).then((response) => {
expect(response.status).to.eq(204);
});
});
it("error when invalid field sent", function () {
cy.request({
method: "PATCH",
url: `${apiTransactions}/${ctx.transactionId}`,
failOnStatusCode: false,
body: {
notATransactionField: "not a transaction field",
},
}).then((response) => {
expect(response.status).to.eq(422);
expect(response.body.errors.length).to.eq(1);
});
});
});
});