This commit is contained in:
jlimolina 2026-08-28 21:10:02 +02:00
parent 3991237685
commit a01050bf85
46 changed files with 3636 additions and 652 deletions

View file

@ -17,6 +17,7 @@ func init() {
auth.SetJWTSecret("test-secret-key-12345")
}
// TestLoginInvalidRequest tests that an empty request body returns 400
func TestLoginInvalidRequest(t *testing.T) {
router := gin.New()
router.POST("/auth/login", Login)
@ -40,22 +41,7 @@ func TestLoginInvalidRequest(t *testing.T) {
}
}
func TestLoginInvalidCredentials(t *testing.T) {
router := gin.New()
router.POST("/auth/login", Login)
body := []byte(`{"email":"invalid@test.com","password":"wrongpass"}`)
req, _ := http.NewRequest("POST", "/auth/login", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusUnauthorized {
t.Errorf("expected status 401, got %d", w.Code)
}
}
// TestRegisterInvalidRequest tests that an empty request body returns 400
func TestRegisterInvalidRequest(t *testing.T) {
router := gin.New()
router.POST("/auth/register", Register)
@ -72,27 +58,7 @@ func TestRegisterInvalidRequest(t *testing.T) {
}
}
func TestCheckFirstUser(t *testing.T) {
router := gin.New()
router.GET("/auth/check-first-user", CheckFirstUser)
req, _ := http.NewRequest("GET", "/auth/check-first-user", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", w.Code)
}
var resp map[string]interface{}
json.Unmarshal(w.Body.Bytes(), &resp)
if _, ok := resp["is_first_user"]; !ok {
t.Error("expected is_first_user in response")
}
}
// TestGetCurrentUserUnauthorized tests that unauthenticated request returns 401
func TestGetCurrentUserUnauthorized(t *testing.T) {
router := gin.New()
router.GET("/auth/me", GetCurrentUser)