refactor: move all tests to backend/tests/ as black-box packages

All test files relocated from internal/X/ to tests/X/ and converted
to package X_test, using only the public API of each package.

- tests/auth/: jwt, service, handler integration tests
- tests/middleware/: auth, request_id, recovery tests
- tests/user/: calories, service, repository integration tests
- tests/locale/: locale tests (already package locale_test, just moved)
- tests/ingredient/: repository integration tests
- tests/recipe/: repository integration tests

mockUserRepo in tests/user/service_test.go redefined locally with
fully-qualified user.* types. Unexported auth.refreshRequest replaced
with a local testRefreshRequest struct in the integration test.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-15 18:57:19 +02:00
parent 0ce111fa08
commit 33a5297c3a
12 changed files with 346 additions and 325 deletions

View File

@@ -0,0 +1,278 @@
//go:build integration
package auth_test
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/food-ai/backend/internal/auth"
"github.com/food-ai/backend/internal/auth/mocks"
"github.com/food-ai/backend/internal/middleware"
"github.com/food-ai/backend/internal/testutil"
"github.com/food-ai/backend/internal/user"
"github.com/go-chi/chi/v5"
)
// testRefreshRequest mirrors the unexported handler request type for test marshalling.
type testRefreshRequest struct {
RefreshToken string `json:"refresh_token"`
}
// testValidator adapts auth.JWTManager to middleware.AccessTokenValidator for tests.
type testValidator struct {
jm *auth.JWTManager
}
func (v *testValidator) ValidateAccessToken(tokenStr string) (*middleware.TokenClaims, error) {
claims, validateError := v.jm.ValidateAccessToken(tokenStr)
if validateError != nil {
return nil, validateError
}
return &middleware.TokenClaims{UserID: claims.UserID, Plan: claims.Plan}, nil
}
func setupIntegrationTest(t *testing.T) (*chi.Mux, *auth.JWTManager) {
t.Helper()
pool := testutil.SetupTestDB(t)
verifier := &mocks.MockTokenVerifier{
VerifyTokenFn: func(ctx context.Context, idToken string) (string, string, string, string, error) {
return "fb-" + idToken, idToken + "@test.com", "Test User", "", nil
},
}
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
repo := user.NewRepository(pool)
svc := auth.NewService(verifier, repo, jm)
handler := auth.NewHandler(svc)
r := chi.NewRouter()
r.Post("/auth/login", handler.Login)
r.Post("/auth/refresh", handler.Refresh)
r.Group(func(r chi.Router) {
r.Use(middleware.Auth(&testValidator{jm: jm}))
r.Post("/auth/logout", handler.Logout)
})
return r, jm
}
func TestIntegration_Login(t *testing.T) {
router, _ := setupIntegrationTest(t)
body := `{"firebase_token":"user1"}`
req := httptest.NewRequest("POST", "/auth/login", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp auth.LoginResponse
json.NewDecoder(rr.Body).Decode(&resp)
if resp.AccessToken == "" {
t.Error("expected non-empty access token")
}
if resp.RefreshToken == "" {
t.Error("expected non-empty refresh token")
}
if resp.User == nil {
t.Fatal("expected user in response")
}
}
func TestIntegration_Login_EmptyToken(t *testing.T) {
router, _ := setupIntegrationTest(t)
body := `{"firebase_token":""}`
req := httptest.NewRequest("POST", "/auth/login", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestIntegration_Login_InvalidBody(t *testing.T) {
router, _ := setupIntegrationTest(t)
req := httptest.NewRequest("POST", "/auth/login", bytes.NewBufferString("invalid"))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestIntegration_Refresh(t *testing.T) {
router, _ := setupIntegrationTest(t)
// First login
loginBody := `{"firebase_token":"user2"}`
loginReq := httptest.NewRequest("POST", "/auth/login", bytes.NewBufferString(loginBody))
loginReq.Header.Set("Content-Type", "application/json")
loginRR := httptest.NewRecorder()
router.ServeHTTP(loginRR, loginReq)
var loginResp auth.LoginResponse
json.NewDecoder(loginRR.Body).Decode(&loginResp)
// Then refresh
refreshBody, _ := json.Marshal(testRefreshRequest{RefreshToken: loginResp.RefreshToken})
refreshReq := httptest.NewRequest("POST", "/auth/refresh", bytes.NewBuffer(refreshBody))
refreshReq.Header.Set("Content-Type", "application/json")
refreshRR := httptest.NewRecorder()
router.ServeHTTP(refreshRR, refreshReq)
if refreshRR.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", refreshRR.Code, refreshRR.Body.String())
}
var resp auth.RefreshResponse
json.NewDecoder(refreshRR.Body).Decode(&resp)
if resp.AccessToken == "" {
t.Error("expected non-empty access token")
}
if resp.RefreshToken == loginResp.RefreshToken {
t.Error("expected rotated refresh token")
}
}
func TestIntegration_Refresh_InvalidToken(t *testing.T) {
router, _ := setupIntegrationTest(t)
body := `{"refresh_token":"nonexistent"}`
req := httptest.NewRequest("POST", "/auth/refresh", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
if rr.Code != http.StatusUnauthorized {
t.Errorf("expected 401, got %d", rr.Code)
}
}
func TestIntegration_Refresh_EmptyToken(t *testing.T) {
router, _ := setupIntegrationTest(t)
body := `{"refresh_token":""}`
req := httptest.NewRequest("POST", "/auth/refresh", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestIntegration_Logout(t *testing.T) {
router, _ := setupIntegrationTest(t)
// Login first
loginBody := `{"firebase_token":"user3"}`
loginReq := httptest.NewRequest("POST", "/auth/login", bytes.NewBufferString(loginBody))
loginReq.Header.Set("Content-Type", "application/json")
loginRR := httptest.NewRecorder()
router.ServeHTTP(loginRR, loginReq)
var loginResp auth.LoginResponse
json.NewDecoder(loginRR.Body).Decode(&loginResp)
// Logout
logoutReq := httptest.NewRequest("POST", "/auth/logout", nil)
logoutReq.Header.Set("Authorization", "Bearer "+loginResp.AccessToken)
logoutRR := httptest.NewRecorder()
router.ServeHTTP(logoutRR, logoutReq)
if logoutRR.Code != http.StatusOK {
t.Errorf("expected 200, got %d: %s", logoutRR.Code, logoutRR.Body.String())
}
}
func TestIntegration_Logout_NoAuth(t *testing.T) {
router, _ := setupIntegrationTest(t)
req := httptest.NewRequest("POST", "/auth/logout", nil)
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
if rr.Code != http.StatusUnauthorized {
t.Errorf("expected 401, got %d", rr.Code)
}
}
func TestIntegration_RefreshAfterLogout(t *testing.T) {
router, _ := setupIntegrationTest(t)
// Login
loginBody := `{"firebase_token":"user4"}`
loginReq := httptest.NewRequest("POST", "/auth/login", bytes.NewBufferString(loginBody))
loginReq.Header.Set("Content-Type", "application/json")
loginRR := httptest.NewRecorder()
router.ServeHTTP(loginRR, loginReq)
var loginResp auth.LoginResponse
json.NewDecoder(loginRR.Body).Decode(&loginResp)
// Logout
logoutReq := httptest.NewRequest("POST", "/auth/logout", nil)
logoutReq.Header.Set("Authorization", "Bearer "+loginResp.AccessToken)
logoutRR := httptest.NewRecorder()
router.ServeHTTP(logoutRR, logoutReq)
// Try to refresh with old token
refreshBody, _ := json.Marshal(testRefreshRequest{RefreshToken: loginResp.RefreshToken})
refreshReq := httptest.NewRequest("POST", "/auth/refresh", bytes.NewBuffer(refreshBody))
refreshReq.Header.Set("Content-Type", "application/json")
refreshRR := httptest.NewRecorder()
router.ServeHTTP(refreshRR, refreshReq)
if refreshRR.Code != http.StatusUnauthorized {
t.Errorf("expected 401 after logout, got %d", refreshRR.Code)
}
}
func TestIntegration_OldRefreshTokenInvalid(t *testing.T) {
router, _ := setupIntegrationTest(t)
// Login
loginBody := `{"firebase_token":"user5"}`
loginReq := httptest.NewRequest("POST", "/auth/login", bytes.NewBufferString(loginBody))
loginReq.Header.Set("Content-Type", "application/json")
loginRR := httptest.NewRecorder()
router.ServeHTTP(loginRR, loginReq)
var loginResp auth.LoginResponse
json.NewDecoder(loginRR.Body).Decode(&loginResp)
oldRefreshToken := loginResp.RefreshToken
// Refresh (rotates token)
refreshBody, _ := json.Marshal(testRefreshRequest{RefreshToken: oldRefreshToken})
refreshReq := httptest.NewRequest("POST", "/auth/refresh", bytes.NewBuffer(refreshBody))
refreshReq.Header.Set("Content-Type", "application/json")
refreshRR := httptest.NewRecorder()
router.ServeHTTP(refreshRR, refreshReq)
// Try old refresh token again
oldRefreshReq := httptest.NewRequest("POST", "/auth/refresh", bytes.NewBuffer(refreshBody))
oldRefreshReq.Header.Set("Content-Type", "application/json")
oldRefreshRR := httptest.NewRecorder()
router.ServeHTTP(oldRefreshRR, oldRefreshReq)
if oldRefreshRR.Code != http.StatusUnauthorized {
t.Errorf("expected 401 for old refresh token, got %d", oldRefreshRR.Code)
}
}

View File

@@ -0,0 +1,88 @@
package auth_test
import (
"testing"
"time"
"github.com/food-ai/backend/internal/auth"
)
func TestGenerateAccessToken(t *testing.T) {
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
token, tokenError := jm.GenerateAccessToken("user-123", "free")
if tokenError != nil {
t.Fatalf("unexpected error: %v", tokenError)
}
if token == "" {
t.Fatal("expected non-empty token")
}
}
func TestValidateAccessToken_Valid(t *testing.T) {
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
token, _ := jm.GenerateAccessToken("user-123", "free")
claims, validateError := jm.ValidateAccessToken(token)
if validateError != nil {
t.Fatalf("unexpected error: %v", validateError)
}
if claims.UserID != "user-123" {
t.Errorf("expected user_id 'user-123', got %q", claims.UserID)
}
if claims.Plan != "free" {
t.Errorf("expected plan 'free', got %q", claims.Plan)
}
}
func TestValidateAccessToken_Expired(t *testing.T) {
jm := auth.NewJWTManager("test-secret", -1*time.Second, 720*time.Hour)
token, _ := jm.GenerateAccessToken("user-123", "free")
_, validateError := jm.ValidateAccessToken(token)
if validateError == nil {
t.Fatal("expected error for expired token")
}
}
func TestValidateAccessToken_WrongSecret(t *testing.T) {
jm1 := auth.NewJWTManager("secret-1", 15*time.Minute, 720*time.Hour)
jm2 := auth.NewJWTManager("secret-2", 15*time.Minute, 720*time.Hour)
token, _ := jm1.GenerateAccessToken("user-123", "free")
_, validateError := jm2.ValidateAccessToken(token)
if validateError == nil {
t.Fatal("expected error for wrong secret")
}
}
func TestValidateAccessToken_InvalidToken(t *testing.T) {
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
_, validateError := jm.ValidateAccessToken("invalid-token")
if validateError == nil {
t.Fatal("expected error for invalid token")
}
}
func TestGenerateRefreshToken(t *testing.T) {
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
token, expiresAt := jm.GenerateRefreshToken()
if token == "" {
t.Fatal("expected non-empty refresh token")
}
if expiresAt.Before(time.Now()) {
t.Fatal("expected future expiration time")
}
}
func TestGenerateRefreshToken_Unique(t *testing.T) {
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
token1, _ := jm.GenerateRefreshToken()
token2, _ := jm.GenerateRefreshToken()
if token1 == token2 {
t.Fatal("expected unique refresh tokens")
}
}

View File

@@ -0,0 +1,214 @@
package auth_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/food-ai/backend/internal/auth"
"github.com/food-ai/backend/internal/auth/mocks"
"github.com/food-ai/backend/internal/user"
umocks "github.com/food-ai/backend/internal/user/mocks"
)
func newTestService(verifier *mocks.MockTokenVerifier, repo *umocks.MockUserRepository) *auth.Service {
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
return auth.NewService(verifier, repo, jm)
}
func TestLogin_Success(t *testing.T) {
verifier := &mocks.MockTokenVerifier{
VerifyTokenFn: func(ctx context.Context, idToken string) (string, string, string, string, error) {
return "fb-uid", "test@example.com", "Test User", "https://avatar.url", nil
},
}
repo := &umocks.MockUserRepository{
UpsertByFirebaseUIDFn: func(ctx context.Context, uid, email, name, avatarURL string) (*user.User, error) {
return &user.User{ID: "user-1", Email: email, Name: name, Plan: "free"}, nil
},
SetRefreshTokenFn: func(ctx context.Context, id, token string, expiresAt time.Time) error {
return nil
},
}
svc := newTestService(verifier, repo)
resp, loginError := svc.Login(context.Background(), "firebase-token")
if loginError != nil {
t.Fatalf("unexpected error: %v", loginError)
}
if resp.AccessToken == "" {
t.Error("expected non-empty access token")
}
if resp.RefreshToken == "" {
t.Error("expected non-empty refresh token")
}
if resp.User.ID != "user-1" {
t.Errorf("expected user ID 'user-1', got %q", resp.User.ID)
}
}
func TestLogin_InvalidFirebaseToken(t *testing.T) {
verifier := &mocks.MockTokenVerifier{
VerifyTokenFn: func(ctx context.Context, idToken string) (string, string, string, string, error) {
return "", "", "", "", fmt.Errorf("invalid token")
},
}
repo := &umocks.MockUserRepository{}
svc := newTestService(verifier, repo)
_, loginError := svc.Login(context.Background(), "bad-token")
if loginError == nil {
t.Fatal("expected error for invalid firebase token")
}
}
func TestLogin_UpsertError(t *testing.T) {
verifier := &mocks.MockTokenVerifier{
VerifyTokenFn: func(ctx context.Context, idToken string) (string, string, string, string, error) {
return "fb-uid", "test@example.com", "Test", "", nil
},
}
repo := &umocks.MockUserRepository{
UpsertByFirebaseUIDFn: func(ctx context.Context, uid, email, name, avatarURL string) (*user.User, error) {
return nil, fmt.Errorf("db error")
},
}
svc := newTestService(verifier, repo)
_, loginError := svc.Login(context.Background(), "token")
if loginError == nil {
t.Fatal("expected error for upsert failure")
}
}
func TestLogin_SetRefreshTokenError(t *testing.T) {
verifier := &mocks.MockTokenVerifier{
VerifyTokenFn: func(ctx context.Context, idToken string) (string, string, string, string, error) {
return "fb-uid", "test@example.com", "Test", "", nil
},
}
repo := &umocks.MockUserRepository{
UpsertByFirebaseUIDFn: func(ctx context.Context, uid, email, name, avatarURL string) (*user.User, error) {
return &user.User{ID: "user-1", Plan: "free"}, nil
},
SetRefreshTokenFn: func(ctx context.Context, id, token string, expiresAt time.Time) error {
return fmt.Errorf("db error")
},
}
svc := newTestService(verifier, repo)
_, loginError := svc.Login(context.Background(), "token")
if loginError == nil {
t.Fatal("expected error for set refresh token failure")
}
}
func TestRefresh_Success(t *testing.T) {
repo := &umocks.MockUserRepository{
FindByRefreshTokenFn: func(ctx context.Context, token string) (*user.User, error) {
return &user.User{ID: "user-1", Plan: "free"}, nil
},
SetRefreshTokenFn: func(ctx context.Context, id, token string, expiresAt time.Time) error {
return nil
},
}
verifier := &mocks.MockTokenVerifier{}
svc := newTestService(verifier, repo)
resp, refreshError := svc.Refresh(context.Background(), "valid-refresh-token")
if refreshError != nil {
t.Fatalf("unexpected error: %v", refreshError)
}
if resp.AccessToken == "" {
t.Error("expected non-empty access token")
}
if resp.RefreshToken == "" {
t.Error("expected non-empty refresh token")
}
}
func TestRefresh_InvalidToken(t *testing.T) {
repo := &umocks.MockUserRepository{
FindByRefreshTokenFn: func(ctx context.Context, token string) (*user.User, error) {
return nil, fmt.Errorf("not found")
},
}
verifier := &mocks.MockTokenVerifier{}
svc := newTestService(verifier, repo)
_, refreshError := svc.Refresh(context.Background(), "bad-token")
if refreshError == nil {
t.Fatal("expected error for invalid refresh token")
}
}
func TestRefresh_SetRefreshTokenError(t *testing.T) {
repo := &umocks.MockUserRepository{
FindByRefreshTokenFn: func(ctx context.Context, token string) (*user.User, error) {
return &user.User{ID: "user-1", Plan: "free"}, nil
},
SetRefreshTokenFn: func(ctx context.Context, id, token string, expiresAt time.Time) error {
return fmt.Errorf("db error")
},
}
verifier := &mocks.MockTokenVerifier{}
svc := newTestService(verifier, repo)
_, refreshError := svc.Refresh(context.Background(), "valid-token")
if refreshError == nil {
t.Fatal("expected error")
}
}
func TestLogout_Success(t *testing.T) {
repo := &umocks.MockUserRepository{
ClearRefreshTokenFn: func(ctx context.Context, id string) error {
return nil
},
}
verifier := &mocks.MockTokenVerifier{}
svc := newTestService(verifier, repo)
logoutError := svc.Logout(context.Background(), "user-1")
if logoutError != nil {
t.Fatalf("unexpected error: %v", logoutError)
}
}
func TestLogout_Error(t *testing.T) {
repo := &umocks.MockUserRepository{
ClearRefreshTokenFn: func(ctx context.Context, id string) error {
return fmt.Errorf("db error")
},
}
verifier := &mocks.MockTokenVerifier{}
svc := newTestService(verifier, repo)
logoutError := svc.Logout(context.Background(), "user-1")
if logoutError == nil {
t.Fatal("expected error")
}
}
func TestLogin_ExpiresIn(t *testing.T) {
verifier := &mocks.MockTokenVerifier{
VerifyTokenFn: func(ctx context.Context, idToken string) (string, string, string, string, error) {
return "fb-uid", "test@example.com", "Test", "", nil
},
}
repo := &umocks.MockUserRepository{
UpsertByFirebaseUIDFn: func(ctx context.Context, uid, email, name, avatarURL string) (*user.User, error) {
return &user.User{ID: "user-1", Plan: "free"}, nil
},
SetRefreshTokenFn: func(ctx context.Context, id, token string, expiresAt time.Time) error {
return nil
},
}
svc := newTestService(verifier, repo)
resp, _ := svc.Login(context.Background(), "token")
if resp.ExpiresIn != 900 {
t.Errorf("expected expires_in 900, got %d", resp.ExpiresIn)
}
}

View File

@@ -0,0 +1,297 @@
//go:build integration
package ingredient_test
import (
"context"
"testing"
"github.com/food-ai/backend/internal/ingredient"
"github.com/food-ai/backend/internal/locale"
"github.com/food-ai/backend/internal/testutil"
)
func TestIngredientRepository_Upsert_Insert(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := ingredient.NewRepository(pool)
ctx := context.Background()
cat := "produce"
unit := "g"
cal := 52.0
mapping := &ingredient.IngredientMapping{
CanonicalName: "apple",
Category: &cat,
DefaultUnit: &unit,
CaloriesPer100g: &cal,
}
got, upsertError := repo.Upsert(ctx, mapping)
if upsertError != nil {
t.Fatalf("upsert: %v", upsertError)
}
if got.ID == "" {
t.Error("expected non-empty ID")
}
if got.CanonicalName != "apple" {
t.Errorf("canonical_name: want apple, got %s", got.CanonicalName)
}
if *got.CaloriesPer100g != 52.0 {
t.Errorf("calories: want 52.0, got %v", got.CaloriesPer100g)
}
}
func TestIngredientRepository_Upsert_ConflictUpdates(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := ingredient.NewRepository(pool)
ctx := context.Background()
cat := "produce"
unit := "g"
first := &ingredient.IngredientMapping{
CanonicalName: "banana",
Category: &cat,
DefaultUnit: &unit,
}
got1, firstUpsertError := repo.Upsert(ctx, first)
if firstUpsertError != nil {
t.Fatalf("first upsert: %v", firstUpsertError)
}
cal := 89.0
second := &ingredient.IngredientMapping{
CanonicalName: "banana",
Category: &cat,
DefaultUnit: &unit,
CaloriesPer100g: &cal,
}
got2, secondUpsertError := repo.Upsert(ctx, second)
if secondUpsertError != nil {
t.Fatalf("second upsert: %v", secondUpsertError)
}
if got1.ID != got2.ID {
t.Errorf("ID changed on conflict update: %s != %s", got1.ID, got2.ID)
}
if got2.CanonicalName != "banana" {
t.Errorf("canonical_name not updated: got %s", got2.CanonicalName)
}
if got2.CaloriesPer100g == nil || *got2.CaloriesPer100g != 89.0 {
t.Errorf("calories not updated: got %v", got2.CaloriesPer100g)
}
}
func TestIngredientRepository_GetByID_Found(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := ingredient.NewRepository(pool)
ctx := context.Background()
cat := "dairy"
unit := "g"
saved, upsertError := repo.Upsert(ctx, &ingredient.IngredientMapping{
CanonicalName: "cheese",
Category: &cat,
DefaultUnit: &unit,
})
if upsertError != nil {
t.Fatalf("upsert: %v", upsertError)
}
got, getError := repo.GetByID(ctx, saved.ID)
if getError != nil {
t.Fatalf("get: %v", getError)
}
if got == nil {
t.Fatal("expected non-nil result")
}
if got.CanonicalName != "cheese" {
t.Errorf("want cheese, got %s", got.CanonicalName)
}
}
func TestIngredientRepository_GetByID_NotFound(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := ingredient.NewRepository(pool)
ctx := context.Background()
got, getError := repo.GetByID(ctx, "00000000-0000-0000-0000-000000000000")
if getError != nil {
t.Fatalf("unexpected error: %v", getError)
}
if got != nil {
t.Error("expected nil result for missing ID")
}
}
func TestIngredientRepository_ListMissingTranslation(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := ingredient.NewRepository(pool)
ctx := context.Background()
cat := "produce"
unit := "g"
// Insert 3 without any translation.
for _, name := range []string{"carrot", "onion", "garlic"} {
_, upsertError := repo.Upsert(ctx, &ingredient.IngredientMapping{
CanonicalName: name,
Category: &cat,
DefaultUnit: &unit,
})
if upsertError != nil {
t.Fatalf("upsert %s: %v", name, upsertError)
}
}
// Insert 1 and add a translation — should not appear in the result.
saved, upsertError := repo.Upsert(ctx, &ingredient.IngredientMapping{
CanonicalName: "tomato",
Category: &cat,
DefaultUnit: &unit,
})
if upsertError != nil {
t.Fatalf("upsert tomato: %v", upsertError)
}
if translationError := repo.UpsertTranslation(ctx, saved.ID, "ru", "помидор"); translationError != nil {
t.Fatalf("upsert translation: %v", translationError)
}
missing, listError := repo.ListMissingTranslation(ctx, "ru", 10, 0)
if listError != nil {
t.Fatalf("list missing translation: %v", listError)
}
for _, mapping := range missing {
if mapping.CanonicalName == "tomato" {
t.Error("translated ingredient should not appear in ListMissingTranslation")
}
}
if len(missing) < 3 {
t.Errorf("expected at least 3 untranslated, got %d", len(missing))
}
}
func TestIngredientRepository_UpsertTranslation(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := ingredient.NewRepository(pool)
ctx := context.Background()
cat := "meat"
unit := "g"
saved, upsertError := repo.Upsert(ctx, &ingredient.IngredientMapping{
CanonicalName: "chicken_breast",
Category: &cat,
DefaultUnit: &unit,
})
if upsertError != nil {
t.Fatalf("upsert: %v", upsertError)
}
if translationError := repo.UpsertTranslation(ctx, saved.ID, "ru", "куриная грудка"); translationError != nil {
t.Fatalf("upsert translation: %v", translationError)
}
// Retrieve with Russian context — CanonicalName should be the Russian name.
russianContext := locale.WithLang(ctx, "ru")
got, getError := repo.GetByID(russianContext, saved.ID)
if getError != nil {
t.Fatalf("get by id: %v", getError)
}
if got.CanonicalName != "куриная грудка" {
t.Errorf("expected CanonicalName='куриная грудка', got %q", got.CanonicalName)
}
// Retrieve with English context (default) — CanonicalName should be the English name.
englishContext := locale.WithLang(ctx, "en")
gotEnglish, getEnglishError := repo.GetByID(englishContext, saved.ID)
if getEnglishError != nil {
t.Fatalf("get by id (en): %v", getEnglishError)
}
if gotEnglish.CanonicalName != "chicken_breast" {
t.Errorf("expected English CanonicalName='chicken_breast', got %q", gotEnglish.CanonicalName)
}
}
func TestIngredientRepository_UpsertAliases(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := ingredient.NewRepository(pool)
ctx := context.Background()
cat := "produce"
unit := "g"
saved, upsertError := repo.Upsert(ctx, &ingredient.IngredientMapping{
CanonicalName: "apple_test_aliases",
Category: &cat,
DefaultUnit: &unit,
})
if upsertError != nil {
t.Fatalf("upsert: %v", upsertError)
}
aliases := []string{"apple", "apples", "red apple"}
if aliasError := repo.UpsertAliases(ctx, saved.ID, "en", aliases); aliasError != nil {
t.Fatalf("upsert aliases: %v", aliasError)
}
// Idempotent — second call should not fail.
if aliasError := repo.UpsertAliases(ctx, saved.ID, "en", aliases); aliasError != nil {
t.Fatalf("second upsert aliases: %v", aliasError)
}
// Retrieve with English context — aliases should appear.
englishContext := locale.WithLang(ctx, "en")
got, getError := repo.GetByID(englishContext, saved.ID)
if getError != nil {
t.Fatalf("get by id: %v", getError)
}
if string(got.Aliases) == "[]" || string(got.Aliases) == "null" {
t.Errorf("expected non-empty aliases, got %s", got.Aliases)
}
}
func TestIngredientRepository_UpsertCategoryTranslation(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := ingredient.NewRepository(pool)
ctx := context.Background()
// Upsert an ingredient with a known category.
cat := "dairy"
unit := "g"
saved, upsertError := repo.Upsert(ctx, &ingredient.IngredientMapping{
CanonicalName: "milk_test_category",
Category: &cat,
DefaultUnit: &unit,
})
if upsertError != nil {
t.Fatalf("upsert: %v", upsertError)
}
// The migration already inserted Russian translations for known categories.
// Retrieve with Russian context — CategoryName should be set.
russianContext := locale.WithLang(ctx, "ru")
got, getError := repo.GetByID(russianContext, saved.ID)
if getError != nil {
t.Fatalf("get by id: %v", getError)
}
if got.CategoryName == nil || *got.CategoryName == "" {
t.Error("expected non-empty CategoryName for 'dairy' in Russian")
}
// Upsert a new translation and verify it is returned.
if translationError := repo.UpsertCategoryTranslation(ctx, "dairy", "de", "Milchprodukte"); translationError != nil {
t.Fatalf("upsert category translation: %v", translationError)
}
germanContext := locale.WithLang(ctx, "de")
gotGerman, getGermanError := repo.GetByID(germanContext, saved.ID)
if getGermanError != nil {
t.Fatalf("get by id (de): %v", getGermanError)
}
if gotGerman.CategoryName == nil || *gotGerman.CategoryName != "Milchprodukte" {
t.Errorf("expected CategoryName='Milchprodukte', got %v", gotGerman.CategoryName)
}
}

View File

@@ -0,0 +1,94 @@
package locale_test
import (
"context"
"net/http"
"testing"
"github.com/food-ai/backend/internal/locale"
)
func TestParse(t *testing.T) {
tests := []struct {
name string
acceptLang string
want string
}{
{
name: "empty header returns default",
acceptLang: "",
want: locale.Default,
},
{
name: "exact match",
acceptLang: "ru",
want: "ru",
},
{
name: "region subtag stripped",
acceptLang: "ru-RU",
want: "ru",
},
{
name: "full browser header picks first supported",
acceptLang: "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
want: "ru",
},
{
name: "unsupported first falls through to supported",
acceptLang: "xx-XX,ru;q=0.8",
want: "ru",
},
{
name: "completely unsupported returns default",
acceptLang: "xx-XX,yy-YY",
want: locale.Default,
},
{
name: "chinese region subtag",
acceptLang: "zh-CN,zh;q=0.9",
want: "zh",
},
{
name: "case insensitive",
acceptLang: "RU-RU",
want: "ru",
},
{
name: "whitespace around tag",
acceptLang: " ru ",
want: "ru",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := locale.Parse(tc.acceptLang)
if got != tc.want {
t.Errorf("Parse(%q) = %q, want %q", tc.acceptLang, got, tc.want)
}
})
}
}
func TestWithLangAndFromContext(t *testing.T) {
ctx := context.Background()
if got := locale.FromContext(ctx); got != locale.Default {
t.Errorf("FromContext on empty ctx = %q, want %q", got, locale.Default)
}
ctx = locale.WithLang(ctx, "ru")
if got := locale.FromContext(ctx); got != "ru" {
t.Errorf("FromContext after WithLang(ru) = %q, want %q", got, "ru")
}
}
func TestFromRequest(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Accept-Language", "es-ES,es;q=0.9")
if got := locale.FromRequest(req); got != "es" {
t.Errorf("FromRequest = %q, want %q", got, "es")
}
}

View File

@@ -0,0 +1,193 @@
package middleware_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/food-ai/backend/internal/middleware"
"github.com/golang-jwt/jwt/v5"
)
// testJWTClaims mirrors auth.Claims for test token generation without importing auth.
type testJWTClaims struct {
UserID string `json:"user_id"`
Plan string `json:"plan"`
jwt.RegisteredClaims
}
func generateTestToken(secret string, userID, plan string, duration time.Duration) string {
claims := testJWTClaims{
UserID: userID,
Plan: plan,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(duration)),
IssuedAt: jwt.NewNumericDate(time.Now()),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenString, _ := token.SignedString([]byte(secret))
return tokenString
}
// testAccessValidator implements middleware.AccessTokenValidator for tests.
type testAccessValidator struct {
secret string
}
func (v *testAccessValidator) ValidateAccessToken(tokenStr string) (*middleware.TokenClaims, error) {
token, parseError := jwt.ParseWithClaims(tokenStr, &testJWTClaims{}, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method")
}
return []byte(v.secret), nil
})
if parseError != nil {
return nil, parseError
}
claims, ok := token.Claims.(*testJWTClaims)
if !ok || !token.Valid {
return nil, fmt.Errorf("invalid token")
}
return &middleware.TokenClaims{UserID: claims.UserID, Plan: claims.Plan}, nil
}
// failingValidator always returns an error.
type failingValidator struct{}
func (v *failingValidator) ValidateAccessToken(tokenStr string) (*middleware.TokenClaims, error) {
return nil, fmt.Errorf("invalid token")
}
func TestAuth_ValidToken(t *testing.T) {
validator := &testAccessValidator{secret: "test-secret"}
token := generateTestToken("test-secret", "user-1", "free", 15*time.Minute)
handler := middleware.Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userID := middleware.UserIDFromCtx(r.Context())
if userID != "user-1" {
t.Errorf("expected user-1, got %s", userID)
}
plan := middleware.UserPlanFromCtx(r.Context())
if plan != "free" {
t.Errorf("expected free, got %s", plan)
}
w.WriteHeader(http.StatusOK)
}))
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Authorization", "Bearer "+token)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("expected 200, got %d", rr.Code)
}
}
func TestAuth_MissingHeader(t *testing.T) {
validator := &testAccessValidator{secret: "test-secret"}
handler := middleware.Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Error("handler should not be called")
}))
req := httptest.NewRequest("GET", "/", nil)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusUnauthorized {
t.Errorf("expected 401, got %d", rr.Code)
}
}
func TestAuth_InvalidBearerFormat(t *testing.T) {
validator := &testAccessValidator{secret: "test-secret"}
handler := middleware.Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Error("handler should not be called")
}))
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Authorization", "Basic abc123")
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusUnauthorized {
t.Errorf("expected 401, got %d", rr.Code)
}
}
func TestAuth_ExpiredToken(t *testing.T) {
validator := &testAccessValidator{secret: "test-secret"}
token := generateTestToken("test-secret", "user-1", "free", -1*time.Second)
handler := middleware.Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Error("handler should not be called")
}))
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Authorization", "Bearer "+token)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusUnauthorized {
t.Errorf("expected 401, got %d", rr.Code)
}
}
func TestAuth_InvalidToken(t *testing.T) {
validator := &testAccessValidator{secret: "test-secret"}
handler := middleware.Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Error("handler should not be called")
}))
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Authorization", "Bearer invalid-token")
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusUnauthorized {
t.Errorf("expected 401, got %d", rr.Code)
}
}
func TestAuth_PaidPlan(t *testing.T) {
validator := &testAccessValidator{secret: "test-secret"}
token := generateTestToken("test-secret", "user-1", "paid", 15*time.Minute)
handler := middleware.Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
plan := middleware.UserPlanFromCtx(r.Context())
if plan != "paid" {
t.Errorf("expected paid, got %s", plan)
}
w.WriteHeader(http.StatusOK)
}))
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Authorization", "Bearer "+token)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("expected 200, got %d", rr.Code)
}
}
func TestAuth_EmptyBearer(t *testing.T) {
handler := middleware.Auth(&failingValidator{})(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Error("handler should not be called")
}))
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Authorization", "Bearer ")
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusUnauthorized {
t.Errorf("expected 401, got %d", rr.Code)
}
}

