Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
153 lines (120 loc) · 3.54 KB
/
Copy pathmain_test.go
File metadata and controls
153 lines (120 loc) · 3.54 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
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
package main
import (
"bytes"
"database/sql"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
_ "github.com/jackc/pgx/v5/stdlib"
)
func TestHashPasswordAndCheck(t *testing.T) {
pass := "mysecret"
hash, err := hashPassword(pass)
if err != nil {
t.Fatalf("hash error: %v", err)
}
if !checkPasswordHash(pass, hash) {
t.Fatalf("password should match hash")
}
}
func TestTokenGenerateValidate(t *testing.T) {
token, err := generateToken(1, "testuser")
if err != nil {
t.Fatalf("token error: %v", err)
}
claims, err := validateToken(token)
if err != nil {
t.Fatalf("validate error: %v", err)
}
if claims.UserID != 1 || claims.Username != "testuser" {
t.Fatalf("claims mismatch")
}
}
func TestAuthMiddleware_Valid(t *testing.T) {
token, _ := generateToken(2, "bob")
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Authorization", "Bearer "+token)
w := httptest.NewRecorder()
handler := authMiddleware(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-User-ID") != "2" {
t.Fatalf("expected user id 2")
}
})
handler(w, req)
}
func TestMain(m *testing.M) {
// Force correct DSN
var err error
db, err = sql.Open("pgx", defaultPostgresDSN)
if err != nil {
panic(err)
}
if err := db.Ping(); err != nil {
panic(err)
}
if err := initTables(db); err != nil {
panic(err)
}
_, _ = db.Exec("DELETE FROM users WHERE email LIKE 'test-%'")
// Run tests
code := m.Run()
// Cleanup test users after tests
_, _ = db.Exec("DELETE FROM users WHERE email LIKE 'test-%'")
_ = db.Close()
os.Exit(code)
}
func TestApiRegisterHandler_Success(t *testing.T) {
reqBody := `{"username":"test-user","email":"test-user@example.com","password":"123456"}`
req := httptest.NewRequest(http.MethodPost, "/api/register", bytes.NewBufferString(reqBody))
w := httptest.NewRecorder()
apiRegisterHandler(w, req)
if w.Code != http.StatusCreated {
t.Fatalf("expected 201, got %d", w.Code)
}
var resp AuthResponse
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if resp.UserID == 0 || resp.Token == "" {
t.Fatalf("expected valid userId and token, got %+v", resp)
}
}
func TestApiLoginHandler_Success(t *testing.T) {
// Ensure user exists
passwordHash, _ := hashPassword("123456")
_, _ = db.Exec(
"INSERT INTO users (username, email, password_hash) VALUES ($1, $2, $3)",
"test-login", "test-login@example.com", passwordHash,
)
reqBody := `{"email":"test-login@example.com","password":"123456"}`
req := httptest.NewRequest(http.MethodPost, "/api/login", bytes.NewBufferString(reqBody))
w := httptest.NewRecorder()
apiLoginHandler(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
var resp AuthResponse
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if resp.UserID == 0 || resp.Token == "" {
t.Fatalf("expected valid userId and token, got %+v", resp)
}
}
func TestApiLoginHandler_InvalidPassword(t *testing.T) {
// Ensure user exists
passwordHash, _ := hashPassword("123456")
_, _ = db.Exec(
"INSERT INTO users (username, email, password_hash) VALUES ($1, $2, $3)",
"test-login2", "test-login2@example.com", passwordHash,
)
reqBody := `{"email":"test-login2@example.com","password":"wrong"}`
req := httptest.NewRequest(http.MethodPost, "/api/login", bytes.NewBufferString(reqBody))
w := httptest.NewRecorder()
apiLoginHandler(w, req)
if w.Code != http.StatusUnauthorized {
t.Fatalf("expected 401, got %d", w.Code)
}
}