Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W4 tests #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ install-deps:
GOBIN=$(LOCAL_BIN) go install google.golang.org/protobuf/cmd/[email protected]
GOBIN=$(LOCAL_BIN) go install -mod=mod google.golang.org/grpc/cmd/[email protected]
GOBIN=$(LOCAL_BIN) go install github.com/golangci/golangci-lint/cmd/[email protected]
GOBIN=$(LOCAL_BIN) go install github.com/vektra/mockery/[email protected]

.PHONY: tidy
tidy:
Expand Down
2 changes: 1 addition & 1 deletion auth-server/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/bin/
/bin/
15 changes: 15 additions & 0 deletions auth-server/.mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
quiet: False
with-expecter: True
mockname: "Mock{{.InterfaceName}}"
filename: "{{.InterfaceName}}.mock.gen.go"
dir: "{{.InterfaceDir}}/mocks"
outpkg: "mocks"
packages:
github.com/aywan/balun_miserv_s2/auth-server/internal/repository:
interfaces:
User:
Audit:
github.com/aywan/balun_miserv_s2/auth-server/internal/service:
interfaces:
User:

6 changes: 6 additions & 0 deletions auth-server/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
LOCAL_BIN?=$(CURDIR)/../bin
GO_EXE?=go
export PATH:=$(PATH):$(LOCAL_BIN)

.PHONY: tidy
tidy:
Expand All @@ -16,6 +17,11 @@ tests:
.PHONY: generate
generate:
make generate-api-user-v1
make mocks

.PHONY: mocks
mocks:
$(LOCAL_BIN)/mockery

.PHONY: generate-api-user-v1
generate-api-user-v1:
Expand Down
2 changes: 2 additions & 0 deletions auth-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/aywan/balun_miserv_s2/shared/lib/db v0.0.0
github.com/aywan/balun_miserv_s2/shared/lib/logger v0.0.0
github.com/aywan/balun_miserv_s2/shared/lib/runutil v0.0.0
github.com/brianvoe/gofakeit/v6 v6.24.0
github.com/fatih/color v1.15.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -38,6 +39,7 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.uber.org/dig v1.17.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/net v0.15.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions auth-server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8
github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/brianvoe/gofakeit/v6 v6.24.0 h1:74yq7RRz/noddscZHRS2T84oHZisW9muwbb8sRnU52A=
github.com/brianvoe/gofakeit/v6 v6.24.0/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8=
github.com/cockroachdb/cockroach-go/v2 v2.2.0 h1:/5znzg5n373N/3ESjHF5SMLxiW4RKB05Ql//KWfeTFs=
github.com/cockroachdb/cockroach-go/v2 v2.2.0/go.mod h1:u3MiKYGupPPjkn3ozknpMUpxPaNLTFWAya419/zv6eI=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -57,11 +59,14 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
Expand Down
129 changes: 129 additions & 0 deletions auth-server/internal/api/user/impl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package user

import (
"context"
"database/sql"
"testing"

"github.com/aywan/balun_miserv_s2/auth-server/internal/model"
"github.com/aywan/balun_miserv_s2/auth-server/internal/repository/user/dto"
"github.com/aywan/balun_miserv_s2/auth-server/internal/service/mocks"
desc "github.com/aywan/balun_miserv_s2/auth-server/pkg/grpc/v1/user_v1"
"github.com/brianvoe/gofakeit/v6"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
)

func TestImplementation_Create(t *testing.T) {
t.Parallel()
log := zaptest.NewLogger(t)
ctx := context.Background()
var err error

userID := gofakeit.Int64()
pwd := gofakeit.HexUint16()

req := &desc.CreateRequest{
User: &desc.UserData{
Name: gofakeit.Name(),
Email: gofakeit.Email(),
Role: desc.UserRole_ADMIN,
},
Credentials: &desc.UserCredentials{
Password: pwd,
PasswordConfirm: pwd,
},
}

userService := mocks.NewMockUser(t)
userService.EXPECT().
Create(ctx, model.UserData{
Name: req.User.Name,
Email: req.User.Email,
PasswordHash: req.Credentials.Password,
Role: int32(req.User.Role),
}).
Return(userID, nil)

srv := New(log, userService)

rsp, err := srv.Create(ctx, req)
require.NoError(t, err)

require.Equal(t, rsp.Id, userID)
}

