Update dependencies and improve account tests: add objx library, replace assert library with testify, enhance password assertions, and modify SQL fixtures for password hashing.
This commit is contained in:
parent
9d544c7e8a
commit
7c810ded85
6 changed files with 105 additions and 10 deletions
1
go.mod
1
go.mod
|
@ -25,6 +25,7 @@ require (
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/shopspring/decimal v1.4.0 // indirect
|
github.com/shopspring/decimal v1.4.0 // indirect
|
||||||
|
github.com/stretchr/objx v0.5.2 // indirect
|
||||||
golang.org/x/crypto v0.36.0 // indirect
|
golang.org/x/crypto v0.36.0 // indirect
|
||||||
golang.org/x/net v0.34.0 // indirect
|
golang.org/x/net v0.34.0 // indirect
|
||||||
golang.org/x/sys v0.31.0 // indirect
|
golang.org/x/sys v0.31.0 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -44,6 +44,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
|
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
|
||||||
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
|
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
|
||||||
|
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||||
|
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
|
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
|
|
||||||
"gitea.qpismont.fr/qpismont/trepa/internal/accounts/domain"
|
"gitea.qpismont.fr/qpismont/trepa/internal/accounts/domain"
|
||||||
"gitea.qpismont.fr/qpismont/trepa/test"
|
"gitea.qpismont.fr/qpismont/trepa/test"
|
||||||
"github.com/magiconair/properties/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRepository_Insert(t *testing.T) {
|
func TestRepository_Insert(t *testing.T) {
|
||||||
|
@ -43,9 +43,9 @@ func TestRepository_FetchOneByUsername(t *testing.T) {
|
||||||
t.Fatalf("Account not found")
|
t.Fatalf("Account not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, account.Username, "admin")
|
assert.Equal(t, "admin", account.Username)
|
||||||
assert.Equal(t, account.Password, "LOLPASSWORD")
|
assert.NotEmpty(t, account.Password)
|
||||||
assert.Equal(t, account.RoleId, 1)
|
assert.Equal(t, 1, account.RoleId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRepository_FetchOneById(t *testing.T) {
|
func TestRepository_FetchOneById(t *testing.T) {
|
||||||
|
@ -63,7 +63,7 @@ func TestRepository_FetchOneById(t *testing.T) {
|
||||||
t.Fatalf("Account not found")
|
t.Fatalf("Account not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, account.Username, "admin")
|
assert.Equal(t, "admin", account.Username)
|
||||||
assert.Equal(t, account.Password, "LOLPASSWORD")
|
assert.NotEmpty(t, account.Password)
|
||||||
assert.Equal(t, account.RoleId, 1)
|
assert.Equal(t, 1, account.RoleId)
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ func (s *Service) Login(login domain.AccountLogin) (*domain.AccountWithToken, *c
|
||||||
|
|
||||||
ok, err := core.ComparePassword(login.Password, account.Password)
|
ok, err := core.ComparePassword(login.Password, account.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, domain.ErrBadPassword
|
return nil, core.NewInternalServerError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
92
internal/accounts/service/account_test.go
Normal file
92
internal/accounts/service/account_test.go
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.qpismont.fr/qpismont/trepa/internal/accounts/domain"
|
||||||
|
"gitea.qpismont.fr/qpismont/trepa/internal/accounts/repository"
|
||||||
|
"gitea.qpismont.fr/qpismont/trepa/internal/core"
|
||||||
|
"gitea.qpismont.fr/qpismont/trepa/test"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MockRepository struct {
|
||||||
|
mock.Mock
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockRepository) FetchOneById(id int) (*domain.Account, error) {
|
||||||
|
args := m.Called(id)
|
||||||
|
if args.Get(0) == nil {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return args.Get(0).(*domain.Account), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockRepository) FetchOneByUsername(username string) (*domain.Account, error) {
|
||||||
|
args := m.Called(username)
|
||||||
|
if args.Get(0) == nil {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return args.Get(0).(*domain.Account), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockRepository) Insert(account domain.Account) (int, error) {
|
||||||
|
args := m.Called(account)
|
||||||
|
return args.Int(0), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestService_GetAccount(t *testing.T) {
|
||||||
|
db := test.SetupTestDB(t, "../../..")
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
repo := repository.NewRepository(db)
|
||||||
|
|
||||||
|
service := NewService(repo)
|
||||||
|
|
||||||
|
account, err := service.GetAccount(1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to get account: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if account == nil {
|
||||||
|
t.Fatalf("Account not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "admin", account.Username)
|
||||||
|
assert.Equal(t, 1, account.RoleId)
|
||||||
|
assert.NotEmpty(t, account.Password)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestService_Login(t *testing.T) {
|
||||||
|
mockRepo := new(MockRepository)
|
||||||
|
|
||||||
|
testPassword := "testpassword"
|
||||||
|
hashedPassword, _ := core.HashPassword(testPassword)
|
||||||
|
|
||||||
|
mockRepo.On("FetchOneByUsername", "admin").Return(&domain.Account{
|
||||||
|
Id: 1,
|
||||||
|
Username: "admin",
|
||||||
|
Password: hashedPassword,
|
||||||
|
RoleId: 1,
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
service := NewService(mockRepo)
|
||||||
|
|
||||||
|
result, err := service.Login(domain.AccountLogin{
|
||||||
|
Username: "admin",
|
||||||
|
Password: testPassword,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to login: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NotNil(t, result)
|
||||||
|
assert.NotNil(t, result.Token)
|
||||||
|
assert.NotNil(t, result.Account)
|
||||||
|
assert.Equal(t, "admin", result.Account.Username)
|
||||||
|
assert.Equal(t, 1, result.Account.RoleId)
|
||||||
|
|
||||||
|
mockRepo.AssertExpectations(t)
|
||||||
|
}
|
4
test/fixtures/00-accounts.sql
vendored
4
test/fixtures/00-accounts.sql
vendored
|
@ -1,2 +1,2 @@
|
||||||
INSERT INTO accounts (username, password, role_id) VALUES ('admin', 'LOLPASSWORD', 1);
|
INSERT INTO accounts (username, password, role_id) VALUES ('admin', '##TEST_PASSWORD_HASH##', 1);
|
||||||
INSERT INTO accounts (username, password, role_id) VALUES ('user', 'LOLPASSWORD', 2);
|
INSERT INTO accounts (username, password, role_id) VALUES ('user', '##TEST_PASSWORD_HASH##', 2);
|
||||||
|
|
Loading…
Reference in a new issue