View File

@@ -0,0 +1,51 @@
package middleware_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/food-ai/backend/internal/middleware"
)
func TestRecovery_NoPanic(t *testing.T) {
handler := middleware.Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
req := httptest.NewRequest("GET", "/", nil)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("expected 200, got %d", rr.Code)
}
}
func TestRecovery_CatchesPanic(t *testing.T) {
handler := middleware.Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("test panic")
}))
req := httptest.NewRequest("GET", "/", nil)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusInternalServerError {
t.Errorf("expected 500, got %d", rr.Code)
}
}
func TestRecovery_CatchesPanicWithError(t *testing.T) {
handler := middleware.Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic(42)
}))
req := httptest.NewRequest("GET", "/", nil)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusInternalServerError {
t.Errorf("expected 500, got %d", rr.Code)
}
}

View File

@@ -0,0 +1,61 @@
package middleware_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/food-ai/backend/internal/middleware"
)
func TestRequestID_GeneratesNew(t *testing.T) {
handler := middleware.RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := middleware.RequestIDFromCtx(r.Context())
if id == "" {
t.Error("expected non-empty request ID in context")
}
}))
req := httptest.NewRequest("GET", "/", nil)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Header().Get("X-Request-ID") == "" {
t.Error("expected X-Request-ID in response header")
}
}
func TestRequestID_PreservesExisting(t *testing.T) {
handler := middleware.RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
id := middleware.RequestIDFromCtx(r.Context())
if id != "existing-id" {
t.Errorf("expected 'existing-id', got %q", id)
}
}))
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("X-Request-ID", "existing-id")
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if rr.Header().Get("X-Request-ID") != "existing-id" {
t.Error("expected preserved X-Request-ID")
}
}
func TestRequestID_UniquePerRequest(t *testing.T) {
var ids []string
handler := middleware.RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ids = append(ids, middleware.RequestIDFromCtx(r.Context()))
}))
for i := 0; i < 3; i++ {
req := httptest.NewRequest("GET", "/", nil)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
}
if ids[0] == ids[1] || ids[1] == ids[2] {
t.Error("expected unique IDs for each request")
}
}