func TestImplementation_Update(t *testing.T) {
t.Parallel()
log := zaptest.NewLogger(t)
ctx := context.Background()
var err error

req := desc.UpdateRequest{
Id: gofakeit.Int64(),
Name: wrapperspb.String(gofakeit.Name()),
Email: wrapperspb.String(gofakeit.Email()),
}

userService := mocks.NewMockUser(t)
userService.EXPECT().
Update(ctx, req.Id, dto.UpdateDTO{
Name: sql.NullString{req.Name.Value, true},
Email: sql.NullString{req.Email.Value, true},
PasswordHash: sql.NullString{},
Role: sql.NullInt32{},
}).
Return(nil)

srv := New(log, userService)

_, err = srv.Update(ctx, &req)
require.NoError(t, err)
}

func TestImplementation_Delete(t *testing.T) {
t.Parallel()
log := zaptest.NewLogger(t)
ctx := context.Background()
var err error

userID := gofakeit.Int64()

userService := mocks.NewMockUser(t)
userService.EXPECT().
Delete(ctx, userID).
Return(nil)

srv := New(log, userService)

_, err = srv.Delete(ctx, &desc.UserIdRequest{Id: userID})
require.NoError(t, err)
}

func TestImplementation_Get(t *testing.T) {
t.Parallel()
log := zaptest.NewLogger(t)
ctx := context.Background()
var err error

userModel := model.FixtureUserWithFaker(t)

userService := mocks.NewMockUser(t)
userService.EXPECT().
Get(ctx, userModel.ID).
Return(userModel, nil)

srv := New(log, userService)

rsp, err := srv.Get(ctx, &desc.UserIdRequest{Id: userModel.ID})
require.NoError(t, err)

require.Equal(t, rsp.Id, userModel.ID)
require.Equal(t, rsp.CreatedAt, timestamppb.New(userModel.CreatedAt))
require.Equal(t, rsp.UpdatedAt, timestamppb.New(userModel.UpdatedAt.Time))
require.Equal(t, rsp.User.Name, userModel.Data.Name)
require.Equal(t, rsp.User.Email, userModel.Data.Email)
require.Equal(t, rsp.User.Role, desc.UserRole(userModel.Data.Role))
}
5 changes: 5 additions & 0 deletions auth-server/internal/model/audit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package model

type AuditReference string

type AuditAction string
27 changes: 27 additions & 0 deletions auth-server/internal/model/user_fixture.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package model

import (
"database/sql"
"testing"

"github.com/brianvoe/gofakeit/v6"
)

func FixtureUserWithFaker(t *testing.T) User {
t.Helper()

userModel := User{
ID: gofakeit.Int64(),
CreatedAt: gofakeit.Date(),
UpdatedAt: sql.NullTime{gofakeit.Date(), true},
DeletedAt: sql.NullTime{},
Data: UserData{
Name: gofakeit.Name(),
Email: gofakeit.Email(),
PasswordHash: gofakeit.HexUint128(),
Role: gofakeit.Int32(),
},
}

return userModel
}
6 changes: 4 additions & 2 deletions auth-server/internal/repository/audit/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package dto

import (
"database/sql"

"github.com/aywan/balun_miserv_s2/auth-server/internal/model"
)

type InsertDTO struct {
CreatorId sql.NullInt64
Reference string
Reference model.AuditReference
ReferenceID int64
Action string
Action model.AuditAction
}
90 changes: 90 additions & 0 deletions auth-server/internal/repository/mocks/Audit.mock.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading