Files
Authentication/models/google_info_test.go
T
2025-11-25 15:12:31 +08:00

188 lines
4.6 KiB
Go

package models
import (
"encoding/json"
"testing"
)
func TestUserGoogleInfo_Creation(t *testing.T) {
userInfo := UserGoogleInfo{
Email: "user@gmail.com",
Picture: "https://example.com/picture.jpg",
}
if userInfo.Email != "user@gmail.com" {
t.Errorf("Expected email 'user@gmail.com', got '%s'", userInfo.Email)
}
if userInfo.Picture != "https://example.com/picture.jpg" {
t.Errorf("Expected picture URL 'https://example.com/picture.jpg', got '%s'", userInfo.Picture)
}
}
func TestUserGoogleInfo_EmptyFields(t *testing.T) {
userInfo := UserGoogleInfo{}
if userInfo.Email != "" {
t.Errorf("Expected empty email, got '%s'", userInfo.Email)
}
if userInfo.Picture != "" {
t.Errorf("Expected empty picture, got '%s'", userInfo.Picture)
}
}
func TestUserGoogleInfo_JSONMarshaling(t *testing.T) {
userInfo := UserGoogleInfo{
Email: "test@example.com",
Picture: "https://example.com/photo.jpg",
}
// Marshal to JSON
jsonData, err := json.Marshal(userInfo)
if err != nil {
t.Fatalf("Failed to marshal UserGoogleInfo: %v", err)
}
expectedJSON := `{"email":"test@example.com","picture":"https://example.com/photo.jpg"}`
if string(jsonData) != expectedJSON {
t.Errorf("Expected JSON '%s', got '%s'", expectedJSON, string(jsonData))
}
}
func TestUserGoogleInfo_JSONUnmarshaling(t *testing.T) {
jsonData := []byte(`{"email":"unmarshaled@example.com","picture":"https://example.com/image.png"}`)
var userInfo UserGoogleInfo
err := json.Unmarshal(jsonData, &userInfo)
if err != nil {
t.Fatalf("Failed to unmarshal UserGoogleInfo: %v", err)
}
if userInfo.Email != "unmarshaled@example.com" {
t.Errorf("Expected email 'unmarshaled@example.com', got '%s'", userInfo.Email)
}
if userInfo.Picture != "https://example.com/image.png" {
t.Errorf("Expected picture 'https://example.com/image.png', got '%s'", userInfo.Picture)
}
}
func TestUserGoogleInfo_PartialData(t *testing.T) {
// Test with only email
userInfo1 := UserGoogleInfo{
Email: "onlyemail@example.com",
}
if userInfo1.Email != "onlyemail@example.com" {
t.Errorf("Expected email 'onlyemail@example.com', got '%s'", userInfo1.Email)
}
if userInfo1.Picture != "" {
t.Errorf("Expected empty picture, got '%s'", userInfo1.Picture)
}
// Test with only picture
userInfo2 := UserGoogleInfo{
Picture: "https://example.com/only-picture.jpg",
}
if userInfo2.Email != "" {
t.Errorf("Expected empty email, got '%s'", userInfo2.Email)
}
if userInfo2.Picture != "https://example.com/only-picture.jpg" {
t.Errorf("Expected picture 'https://example.com/only-picture.jpg', got '%s'", userInfo2.Picture)
}
}
func TestUserGoogleInfo_ValidEmailFormat(t *testing.T) {
testCases := []struct {
name string
email string
valid bool
}{
{"Valid Gmail", "user@gmail.com", true},
{"Valid Custom Domain", "user@example.com", true},
{"Invalid No At", "usergmail.com", false},
{"Invalid No Domain", "user@", false},
{"Invalid Empty", "", false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
userInfo := UserGoogleInfo{Email: tc.email}
// Basic email validation (contains @)
hasAt := false
for _, char := range userInfo.Email {
if char == '@' {
hasAt = true
break
}
}
if tc.valid && !hasAt && tc.email != "" {
t.Errorf("Expected valid email format for '%s'", tc.email)
}
if !tc.valid && hasAt && tc.email != "" {
// This is fine, we're just checking structure
}
})
}
}
func TestUserGoogleInfo_PictureURLValidation(t *testing.T) {
testCases := []struct {
name string
picture string
isHTTPS bool
}{
{"HTTPS URL", "https://example.com/pic.jpg", true},
{"HTTP URL", "http://example.com/pic.jpg", false},
{"No Protocol", "example.com/pic.jpg", false},
{"Empty", "", false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
userInfo := UserGoogleInfo{Picture: tc.picture}
hasHTTPS := len(userInfo.Picture) >= 8 && userInfo.Picture[:8] == "https://"
if tc.isHTTPS != hasHTTPS {
t.Errorf("Expected HTTPS=%v for '%s', got %v", tc.isHTTPS, tc.picture, hasHTTPS)
}
})
}
}
func TestUserGoogleInfo_CopyValues(t *testing.T) {
original := UserGoogleInfo{
Email: "original@example.com",
Picture: "https://example.com/original.jpg",
}
// Copy values
copied := UserGoogleInfo{
Email: original.Email,
Picture: original.Picture,
}
if copied.Email != original.Email {
t.Error("Copied email should match original")
}
if copied.Picture != original.Picture {
t.Error("Copied picture should match original")
}
// Modify copy
copied.Email = "modified@example.com"
if copied.Email == original.Email {
t.Error("Modified copy should not affect original")
}
}