View File

@@ -0,0 +1,36 @@
//go:build integration
package recipe_test
import (
"context"
"testing"
"github.com/food-ai/backend/internal/recipe"
"github.com/food-ai/backend/internal/testutil"
)
func TestRecipeRepository_GetByID_NotFound(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := recipe.NewRepository(pool)
ctx := context.Background()
got, getError := repo.GetByID(ctx, "00000000-0000-0000-0000-000000000000")
if getError != nil {
t.Fatalf("unexpected error: %v", getError)
}
if got != nil {
t.Error("expected nil for non-existent ID")
}
}
func TestRecipeRepository_Count(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := recipe.NewRepository(pool)
ctx := context.Background()
_, countError := repo.Count(ctx)
if countError != nil {
t.Fatalf("count: %v", countError)
}
}

View File

@@ -0,0 +1,113 @@
package user_test
import (
"testing"
"time"
"github.com/food-ai/backend/internal/user"
)
func ptr[T any](v T) *T { return &v }
func TestAgeFromDOB_Nil(t *testing.T) {
if user.AgeFromDOB(nil) != nil {
t.Fatal("expected nil for nil input")
}
}
func TestAgeFromDOB_InvalidFormat(t *testing.T) {
s := "not-a-date"
if user.AgeFromDOB(&s) != nil {
t.Fatal("expected nil for invalid format")
}
}
func TestAgeFromDOB_ExactAge(t *testing.T) {
dob := time.Now().AddDate(-30, 0, 0).Format("2006-01-02")
age := user.AgeFromDOB(&dob)
if age == nil {
t.Fatal("expected non-nil result")
}
if *age != 30 {
t.Errorf("expected 30, got %d", *age)
}
}
func TestAgeFromDOB_BeforeBirthday(t *testing.T) {
// Birthday is one day in the future relative to today-25y, so age should be 24
now := time.Now()
dob := time.Date(now.Year()-25, now.Month(), now.Day()+1, 0, 0, 0, 0, time.UTC).Format("2006-01-02")
age := user.AgeFromDOB(&dob)
if age == nil {
t.Fatal("expected non-nil result")
}
if *age != 24 {
t.Errorf("expected 24, got %d", *age)
}
}
func TestCalculateDailyCalories_MaleMaintain(t *testing.T) {
cal := user.CalculateDailyCalories(ptr(180), ptr(80.0), ptr(30), ptr("male"), ptr("moderate"), ptr("maintain"))
if cal == nil {
t.Fatal("expected non-nil result")
}
// BMR = 10*80 + 6.25*180 - 5*30 + 5 = 800 + 1125 - 150 + 5 = 1780
// TDEE = 1780 * 1.55 = 2759
if *cal != 2759 {
t.Errorf("expected 2759, got %d", *cal)
}
}
func TestCalculateDailyCalories_FemaleLose(t *testing.T) {
cal := user.CalculateDailyCalories(ptr(165), ptr(60.0), ptr(25), ptr("female"), ptr("low"), ptr("lose"))
if cal == nil {
t.Fatal("expected non-nil result")
}
// BMR = 10*60 + 6.25*165 - 5*25 - 161 = 600 + 1031.25 - 125 - 161 = 1345.25
// TDEE = 1345.25 * 1.375 = 1849.72
// Goal: -500 = 1349.72 → 1350
if *cal != 1350 {
t.Errorf("expected 1350, got %d", *cal)
}
}
func TestCalculateDailyCalories_MaleGain(t *testing.T) {
cal := user.CalculateDailyCalories(ptr(175), ptr(70.0), ptr(28), ptr("male"), ptr("high"), ptr("gain"))
if cal == nil {
t.Fatal("expected non-nil result")
}
// BMR = 10*70 + 6.25*175 - 5*28 + 5 = 700 + 1093.75 - 140 + 5 = 1658.75
// TDEE = 1658.75 * 1.725 = 2861.34
// Goal: +300 = 3161.34 → 3161
if *cal != 3161 {
t.Errorf("expected 3161, got %d", *cal)
}
}
func TestCalculateDailyCalories_NilHeight(t *testing.T) {
cal := user.CalculateDailyCalories(nil, ptr(80.0), ptr(30), ptr("male"), ptr("moderate"), ptr("maintain"))
if cal != nil {
t.Fatal("expected nil when height is nil")
}
}
func TestCalculateDailyCalories_NilWeight(t *testing.T) {
cal := user.CalculateDailyCalories(ptr(180), nil, ptr(30), ptr("male"), ptr("moderate"), ptr("maintain"))
if cal != nil {
t.Fatal("expected nil when weight is nil")
}
}
func TestCalculateDailyCalories_InvalidGender(t *testing.T) {
cal := user.CalculateDailyCalories(ptr(180), ptr(80.0), ptr(30), ptr("other"), ptr("moderate"), ptr("maintain"))
if cal != nil {
t.Fatal("expected nil for invalid gender")
}
}
func TestCalculateDailyCalories_InvalidActivity(t *testing.T) {
cal := user.CalculateDailyCalories(ptr(180), ptr(80.0), ptr(30), ptr("male"), ptr("extreme"), ptr("maintain"))
if cal != nil {
t.Fatal("expected nil for invalid activity")
}
}

