Skip to content

Commit

Permalink
fix: Fixing interface dependencies
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 8, 2021
1 parent f85bafc commit d6bb28f
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 383 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ style:
mocks:
find . -name "mocks" -type d -exec rm -r "{}" \+
mockgen -destination pkg/mocks/mailer.go -mock_names Mailer=Mailer -package mocks github.com/ViBiOh/ketchup/pkg/notifier Mailer
mockgen -destination pkg/mocks/auth.go -mock_names AuthService=Auth -package mocks github.com/ViBiOh/ketchup/pkg/service/user AuthService

## test: Shortcut to launch all the test tasks (unit, functional and integration).
.PHONY: test
Expand Down
2 changes: 1 addition & 1 deletion cmd/ketchup/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func main() {

authServiceApp, authMiddlewareApp := initAuth(ketchupDb)

userServiceApp := userService.New(userStore.New(ketchupDb), authServiceApp)
userServiceApp := userService.New(userStore.New(ketchupDb), &authServiceApp)
githubApp := github.New(githubConfig, redisApp)
dockerApp := docker.New(dockerConfig)
helmApp := helm.New()
Expand Down
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ go 1.16

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/ViBiOh/auth/v2 v2.11.1
github.com/ViBiOh/auth/v2 v2.11.2
github.com/ViBiOh/httputils/v4 v4.19.3
github.com/ViBiOh/mailer v1.22.1
github.com/golang/mock v1.6.0
github.com/lib/pq v1.10.2
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.29.0 // indirect
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 // indirect
google.golang.org/protobuf v1.27.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
322 changes: 8 additions & 314 deletions go.sum

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions pkg/middleware/middleware_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package middleware

import (
"context"
"net/http"
"net/http/httptest"
"testing"

authModel "github.com/ViBiOh/auth/v2/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/request"
"github.com/ViBiOh/ketchup/pkg/model"
"github.com/ViBiOh/ketchup/pkg/service/user/usertest"
"github.com/ViBiOh/ketchup/pkg/service/user"
"github.com/ViBiOh/ketchup/pkg/store/user/usertest"
)

func TestMiddleware(t *testing.T) {
Expand All @@ -26,7 +29,7 @@ func TestMiddleware(t *testing.T) {
t.Errorf("unable to write: %s", err)
}
}),
httptest.NewRequest(http.MethodGet, "/", nil),
httptest.NewRequest(http.MethodGet, "/", nil).WithContext(authModel.StoreUser(context.Background(), authModel.NewUser(8000, "test"))),
"nobody@localhost",
http.StatusOK,
http.Header{},
Expand All @@ -44,7 +47,8 @@ func TestMiddleware(t *testing.T) {
for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
writer := httptest.NewRecorder()
New(usertest.NewApp()).Middleware(tc.next).ServeHTTP(writer, tc.request)
userService := user.New(usertest.New().SetGetByLoginID(model.NewUser(1, "nobody@localhost", authModel.NewUser(8000, "test")), nil), nil)
New(userService).Middleware(tc.next).ServeHTTP(writer, tc.request)

if got := writer.Code; got != tc.wantStatus {
t.Errorf("Middleware = %d, want %d", got, tc.wantStatus)
Expand Down
38 changes: 19 additions & 19 deletions pkg/service/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,34 @@ import (
"strings"

authModel "github.com/ViBiOh/auth/v2/pkg/model"
authService "github.com/ViBiOh/auth/v2/pkg/service"
"github.com/ViBiOh/httputils/v4/pkg/logger"
httpModel "github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/ketchup/pkg/model"
"github.com/ViBiOh/ketchup/pkg/store/user"
)

// App of package
type App interface {
Create(ctx context.Context, o model.User) (model.User, error)
StoreInContext(ctx context.Context) context.Context
Count(ctx context.Context) (uint64, error)
// AuthService defines interaction with storage and provider from User
type AuthService interface {
Create(context.Context, authModel.User) (authModel.User, error)
Check(context.Context, authModel.User, authModel.User) error
}

type app struct {
// App of package
type App struct {
userStore user.App

authService authService.App
authApp AuthService
}

// New creates new App from Config
func New(userStore user.App, authService authService.App) App {
return app{
func New(userStore user.App, authApp AuthService) App {
return App{
userStore: userStore,

authService: authService,
authApp: authApp,
}
}

func (a app) StoreInContext(ctx context.Context) context.Context {
// StoreInContext read login user from context and store app user in context
func (a App) StoreInContext(ctx context.Context) context.Context {
id := authModel.ReadUser(ctx).ID
if id == 0 {
logger.Warn("no login user in context")
Expand All @@ -52,19 +50,20 @@ func (a app) StoreInContext(ctx context.Context) context.Context {
return model.StoreUser(ctx, item)
}

func (a app) Create(ctx context.Context, item model.User) (model.User, error) {
// Create user
func (a App) Create(ctx context.Context, item model.User) (model.User, error) {
if err := a.check(ctx, model.NoneUser, item); err != nil {
return model.NoneUser, httpModel.WrapInvalid(err)
}

if err := a.authService.Check(ctx, authModel.NoneUser, item.Login); err != nil {
if err := a.authApp.Check(ctx, authModel.NoneUser, item.Login); err != nil {
return model.NoneUser, httpModel.WrapInvalid(err)
}

var output model.User

err := a.userStore.DoAtomic(ctx, func(ctx context.Context) error {
loginUser, err := a.authService.Create(ctx, item.Login)
loginUser, err := a.authApp.Create(ctx, item.Login)
if err != nil {
return httpModel.WrapInternal(fmt.Errorf("unable to create login: %w", err))
}
Expand All @@ -85,7 +84,7 @@ func (a app) Create(ctx context.Context, item model.User) (model.User, error) {
return output, err
}

func (a app) check(ctx context.Context, _, new model.User) error {
func (a App) check(ctx context.Context, _, new model.User) error {
if new == model.NoneUser {
return nil
}
Expand All @@ -105,6 +104,7 @@ func (a app) check(ctx context.Context, _, new model.User) error {
return httpModel.ConcatError(output)
}

func (a app) Count(ctx context.Context) (uint64, error) {
// Count users
func (a App) Count(ctx context.Context) (uint64, error) {
return a.userStore.Count(ctx)
}
59 changes: 47 additions & 12 deletions pkg/service/user/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"testing"

authModel "github.com/ViBiOh/auth/v2/pkg/model"
"github.com/ViBiOh/auth/v2/pkg/service/servicetest"
httpModel "github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/ketchup/pkg/mocks"
"github.com/ViBiOh/ketchup/pkg/model"
"github.com/ViBiOh/ketchup/pkg/store/user/usertest"
"github.com/golang/mock/gomock"
)

var (
Expand Down Expand Up @@ -89,7 +90,9 @@ func TestCreate(t *testing.T) {
}{
{
"invalid user",
New(usertest.New(), servicetest.New()),
App{
userStore: usertest.New(),
},
args{
ctx: context.TODO(),
item: model.NewUser(1, "", authModel.NewUser(1, "")),
Expand All @@ -99,7 +102,9 @@ func TestCreate(t *testing.T) {
},
{
"invalid auth",
New(usertest.New(), servicetest.New().SetCheck(errors.New("failed"))),
App{
userStore: usertest.New(),
},
args{
ctx: context.TODO(),
item: model.NewUser(0, testEmail, authModel.NewUser(0, "")),
Expand All @@ -109,7 +114,9 @@ func TestCreate(t *testing.T) {
},
{
"start atomic error",
New(usertest.New().SetGetByEmail(model.NoneUser, nil).SetDoAtomic(errAtomicStart), servicetest.New()),
App{
userStore: usertest.New().SetGetByEmail(model.NoneUser, nil).SetDoAtomic(errAtomicStart),
},
args{
ctx: context.TODO(),
item: model.NewUser(1, testEmail, authModel.NewUser(1, "")),
Expand All @@ -119,7 +126,9 @@ func TestCreate(t *testing.T) {
},
{
"login create error",
New(usertest.New().SetGetByEmail(model.NoneUser, nil), servicetest.New().SetCreate(authModel.NoneUser, errors.New("failed"))),
App{
userStore: usertest.New().SetGetByEmail(model.NoneUser, nil),
},
args{
ctx: context.Background(),
item: model.NewUser(1, testEmail, authModel.NewUser(1, "")),
Expand All @@ -129,7 +138,9 @@ func TestCreate(t *testing.T) {
},
{
"user create error",
New(usertest.New().SetGetByEmail(model.NoneUser, nil).SetCreate(0, errors.New("failed")), servicetest.New()),
App{
userStore: usertest.New().SetGetByEmail(model.NoneUser, nil).SetCreate(0, errors.New("failed")),
},
args{
ctx: context.Background(),
item: model.NewUser(2, testEmail, authModel.NewUser(2, "")),
Expand All @@ -139,7 +150,9 @@ func TestCreate(t *testing.T) {
},
{
"success",
New(usertest.New().SetGetByEmail(model.NoneUser, nil).SetCreate(2, nil), servicetest.New().SetCreate(authModel.NewUser(2, "admin"), nil)),
App{
userStore: usertest.New().SetGetByEmail(model.NoneUser, nil).SetCreate(2, nil),
},
args{
ctx: context.Background(),
item: model.NewUser(2, testEmail, authModel.NewUser(2, "")),
Expand All @@ -151,6 +164,28 @@ func TestCreate(t *testing.T) {

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

authApp := mocks.NewAuth(ctrl)
tc.instance.authApp = authApp

switch tc.intention {
case "invalid auth":
authApp.EXPECT().Check(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("failed"))
case "start atomic error":
authApp.EXPECT().Check(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
case "login create error":
authApp.EXPECT().Check(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
authApp.EXPECT().Create(gomock.Any(), gomock.Any()).Return(authModel.NoneUser, errors.New("failed"))
case "user create error":
authApp.EXPECT().Check(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
authApp.EXPECT().Create(gomock.Any(), gomock.Any()).Return(authModel.NoneUser, errors.New("failed"))
case "success":
authApp.EXPECT().Check(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
authApp.EXPECT().Create(gomock.Any(), gomock.Any()).Return(authModel.NewUser(2, "admin"), nil)
}

got, gotErr := tc.instance.Create(tc.args.ctx, tc.args.item)

failed := false
Expand All @@ -177,21 +212,21 @@ func TestCheck(t *testing.T) {

var cases = []struct {
intention string
instance app
instance App
args args
wantErr error
}{
{
"delete",
app{userStore: usertest.New()},
App{userStore: usertest.New()},
args{
ctx: context.Background(),
},
nil,
},
{
"no name",
app{userStore: usertest.New()},
App{userStore: usertest.New()},
args{
ctx: context.Background(),
new: model.NewUser(1, "", authModel.NewUser(1, "")),
Expand All @@ -200,7 +235,7 @@ func TestCheck(t *testing.T) {
},
{
"get error",
app{userStore: usertest.New().SetGetByEmail(model.NoneUser, errors.New("failed"))},
App{userStore: usertest.New().SetGetByEmail(model.NoneUser, errors.New("failed"))},
args{
ctx: context.Background(),
new: model.NewUser(1, testEmail, authModel.NewUser(1, "")),
Expand All @@ -209,7 +244,7 @@ func TestCheck(t *testing.T) {
},
{
"already used",
app{userStore: usertest.New().SetGetByEmail(model.NewUser(1, testEmail, authModel.NewUser(1, "")), nil)},
App{userStore: usertest.New().SetGetByEmail(model.NewUser(1, testEmail, authModel.NewUser(1, "")), nil)},
args{
ctx: context.Background(),
new: model.NewUser(1, testEmail, authModel.NewUser(1, "")),
Expand Down
30 changes: 0 additions & 30 deletions pkg/service/user/usertest/usertest.go

This file was deleted.

0 comments on commit d6bb28f

Please sign in to comment.