-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorchestrator.js
More file actions
90 lines (70 loc) · 2.26 KB
/
orchestrator.js
File metadata and controls
90 lines (70 loc) · 2.26 KB
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
import retry from "async-retry";
import { faker } from "@faker-js/faker";
import database from "infra/database.js";
import migrator from "models/migrator.js";
import user from "models/user.js";
import session from "models/session";
const emailHttpUrl = `http://${process.env.EMAIL_HTTP_HOST}:${process.env.EMAIL_HTTP_PORT}`;
async function waitForAllServices() {
await waitForWebServer();
await waitForEmailServer();
async function waitForWebServer() {
return retry(fetchStatusPage, { retries: 100, maxTimeout: 1000 });
async function fetchStatusPage() {
const response = await fetch("http://localhost:3000/api/v1/status");
if (response.status !== 200) {
throw Error();
}
}
}
async function waitForEmailServer() {
return retry(fetchEmailPage, { retries: 100, maxTimeout: 1000 });
async function fetchEmailPage() {
const response = await fetch(emailHttpUrl);
if (response.status !== 200) {
throw Error();
}
}
}
}
async function clearDatabase() {
await database.query("DROP SCHEMA public CASCADE; CREATE SCHEMA public;");
}
async function runPendingMigrations() {
await migrator.runPendingMigrations();
}
async function createUser(userObject) {
return await user.create({
username:
userObject?.username || faker.internet.username().replace(/[_.-]/g, ""),
email: userObject?.email || faker.internet.email({ provider: "test.dev" }),
password: userObject?.password || "validPassword",
});
}
async function createSession(userId) {
return await session.create(userId);
}
async function deleteAllEmails() {
await fetch(`${emailHttpUrl}/messages`, { method: "DELETE" });
}
async function getLastEmail() {
const emailListResponse = await fetch(`${emailHttpUrl}/messages`);
const emailListBody = await emailListResponse.json();
const lastEmailItem = emailListBody.pop();
const emailTextResponse = await fetch(
`${emailHttpUrl}/messages/${lastEmailItem.id}.plain`,
);
const emailTextBody = await emailTextResponse.text();
lastEmailItem.text = emailTextBody;
return lastEmailItem;
}
const orchestrator = {
waitForAllServices,
clearDatabase,
runPendingMigrations,
createUser,
createSession,
deleteAllEmails,
getLastEmail,
};
export default orchestrator;