Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
"description": "Implementação do https://www.tabnews.com.br para o https://curso.dev",
"main": "index.js",
"scripts": {
"dev": "npm run services:up && npm run wait-for-postgres && npm run migration:up && next dev",
"dev": "npm run services:up && npm run services:wait:database && npm run migrations:up && next dev",
"test": "npm run services:up && concurrently -n next,jest --hide next -k -s command-jest \"next dev\" \"jest --runInBand --verbose\"",
"posttest": "npm run services:stop",
"test:watch": "jest --watchAll --runInBand --verbose",
"services:up": "docker compose -f infra/compose.yaml up -d",
"services:stop": "docker compose -f infra/compose.yaml stop",
"services:down": "docker compose -f infra/compose.yaml down",
"services:wait:database": "node infra/scripts/wait-for-postgres.js",
"migrations:create": "node-pg-migrate -m infra/migrations create",
"migrations:up": "node-pg-migrate -m infra/migrations --envPath .env.development up",
"lint:prettier:check": "prettier --check .",
"lint:prettier:fix": "prettier --write .",
"lint:eslint:check": "next lint --dir .",
"test": "npm run services:up && concurrently -n next,jest --hide next -k -s command-jest \"next dev\" \"jest --runInBand --verbose\"",
"test:watch": "jest --watchAll --runInBand",
"migration:create": "node-pg-migrate -m infra/migrations create",
"migration:up": "node-pg-migrate -m infra/migrations --envPath .env.development up",
"wait-for-postgres": "node infra/scripts/wait-for-postgres.js",
"prepare": "husky",
"commit": "cz"
},
Expand Down
4 changes: 2 additions & 2 deletions pages/api/v1/migrations/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import migrationRunner from "node-pg-migrate";
import { join } from "node:path";
import { resolve } from "node:path";
import database from "infra/database.js";

export default async function migrations(request, response) {
Expand All @@ -18,7 +18,7 @@ export default async function migrations(request, response) {
const defaultMigrationOptions = {
dbClient: dbClient,
dryRun: true,
dir: join("infra", "migrations"),
dir: resolve("infra", "migrations"),
direction: "up",
verbose: true,
migrationsTable: "pgmigrations",
Expand Down
19 changes: 11 additions & 8 deletions tests/integration/api/v1/migrations/get.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import database from "infra/database.js";
import orchestrator from "tests/orchestrator.js";

beforeAll(async () => {
await orchestrator.waitForAllServices();
await database.query("drop schema public cascade; create schema public;");
await orchestrator.clearDatabase();
});

test("GET to /api/v1/migrations should return 200", async () => {
const response = await fetch("http://localhost:3000/api/v1/migrations");
expect(response.status).toBe(200);
describe("GET /api/v1/migrations", () => {
describe("Anonymous user", () => {
test("Retrieving pending migrations", async () => {
const response = await fetch("http://localhost:3000/api/v1/migrations");
expect(response.status).toBe(200);

const responseBody = await response.json();
const responseBody = await response.json();

expect(Array.isArray(responseBody)).toBe(true);
expect(responseBody.length).toBeGreaterThan(0);
expect(Array.isArray(responseBody)).toBe(true);
expect(responseBody.length).toBeGreaterThan(0);
});
});
});
48 changes: 30 additions & 18 deletions tests/integration/api/v1/migrations/post.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
import database from "infra/database.js";
import orchestrator from "tests/orchestrator.js";

beforeAll(async () => {
await orchestrator.waitForAllServices();
await database.query("drop schema public cascade; create schema public;");
await orchestrator.clearDatabase();
});

test("POST to /api/v1/migrations should return 200", async () => {
const response1 = await fetch("http://localhost:3000/api/v1/migrations", {
method: "POST",
});
expect(response1.status).toBe(201);

const response1Body = await response1.json();
describe("POST /api/v1/migrations", () => {
describe("Anonymous user", () => {
describe("Running pending migrations", () => {
test("For the first time", async () => {
const response1 = await fetch(
"http://localhost:3000/api/v1/migrations",
{
method: "POST",
}
);
expect(response1.status).toBe(201);

expect(Array.isArray(response1Body)).toBe(true);
expect(response1Body.length).toBeGreaterThan(0);
const response1Body = await response1.json();

const response2 = await fetch("http://localhost:3000/api/v1/migrations", {
method: "POST",
});
expect(response2.status).toBe(200);
expect(Array.isArray(response1Body)).toBe(true);
expect(response1Body.length).toBeGreaterThan(0);
});
test("For the second time", async () => {
const response2 = await fetch(
"http://localhost:3000/api/v1/migrations",
{
method: "POST",
}
);
expect(response2.status).toBe(200);

const response2Body = await response2.json();
const response2Body = await response2.json();

expect(Array.isArray(response2Body)).toBe(true);
expect(response2Body.length).toBe(0);
expect(Array.isArray(response2Body)).toBe(true);
expect(response2Body.length).toBe(0);
});
});
});
});
22 changes: 13 additions & 9 deletions tests/integration/api/v1/status/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ beforeAll(async () => {
await orchestrator.waitForAllServices();
});

test("GET to /api/v1/status should return 200", async () => {
const response = await fetch("http://localhost:3000/api/v1/status");
expect(response.status).toBe(200);
describe("GET /api/v1/status", () => {
describe("Anonymous user", () => {
test("Retrieving current system status", async () => {
const response = await fetch("http://localhost:3000/api/v1/status");
expect(response.status).toBe(200);

const responseBody = await response.json();
const responseBody = await response.json();

const parsedUpdatedAt = new Date(responseBody.updated_at).toISOString();
expect(responseBody.updated_at).toEqual(parsedUpdatedAt);
const parsedUpdatedAt = new Date(responseBody.updated_at).toISOString();
expect(responseBody.updated_at).toEqual(parsedUpdatedAt);

expect(responseBody.dependencies.database.version).toEqual("16.0");
expect(responseBody.dependencies.database.max_connections).toEqual(100);
expect(responseBody.dependencies.database.opened_connections).toEqual(1);
expect(responseBody.dependencies.database.version).toEqual("16.0");
expect(responseBody.dependencies.database.max_connections).toEqual(100);
expect(responseBody.dependencies.database.opened_connections).toEqual(1);
});
});
});
6 changes: 6 additions & 0 deletions tests/orchestrator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import retry from "async-retry";
import database from "infra/database.js";

async function waitForAllServices() {
await waitForWebServer();
Expand All @@ -19,8 +20,13 @@ async function waitForAllServices() {
}
}

async function clearDatabase() {
await database.query("drop schema public cascade; create schema public;");
}

const orchestrator = {
waitForAllServices,
clearDatabase,
};

export default orchestrator;