From fd6c68a71bb179f2bcba2fb840b03bceb62a28de Mon Sep 17 00:00:00 2001 From: Tim Wundenberg Date: Fri, 4 Oct 2024 23:54:32 +0200 Subject: [PATCH] fix: switch to mockery instead of handcrafted stubs --- .gitignore | 1 + .mockery.yaml | 6 +++ Dockerfile | 4 +- go.mod | 5 +++ go.sum | 11 +++++ service/auth_test.go | 95 ++++++++++++++++++++------------------------ 6 files changed, 69 insertions(+), 53 deletions(-) create mode 100644 .mockery.yaml diff --git a/.gitignore b/.gitignore index 6dfd4c4..ec48227 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ node_modules/ static/css/tailwind.css static/js/htmx.min.js tmp/ +mocks/ diff --git a/.mockery.yaml b/.mockery.yaml new file mode 100644 index 0000000..5ff222d --- /dev/null +++ b/.mockery.yaml @@ -0,0 +1,6 @@ +with-expecter: True +dir: mocks/ +packages: + me-fit/db: + interfaces: + DbAuth: diff --git a/Dockerfile b/Dockerfile index 6526bc4..e533ba4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ FROM golang:1.23.2@sha256:adee809c2d0009a4199a11a1b2618990b244c6515149fe609e2788ddf164bd10 AS builder_go 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 . ./ -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 diff --git a/go.mod b/go.mod index 3376eeb..a1e369d 100644 --- a/go.mod +++ b/go.mod @@ -9,20 +9,25 @@ require ( github.com/joho/godotenv v1.5.1 github.com/mattn/go-sqlite3 v1.14.23 github.com/prometheus/client_golang v1.20.4 + github.com/stretchr/testify v1.9.0 golang.org/x/crypto v0.27.0 ) require ( github.com/beorn7/perks v1.0.1 // 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/go-multierror v1.1.1 // indirect github.com/klauspost/compress v1.17.9 // 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/common v0.55.0 // 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 golang.org/x/sys v0.25.0 // indirect google.golang.org/protobuf v1.34.2 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index ce13c35..4c223da 100644 --- a/go.sum +++ b/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/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= 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/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= 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/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= 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/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 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= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= 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/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/service/auth_test.go b/service/auth_test.go index 1ee4250..0e12e14 100644 --- a/service/auth_test.go +++ b/service/auth_test.go @@ -2,6 +2,7 @@ package service import ( "me-fit/db" + m "me-fit/mocks" "me-fit/types" "errors" @@ -11,45 +12,38 @@ import ( "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) { t.Parallel() t.Run("should return user if password is correct", func(t *testing.T) { t.Parallel() salt := []byte("salt") 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 { t.Errorf("Expected nil, got %v", err) } expectedUser := User{ - Id: stub.user.Id, - Email: stub.user.Email, - EmailVerified: stub.user.EmailVerified, + Id: user.Id, + Email: user.Email, + EmailVerified: user.EmailVerified, } if *actualUser != expectedUser { t.Errorf("Expected %v, got %v", expectedUser, actualUser) @@ -60,20 +54,21 @@ func TestSignIn(t *testing.T) { t.Parallel() salt := []byte("salt") 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) + 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(user.Email).Return(user, nil) + + underTest := NewServiceAuthImpl(mockDbAuth) _, err := underTest.SignIn("test@test.de", "wrong password") 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.Parallel() - stub := DbAuthStub{ - user: nil, - err: db.ErrUserNotFound, - } - underTest := NewServiceAuthImpl(stub) + mockDbAuth := m.NewMockDbAuth(t) + mockDbAuth.EXPECT().GetUser("test").Return(nil, db.ErrUserNotFound) + + underTest := NewServiceAuthImpl(mockDbAuth) _, err := underTest.SignIn("test", "test") 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.Parallel() - stub := DbAuthStub{ - user: nil, - err: errors.New("Some error"), - } - underTest := NewServiceAuthImpl(stub) + mockDbAuth := m.NewMockDbAuth(t) + mockDbAuth.EXPECT().GetUser("test").Return(nil, errors.New("Some error")) + + underTest := NewServiceAuthImpl(mockDbAuth) _, err := underTest.SignIn("test", "test") if err != types.ErrInternal {