Compare commits
6 Commits
62d466e4fa
...
4b04461ebb
| Author | SHA1 | Date | |
|---|---|---|---|
| 4b04461ebb | |||
| 533e8c3d46 | |||
| df568291f1 | |||
| 81acce62b1 | |||
| 989a61e7d5 | |||
| 3ffd83b0fb |
1
.prettierignore
Normal file
1
.prettierignore
Normal file
@@ -0,0 +1 @@
|
||||
docs/
|
||||
4
Makefile
4
Makefile
@@ -44,8 +44,8 @@ clean:
|
||||
rm -fr dist/*
|
||||
|
||||
format:
|
||||
$(PRETTIER) -w .
|
||||
$(GO) fmt ./...
|
||||
$(PRETTIER) -w . --ignore-path .prettierignore
|
||||
$(GO) fmt $(shell $(GO) list ./... | grep -v 'docs')
|
||||
|
||||
lint:
|
||||
$(GOLANGCI_LINT) run
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -238,24 +237,12 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po
|
||||
}
|
||||
|
||||
func findExistingSeedUser(userRepo repositories.UserRepository) (*database.User, error) {
|
||||
users, err := userRepo.GetAll(100, 0)
|
||||
user, err := userRepo.GetByUsernamePrefix("seed_admin_")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
if len(user.Username) >= 11 && user.Username[:11] == "seed_admin_" {
|
||||
if len(user.Email) >= 13 && strings.HasSuffix(user.Email, "@goyco.local") {
|
||||
emailPrefix := user.Email[:len(user.Email)-13]
|
||||
if len(emailPrefix) >= 11 && emailPrefix[:11] == "seed_admin_" {
|
||||
return &user, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("no existing seed user found")
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func ensureSeedUser(userRepo repositories.UserRepository, passwordHash string) (*database.User, error) {
|
||||
existingUser, err := findExistingSeedUser(userRepo)
|
||||
@@ -289,7 +276,6 @@ func ensureSeedUser(userRepo repositories.UserRepository, passwordHash string) (
|
||||
return nil, fmt.Errorf("failed to create seed user after %d attempts", maxRetries)
|
||||
}
|
||||
|
||||
|
||||
func getVoteCounts(voteRepo repositories.VoteRepository, postID uint) (int, int, error) {
|
||||
return voteRepo.GetVoteCountsByPostID(postID)
|
||||
}
|
||||
|
||||
@@ -669,7 +669,9 @@ func (e *errorVoteRepository) Delete(uint) error { ret
|
||||
func (e *errorVoteRepository) Count() (int64, error) { return 0, nil }
|
||||
func (e *errorVoteRepository) CountByPostID(uint) (int64, error) { return 0, nil }
|
||||
func (e *errorVoteRepository) CountByUserID(uint) (int64, error) { return 0, nil }
|
||||
func (e *errorVoteRepository) GetVoteCountsByPostID(uint) (int, int, error) { return 0, 0, errors.New("database error") }
|
||||
func (e *errorVoteRepository) GetVoteCountsByPostID(uint) (int, int, error) {
|
||||
return 0, 0, errors.New("database error")
|
||||
}
|
||||
func (e *errorVoteRepository) WithTx(*gorm.DB) repositories.VoteRepository { return e }
|
||||
|
||||
func TestPostHandler_EdgeCases(t *testing.T) {
|
||||
|
||||
@@ -28,6 +28,7 @@ type UserRepository interface {
|
||||
Unlock(id uint) error
|
||||
GetPosts(userID uint, limit, offset int) ([]database.Post, error)
|
||||
GetDeletedUsers() ([]database.User, error)
|
||||
GetByUsernamePrefix(prefix string) (*database.User, error)
|
||||
HardDeleteAll() (int64, error)
|
||||
Count() (int64, error)
|
||||
WithTx(tx *gorm.DB) UserRepository
|
||||
@@ -240,6 +241,17 @@ func (r *userRepository) GetDeletedUsers() ([]database.User, error) {
|
||||
return users, err
|
||||
}
|
||||
|
||||
func (r *userRepository) GetByUsernamePrefix(prefix string) (*database.User, error) {
|
||||
var user database.User
|
||||
err := r.db.
|
||||
Where("username LIKE ? AND email LIKE ?", prefix+"%", prefix+"%@goyco.local").
|
||||
First(&user).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (r *userRepository) HardDeleteAll() (int64, error) {
|
||||
var totalDeleted int64
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
|
||||
@@ -422,6 +422,24 @@ func (m *MockUserRepository) GetDeletedUsers() ([]database.User, error) {
|
||||
return []database.User{}, nil
|
||||
}
|
||||
|
||||
func (m *MockUserRepository) GetByUsernamePrefix(prefix string) (*database.User, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
for _, user := range m.users {
|
||||
if len(user.Username) >= len(prefix) && user.Username[:len(prefix)] == prefix {
|
||||
if len(user.Email) >= 13 && strings.HasSuffix(user.Email, "@goyco.local") {
|
||||
emailPrefix := user.Email[:len(user.Email)-13]
|
||||
if len(emailPrefix) >= len(prefix) && emailPrefix[:len(prefix)] == prefix {
|
||||
return user, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
func (m *MockUserRepository) HardDeleteAll() (int64, error) {
|
||||
if m.HardDeleteAllFunc != nil {
|
||||
return m.HardDeleteAllFunc()
|
||||
|
||||
@@ -153,6 +153,7 @@ type UserRepositoryStub struct {
|
||||
UnlockFn func(uint) error
|
||||
GetPostsFn func(uint, int, int) ([]database.Post, error)
|
||||
GetDeletedUsersFn func() ([]database.User, error)
|
||||
GetByUsernamePrefixFn func(string) (*database.User, error)
|
||||
HardDeleteAllFn func() (int64, error)
|
||||
CountFn func() (int64, error)
|
||||
WithTxFn func(*gorm.DB) repositories.UserRepository
|
||||
@@ -281,6 +282,13 @@ func (s *UserRepositoryStub) GetDeletedUsers() ([]database.User, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *UserRepositoryStub) GetByUsernamePrefix(prefix string) (*database.User, error) {
|
||||
if s != nil && s.GetByUsernamePrefixFn != nil {
|
||||
return s.GetByUsernamePrefixFn(prefix)
|
||||
}
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
func (s *UserRepositoryStub) HardDeleteAll() (int64, error) {
|
||||
if s != nil && s.HardDeleteAllFn != nil {
|
||||
return s.HardDeleteAllFn()
|
||||
|
||||
Reference in New Issue
Block a user