-
Notifications
You must be signed in to change notification settings - Fork 20
/
booking.ts
180 lines (156 loc) · 5.25 KB
/
booking.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { expect, request } from "@playwright/test";
import { stringDateByDays } from "../helpers/date";
import { faker } from "@faker-js/faker";
import { createHeaders } from "../helpers/createHeaders";
let url = process.env.URL || "https://automationintesting.online/";
let bookingBody;
let checkOutArray;
export async function createRandomBookingBody(
roomId: number,
checkInString: string,
checkOutString: string
) {
let bookingBody = {
roomid: roomId,
firstname: faker.name.firstName(),
lastname: faker.name.lastName(),
depositpaid: Math.random() < 0.5, //returns true or false
email: faker.internet.email(),
phone: faker.phone.number("###########"),
bookingdates: {
checkin: checkInString,
checkout: checkOutString,
},
};
return bookingBody;
}
/**
* This function will create a booking with provided roomId and a checkinDate
* A checkout date will be randomly generated between 1 and 4 days after the checkinDate
*
* @param roomId: number for the room to create a booking for
* @returns the body of the booking just created
*
* This code is wrapped in an assert retry details can be found
* https://playwright.dev/docs/test-assertions#retrying
*/
export async function createFutureBooking(roomId: number) {
let body;
await expect(async () => {
let headers = await createHeaders();
let futureCheckinDate = await futureOpenCheckinDate(roomId);
let randBookingLength = faker.datatype.number({ min: 1, max: 4 });
let checkInString = futureCheckinDate.toISOString().split("T")[0];
let checkOutString = stringDateByDays(futureCheckinDate, randBookingLength);
// console.log("booking length: " + randBookingLength);
// console.log("checkin string: " + checkInString);
// console.log("checkout string: " + checkOutString);
bookingBody = {
roomid: roomId,
firstname: faker.name.firstName(),
lastname: faker.name.lastName(),
depositpaid: Math.random() < 0.5, //returns true or false
email: faker.internet.email(),
phone: faker.phone.number("###########"),
bookingdates: {
checkin: checkInString,
checkout: checkOutString,
},
};
const createRequestContext = await request.newContext();
const response = await createRequestContext.post(url + "booking/", {
headers: headers,
data: bookingBody,
});
expect(response.status()).toBe(201);
body = await response.json();
}).toPass({
intervals: [1_000, 2_000, 5_000],
timeout: 20_000,
});
return body;
}
/**
* This function will return all the bookings for a roomId
*
* @param roomId: number for the room you want to get the bookings for
* @returns the body of the bookings for the room
*/
export async function getBookings(roomId: number) {
let headers = await createHeaders();
const createRequestContext = await request.newContext();
const response = await createRequestContext.get(
url + "booking/?roomid=" + roomId,
{
headers: headers,
}
);
expect(response.status()).toBe(200);
const body = await response.json();
// console.log(JSON.stringify(body));
return body;
}
/**
*
* @param bookingId: number for the booking you want to see the summary of
* @returns the body of the booking/summary?roomid=${bookingId} endpoint
*/
export async function getBookingSummary(bookingId: number) {
const createRequestContext = await request.newContext();
const response = await createRequestContext.get(
url + `booking/summary?roomid=${bookingId}`
);
expect(response.status()).toBe(200);
const body = await response.json();
return body;
}
/**
*
* @param bookingId number for the booking you want to see the details of
* @returns the body of the booking/${bookingId} endpoint
*/
export async function getBookingById(bookingId: number) {
let headers = await createHeaders();
const createRequestContext = await request.newContext();
const response = await createRequestContext.get(
url + `booking/${bookingId}`,
{
headers: headers,
}
);
expect(response.status()).toBe(200);
const body = await response.json();
return body;
}
/**
*
* @param roomId
* @returns the most future checkout date for a room
* @example
*
* let futureCheckinDate = await futureOpenCheckinDate(roomId); // "2023-03-31T00:00:00.000Z"
* let checkInString = futureCheckinDate.toISOString().split("T")[0]; // "2023-03-31"
* let checkOutString = stringDateByDays(futureCheckinDate, 2); // "2023-04-02"
*/
export async function futureOpenCheckinDate(roomId: number) {
let currentBookings = await getBookings(roomId);
checkOutArray = new Array();
// Iterate through current bookings and get checkout dates
for (let i = 0; i < (await currentBookings.bookings.length); i++) {
let today = new Date();
let checkOut = new Date(currentBookings.bookings[i].bookingdates.checkout);
if (today < checkOut) {
// pushing the checkout date into an array
checkOutArray.push(checkOut);
}
}
// Find the most future checkout date and return it if no future dates exist return today
let mostFutureDate =
checkOutArray
.sort(function (a, b) {
return a - b;
})
.pop() || new Date();
// console.log("Last Checkout Date: " + mostFutureDate);
return mostFutureDate;
}