Backend (Go): - Project structure with chi router, pgxpool, goose migrations - JWT auth (access/refresh tokens) with Firebase token verification - NoopTokenVerifier for local dev without Firebase credentials - PostgreSQL user repository with atomic profile updates (transactions) - Mifflin-St Jeor calorie calculation based on profile data - REST API: POST /auth/login, /auth/refresh, /auth/logout, GET/PUT /profile, GET /health - Middleware: auth, CORS (localhost wildcard), logging, recovery, request_id - Unit tests (51 passing) and integration tests (testcontainers) - Docker Compose setup with postgres healthcheck and graceful shutdown Flutter client: - Riverpod state management with GoRouter navigation - Firebase Auth (email/password + Google sign-in with web popup support) - Platform-aware API URLs (web/Android/iOS) - Dio HTTP client with JWT auth interceptor and concurrent refresh handling - Secure token storage - Screens: Login, Register, Home (tabs: Menu, Recipes, Products, Profile) - Unit tests (17 passing) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
210 lines
5.9 KiB
Go
210 lines
5.9 KiB
Go
//go:build integration
|
|
|
|
package user
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/food-ai/backend/internal/testutil"
|
|
)
|
|
|
|
func TestRepository_UpsertByFirebaseUID_Insert(t *testing.T) {
|
|
pool := testutil.SetupTestDB(t)
|
|
repo := NewRepository(pool)
|
|
ctx := context.Background()
|
|
|
|
u, err := repo.UpsertByFirebaseUID(ctx, "fb-123", "test@example.com", "Test User", "https://avatar.url")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
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 := NewRepository(pool)
|
|
ctx := context.Background()
|
|
|
|
_, _ = repo.UpsertByFirebaseUID(ctx, "fb-123", "old@example.com", "Old Name", "")
|
|
u, err := repo.UpsertByFirebaseUID(ctx, "fb-123", "new@example.com", "New Name", "https://new-avatar.url")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
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 := NewRepository(pool)
|
|
ctx := context.Background()
|
|
|
|
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-get", "get@example.com", "Get User", "")
|
|
u, err := repo.GetByID(ctx, created.ID)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
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 := NewRepository(pool)
|
|
ctx := context.Background()
|
|
|
|
_, err := repo.GetByID(ctx, "00000000-0000-0000-0000-000000000000")
|
|
if err == nil {
|
|
t.Fatal("expected error for non-existent user")
|
|
}
|
|
}
|
|
|
|
func TestRepository_Update(t *testing.T) {
|
|
pool := testutil.SetupTestDB(t)
|
|
repo := NewRepository(pool)
|
|
ctx := context.Background()
|
|
|
|
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-upd", "upd@example.com", "Update User", "")
|
|
height := 180
|
|
u, err := repo.Update(ctx, created.ID, UpdateProfileRequest{HeightCM: &height})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
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 := NewRepository(pool)
|
|
ctx := context.Background()
|
|
|
|
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-multi", "multi@example.com", "Multi User", "")
|
|
height := 175
|
|
weight := 70.5
|
|
age := 25
|
|
gender := "male"
|
|
activity := "moderate"
|
|
goal := "maintain"
|
|
u, err := repo.Update(ctx, created.ID, UpdateProfileRequest{
|
|
HeightCM: &height,
|
|
WeightKG: &weight,
|
|
Age: &age,
|
|
Gender: &gender,
|
|
Activity: &activity,
|
|
Goal: &goal,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
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 := NewRepository(pool)
|
|
ctx := context.Background()
|
|
|
|
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-prefs", "prefs@example.com", "Prefs User", "")
|
|
prefs := json.RawMessage(`{"cuisines":["russian","asian"]}`)
|
|
u, err := repo.Update(ctx, created.ID, UpdateProfileRequest{Preferences: &prefs})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
var p map[string]interface{}
|
|
json.Unmarshal(u.Preferences, &p)
|
|
if p["cuisines"] == nil {
|
|
t.Error("expected cuisines in preferences")
|
|
}
|
|
}
|
|
|
|
func TestRepository_SetAndFindRefreshToken(t *testing.T) {
|
|
pool := testutil.SetupTestDB(t)
|
|
repo := NewRepository(pool)
|
|
ctx := context.Background()
|
|
|
|
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-token", "token@example.com", "Token User", "")
|
|
err := repo.SetRefreshToken(ctx, created.ID, "refresh-token-123", time.Now().Add(24*time.Hour))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error setting token: %v", err)
|
|
}
|
|
|
|
u, err := repo.FindByRefreshToken(ctx, "refresh-token-123")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error finding by token: %v", err)
|
|
}
|
|
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 := 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))
|
|
|
|
_, err := repo.FindByRefreshToken(ctx, "expired-token")
|
|
if err == nil {
|
|
t.Fatal("expected error for expired token")
|
|
}
|
|
}
|
|
|
|
func TestRepository_ClearRefreshToken(t *testing.T) {
|
|
pool := testutil.SetupTestDB(t)
|
|
repo := 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))
|
|
err := repo.ClearRefreshToken(ctx, created.ID)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
_, err = repo.FindByRefreshToken(ctx, "token-to-clear")
|
|
if err == nil {
|
|
t.Fatal("expected error after clearing token")
|
|
}
|
|
}
|
|
|
|
func TestRepository_Update_NoFields(t *testing.T) {
|
|
pool := testutil.SetupTestDB(t)
|
|
repo := NewRepository(pool)
|
|
ctx := context.Background()
|
|
|
|
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-noop", "noop@example.com", "Noop User", "")
|
|
u, err := repo.Update(ctx, created.ID, UpdateProfileRequest{})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if u.ID != created.ID {
|
|
t.Errorf("expected %s, got %s", created.ID, u.ID)
|
|
}
|
|
}
|