Compare commits
4 Commits
322b441c70
...
3b18a15494
Author | SHA1 | Date | |
---|---|---|---|
|
3b18a15494 | ||
|
c94b0b532b | ||
|
606289be1a | ||
|
382da3d811 |
@ -64,19 +64,14 @@ type createParams struct {
|
|||||||
// Since we use JWT method, this token is not stored anywhere. Thus it
|
// Since we use JWT method, this token is not stored anywhere. Thus it
|
||||||
// stops at the controller level.
|
// stops at the controller level.
|
||||||
func (sc *SessionController) Create(ctx *gin.Context) {
|
func (sc *SessionController) Create(ctx *gin.Context) {
|
||||||
var params createParams
|
var user model.UserExistDTO
|
||||||
|
|
||||||
if err := ctx.Bind(¶ms); err != nil {
|
if err := ctx.Bind(&user); err != nil {
|
||||||
log.ErrorLog("param error", "err", err)
|
log.ErrorLog("param error", "err", err)
|
||||||
core.WriteResponse(ctx, UserParamsErr, nil)
|
core.WriteResponse(ctx, UserParamsErr, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user := model.User{
|
|
||||||
Email: params.Email,
|
|
||||||
Password: params.Password,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := sc.userUsecase.Exist(ctx, &user)
|
err := sc.userUsecase.Exist(ctx, &user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.WriteResponse(ctx, err, nil)
|
core.WriteResponse(ctx, err, nil)
|
||||||
|
@ -24,7 +24,9 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -32,12 +34,43 @@ import (
|
|||||||
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/controller/usecasemock"
|
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/controller/usecasemock"
|
||||||
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/usecase/usecase"
|
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/usecase/usecase"
|
||||||
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno"
|
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno"
|
||||||
|
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/middleware/authn"
|
||||||
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/test"
|
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/test"
|
||||||
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/token"
|
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/token"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// {{{ Test Cache
|
||||||
|
|
||||||
|
type testCache struct {
|
||||||
|
kvMap map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *testCache) Get(ctx context.Context, key string) (string, error) {
|
||||||
|
val, ok := c.kvMap[key]
|
||||||
|
if ok {
|
||||||
|
return val.(string), nil
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *testCache) Set(
|
||||||
|
ctx context.Context,
|
||||||
|
key string,
|
||||||
|
value interface{},
|
||||||
|
expiration time.Duration,
|
||||||
|
) error {
|
||||||
|
c.kvMap[key] = value
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *testCache) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
func TestSessionCreate(t *testing.T) {
|
func TestSessionCreate(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
Name string
|
Name string
|
||||||
@ -93,3 +126,57 @@ func TestSessionCreate(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSessionDelete(t *testing.T) {
|
||||||
|
testUserUsecase := usecasemock.NewtestUserUsecase()
|
||||||
|
kvMap := make(map[string]interface{}, 1)
|
||||||
|
tc := &testCache{kvMap: kvMap}
|
||||||
|
sessionController := NewSessionController(testUserUsecase, tc)
|
||||||
|
r := gin.New()
|
||||||
|
session := r.Group("/session")
|
||||||
|
{
|
||||||
|
session.POST("/create", func(ctx *gin.Context) { sessionController.Create(ctx) })
|
||||||
|
session.Use(authn.Authn(tc))
|
||||||
|
session.POST("/delete", func(ctx *gin.Context) { sessionController.Delete(ctx) })
|
||||||
|
}
|
||||||
|
|
||||||
|
params := createParams{
|
||||||
|
Email: "correct@correct.com",
|
||||||
|
Password: "strong password",
|
||||||
|
}
|
||||||
|
user, _ := json.Marshal(params)
|
||||||
|
res := test.PerformRequest(t, r, "POST", "/session/create", bytes.NewReader(user),
|
||||||
|
test.Header{
|
||||||
|
Key: "content-type",
|
||||||
|
Value: "application/json",
|
||||||
|
})
|
||||||
|
|
||||||
|
var tk Token
|
||||||
|
_ = json.NewDecoder(res.Result().Body).Decode(&tk)
|
||||||
|
tkResp, _ := token.Parse(tk.Token)
|
||||||
|
|
||||||
|
// Log out
|
||||||
|
res = test.PerformRequest(t, r, "POST", "/session/delete", nil,
|
||||||
|
test.Header{
|
||||||
|
Key: "Authorization",
|
||||||
|
Value: fmt.Sprintf("Bearer %s", tkResp.Raw),
|
||||||
|
})
|
||||||
|
|
||||||
|
var loggedOut string
|
||||||
|
err := json.NewDecoder(res.Result().Body).Decode(&loggedOut)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "logged out", loggedOut)
|
||||||
|
|
||||||
|
// Try to access the handler with the old token
|
||||||
|
res = test.PerformRequest(t, r, "POST", "/session/delete", nil,
|
||||||
|
test.Header{
|
||||||
|
Key: "Authorization",
|
||||||
|
Value: fmt.Sprintf("Bearer %s", tkResp.Raw),
|
||||||
|
})
|
||||||
|
|
||||||
|
var unauth errno.Errno
|
||||||
|
err = json.NewDecoder(res.Result().Body).Decode(&unauth)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
unauth.HTTP = res.Result().StatusCode
|
||||||
|
assert.Equal(t, *authn.ErrLoggedOut, unauth)
|
||||||
|
}
|
||||||
|
@ -35,11 +35,11 @@ func NewtestUserUsecase() usecase.User {
|
|||||||
return &testUserUsecase{}
|
return &testUserUsecase{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*testUserUsecase) Create(ctx context.Context, u *model.User) (*model.User, error) {
|
func (*testUserUsecase) Create(ctx context.Context, u *model.UserCreateDTO) (*model.User, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*testUserUsecase) Exist(ctx context.Context, u *model.User) error {
|
func (*testUserUsecase) Exist(ctx context.Context, u *model.UserExistDTO) error {
|
||||||
switch u.Email {
|
switch u.Email {
|
||||||
case "a@b.c":
|
case "a@b.c":
|
||||||
if u.Password == "strong password" {
|
if u.Password == "strong password" {
|
||||||
|
@ -57,26 +57,14 @@ func NewUserController(us usecase.User) User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (uc *UserController) Create(ctx core.Context) {
|
func (uc *UserController) Create(ctx core.Context) {
|
||||||
var params struct {
|
var userDTO model.UserCreateDTO
|
||||||
Email string `json:"email" binding:"required,email"`
|
|
||||||
FirstName string `json:"first_name" binding:"required"`
|
|
||||||
LastName string `json:"last_name" binding:"required"`
|
|
||||||
Password string `json:"password" binding:"required"`
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ctx.Bind(¶ms); err != nil {
|
if err := ctx.Bind(&userDTO); err != nil {
|
||||||
core.WriteResponse(ctx, UserParamsErr, nil)
|
core.WriteResponse(ctx, UserParamsErr, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user := model.User{
|
_, err := uc.userUsecase.Create(ctx, &userDTO)
|
||||||
Email: params.Email,
|
|
||||||
FirstName: params.FirstName,
|
|
||||||
LastName: params.LastName,
|
|
||||||
Password: params.Password,
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := uc.userUsecase.Create(ctx, &user)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.WriteResponse(ctx, err, nil)
|
core.WriteResponse(ctx, err, nil)
|
||||||
return
|
return
|
||||||
|
38
internal/howmuch/model/event.go
Normal file
38
internal/howmuch/model/event.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2024 vinchent <vinchent@vinchent.xyz>
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
|
||||||
|
package model
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Event struct {
|
||||||
|
ID int
|
||||||
|
Name string
|
||||||
|
Description string
|
||||||
|
Users []*User
|
||||||
|
Expenses []*Expense
|
||||||
|
TotalAmount Money
|
||||||
|
DefaultCurrency Currency
|
||||||
|
CreatedBy User
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
}
|
42
internal/howmuch/model/expense.go
Normal file
42
internal/howmuch/model/expense.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2024 vinchent <vinchent@vinchent.xyz>
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
|
||||||
|
package model
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type ExpenseDetail struct {
|
||||||
|
Name string
|
||||||
|
Place string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Expense struct {
|
||||||
|
ID int
|
||||||
|
Amount Money
|
||||||
|
Currency Currency
|
||||||
|
PayerIDs []int
|
||||||
|
RecipientIDs []int
|
||||||
|
EventID int
|
||||||
|
Detail ExpenseDetail
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
}
|
66
internal/howmuch/model/money.go
Normal file
66
internal/howmuch/model/money.go
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
// Copyright (c) 2024 vinchent <vinchent@vinchent.xyz>
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
|
||||||
|
package model
|
||||||
|
|
||||||
|
type Currency string
|
||||||
|
|
||||||
|
// TODO: may handle a more complexe logic with the exchange rate.
|
||||||
|
|
||||||
|
// XXX: Here we suppose that the currency is the same for every piece
|
||||||
|
// of money involved in the calculate.
|
||||||
|
|
||||||
|
const (
|
||||||
|
EUR Currency = "EUR"
|
||||||
|
USD Currency = "USD"
|
||||||
|
CNY Currency = "CNY"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Money struct {
|
||||||
|
ammount int
|
||||||
|
currency Currency
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeMoney(amount int, currency Currency) Money {
|
||||||
|
return Money{amount, currency}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Add(cur Currency, money ...Money) Money {
|
||||||
|
var sum Money
|
||||||
|
sum.currency = cur
|
||||||
|
|
||||||
|
for _, m := range money {
|
||||||
|
sum.ammount += m.ammount
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
func Diff(cur Currency, money1 Money, money2 Money) Money {
|
||||||
|
var diff Money
|
||||||
|
|
||||||
|
diff.currency = cur
|
||||||
|
|
||||||
|
diff.ammount = money1.ammount - money2.ammount
|
||||||
|
|
||||||
|
return diff
|
||||||
|
}
|
@ -24,6 +24,18 @@ package model
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
type UserCreateDTO struct {
|
||||||
|
Email string `json:"email" binding:"required,email"`
|
||||||
|
FirstName string `json:"first_name" binding:"required"`
|
||||||
|
LastName string `json:"last_name" binding:"required"`
|
||||||
|
Password string `json:"password" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserExistDTO struct {
|
||||||
|
Email string `json:"email" binding:"required,email"`
|
||||||
|
Password string `json:"password" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
// User model
|
// User model
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int
|
ID int
|
||||||
|
@ -60,8 +60,8 @@ type userUsecase struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type User interface {
|
type User interface {
|
||||||
Create(ctx context.Context, u *model.User) (*model.User, error)
|
Create(ctx context.Context, u *model.UserCreateDTO) (*model.User, error)
|
||||||
Exist(ctx context.Context, u *model.User) error
|
Exist(ctx context.Context, u *model.UserExistDTO) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserUsecase(r repo.UserRepository, d repo.DBRepository) User {
|
func NewUserUsecase(r repo.UserRepository, d repo.DBRepository) User {
|
||||||
@ -71,7 +71,7 @@ func NewUserUsecase(r repo.UserRepository, d repo.DBRepository) User {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uuc *userUsecase) Create(ctx context.Context, u *model.User) (*model.User, error) {
|
func (uuc *userUsecase) Create(ctx context.Context, u *model.UserCreateDTO) (*model.User, error) {
|
||||||
// Hash the password
|
// Hash the password
|
||||||
encrypted, err := bcrypt.GenerateFromPassword([]byte(u.Password), 12)
|
encrypted, err := bcrypt.GenerateFromPassword([]byte(u.Password), 12)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -82,7 +82,12 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.User) (*model.User,
|
|||||||
data, err := uuc.dbRepo.Transaction(
|
data, err := uuc.dbRepo.Transaction(
|
||||||
ctx,
|
ctx,
|
||||||
func(txCtx context.Context, tx interface{}) (interface{}, error) {
|
func(txCtx context.Context, tx interface{}) (interface{}, error) {
|
||||||
u, err := uuc.userRepo.Create(txCtx, tx, u)
|
created, err := uuc.userRepo.Create(txCtx, tx, &model.User{
|
||||||
|
Email: u.Email,
|
||||||
|
Password: u.Password,
|
||||||
|
FirstName: u.FirstName,
|
||||||
|
LastName: u.LastName,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
match, _ := regexp.MatchString("SQLSTATE 23505", err.Error())
|
match, _ := regexp.MatchString("SQLSTATE 23505", err.Error())
|
||||||
if match {
|
if match {
|
||||||
@ -100,7 +105,7 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.User) (*model.User,
|
|||||||
fmt.Sprintf("%s %s", u.FirstName, u.LastName),
|
fmt.Sprintf("%s %s", u.FirstName, u.LastName),
|
||||||
)
|
)
|
||||||
|
|
||||||
return u, err
|
return created, err
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -113,7 +118,7 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.User) (*model.User,
|
|||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uuc *userUsecase) Exist(ctx context.Context, u *model.User) error {
|
func (uuc *userUsecase) Exist(ctx context.Context, u *model.UserExistDTO) error {
|
||||||
got, err := uuc.userRepo.GetByEmail(ctx, u.Email)
|
got, err := uuc.userRepo.GetByEmail(ctx, u.Email)
|
||||||
// Any query error?
|
// Any query error?
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -29,30 +29,42 @@ import (
|
|||||||
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/model"
|
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/model"
|
||||||
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/usecase/usecase/repomock"
|
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/usecase/usecase/repomock"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateUser(t *testing.T) {
|
func TestCreateUser(t *testing.T) {
|
||||||
t.Run("normal create", func(t *testing.T) {
|
t.Run("normal create", func(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
userUsecase := NewUserUsecase(&repomock.TestUserRepository{}, &repomock.TestDBRepository{})
|
userUsecase := NewUserUsecase(&repomock.TestUserRepository{}, &repomock.TestDBRepository{})
|
||||||
input := &model.User{
|
input := &model.UserCreateDTO{
|
||||||
Email: "a@b.c",
|
Email: "a@b.c",
|
||||||
FirstName: "James",
|
FirstName: "James",
|
||||||
LastName: "Bond",
|
LastName: "Bond",
|
||||||
Password: "verystrong",
|
Password: "verystrong",
|
||||||
}
|
}
|
||||||
want := input
|
want := &model.User{
|
||||||
want.ID = 123
|
ID: 123,
|
||||||
|
Email: input.Email,
|
||||||
|
FirstName: input.FirstName,
|
||||||
|
LastName: input.LastName,
|
||||||
|
// Password is hashed
|
||||||
|
Password: "verystrong",
|
||||||
|
}
|
||||||
|
|
||||||
got, err := userUsecase.Create(ctx, input)
|
got, err := userUsecase.Create(ctx, input)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, want, got)
|
assert.Equal(t, want.ID, got.ID)
|
||||||
|
|
||||||
|
assert.NoError(
|
||||||
|
t,
|
||||||
|
bcrypt.CompareHashAndPassword([]byte(got.Password), []byte(want.Password)),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("duplicate create", func(t *testing.T) {
|
t.Run("duplicate create", func(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
userUsecase := NewUserUsecase(&repomock.TestUserRepository{}, &repomock.TestDBRepository{})
|
userUsecase := NewUserUsecase(&repomock.TestUserRepository{}, &repomock.TestDBRepository{})
|
||||||
input := &model.User{
|
input := &model.UserCreateDTO{
|
||||||
Email: "duplicate@error.com",
|
Email: "duplicate@error.com",
|
||||||
FirstName: "James",
|
FirstName: "James",
|
||||||
LastName: "Bond",
|
LastName: "Bond",
|
||||||
@ -67,22 +79,22 @@ func TestCreateUser(t *testing.T) {
|
|||||||
func TestUserExist(t *testing.T) {
|
func TestUserExist(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
Name string
|
Name string
|
||||||
User *model.User
|
User *model.UserExistDTO
|
||||||
ExpErr error
|
ExpErr error
|
||||||
}{
|
}{
|
||||||
{"user exists", &model.User{
|
{"user exists", &model.UserExistDTO{
|
||||||
Email: "a@b.c",
|
Email: "a@b.c",
|
||||||
Password: "strongHashed",
|
Password: "strongHashed",
|
||||||
}, nil},
|
}, nil},
|
||||||
{"query error", &model.User{
|
{"query error", &model.UserExistDTO{
|
||||||
Email: "query@error.com",
|
Email: "query@error.com",
|
||||||
Password: "strongHashed",
|
Password: "strongHashed",
|
||||||
}, repomock.UserTestDummyErr},
|
}, repomock.UserTestDummyErr},
|
||||||
{"user doesn not exist", &model.User{
|
{"user doesn not exist", &model.UserExistDTO{
|
||||||
Email: "inexist@error.com",
|
Email: "inexist@error.com",
|
||||||
Password: "strongHashed",
|
Password: "strongHashed",
|
||||||
}, UserNotExist},
|
}, UserNotExist},
|
||||||
{"wrong password", &model.User{
|
{"wrong password", &model.UserExistDTO{
|
||||||
Email: "a@b.c",
|
Email: "a@b.c",
|
||||||
Password: "wrongHashed",
|
Password: "wrongHashed",
|
||||||
}, UserWrongPassword},
|
}, UserWrongPassword},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user