View File

@@ -0,0 +1,210 @@
//go:build integration
package user_test
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/food-ai/backend/internal/testutil"
"github.com/food-ai/backend/internal/user"
)
func TestRepository_UpsertByFirebaseUID_Insert(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := user.NewRepository(pool)
ctx := context.Background()
u, upsertError := repo.UpsertByFirebaseUID(ctx, "fb-123", "test@example.com", "Test User", "https://avatar.url")
if upsertError != nil {
t.Fatalf("unexpected error: %v", upsertError)
}
if u.FirebaseUID != "fb-123" {
t.Errorf("expected fb-123, got %s", u.FirebaseUID)
}
if u.Email != "test@example.com" {
t.Errorf("expected test@example.com, got %s", u.Email)
}
if u.Plan != "free" {
t.Errorf("expected free, got %s", u.Plan)
}
}
func TestRepository_UpsertByFirebaseUID_Update(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := user.NewRepository(pool)
ctx := context.Background()
_, _ = repo.UpsertByFirebaseUID(ctx, "fb-123", "old@example.com", "Old Name", "")
u, upsertError := repo.UpsertByFirebaseUID(ctx, "fb-123", "new@example.com", "New Name", "https://new-avatar.url")
if upsertError != nil {
t.Fatalf("unexpected error: %v", upsertError)
}
if u.Email != "new@example.com" {
t.Errorf("expected new email, got %s", u.Email)
}
// Name should not be overwritten if already set
if u.Name != "Old Name" {
t.Errorf("expected name to be preserved as 'Old Name', got %s", u.Name)
}
}
func TestRepository_GetByID(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := user.NewRepository(pool)
ctx := context.Background()
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-get", "get@example.com", "Get User", "")
u, getError := repo.GetByID(ctx, created.ID)
if getError != nil {
t.Fatalf("unexpected error: %v", getError)
}
if u.ID != created.ID {
t.Errorf("expected %s, got %s", created.ID, u.ID)
}
}
func TestRepository_GetByID_NotFound(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := user.NewRepository(pool)
ctx := context.Background()
_, getError := repo.GetByID(ctx, "00000000-0000-0000-0000-000000000000")
if getError == nil {
t.Fatal("expected error for non-existent user")
}
}
func TestRepository_Update(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := user.NewRepository(pool)
ctx := context.Background()
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-upd", "upd@example.com", "Update User", "")
height := 180
u, updateError := repo.Update(ctx, created.ID, user.UpdateProfileRequest{HeightCM: &height})
if updateError != nil {
t.Fatalf("unexpected error: %v", updateError)
}
if u.HeightCM == nil || *u.HeightCM != 180 {
t.Errorf("expected height 180, got %v", u.HeightCM)
}
}
func TestRepository_Update_MultipleFields(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := user.NewRepository(pool)
ctx := context.Background()
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-multi", "multi@example.com", "Multi User", "")
height := 175
weight := 70.5
dob := "2001-03-09"
gender := "male"
activity := "moderate"
goal := "maintain"
u, updateError := repo.Update(ctx, created.ID, user.UpdateProfileRequest{
HeightCM: &height,
WeightKG: &weight,
DateOfBirth: &dob,
Gender: &gender,
Activity: &activity,
Goal: &goal,
})
if updateError != nil {
t.Fatalf("unexpected error: %v", updateError)
}
if u.HeightCM == nil || *u.HeightCM != 175 {
t.Errorf("expected 175, got %v", u.HeightCM)
}
if u.WeightKG == nil || *u.WeightKG != 70.5 {
t.Errorf("expected 70.5, got %v", u.WeightKG)
}
}
func TestRepository_Update_Preferences(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := user.NewRepository(pool)
ctx := context.Background()
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-prefs", "prefs@example.com", "Prefs User", "")
prefs := json.RawMessage(`{"cuisines":["russian","asian"]}`)
u, updateError := repo.Update(ctx, created.ID, user.UpdateProfileRequest{Preferences: &prefs})
if updateError != nil {
t.Fatalf("unexpected error: %v", updateError)
}
var preferences map[string]interface{}
json.Unmarshal(u.Preferences, &preferences)
if preferences["cuisines"] == nil {
t.Error("expected cuisines in preferences")
}
}
func TestRepository_SetAndFindRefreshToken(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := user.NewRepository(pool)
ctx := context.Background()
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-token", "token@example.com", "Token User", "")
setError := repo.SetRefreshToken(ctx, created.ID, "refresh-token-123", time.Now().Add(24*time.Hour))
if setError != nil {
t.Fatalf("unexpected error setting token: %v", setError)
}
u, findError := repo.FindByRefreshToken(ctx, "refresh-token-123")
if findError != nil {
t.Fatalf("unexpected error finding by token: %v", findError)
}
if u.ID != created.ID {
t.Errorf("expected %s, got %s", created.ID, u.ID)
}
}
func TestRepository_FindByRefreshToken_Expired(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := user.NewRepository(pool)
ctx := context.Background()
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-expired", "expired@example.com", "Expired User", "")
_ = repo.SetRefreshToken(ctx, created.ID, "expired-token", time.Now().Add(-1*time.Hour))
_, findError := repo.FindByRefreshToken(ctx, "expired-token")
if findError == nil {
t.Fatal("expected error for expired token")
}
}
func TestRepository_ClearRefreshToken(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := user.NewRepository(pool)
ctx := context.Background()
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-clear", "clear@example.com", "Clear User", "")
_ = repo.SetRefreshToken(ctx, created.ID, "token-to-clear", time.Now().Add(24*time.Hour))
clearError := repo.ClearRefreshToken(ctx, created.ID)
if clearError != nil {
t.Fatalf("unexpected error: %v", clearError)
}
_, findError := repo.FindByRefreshToken(ctx, "token-to-clear")
if findError == nil {
t.Fatal("expected error after clearing token")
}
}
func TestRepository_Update_NoFields(t *testing.T) {
pool := testutil.SetupTestDB(t)
repo := user.NewRepository(pool)
ctx := context.Background()
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-noop", "noop@example.com", "Noop User", "")
u, updateError := repo.Update(ctx, created.ID, user.UpdateProfileRequest{})
if updateError != nil {
t.Fatalf("unexpected error: %v", updateError)
}
if u.ID != created.ID {
t.Errorf("expected %s, got %s", created.ID, u.ID)
}
}

