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>
248 lines
7.6 KiB
Go
248 lines
7.6 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
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-package mock to avoid import cycles.
|
|
type mockUserRepo struct {
|
|
getByIDFn func(ctx context.Context, id string) (*User, error)
|
|
updateFn func(ctx context.Context, id string, req UpdateProfileRequest) (*User, error)
|
|
updateInTxFn func(ctx context.Context, id string, profileReq UpdateProfileRequest, caloriesReq *UpdateProfileRequest) (*User, error)
|
|
upsertByFirebaseUIDFn func(ctx context.Context, uid, email, name, avatarURL string) (*User, error)
|
|
setRefreshTokenFn func(ctx context.Context, id, token string, expiresAt time.Time) error
|
|
findByRefreshTokenFn func(ctx context.Context, token string) (*User, error)
|
|
clearRefreshTokenFn func(ctx context.Context, id string) error
|
|
}
|
|
|
|
func (m *mockUserRepo) UpsertByFirebaseUID(ctx context.Context, uid, email, name, avatarURL string) (*User, error) {
|
|
return m.upsertByFirebaseUIDFn(ctx, uid, email, name, avatarURL)
|
|
}
|
|
func (m *mockUserRepo) GetByID(ctx context.Context, id string) (*User, error) {
|
|
return m.getByIDFn(ctx, id)
|
|
}
|
|
func (m *mockUserRepo) Update(ctx context.Context, id string, req UpdateProfileRequest) (*User, error) {
|
|
return m.updateFn(ctx, id, req)
|
|
}
|
|
func (m *mockUserRepo) UpdateInTx(ctx context.Context, id string, profileReq UpdateProfileRequest, caloriesReq *UpdateProfileRequest) (*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, 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, error) {
|
|
return &User{ID: id, Email: "test@example.com", Plan: "free"}, nil
|
|
},
|
|
}
|
|
svc := NewService(repo)
|
|
|
|
u, err := svc.GetProfile(context.Background(), "user-1")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
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, error) {
|
|
return nil, fmt.Errorf("not found")
|
|
},
|
|
}
|
|
svc := NewService(repo)
|
|
|
|
_, err := svc.GetProfile(context.Background(), "nonexistent")
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
}
|
|
|
|
func TestUpdateProfile_NameOnly(t *testing.T) {
|
|
repo := &mockUserRepo{
|
|
updateFn: func(ctx context.Context, id string, req UpdateProfileRequest) (*User, error) {
|
|
return &User{ID: id, Name: *req.Name, Plan: "free"}, nil
|
|
},
|
|
}
|
|
svc := NewService(repo)
|
|
|
|
u, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{Name: ptrStr("New Name")})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if u.Name != "New Name" {
|
|
t.Errorf("expected 'New Name', got %q", u.Name)
|
|
}
|
|
}
|
|
|
|
func TestUpdateProfile_WithCaloriesRecalculation(t *testing.T) {
|
|
profileReq := UpdateProfileRequest{
|
|
HeightCM: ptrInt(180),
|
|
WeightKG: ptrFloat(80),
|
|
Age: ptrInt(30),
|
|
Gender: ptrStr("male"),
|
|
Activity: ptrStr("moderate"),
|
|
Goal: ptrStr("maintain"),
|
|
}
|
|
finalUser := &User{
|
|
ID: "user-1",
|
|
HeightCM: ptrInt(180),
|
|
WeightKG: ptrFloat(80),
|
|
Age: ptrInt(30),
|
|
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, error) {
|
|
return &User{ID: id, Plan: "free"}, nil
|
|
},
|
|
updateInTxFn: func(ctx context.Context, id string, pReq UpdateProfileRequest, cReq *UpdateProfileRequest) (*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 := NewService(repo)
|
|
|
|
u, err := svc.UpdateProfile(context.Background(), "user-1", profileReq)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
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 := NewService(&mockUserRepo{})
|
|
|
|
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{HeightCM: ptrInt(50)})
|
|
if err == nil {
|
|
t.Fatal("expected validation error")
|
|
}
|
|
}
|
|
|
|
func TestUpdateProfile_InvalidHeight_TooHigh(t *testing.T) {
|
|
svc := NewService(&mockUserRepo{})
|
|
|
|
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{HeightCM: ptrInt(300)})
|
|
if err == nil {
|
|
t.Fatal("expected validation error")
|
|
}
|
|
}
|
|
|
|
func TestUpdateProfile_InvalidWeight_TooLow(t *testing.T) {
|
|
svc := NewService(&mockUserRepo{})
|
|
|
|
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{WeightKG: ptrFloat(10)})
|
|
if err == nil {
|
|
t.Fatal("expected validation error")
|
|
}
|
|
}
|
|
|
|
func TestUpdateProfile_InvalidWeight_TooHigh(t *testing.T) {
|
|
svc := NewService(&mockUserRepo{})
|
|
|
|
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{WeightKG: ptrFloat(400)})
|
|
if err == nil {
|
|
t.Fatal("expected validation error")
|
|
}
|
|
}
|
|
|
|
func TestUpdateProfile_InvalidAge_TooLow(t *testing.T) {
|
|
svc := NewService(&mockUserRepo{})
|
|
|
|
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{Age: ptrInt(5)})
|
|
if err == nil {
|
|
t.Fatal("expected validation error")
|
|
}
|
|
}
|
|
|
|
func TestUpdateProfile_InvalidAge_TooHigh(t *testing.T) {
|
|
svc := NewService(&mockUserRepo{})
|
|
|
|
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{Age: ptrInt(150)})
|
|
if err == nil {
|
|
t.Fatal("expected validation error")
|
|
}
|
|
}
|
|
|
|
func TestUpdateProfile_InvalidGender(t *testing.T) {
|
|
svc := NewService(&mockUserRepo{})
|
|
|
|
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{Gender: ptrStr("other")})
|
|
if err == nil {
|
|
t.Fatal("expected validation error")
|
|
}
|
|
}
|
|
|
|
func TestUpdateProfile_InvalidActivity(t *testing.T) {
|
|
svc := NewService(&mockUserRepo{})
|
|
|
|
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{Activity: ptrStr("extreme")})
|
|
if err == nil {
|
|
t.Fatal("expected validation error")
|
|
}
|
|
}
|
|
|
|
func TestUpdateProfile_InvalidGoal(t *testing.T) {
|
|
svc := NewService(&mockUserRepo{})
|
|
|
|
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{Goal: ptrStr("bulk")})
|
|
if err == 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 UpdateProfileRequest) (*User, error) {
|
|
return &User{
|
|
ID: id,
|
|
Plan: "free",
|
|
Preferences: *req.Preferences,
|
|
UpdatedAt: time.Now(),
|
|
}, nil
|
|
},
|
|
}
|
|
svc := NewService(repo)
|
|
|
|
u, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{Preferences: &prefs})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if string(u.Preferences) != `{"cuisines":["russian"]}` {
|
|
t.Errorf("unexpected preferences: %s", u.Preferences)
|
|
}
|
|
}
|