Files
jd-book-uploader-backend/services/firebase_test.go
ianshaloom ebeae34e01 Add initial project structure with core functionality for book and stationery uploads
- Created main application entry point in `main.go`.
- Added configuration management in `config/config.go` and tests in `config/config_test.go`.
- Implemented handlers for book and stationery uploads in `handlers/book.go` and `handlers/stationery.go`, including validation logic.
- Established database connection and services in `services/database.go` and `services/book_service.go`.
- Defined models for books and stationery in `models/book.go` and `models/stationery.go`.
- Set up Firebase integration for image uploads in `services/firebase.go`.
- Created migration scripts for database schema in `migrations/001_create_tables.sql` and subsequent updates.
- Added CORS and error handling middleware.
- Included comprehensive tests for handlers, services, and utilities.
- Documented API endpoints and usage in `README.md` and migration instructions in `migrations/README.md`.
- Introduced `.gitignore` to exclude unnecessary files and directories.
- Added Go module support with `go.mod` and `go.sum` files.
- Implemented utility functions for slug generation and validation in `utils/slug.go` and `utils/validation.go`.
2025-11-21 08:50:27 +03:00

109 lines
2.8 KiB
Go

package services
import (
"context"
"os"
"testing"
"jd-book-uploader/config"
)
func TestInitFirebase(t *testing.T) {
// Load config from environment
cfg, err := config.LoadConfig()
if err != nil {
t.Skipf("Skipping Firebase test - config not available: %v", err)
return
}
// Check if credentials file exists
if cfg.FirebaseCredentialsFile == "" && os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") == "" {
t.Skip("Skipping Firebase test - no credentials file configured")
return
}
// Test Firebase initialization
client, err := InitFirebase(cfg)
if err != nil {
t.Fatalf("InitFirebase() error = %v", err)
}
if client == nil {
t.Fatal("InitFirebase() returned nil client")
}
// Verify client can access bucket
ctx := context.Background()
bucket, err := client.DefaultBucket()
if err != nil {
t.Fatalf("Failed to get default bucket: %v", err)
}
if bucket == nil {
t.Fatal("DefaultBucket() returned nil")
}
// Test bucket name matches config
bucketAttrs, err := bucket.Attrs(ctx)
if err != nil {
t.Fatalf("Failed to get bucket attributes: %v", err)
}
expectedBucket := cfg.FirebaseStorageBucket
if bucketAttrs.Name != expectedBucket {
t.Logf("Warning: Bucket name mismatch. Expected: %s, Got: %s", expectedBucket, bucketAttrs.Name)
}
t.Logf("Firebase initialized successfully. Bucket: %s", bucketAttrs.Name)
}
func TestUploadImage(t *testing.T) {
// Load config from environment
cfg, err := config.LoadConfig()
if err != nil {
t.Skipf("Skipping Firebase upload test - config not available: %v", err)
return
}
// Initialize Firebase first
client, err := InitFirebase(cfg)
if err != nil {
t.Skipf("Skipping Firebase upload test - initialization failed: %v", err)
return
}
if client == nil {
t.Skip("Skipping Firebase upload test - client is nil")
return
}
ctx := context.Background()
// Create a small test image (1x1 PNG)
// PNG header + minimal valid PNG data
testImageData := []byte{
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // PNG signature
0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, // IHDR chunk
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, // 1x1 dimensions
0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, 0xDE, // IHDR data
0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, // IDAT chunk
0x08, 0x99, 0x01, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, // IDAT data
0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82, // IEND
}
// Upload test image
url, err := UploadImage(ctx, testImageData, "test", "test-image.png")
if err != nil {
t.Fatalf("UploadImage() error = %v", err)
}
if url == "" {
t.Fatal("UploadImage() returned empty URL")
}
t.Logf("Image uploaded successfully. URL: %s", url)
// Verify URL is accessible (optional - can be skipped if network issues)
// This would require HTTP client to check if URL is accessible
}