parent
043f387224
commit
fe329d2336
@ -1,39 +1,166 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.vdhsn.com/barretthousen/barretthousen/src/auth/internal/data"
|
||||
)
|
||||
|
||||
func TestDomain_CreateAccount(t *testing.T) {
|
||||
type fields struct {
|
||||
Storage Storage
|
||||
SetupTest()
|
||||
|
||||
tests := map[string]struct {
|
||||
input CreateAccountCommand
|
||||
wantOut AccountCreated
|
||||
wantErr bool
|
||||
}{
|
||||
"new account": {
|
||||
input: CreateAccountCommand{
|
||||
Email: "test@example.com",
|
||||
Password: "password",
|
||||
},
|
||||
wantOut: AccountCreated{
|
||||
Account: Account{
|
||||
ID: 0,
|
||||
Email: "test@example.com",
|
||||
PasswordHash: "password",
|
||||
Enabled: true,
|
||||
Role: UserRole,
|
||||
},
|
||||
},
|
||||
},
|
||||
"no email": {
|
||||
input: CreateAccountCommand{
|
||||
Password: "password",
|
||||
Role: UserRole,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
"no password": {
|
||||
input: CreateAccountCommand{
|
||||
Email: "test@example.com",
|
||||
Role: UserRole,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
"no inputs": {
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
type args struct {
|
||||
in CreateAccountCommand
|
||||
|
||||
c := ioc.Scope("CreateAccountTest")
|
||||
|
||||
Must(c.Provide(func(s Storage, ms *MockStorage) *Domain {
|
||||
return &Domain{Storage: s}
|
||||
}))
|
||||
|
||||
for name, v := range tests {
|
||||
params := v
|
||||
t.Run(name, func(t *testing.T) {
|
||||
Must(c.Invoke(func(sut *Domain, ms *MockStorage) {
|
||||
var a Account
|
||||
ms.RegisterAccountFunc = func(ctx context.Context, rai data.RegisterAccountInput) (int, error) {
|
||||
a = Account{
|
||||
Email: rai.Email,
|
||||
PasswordHash: rai.PasswordHash,
|
||||
Role: Role(rai.Role),
|
||||
Enabled: true,
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
ms.GetAccountFunc = func(ctx context.Context, gai data.GetAccountInput) (data.AccountResult, error) {
|
||||
if gai.Email != a.Email {
|
||||
return data.AccountResult{}, errors.New("error")
|
||||
}
|
||||
return data.AccountResult{
|
||||
Email: a.Email,
|
||||
PasswordHash: a.PasswordHash,
|
||||
Role: a.Role.String(),
|
||||
Enabled: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
out, err := sut.CreateAccount(context.Background(), params.input)
|
||||
if (err != nil) != params.wantErr {
|
||||
t.Errorf("Domain.CreateAccount() error = %v, wantErr %v", err, params.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(out, params.wantOut) {
|
||||
t.Errorf("Domain.CreateAccount():\n%+v\n, want:\n%+v", out, params.wantOut)
|
||||
}
|
||||
}))
|
||||
})
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantOut AccountCreated
|
||||
}
|
||||
|
||||
func TestDomain_Login(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
input LoginCommand
|
||||
wantOut LoggedIn
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
"Login Valid User": {
|
||||
input: LoginCommand{
|
||||
Email: "test@example.com",
|
||||
Password: "password",
|
||||
},
|
||||
wantOut: LoggedIn{
|
||||
Account: Account{
|
||||
ID: 1,
|
||||
Email: "test@example.com",
|
||||
PasswordHash: "",
|
||||
Enabled: true,
|
||||
Role: UserRole,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
d := &Domain{
|
||||
Storage: tt.fields.Storage,
|
||||
}
|
||||
gotOut, err := d.CreateAccount(tt.args.in)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("Domain.CreateAccount() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(gotOut, tt.wantOut) {
|
||||
t.Errorf("Domain.CreateAccount() = %v, want %v", gotOut, tt.wantOut)
|
||||
}
|
||||
|
||||
c := ioc.Scope("LoginTest")
|
||||
|
||||
Must(c.Provide(func(s Storage) *Domain {
|
||||
return &Domain{Storage: s}
|
||||
}))
|
||||
|
||||
for name, v := range tests {
|
||||
params := v
|
||||
t.Run(name, func(t *testing.T) {
|
||||
Must(c.Invoke(func(sut *Domain, ms *MockStorage) {
|
||||
ms.CreateSessionFunc = func(ctx context.Context, in data.CreateSessionInput) (data.Session, error) {
|
||||
if strings.EqualFold(in.Email, "test@example.com") {
|
||||
return data.Session{AccountID: 1}, nil
|
||||
}
|
||||
|
||||
return data.Session{}, data.ErrCouldNotCreateSession
|
||||
}
|
||||
|
||||
ms.GetAccountFunc = func(ctx context.Context, gai data.GetAccountInput) (data.AccountResult, error) {
|
||||
if strings.EqualFold(gai.Email, "test@example.com") || gai.AccountID == 1 {
|
||||
return data.AccountResult{
|
||||
ID: 1,
|
||||
Email: "test@example.com",
|
||||
Role: UserRole.String(),
|
||||
Enabled: true,
|
||||
}, nil
|
||||
}
|
||||
return data.AccountResult{}, data.ErrAccountNotFound
|
||||
}
|
||||
|
||||
out, err := sut.Login(context.Background(), params.input)
|
||||
if (err != nil) != params.wantErr {
|
||||
t.Errorf("Domain.Login() error = %v, wantErr %v", err, params.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(out, params.wantOut) {
|
||||
t.Errorf("Domain.Login() = %v, want %v", out, params.wantOut)
|
||||
}
|
||||
}))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,65 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.vdhsn.com/barretthousen/barretthousen/src/auth/internal/data"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
var ioc dig.Container
|
||||
|
||||
func SetupTest() {
|
||||
ioc = *dig.New()
|
||||
|
||||
if err := ioc.Provide(func() (Storage, *MockStorage) {
|
||||
ms := &MockStorage{}
|
||||
return ms, ms
|
||||
}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Must panics is err is not nil
|
||||
func Must(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type MockStorage struct {
|
||||
RegisterAccountFunc func(context.Context, data.RegisterAccountInput) (int, error)
|
||||
GetAccountFunc func(context.Context, data.GetAccountInput) (data.AccountResult, error)
|
||||
CreateSessionFunc func(context.Context, data.CreateSessionInput) (data.Session, error)
|
||||
}
|
||||
|
||||
// CreateSession implements Storage.
|
||||
func (m *MockStorage) CreateSession(ctx context.Context, in data.CreateSessionInput) (data.Session, error) {
|
||||
if m.CreateSessionFunc == nil {
|
||||
panic("CreateSessionFunc is unset")
|
||||
}
|
||||
return m.CreateSessionFunc(ctx, in)
|
||||
}
|
||||
|
||||
// GetAccount implements Storage.
|
||||
func (m *MockStorage) GetAccount(ctx context.Context, in data.GetAccountInput) (data.AccountResult, error) {
|
||||
if m.GetAccountFunc == nil {
|
||||
panic("GetAccountFunc is unset")
|
||||
}
|
||||
|
||||
return m.GetAccountFunc(ctx, in)
|
||||
}
|
||||
|
||||
// RegisterAccount implements Storage.
|
||||
func (m *MockStorage) RegisterAccount(ctx context.Context, in data.RegisterAccountInput) (int, error) {
|
||||
if m.RegisterAccountFunc == nil {
|
||||
panic("RegisterAccountFunc is unset")
|
||||
}
|
||||
|
||||
return m.RegisterAccountFunc(ctx, in)
|
||||
}
|
||||
|
||||
// VerifyAccount implements Storage.
|
||||
func (*MockStorage) VerifyAccount(context.Context, data.VerifyAccountInput) error {
|
||||
panic("unimplemented")
|
||||
}
|
Loading…
Reference in new issue