eletrotupi / tcc/ api/tests/users/authentication.test.js56ae9d8272c35d408c95fb441f727bf93b0507d3
3.7 KB | 148 lines Raw
1
import test from "ava";
2
import request from "supertest";
3
import bcrypt from "bcryptjs";
4
import { createApp } from "../../src/create-app.js";
5
import { setupTestDb, cleanupTestDb } from "../test-helper.js";
6

7
let app;
8
let db;
9

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

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

18
  // Create a test user for login tests
19
  const hashedPassword = bcrypt.hashSync("senha123", 10);
20
  db.prepare(`
21
    INSERT INTO users (email, password, first_name, last_name, timezone)
22
    VALUES (?, ?, ?, ?, ?)
23
  `).run("teste@example.com", hashedPassword, "Teste", "User", "UTC");
24
});
25

26
test("Login bem-sucedido com credenciais corretas", async (t) => {
27
  const res = await request(app)
28
    .post("/users/login")
29
    .send({
30
      email: "teste@example.com",
31
      password: "senha123"
32
    });
33

34
  t.is(res.status, 200);
35
  t.true(res.body.success);
36
  t.truthy(res.body.user);
37
  t.is(res.body.user.email, "teste@example.com");
38
  t.is(res.body.user.first_name, "Teste");
39
  t.is(res.body.user.last_name, "User");
40
  t.falsy(res.body.user.password); // Password should not be in response
41
});
42

43
test("Login com email em maiúsculas deve funcionar", async (t) => {
44
  const res = await request(app)
45
    .post("/users/login")
46
    .send({
47
      email: "TESTE@EXAMPLE.COM",
48
      password: "senha123"
49
    });
50

51
  t.is(res.status, 200);
52
  t.true(res.body.success);
53
});
54

55
test("Falha no login com senha incorreta", async (t) => {
56
  const res = await request(app)
57
    .post("/users/login")
58
    .send({
59
      email: "teste@example.com",
60
      password: "senhaerrada"
61
    });
62

63
  t.is(res.status, 401);
64
  t.is(res.body.error, "Credenciais inválidas");
65
});
66

67
test("Falha no login com email não cadastrado", async (t) => {
68
  const res = await request(app)
69
    .post("/users/login")
70
    .send({
71
      email: "naocadastrado@example.com",
72
      password: "senha123"
73
    });
74

75
  t.is(res.status, 401);
76
  t.is(res.body.error, "Credenciais inválidas");
77
});
78

79
test("Falha no login sem email", async (t) => {
80
  const res = await request(app)
81
    .post("/users/login")
82
    .send({
83
      password: "senha123"
84
    });
85

86
  t.is(res.status, 400);
87
  t.is(res.body.error, "Email e senha são obrigatórios");
88
});
89

90
test("Falha no login sem senha", async (t) => {
91
  const res = await request(app)
92
    .post("/users/login")
93
    .send({
94
      email: "teste@example.com"
95
    });
96

97
  t.is(res.status, 400);
98
  t.is(res.body.error, "Email e senha são obrigatórios");
99
});
100

101
test("Falha no login sem credenciais", async (t) => {
102
  const res = await request(app)
103
    .post("/users/login")
104
    .send({});
105

106
  t.is(res.status, 400);
107
  t.is(res.body.error, "Email e senha são obrigatórios");
108
});
109

110
test("Login não deve retornar a senha do usuário", async (t) => {
111
  const res = await request(app)
112
    .post("/users/login")
113
    .send({
114
      email: "teste@example.com",
115
      password: "senha123"
116
    });
117

118
  t.is(res.status, 200);
119
  t.falsy(res.body.user.password);
120
  t.is(typeof res.body.user.password, 'undefined');
121
});
122

123
test("Login deve retornar informações completas do usuário", async (t) => {
124
  const res = await request(app)
125
    .post("/users/login")
126
    .send({
127
      email: "teste@example.com",
128
      password: "senha123"
129
    });
130

131
  t.is(res.status, 200);
132
  t.truthy(res.body.user.id);
133
  t.is(res.body.user.email, "teste@example.com");
134
  t.is(res.body.user.first_name, "Teste");
135
  t.is(res.body.user.last_name, "User");
136
  t.is(res.body.user.timezone, "UTC");
137
});
138

139
test("Login com espaços em branco deve ser tratado corretamente", async (t) => {
140
  const res = await request(app)
141
    .post("/users/login")
142
    .send({
143
      email: "  teste@example.com  ",
144
      password: "senha123"
145
    });
146

147
  t.is(res.status, 401); // Should fail as email with spaces doesn't match
148
});