init commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"authentication/db"
|
||||
"authentication/models"
|
||||
)
|
||||
|
||||
func InsertAccessLogLogin(log models.UserAccessLog) error {
|
||||
query := `INSERT INTO access_log (
|
||||
user_id,
|
||||
participant_id,
|
||||
activity_type,
|
||||
ip_address,
|
||||
field_updated,
|
||||
time)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`
|
||||
|
||||
_, err := db.DB.Exec(query, log.UserID, log.ParticipantID, log.ActivityType, log.IPAddress, log.FieldUpdated, log.Time)
|
||||
return err
|
||||
}
|
||||
|
||||
func GetActivityMessages(id int) (description string, err error) {
|
||||
query := `SELECT Description FROM activity_type WHERE id = ?`
|
||||
err = db.DB.QueryRow(query, id).Scan(&description)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return description, nil
|
||||
}
|
||||
@@ -0,0 +1,330 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"authentication/models"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
)
|
||||
|
||||
func TestInsertAccessLogLogin(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
userID := "user123"
|
||||
participantID := "part456"
|
||||
activityType := 17
|
||||
ipAddress := "192.168.1.1"
|
||||
fieldData := json.RawMessage(`{"key": "value"}`)
|
||||
currentTime := time.Now()
|
||||
|
||||
accessLog := models.UserAccessLog{
|
||||
UserID: &userID,
|
||||
ParticipantID: &participantID,
|
||||
ActivityType: activityType,
|
||||
IPAddress: ipAddress,
|
||||
FieldUpdated: &fieldData,
|
||||
Time: currentTime,
|
||||
}
|
||||
|
||||
mock.ExpectExec(`INSERT INTO access_log \( user_id, participant_id, activity_type, ip_address, field_updated, time\) VALUES \(\?, \?, \?, \?, \?, \?\)`).
|
||||
WithArgs(&userID, &participantID, activityType, ipAddress, &fieldData, currentTime).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
||||
err := InsertAccessLogLogin(accessLog)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("Unfulfilled expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertAccessLogLoginNullFields(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
activityType := 5
|
||||
ipAddress := "10.0.0.1"
|
||||
currentTime := time.Now()
|
||||
|
||||
accessLog := models.UserAccessLog{
|
||||
UserID: nil,
|
||||
ParticipantID: nil,
|
||||
ActivityType: activityType,
|
||||
IPAddress: ipAddress,
|
||||
FieldUpdated: nil,
|
||||
Time: currentTime,
|
||||
}
|
||||
|
||||
mock.ExpectExec(`INSERT INTO access_log \( user_id, participant_id, activity_type, ip_address, field_updated, time\) VALUES \(\?, \?, \?, \?, \?, \?\)`).
|
||||
WithArgs(nil, nil, activityType, ipAddress, nil, currentTime).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
||||
err := InsertAccessLogLogin(accessLog)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertAccessLogLoginError(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
userID := "user999"
|
||||
activityType := 17
|
||||
ipAddress := "172.16.0.1"
|
||||
currentTime := time.Now()
|
||||
|
||||
accessLog := models.UserAccessLog{
|
||||
UserID: &userID,
|
||||
ActivityType: activityType,
|
||||
IPAddress: ipAddress,
|
||||
Time: currentTime,
|
||||
}
|
||||
|
||||
mock.ExpectExec(`INSERT INTO access_log`).
|
||||
WillReturnError(sql.ErrConnDone)
|
||||
|
||||
err := InsertAccessLogLogin(accessLog)
|
||||
|
||||
if err == nil {
|
||||
t.Error("Expected error, got nil")
|
||||
}
|
||||
|
||||
if err != sql.ErrConnDone {
|
||||
t.Errorf("Expected sql.ErrConnDone, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetActivityMessages(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
activityID := 17
|
||||
expectedDescription := "User logged in"
|
||||
|
||||
rows := sqlmock.NewRows([]string{"Description"}).
|
||||
AddRow(expectedDescription)
|
||||
|
||||
mock.ExpectQuery(`SELECT Description FROM activity_type WHERE id = \?`).
|
||||
WithArgs(activityID).
|
||||
WillReturnRows(rows)
|
||||
|
||||
description, err := GetActivityMessages(activityID)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
|
||||
if description != expectedDescription {
|
||||
t.Errorf("Expected description '%s', got '%s'", expectedDescription, description)
|
||||
}
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("Unfulfilled expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetActivityMessagesNotFound(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
activityID := 999
|
||||
|
||||
mock.ExpectQuery(`SELECT Description FROM activity_type WHERE id = \?`).
|
||||
WithArgs(activityID).
|
||||
WillReturnError(sql.ErrNoRows)
|
||||
|
||||
description, err := GetActivityMessages(activityID)
|
||||
|
||||
if err != sql.ErrNoRows {
|
||||
t.Errorf("Expected sql.ErrNoRows, got: %v", err)
|
||||
}
|
||||
|
||||
if description != "" {
|
||||
t.Errorf("Expected empty description, got '%s'", description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetActivityMessagesError(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
activityID := 5
|
||||
|
||||
mock.ExpectQuery(`SELECT Description FROM activity_type WHERE id = \?`).
|
||||
WithArgs(activityID).
|
||||
WillReturnError(sql.ErrConnDone)
|
||||
|
||||
description, err := GetActivityMessages(activityID)
|
||||
|
||||
if err == nil {
|
||||
t.Error("Expected error, got nil")
|
||||
}
|
||||
|
||||
if description != "" {
|
||||
t.Errorf("Expected empty description on error, got '%s'", description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertAccessLogLoginMultipleActivityTypes(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
activityTypes := []int{1, 5, 10, 17, 20}
|
||||
|
||||
for _, actType := range activityTypes {
|
||||
userID := "user123"
|
||||
ipAddress := "192.168.1.1"
|
||||
currentTime := time.Now()
|
||||
|
||||
accessLog := models.UserAccessLog{
|
||||
UserID: &userID,
|
||||
ActivityType: actType,
|
||||
IPAddress: ipAddress,
|
||||
Time: currentTime,
|
||||
}
|
||||
|
||||
mock.ExpectExec(`INSERT INTO access_log`).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
||||
err := InsertAccessLogLogin(accessLog)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error for activity type %d, got: %v", actType, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetActivityMessagesMultipleTypes(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
testCases := []struct {
|
||||
id int
|
||||
description string
|
||||
}{
|
||||
{1, "User created"},
|
||||
{5, "User updated"},
|
||||
{10, "User deleted"},
|
||||
{17, "User logged in"},
|
||||
{20, "Password changed"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.description, func(t *testing.T) {
|
||||
rows := sqlmock.NewRows([]string{"Description"}).
|
||||
AddRow(tc.description)
|
||||
|
||||
mock.ExpectQuery(`SELECT Description FROM activity_type WHERE id = \?`).
|
||||
WithArgs(tc.id).
|
||||
WillReturnRows(rows)
|
||||
|
||||
description, err := GetActivityMessages(tc.id)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
|
||||
if description != tc.description {
|
||||
t.Errorf("Expected '%s', got '%s'", tc.description, description)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertAccessLogLoginWithJSONField(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
userID := "user789"
|
||||
activityType := 17
|
||||
ipAddress := "192.168.1.100"
|
||||
currentTime := time.Now()
|
||||
|
||||
// Complex JSON field
|
||||
complexJSON := map[string]interface{}{
|
||||
"action": "login",
|
||||
"timestamp": time.Now().Unix(),
|
||||
"metadata": map[string]string{
|
||||
"browser": "Chrome",
|
||||
"os": "Windows",
|
||||
},
|
||||
}
|
||||
jsonData, _ := json.Marshal(complexJSON)
|
||||
fieldData := json.RawMessage(jsonData)
|
||||
|
||||
accessLog := models.UserAccessLog{
|
||||
UserID: &userID,
|
||||
ActivityType: activityType,
|
||||
IPAddress: ipAddress,
|
||||
FieldUpdated: &fieldData,
|
||||
Time: currentTime,
|
||||
}
|
||||
|
||||
mock.ExpectExec(`INSERT INTO access_log`).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
||||
err := InsertAccessLogLogin(accessLog)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertAccessLogLoginIPv6(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
userID := "user123"
|
||||
activityType := 17
|
||||
ipAddressV6 := "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
|
||||
currentTime := time.Now()
|
||||
|
||||
accessLog := models.UserAccessLog{
|
||||
UserID: &userID,
|
||||
ActivityType: activityType,
|
||||
IPAddress: ipAddressV6,
|
||||
Time: currentTime,
|
||||
}
|
||||
|
||||
mock.ExpectExec(`INSERT INTO access_log`).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
||||
err := InsertAccessLogLogin(accessLog)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetActivityMessagesEmptyDescription(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
activityID := 99
|
||||
|
||||
rows := sqlmock.NewRows([]string{"Description"}).
|
||||
AddRow("")
|
||||
|
||||
mock.ExpectQuery(`SELECT Description FROM activity_type WHERE id = \?`).
|
||||
WithArgs(activityID).
|
||||
WillReturnRows(rows)
|
||||
|
||||
description, err := GetActivityMessages(activityID)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
|
||||
if description != "" {
|
||||
t.Errorf("Expected empty description, got '%s'", description)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"authentication/db"
|
||||
"log"
|
||||
)
|
||||
|
||||
func GetUser(email string) (string, *string, *string, string, error) {
|
||||
log.Print(email)
|
||||
query := `SELECT id, first_name, last_name, email_address FROM users WHERE email_address = ? AND is_deleted = 0 LIMIT 1;`
|
||||
var id string
|
||||
var firstName *string
|
||||
var lastName *string
|
||||
var emailAddress string
|
||||
err := db.DB.QueryRow(query, email).Scan(&id, &firstName, &lastName, &emailAddress)
|
||||
if err != nil {
|
||||
return "", nil, nil, "", err
|
||||
}
|
||||
return id, firstName, lastName, emailAddress, nil
|
||||
}
|
||||
|
||||
func GetUserID(email string) (string, error) {
|
||||
log.Print(email)
|
||||
query := `SELECT id, FROM users WHERE email_address = ? AND is_deleted = 0 LIMIT 1;`
|
||||
var id string
|
||||
err := db.DB.QueryRow(query, email).Scan(&id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func CheckEmailInDB(email string) (bool, error) {
|
||||
var exists bool
|
||||
query := `SELECT EXISTS (
|
||||
SELECT 1 FROM users WHERE email_address = ? AND is_deleted = 0);`
|
||||
err := db.DB.QueryRow(query, email).Scan(&exists)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func GetUserIDFromEmail(email string) (string, error) {
|
||||
log.Print(email)
|
||||
query := `SELECT id
|
||||
FROM (
|
||||
SELECT id, 1 AS priority
|
||||
FROM users
|
||||
WHERE email_address = ?
|
||||
AND is_deleted = 0
|
||||
) t
|
||||
ORDER BY priority ASC
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
var id string
|
||||
err := db.DB.QueryRow(query, email).Scan(&id)
|
||||
if err != nil {
|
||||
log.Println("Hello")
|
||||
|
||||
return "", err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
@@ -0,0 +1,407 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
"authentication/db"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
)
|
||||
|
||||
func setupMockDB(t *testing.T) (sqlmock.Sqlmock, func()) {
|
||||
mockDB, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create mock database: %v", err)
|
||||
}
|
||||
|
||||
originalDB := db.DB
|
||||
db.DB = mockDB
|
||||
|
||||
cleanup := func() {
|
||||
mockDB.Close()
|
||||
db.DB = originalDB
|
||||
}
|
||||
|
||||
return mock, cleanup
|
||||
}
|
||||
|
||||
func TestGetUser(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
email := "test@example.com"
|
||||
expectedID := "user123"
|
||||
expectedFirstName := "John"
|
||||
expectedLastName := "Doe"
|
||||
expectedEmail := "test@example.com"
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id", "first_name", "last_name", "email_address"}).
|
||||
AddRow(expectedID, expectedFirstName, expectedLastName, expectedEmail)
|
||||
|
||||
mock.ExpectQuery(`SELECT id, first_name, last_name, email_address FROM users WHERE email_address = \? AND is_deleted = 0 LIMIT 1`).
|
||||
WithArgs(email).
|
||||
WillReturnRows(rows)
|
||||
|
||||
id, firstName, lastName, emailAddress, err := GetUser(email)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
|
||||
if id != expectedID {
|
||||
t.Errorf("Expected ID %s, got %s", expectedID, id)
|
||||
}
|
||||
|
||||
if firstName == nil || *firstName != expectedFirstName {
|
||||
t.Errorf("Expected first name %s, got %v", expectedFirstName, firstName)
|
||||
}
|
||||
|
||||
if lastName == nil || *lastName != expectedLastName {
|
||||
t.Errorf("Expected last name %s, got %v", expectedLastName, lastName)
|
||||
}
|
||||
|
||||
if emailAddress != expectedEmail {
|
||||
t.Errorf("Expected email %s, got %s", expectedEmail, emailAddress)
|
||||
}
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("Unfulfilled expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserNotFound(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
email := "nonexistent@example.com"
|
||||
|
||||
mock.ExpectQuery(`SELECT id, first_name, last_name, email_address FROM users WHERE email_address = \? AND is_deleted = 0 LIMIT 1`).
|
||||
WithArgs(email).
|
||||
WillReturnError(sql.ErrNoRows)
|
||||
|
||||
id, firstName, lastName, emailAddress, err := GetUser(email)
|
||||
|
||||
if err != sql.ErrNoRows {
|
||||
t.Errorf("Expected sql.ErrNoRows, got: %v", err)
|
||||
}
|
||||
|
||||
if id != "" {
|
||||
t.Errorf("Expected empty ID, got %s", id)
|
||||
}
|
||||
|
||||
if firstName != nil {
|
||||
t.Errorf("Expected nil firstName, got %v", firstName)
|
||||
}
|
||||
|
||||
if lastName != nil {
|
||||
t.Errorf("Expected nil lastName, got %v", lastName)
|
||||
}
|
||||
|
||||
if emailAddress != "" {
|
||||
t.Errorf("Expected empty email, got %s", emailAddress)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserNullNames(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
email := "test@example.com"
|
||||
expectedID := "user456"
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id", "first_name", "last_name", "email_address"}).
|
||||
AddRow(expectedID, nil, nil, email)
|
||||
|
||||
mock.ExpectQuery(`SELECT id, first_name, last_name, email_address FROM users WHERE email_address = \? AND is_deleted = 0 LIMIT 1`).
|
||||
WithArgs(email).
|
||||
WillReturnRows(rows)
|
||||
|
||||
id, firstName, lastName, emailAddress, err := GetUser(email)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
|
||||
if id != expectedID {
|
||||
t.Errorf("Expected ID %s, got %s", expectedID, id)
|
||||
}
|
||||
|
||||
if firstName != nil {
|
||||
t.Errorf("Expected nil firstName for NULL value, got %v", firstName)
|
||||
}
|
||||
|
||||
if lastName != nil {
|
||||
t.Errorf("Expected nil lastName for NULL value, got %v", lastName)
|
||||
}
|
||||
|
||||
if emailAddress != email {
|
||||
t.Errorf("Expected email %s, got %s", email, emailAddress)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserID(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
email := "test@example.com"
|
||||
expectedID := "user789"
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id"}).
|
||||
AddRow(expectedID)
|
||||
|
||||
// Note: The query has a typo "SELECT id, FROM" but we match it as-is
|
||||
mock.ExpectQuery(`SELECT id, FROM users WHERE email_address = \? AND is_deleted = 0 LIMIT 1`).
|
||||
WithArgs(email).
|
||||
WillReturnRows(rows)
|
||||
|
||||
id, err := GetUserID(email)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
|
||||
if id != expectedID {
|
||||
t.Errorf("Expected ID %s, got %s", expectedID, id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckEmailInDB(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
email := "existing@example.com"
|
||||
|
||||
rows := sqlmock.NewRows([]string{"exists"}).
|
||||
AddRow(true)
|
||||
|
||||
mock.ExpectQuery(`SELECT EXISTS \( SELECT 1 FROM users WHERE email_address = \? AND is_deleted = 0\)`).
|
||||
WithArgs(email).
|
||||
WillReturnRows(rows)
|
||||
|
||||
exists, err := CheckEmailInDB(email)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
|
||||
if !exists {
|
||||
t.Error("Expected email to exist")
|
||||
}
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("Unfulfilled expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckEmailInDBNotExists(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
email := "nonexistent@example.com"
|
||||
|
||||
rows := sqlmock.NewRows([]string{"exists"}).
|
||||
AddRow(false)
|
||||
|
||||
mock.ExpectQuery(`SELECT EXISTS \( SELECT 1 FROM users WHERE email_address = \? AND is_deleted = 0\)`).
|
||||
WithArgs(email).
|
||||
WillReturnRows(rows)
|
||||
|
||||
exists, err := CheckEmailInDB(email)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
|
||||
if exists {
|
||||
t.Error("Expected email to not exist")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckEmailInDBError(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
email := "error@example.com"
|
||||
|
||||
mock.ExpectQuery(`SELECT EXISTS \( SELECT 1 FROM users WHERE email_address = \? AND is_deleted = 0\)`).
|
||||
WithArgs(email).
|
||||
WillReturnError(sql.ErrConnDone)
|
||||
|
||||
exists, err := CheckEmailInDB(email)
|
||||
|
||||
if err == nil {
|
||||
t.Error("Expected error, got nil")
|
||||
}
|
||||
|
||||
if exists {
|
||||
t.Error("Expected false when error occurs")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserIDFromEmail(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
email := "test@example.com"
|
||||
expectedID := "user999"
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id"}).
|
||||
AddRow(expectedID)
|
||||
|
||||
mock.ExpectQuery(`SELECT id FROM \( SELECT id, 1 AS priority FROM users WHERE email_address = \? AND is_deleted = 0 \) t ORDER BY priority ASC LIMIT 1`).
|
||||
WithArgs(email).
|
||||
WillReturnRows(rows)
|
||||
|
||||
id, err := GetUserIDFromEmail(email)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
|
||||
if id != expectedID {
|
||||
t.Errorf("Expected ID %s, got %s", expectedID, id)
|
||||
}
|
||||
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("Unfulfilled expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserIDFromEmailNotFound(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
email := "notfound@example.com"
|
||||
|
||||
mock.ExpectQuery(`SELECT id FROM \( SELECT id, 1 AS priority FROM users WHERE email_address = \? AND is_deleted = 0 \) t ORDER BY priority ASC LIMIT 1`).
|
||||
WithArgs(email).
|
||||
WillReturnError(sql.ErrNoRows)
|
||||
|
||||
id, err := GetUserIDFromEmail(email)
|
||||
|
||||
if err == nil {
|
||||
t.Error("Expected error, got nil")
|
||||
}
|
||||
|
||||
if id != "" {
|
||||
t.Errorf("Expected empty ID on error, got %s", id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserIDFromEmailDBError(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
email := "error@example.com"
|
||||
|
||||
mock.ExpectQuery(`SELECT id FROM \( SELECT id, 1 AS priority FROM users WHERE email_address = \? AND is_deleted = 0 \) t ORDER BY priority ASC LIMIT 1`).
|
||||
WithArgs(email).
|
||||
WillReturnError(sql.ErrConnDone)
|
||||
|
||||
id, err := GetUserIDFromEmail(email)
|
||||
|
||||
if err == nil {
|
||||
t.Error("Expected error, got nil")
|
||||
}
|
||||
|
||||
if err != sql.ErrConnDone {
|
||||
t.Errorf("Expected sql.ErrConnDone, got: %v", err)
|
||||
}
|
||||
|
||||
if id != "" {
|
||||
t.Errorf("Expected empty ID on error, got %s", id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserMultipleEmails(t *testing.T) {
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
testCases := []struct {
|
||||
email string
|
||||
userID string
|
||||
hasNames bool
|
||||
}{
|
||||
{"user1@example.com", "id1", true},
|
||||
{"user2@example.com", "id2", false},
|
||||
{"user3@example.com", "id3", true},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.email, func(t *testing.T) {
|
||||
var firstName, lastName interface{}
|
||||
if tc.hasNames {
|
||||
firstName = "First"
|
||||
lastName = "Last"
|
||||
} else {
|
||||
firstName = nil
|
||||
lastName = nil
|
||||
}
|
||||
|
||||
rows := sqlmock.NewRows([]string{"id", "first_name", "last_name", "email_address"}).
|
||||
AddRow(tc.userID, firstName, lastName, tc.email)
|
||||
|
||||
mock.ExpectQuery(`SELECT id, first_name, last_name, email_address FROM users WHERE email_address = \? AND is_deleted = 0 LIMIT 1`).
|
||||
WithArgs(tc.email).
|
||||
WillReturnRows(rows)
|
||||
|
||||
id, fn, ln, email, err := GetUser(tc.email)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
|
||||
if id != tc.userID {
|
||||
t.Errorf("Expected ID %s, got %s", tc.userID, id)
|
||||
}
|
||||
|
||||
if tc.hasNames {
|
||||
if fn == nil || ln == nil {
|
||||
t.Error("Expected names to be present")
|
||||
}
|
||||
} else {
|
||||
if fn != nil || ln != nil {
|
||||
t.Error("Expected names to be nil")
|
||||
}
|
||||
}
|
||||
|
||||
if email != tc.email {
|
||||
t.Errorf("Expected email %s, got %s", tc.email, email)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckEmailInDBVariousEmails(t *testing.T) {
|
||||
testEmails := []string{
|
||||
"normal@example.com",
|
||||
"with+plus@example.com",
|
||||
"with.dot@example.com",
|
||||
"with-dash@example.com",
|
||||
}
|
||||
|
||||
mock, cleanup := setupMockDB(t)
|
||||
defer cleanup()
|
||||
|
||||
for i, email := range testEmails {
|
||||
exists := i%2 == 0 // Alternate between true and false
|
||||
|
||||
rows := sqlmock.NewRows([]string{"exists"}).
|
||||
AddRow(exists)
|
||||
|
||||
mock.ExpectQuery(`SELECT EXISTS \( SELECT 1 FROM users WHERE email_address = \? AND is_deleted = 0\)`).
|
||||
WithArgs(email).
|
||||
WillReturnRows(rows)
|
||||
|
||||
result, err := CheckEmailInDB(email)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error for %s, got: %v", email, err)
|
||||
}
|
||||
|
||||
if result != exists {
|
||||
t.Errorf("Expected %v for %s, got %v", exists, email, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user