eletrotupi / tcc/ api/tests/users.test.js86b807288e7e3138f09761dcc61fdf84ba213b13
1.4 KB | 68 lines Raw
1
import test from "ava";
2
import request from "supertest";
3
import { createApp } from "../src/create-app.js";
4
import { setupTestDb, cleanupTestDb } from "./test-helper.js";
5

6
let app;
7
let db;
8

9
test.before(() => {
10
  db = setupTestDb();
11
  app = createApp(db);
12
});
13

14
test.beforeEach(() => {
15
  cleanupTestDb();
16
});
17

18
test("Criar usuário", async (t) => {
19
  const res = await request(app)
20
    .post("/users")
21
    .send({
22
      email: "john@example.com",
23
      password: "123456",
24
      first_name: "John",
25
      last_name: "Doe",
26
    });
27

28
  t.is(res.status, 201);
29
  t.truthy(res.body.id);
30
});
31

32
test("Falha ao criar usuário sem email", async (t) => {
33
  const res = await request(app)
34
    .post("/users")
35
    .send({
36
      password: "123456",
37
      first_name: "John",
38
      last_name: "Doe",
39
    });
40

41
  t.is(res.status, 400);
42
  t.is(res.body.error, "Campos faltantes");
43
});
44

45
test("Falha ao criar usuário com email duplicado", async (t) => {
46
  // First user
47
  await request(app)
48
    .post("/users")
49
    .send({
50
      email: "duplicate@example.com",
51
      password: "123456",
52
      first_name: "John",
53
      last_name: "Doe",
54
    });
55

56
  // Duplicate email
57
  const res = await request(app)
58
    .post("/users")
59
    .send({
60
      email: "duplicate@example.com",
61
      password: "123456",
62
      first_name: "Jane",
63
      last_name: "Smith",
64
    });
65

66
  t.is(res.status, 400);
67
  t.is(res.body.error, "Email já cadastrado");
68
});