fix: switch to mockery instead of handcrafted stubs #203
1
.gitignore
vendored
1
.gitignore
vendored
@@ -32,3 +32,4 @@ node_modules/
|
|||||||
static/css/tailwind.css
|
static/css/tailwind.css
|
||||||
static/js/htmx.min.js
|
static/js/htmx.min.js
|
||||||
tmp/
|
tmp/
|
||||||
|
mocks/
|
||||||
|
|||||||
6
.mockery.yaml
Normal file
6
.mockery.yaml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
with-expecter: True
|
||||||
|
dir: mocks/
|
||||||
|
packages:
|
||||||
|
me-fit/db:
|
||||||
|
interfaces:
|
||||||
|
DbAuth:
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
FROM golang:1.23.2@sha256:adee809c2d0009a4199a11a1b2618990b244c6515149fe609e2788ddf164bd10 AS builder_go
|
FROM golang:1.23.2@sha256:adee809c2d0009a4199a11a1b2618990b244c6515149fe609e2788ddf164bd10 AS builder_go
|
||||||
WORKDIR /me-fit
|
WORKDIR /me-fit
|
||||||
RUN go install github.com/a-h/templ/cmd/templ@latest
|
RUN go install github.com/a-h/templ/cmd/templ@latest && go install github.com/vektra/mockery/v2@latest
|
||||||
COPY . ./
|
COPY . ./
|
||||||
RUN templ generate && go test ./... && go build -o /me-fit/me-fit .
|
RUN templ generate && mockery && go test ./... && go build -o /me-fit/me-fit .
|
||||||
|
|
||||||
|
|
||||||
FROM node:22.9.0@sha256:69e667a79aa41ec0db50bc452a60e705ca16f35285eaf037ebe627a65a5cdf52 AS builder_node
|
FROM node:22.9.0@sha256:69e667a79aa41ec0db50bc452a60e705ca16f35285eaf037ebe627a65a5cdf52 AS builder_node
|
||||||
|
|||||||
5
go.mod
5
go.mod
@@ -9,20 +9,25 @@ require (
|
|||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
github.com/mattn/go-sqlite3 v1.14.23
|
github.com/mattn/go-sqlite3 v1.14.23
|
||||||
github.com/prometheus/client_golang v1.20.4
|
github.com/prometheus/client_golang v1.20.4
|
||||||
|
github.com/stretchr/testify v1.9.0
|
||||||
golang.org/x/crypto v0.27.0
|
golang.org/x/crypto v0.27.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/beorn7/perks v1.0.1 // indirect
|
github.com/beorn7/perks v1.0.1 // indirect
|
||||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||||
github.com/klauspost/compress v1.17.9 // indirect
|
github.com/klauspost/compress v1.17.9 // indirect
|
||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/prometheus/client_model v0.6.1 // indirect
|
github.com/prometheus/client_model v0.6.1 // indirect
|
||||||
github.com/prometheus/common v0.55.0 // indirect
|
github.com/prometheus/common v0.55.0 // indirect
|
||||||
github.com/prometheus/procfs v0.15.1 // indirect
|
github.com/prometheus/procfs v0.15.1 // indirect
|
||||||
|
github.com/stretchr/objx v0.5.2 // indirect
|
||||||
go.uber.org/atomic v1.11.0 // indirect
|
go.uber.org/atomic v1.11.0 // indirect
|
||||||
golang.org/x/sys v0.25.0 // indirect
|
golang.org/x/sys v0.25.0 // indirect
|
||||||
google.golang.org/protobuf v1.34.2 // indirect
|
google.golang.org/protobuf v1.34.2 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
11
go.sum
11
go.sum
@@ -21,6 +21,10 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
|||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
|
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
|
||||||
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
||||||
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
@@ -39,6 +43,10 @@ github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G
|
|||||||
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
|
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
|
||||||
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
|
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
|
||||||
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
|
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
|
||||||
|
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
|
||||||
|
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
||||||
|
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.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||||
@@ -49,5 +57,8 @@ golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
|||||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
||||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"me-fit/db"
|
"me-fit/db"
|
||||||
|
m "me-fit/mocks"
|
||||||
"me-fit/types"
|
"me-fit/types"
|
||||||
|
|
||||||
"errors"
|
"errors"
|
||||||
@@ -11,45 +12,38 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DbAuthStub struct {
|
|
||||||
user *db.User
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d DbAuthStub) GetUser(email string) (*db.User, error) {
|
|
||||||
return d.user, d.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSignIn(t *testing.T) {
|
func TestSignIn(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
t.Run("should return user if password is correct", func(t *testing.T) {
|
t.Run("should return user if password is correct", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
salt := []byte("salt")
|
salt := []byte("salt")
|
||||||
verifiedAt := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
|
verifiedAt := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
|
||||||
stub := DbAuthStub{
|
|
||||||
user: db.NewUser(
|
|
||||||
uuid.New(),
|
|
||||||
"test@test.de",
|
|
||||||
true,
|
|
||||||
&verifiedAt,
|
|
||||||
false,
|
|
||||||
GetHashPassword("password", salt),
|
|
||||||
salt,
|
|
||||||
time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
|
|
||||||
),
|
|
||||||
err: nil,
|
|
||||||
}
|
|
||||||
underTest := NewServiceAuthImpl(stub)
|
|
||||||
|
|
||||||
actualUser, err := underTest.SignIn("test@test.de", "password")
|
user := db.NewUser(
|
||||||
|
uuid.New(),
|
||||||
|
"test@test.de",
|
||||||
|
true,
|
||||||
|
&verifiedAt,
|
||||||
|
false,
|
||||||
|
GetHashPassword("password", salt),
|
||||||
|
salt,
|
||||||
|
time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||||
|
)
|
||||||
|
|
||||||
|
mockDbAuth := m.NewMockDbAuth(t)
|
||||||
|
mockDbAuth.EXPECT().GetUser("test@test.de").Return(user, nil)
|
||||||
|
|
||||||
|
underTest := NewServiceAuthImpl(mockDbAuth)
|
||||||
|
|
||||||
|
actualUser, err := underTest.SignIn(user.Email, "password")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected nil, got %v", err)
|
t.Errorf("Expected nil, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedUser := User{
|
expectedUser := User{
|
||||||
Id: stub.user.Id,
|
Id: user.Id,
|
||||||
Email: stub.user.Email,
|
Email: user.Email,
|
||||||
EmailVerified: stub.user.EmailVerified,
|
EmailVerified: user.EmailVerified,
|
||||||
}
|
}
|
||||||
if *actualUser != expectedUser {
|
if *actualUser != expectedUser {
|
||||||
t.Errorf("Expected %v, got %v", expectedUser, actualUser)
|
t.Errorf("Expected %v, got %v", expectedUser, actualUser)
|
||||||
@@ -60,20 +54,21 @@ func TestSignIn(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
salt := []byte("salt")
|
salt := []byte("salt")
|
||||||
verifiedAt := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
|
verifiedAt := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
|
||||||
stub := DbAuthStub{
|
user := db.NewUser(
|
||||||
user: db.NewUser(
|
uuid.New(),
|
||||||
uuid.New(),
|
"test@test.de",
|
||||||
"test@test.de",
|
true,
|
||||||
true,
|
&verifiedAt,
|
||||||
&verifiedAt,
|
false,
|
||||||
false,
|
GetHashPassword("password", salt),
|
||||||
GetHashPassword("password", salt),
|
salt,
|
||||||
salt,
|
time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||||
time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
|
)
|
||||||
),
|
|
||||||
err: nil,
|
mockDbAuth := m.NewMockDbAuth(t)
|
||||||
}
|
mockDbAuth.EXPECT().GetUser(user.Email).Return(user, nil)
|
||||||
underTest := NewServiceAuthImpl(stub)
|
|
||||||
|
underTest := NewServiceAuthImpl(mockDbAuth)
|
||||||
|
|
||||||
_, err := underTest.SignIn("test@test.de", "wrong password")
|
_, err := underTest.SignIn("test@test.de", "wrong password")
|
||||||
if err != ErrInvaidCredentials {
|
if err != ErrInvaidCredentials {
|
||||||
@@ -82,11 +77,10 @@ func TestSignIn(t *testing.T) {
|
|||||||
})
|
})
|
||||||
t.Run("should return ErrInvalidCretentials if user has not been found", func(t *testing.T) {
|
t.Run("should return ErrInvalidCretentials if user has not been found", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
stub := DbAuthStub{
|
mockDbAuth := m.NewMockDbAuth(t)
|
||||||
user: nil,
|
mockDbAuth.EXPECT().GetUser("test").Return(nil, db.ErrUserNotFound)
|
||||||
err: db.ErrUserNotFound,
|
|
||||||
}
|
underTest := NewServiceAuthImpl(mockDbAuth)
|
||||||
underTest := NewServiceAuthImpl(stub)
|
|
||||||
|
|
||||||
_, err := underTest.SignIn("test", "test")
|
_, err := underTest.SignIn("test", "test")
|
||||||
if err != ErrInvaidCredentials {
|
if err != ErrInvaidCredentials {
|
||||||
@@ -95,11 +89,10 @@ func TestSignIn(t *testing.T) {
|
|||||||
})
|
})
|
||||||
t.Run("should forward ErrInternal on any other error", func(t *testing.T) {
|
t.Run("should forward ErrInternal on any other error", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
stub := DbAuthStub{
|
mockDbAuth := m.NewMockDbAuth(t)
|
||||||
user: nil,
|
mockDbAuth.EXPECT().GetUser("test").Return(nil, errors.New("Some error"))
|
||||||
err: errors.New("Some error"),
|
|
||||||
}
|
underTest := NewServiceAuthImpl(mockDbAuth)
|
||||||
underTest := NewServiceAuthImpl(stub)
|
|
||||||
|
|
||||||
_, err := underTest.SignIn("test", "test")
|
_, err := underTest.SignIn("test", "test")
|
||||||
if err != types.ErrInternal {
|
if err != types.ErrInternal {
|
||||||
|
|||||||
Reference in New Issue
Block a user