added test (#5)
Reviewed-on: phoenix/textsender-auth#5 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -41,7 +41,7 @@ func Load() *Config {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
unpackedConnString := unpackDBConnString()
|
||||
unpackedConnString := UnpackDBConnString()
|
||||
dbConnString := unpackedConnString.Parse()
|
||||
|
||||
return &Config{
|
||||
@@ -51,7 +51,7 @@ func Load() *Config {
|
||||
}
|
||||
}
|
||||
|
||||
func unpackDBConnString() (connInfo ConnectionInfo) {
|
||||
func UnpackDBConnString() (connInfo ConnectionInfo) {
|
||||
username := os.Getenv("DB_USER")
|
||||
password := os.Getenv("DB_PASSWORD")
|
||||
host := os.Getenv("DB_HOST")
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -82,7 +83,13 @@ func (db *Database) ResetDatabase(ctx context.Context) error {
|
||||
|
||||
schemaContent, err := os.ReadFile("migrations/schema.sql")
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading schema file: %v", err)
|
||||
log.Println("Default migrations not found. Checking different directory")
|
||||
cwd, _ := os.Getwd()
|
||||
migrationsPath := path.Join(cwd, "../..", "migrations/schema.sql")
|
||||
schemaContent, err = os.ReadFile(migrationsPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading schema file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
statements := strings.Split(string(schemaContent), ";")
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-auth/internal/model"
|
||||
)
|
||||
|
||||
type MockUserStore struct {
|
||||
Users map[uuid.UUID]*model.User
|
||||
UsersByUsername map[string]*model.User
|
||||
mu sync.RWMutex
|
||||
Error error // Optional: simulate errors
|
||||
}
|
||||
|
||||
func NewMockUserStore() *MockUserStore {
|
||||
return &MockUserStore{
|
||||
Users: make(map[uuid.UUID]*model.User),
|
||||
UsersByUsername: make(map[string]*model.User),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockUserStore) CreateUser(ctx context.Context, user *model.User) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return m.Error
|
||||
}
|
||||
|
||||
if user.Id == uuid.Nil {
|
||||
user.Id = uuid.New()
|
||||
}
|
||||
|
||||
if _, exists := m.UsersByUsername[user.Username]; exists {
|
||||
return errors.New("User with email already exists")
|
||||
}
|
||||
|
||||
m.Users[user.Id] = user
|
||||
m.UsersByUsername[user.Username] = user
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockUserStore) GetUserByID(ctx context.Context, id uuid.UUID) (*model.User, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
user, exists := m.Users[id]
|
||||
if !exists {
|
||||
return nil, errors.New("User not found")
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (m *MockUserStore) GetUserByUsername(ctx context.Context, username string) (*model.User, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
user, exists := m.UsersByUsername[username]
|
||||
if !exists {
|
||||
return nil, errors.New("User not found")
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (m *MockUserStore) GetAllUsers(ctx context.Context) ([]*model.User, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
users := make([]*model.User, 0, len(m.Users))
|
||||
for _, user := range m.Users {
|
||||
users = append(users, user)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (m *MockUserStore) UserExists(ctx context.Context, username string) (bool, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return false, m.Error
|
||||
}
|
||||
|
||||
_, exists := m.UsersByUsername[username]
|
||||
if !exists {
|
||||
return exists, errors.New("User not found")
|
||||
}
|
||||
|
||||
return exists, nil
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-auth/internal/db"
|
||||
"git.kundeng.us/phoenix/textsender-auth/internal/handler/endpoint"
|
||||
"git.kundeng.us/phoenix/textsender-auth/internal/model"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCreateUserWithMock(t *testing.T) {
|
||||
mockstore := NewMockUserStore()
|
||||
handler := NewUserHandler(mockstore)
|
||||
|
||||
testUser := model.User{Username: "ghost", PhoneNumber: "+1234567890", Password: "dfgdffddfd"}
|
||||
jsonValue, _ := json.Marshal(testUser)
|
||||
|
||||
req, _ := http.NewRequest("POST", endpoint.Register, strings.NewReader(string(jsonValue)))
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
handler.Register(rr, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, rr.Code)
|
||||
|
||||
var response RegisterResponse
|
||||
err := json.Unmarshal(rr.Body.Bytes(), &response)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.NotNil(t, response.Data[0].Id, "Id should not be nil")
|
||||
}
|
||||
|
||||
func resetTestDB(t *testing.T) {
|
||||
t.Helper()
|
||||
_, err := db.Pool.Exec(context.Background(), "DELETE FROM users")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to reset test database: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ type UserStore interface {
|
||||
GetUserByID(ctx context.Context, id uuid.UUID) (*User, error)
|
||||
GetUserByUsername(ctx context.Context, username string) (*User, error)
|
||||
GetAllUsers(ctx context.Context) ([]*User, error)
|
||||
UserExists(ctx context.Context, email string) (bool, error)
|
||||
UserExists(ctx context.Context, username string) (bool, error)
|
||||
}
|
||||
|
||||
type PGUserStore struct {
|
||||
|
||||
Reference in New Issue
Block a user