View File

@@ -0,0 +1,262 @@
package user_test
import (
"context"
"encoding/json"
"fmt"
"testing"
"time"
"github.com/food-ai/backend/internal/user"
)
func ptrStr(s string) *string { return &s }
func ptrInt(i int) *int { return &i }
func ptrFloat(f float64) *float64 { return &f }
// mockUserRepo is an in-test mock implementing user.UserRepository.
type mockUserRepo struct {
getByIDFn func(ctx context.Context, id string) (*user.User, error)
updateFn func(ctx context.Context, id string, req user.UpdateProfileRequest) (*user.User, error)
updateInTxFn func(ctx context.Context, id string, profileReq user.UpdateProfileRequest, caloriesReq *user.UpdateProfileRequest) (*user.User, error)
upsertByFirebaseUIDFn func(ctx context.Context, uid, email, name, avatarURL string) (*user.User, error)
setRefreshTokenFn func(ctx context.Context, id, token string, expiresAt time.Time) error
findByRefreshTokenFn func(ctx context.Context, token string) (*user.User, error)
clearRefreshTokenFn func(ctx context.Context, id string) error
}
func (m *mockUserRepo) UpsertByFirebaseUID(ctx context.Context, uid, email, name, avatarURL string) (*user.User, error) {
return m.upsertByFirebaseUIDFn(ctx, uid, email, name, avatarURL)
}
func (m *mockUserRepo) GetByID(ctx context.Context, id string) (*user.User, error) {
return m.getByIDFn(ctx, id)
}
func (m *mockUserRepo) Update(ctx context.Context, id string, req user.UpdateProfileRequest) (*user.User, error) {
return m.updateFn(ctx, id, req)
}
func (m *mockUserRepo) UpdateInTx(ctx context.Context, id string, profileReq user.UpdateProfileRequest, caloriesReq *user.UpdateProfileRequest) (*user.User, error) {
return m.updateInTxFn(ctx, id, profileReq, caloriesReq)
}
func (m *mockUserRepo) SetRefreshToken(ctx context.Context, id, token string, expiresAt time.Time) error {
return m.setRefreshTokenFn(ctx, id, token, expiresAt)
}
func (m *mockUserRepo) FindByRefreshToken(ctx context.Context, token string) (*user.User, error) {
return m.findByRefreshTokenFn(ctx, token)
}
func (m *mockUserRepo) ClearRefreshToken(ctx context.Context, id string) error {
return m.clearRefreshTokenFn(ctx, id)
}
func TestGetProfile_Success(t *testing.T) {
repo := &mockUserRepo{
getByIDFn: func(ctx context.Context, id string) (*user.User, error) {
return &user.User{ID: id, Email: "test@example.com", Plan: "free"}, nil
},
}
svc := user.NewService(repo)
u, getError := svc.GetProfile(context.Background(), "user-1")
if getError != nil {
t.Fatalf("unexpected error: %v", getError)
}
if u.ID != "user-1" {
t.Errorf("expected user-1, got %s", u.ID)
}
}
func TestGetProfile_NotFound(t *testing.T) {
repo := &mockUserRepo{
getByIDFn: func(ctx context.Context, id string) (*user.User, error) {
return nil, fmt.Errorf("not found")
},
}
svc := user.NewService(repo)
_, getError := svc.GetProfile(context.Background(), "nonexistent")
if getError == nil {
t.Fatal("expected error")
}
}
func TestUpdateProfile_NameOnly(t *testing.T) {
repo := &mockUserRepo{
updateFn: func(ctx context.Context, id string, req user.UpdateProfileRequest) (*user.User, error) {
return &user.User{ID: id, Name: *req.Name, Plan: "free"}, nil
},
}
svc := user.NewService(repo)
u, updateError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{Name: ptrStr("New Name")})
if updateError != nil {
t.Fatalf("unexpected error: %v", updateError)
}
if u.Name != "New Name" {
t.Errorf("expected 'New Name', got %q", u.Name)
}
}
func TestUpdateProfile_WithCaloriesRecalculation(t *testing.T) {
dob := time.Now().AddDate(-30, 0, 0).Format("2006-01-02")
profileReq := user.UpdateProfileRequest{
HeightCM: ptrInt(180),
WeightKG: ptrFloat(80),
DateOfBirth: &dob,
Gender: ptrStr("male"),
Activity: ptrStr("moderate"),
Goal: ptrStr("maintain"),
}
finalUser := &user.User{
ID: "user-1",
HeightCM: ptrInt(180),
WeightKG: ptrFloat(80),
DateOfBirth: &dob,
Gender: ptrStr("male"),
Activity: ptrStr("moderate"),
Goal: ptrStr("maintain"),
DailyCalories: ptrInt(2759),
Plan: "free",
}
repo := &mockUserRepo{
// Service calls GetByID first to merge current values for calorie calculation
getByIDFn: func(ctx context.Context, id string) (*user.User, error) {
return &user.User{ID: id, Plan: "free"}, nil
},
updateInTxFn: func(ctx context.Context, id string, pReq user.UpdateProfileRequest, cReq *user.UpdateProfileRequest) (*user.User, error) {
if cReq == nil {
t.Error("expected caloriesReq to be non-nil")
} else if *cReq.DailyCalories != 2759 {
t.Errorf("expected calories 2759, got %d", *cReq.DailyCalories)
}
return finalUser, nil
},
}
svc := user.NewService(repo)
u, updateError := svc.UpdateProfile(context.Background(), "user-1", profileReq)
if updateError != nil {
t.Fatalf("unexpected error: %v", updateError)
}
if u.DailyCalories == nil {
t.Fatal("expected daily_calories to be set")
}
if *u.DailyCalories != 2759 {
t.Errorf("expected 2759, got %d", *u.DailyCalories)
}
}
func TestUpdateProfile_InvalidHeight_TooLow(t *testing.T) {
svc := user.NewService(&mockUserRepo{})
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{HeightCM: ptrInt(50)})
if validationError == nil {
t.Fatal("expected validation error")
}
}
func TestUpdateProfile_InvalidHeight_TooHigh(t *testing.T) {
svc := user.NewService(&mockUserRepo{})
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{HeightCM: ptrInt(300)})
if validationError == nil {
t.Fatal("expected validation error")
}
}
func TestUpdateProfile_InvalidWeight_TooLow(t *testing.T) {
svc := user.NewService(&mockUserRepo{})
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{WeightKG: ptrFloat(10)})
if validationError == nil {
t.Fatal("expected validation error")
}
}
func TestUpdateProfile_InvalidWeight_TooHigh(t *testing.T) {
svc := user.NewService(&mockUserRepo{})
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{WeightKG: ptrFloat(400)})
if validationError == nil {
t.Fatal("expected validation error")
}
}
func TestUpdateProfile_InvalidDOB_TooRecent(t *testing.T) {
svc := user.NewService(&mockUserRepo{})
dob := time.Now().AddDate(-5, 0, 0).Format("2006-01-02")
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{DateOfBirth: &dob})
if validationError == nil {
t.Fatal("expected validation error")
}
}
func TestUpdateProfile_InvalidDOB_TooOld(t *testing.T) {
svc := user.NewService(&mockUserRepo{})
dob := time.Now().AddDate(-150, 0, 0).Format("2006-01-02")
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{DateOfBirth: &dob})
if validationError == nil {
t.Fatal("expected validation error")
}
}
func TestUpdateProfile_InvalidDOB_BadFormat(t *testing.T) {
svc := user.NewService(&mockUserRepo{})
dob := "not-a-date"
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{DateOfBirth: &dob})
if validationError == nil {
t.Fatal("expected validation error")
}
}
func TestUpdateProfile_InvalidGender(t *testing.T) {
svc := user.NewService(&mockUserRepo{})
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{Gender: ptrStr("other")})
if validationError == nil {
t.Fatal("expected validation error")
}
}
func TestUpdateProfile_InvalidActivity(t *testing.T) {
svc := user.NewService(&mockUserRepo{})
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{Activity: ptrStr("extreme")})
if validationError == nil {
t.Fatal("expected validation error")
}
}
func TestUpdateProfile_InvalidGoal(t *testing.T) {
svc := user.NewService(&mockUserRepo{})
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{Goal: ptrStr("bulk")})
if validationError == nil {
t.Fatal("expected validation error")
}
}
func TestUpdateProfile_Preferences(t *testing.T) {
prefs := json.RawMessage(`{"cuisines":["russian"]}`)
repo := &mockUserRepo{
updateFn: func(ctx context.Context, id string, req user.UpdateProfileRequest) (*user.User, error) {
return &user.User{
ID: id,
Plan: "free",
Preferences: *req.Preferences,
UpdatedAt: time.Now(),
}, nil
},
}
svc := user.NewService(repo)
u, updateError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{Preferences: &prefs})
if updateError != nil {
t.Fatalf("unexpected error: %v", updateError)
}
if string(u.Preferences) != `{"cuisines":["russian"]}` {
t.Errorf("unexpected preferences: %s", u.Preferences)
}
}