-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
api-bankaccounts.spec.ts
149 lines (134 loc) · 4.69 KB
/
api-bankaccounts.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
// check this file using TypeScript if available
// @ts-check
import { faker } from "@faker-js/faker";
import { User, BankAccount } from "../../../src/models";
const apiBankAccounts = `${Cypress.env("apiUrl")}/bankAccounts`;
const apiGraphQL = `${Cypress.env("apiUrl")}/graphql`;
type TestBankAccountsCtx = {
allUsers?: User[];
authenticatedUser?: User;
bankAccounts?: BankAccount[];
};
describe("Bank Accounts API", function () {
let ctx: TestBankAccountsCtx = {};
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("filter", "bankaccounts").then((bankAccounts: BankAccount[]) => {
ctx.bankAccounts = bankAccounts;
});
});
context("GET /bankAccounts", function () {
it("gets a list of bank accounts for user", function () {
const { id: userId } = ctx.authenticatedUser!;
cy.request("GET", `${apiBankAccounts}`).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.results[0].userId).to.eq(userId);
});
});
});
context("GET /bankAccounts/:bankAccountId", function () {
it("gets a bank account", function () {
const { id: userId } = ctx.authenticatedUser!;
const { id: bankAccountId } = ctx.bankAccounts![0];
cy.request("GET", `${apiBankAccounts}/${bankAccountId}`).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.account.userId).to.eq(userId);
});
});
});
context("POST /bankAccounts", function () {
it("creates a new bank account", function () {
const { id: userId } = ctx.authenticatedUser!;
cy.request("POST", `${apiBankAccounts}`, {
bankName: `${faker.company.companyName()} Bank`,
accountNumber: faker.finance.account(10),
routingNumber: faker.finance.account(9),
}).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.account.id).to.be.a("string");
expect(response.body.account.userId).to.eq(userId);
});
});
});
context("DELETE /contacts/:bankAccountId", function () {
it("deletes a bank account", function () {
const { id: bankAccountId } = ctx.bankAccounts![0];
cy.request("DELETE", `${apiBankAccounts}/${bankAccountId}`).then((response) => {
expect(response.status).to.eq(200);
});
});
});
context("/graphql", function () {
it("gets a list of bank accounts for user", function () {
const { id: userId } = ctx.authenticatedUser!;
cy.request("POST", `${apiGraphQL}`, {
query: `query {
listBankAccount {
id
uuid
userId
bankName
accountNumber
routingNumber
isDeleted
createdAt
modifiedAt
}
}`,
}).then((response) => {
expect(response.status).to.eq(200);
expect(JSON.stringify(response.body.errors || "notThere")).to.eq('"notThere"');
expect(response.body.data.listBankAccount[0].userId).to.eq(userId);
});
});
it("creates a new bank account", function () {
const { id: userId } = ctx.authenticatedUser!;
cy.request("POST", `${apiGraphQL}`, {
query: `mutation createBankAccount ($bankName: String!, $accountNumber: String!, $routingNumber: String!) {
createBankAccount(
bankName: $bankName,
accountNumber: $accountNumber,
routingNumber: $routingNumber
) {
id
uuid
userId
bankName
accountNumber
routingNumber
isDeleted
createdAt
}
}`,
variables: {
bankName: `${faker.company.companyName()} Bank`,
accountNumber: faker.finance.account(10),
routingNumber: faker.finance.account(9),
},
}).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.data.createBankAccount.userId).to.eq(userId);
});
});
it("deletes a bank account", function () {
const { id: bankAccountId } = ctx.bankAccounts![0];
cy.request("POST", `${apiGraphQL}`, {
query: `mutation deleteBankAccount ($id: ID!) {
deleteBankAccount(id: $id)
}`,
variables: { id: bankAccountId },
}).then((response) => {
expect(response.status).to.eq(200);
});
});
});
});