forked from cypress-io/cypress-realworld-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-bankaccounts.spec.ts
78 lines (65 loc) · 2.42 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
// check this file using TypeScript if available
// @ts-check
import faker from "faker";
import { User, BankAccount } from "../../../src/models";
const apiBankAccounts = `${Cypress.env("apiUrl")}/bankAccounts`;
type TestBankAccountsCtx = {
allUsers?: User[];
authenticatedUser?: User;
bankAccounts?: BankAccount[];
};
describe("Bank Accounts API", function () {
let ctx: TestBankAccountsCtx = {};
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);
});
});
});
});