Compare commits

...

4 Commits

Author SHA1 Message Date
Muyao CHEN
3b18a15494 feat: add event and expense BOs
All checks were successful
Build and test / Build (push) Successful in 2m18s
2024-10-16 00:13:38 +02:00
Muyao CHEN
c94b0b532b feat: add money value object 2024-10-16 00:05:05 +02:00
Muyao CHEN
606289be1a fix: create DTO in model and handle it in service level 2024-10-16 00:04:28 +02:00
Muyao CHEN
382da3d811 test: add test for session delete 2024-10-15 21:39:08 +02:00
10 changed files with 285 additions and 40 deletions

View File

@ -64,19 +64,14 @@ type createParams struct {
// Since we use JWT method, this token is not stored anywhere. Thus it
// stops at the controller level.
func (sc *SessionController) Create(ctx *gin.Context) {
var params createParams
var user model.UserExistDTO
if err := ctx.Bind(&params); err != nil {
if err := ctx.Bind(&user); err != nil {
log.ErrorLog("param error", "err", err)
core.WriteResponse(ctx, UserParamsErr, nil)
return
}
user := model.User{
Email: params.Email,
Password: params.Password,
}
err := sc.userUsecase.Exist(ctx, &user)
if err != nil {
core.WriteResponse(ctx, err, nil)

View File

@ -24,7 +24,9 @@ package controller
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"time"
@ -32,12 +34,43 @@ import (
"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/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/token"
"github.com/gin-gonic/gin"
"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) {
tests := []struct {
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)
}

View File

@ -35,11 +35,11 @@ func NewtestUserUsecase() usecase.User {
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
}
func (*testUserUsecase) Exist(ctx context.Context, u *model.User) error {
func (*testUserUsecase) Exist(ctx context.Context, u *model.UserExistDTO) error {
switch u.Email {
case "a@b.c":
if u.Password == "strong password" {

View File

@ -57,26 +57,14 @@ func NewUserController(us usecase.User) User {
}
func (uc *UserController) Create(ctx core.Context) {
var params 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"`
}
var userDTO model.UserCreateDTO
if err := ctx.Bind(&params); err != nil {
if err := ctx.Bind(&userDTO); err != nil {
core.WriteResponse(ctx, UserParamsErr, nil)
return
}
user := model.User{
Email: params.Email,
FirstName: params.FirstName,
LastName: params.LastName,
Password: params.Password,
}
_, err := uc.userUsecase.Create(ctx, &user)
_, err := uc.userUsecase.Create(ctx, &userDTO)
if err != nil {
core.WriteResponse(ctx, err, nil)
return

View 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
}

View 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
}

View 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
}

View File

@ -24,6 +24,18 @@ package model
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
type User struct {
ID int

View File

@ -60,8 +60,8 @@ type userUsecase struct {
}
type User interface {
Create(ctx context.Context, u *model.User) (*model.User, error)
Exist(ctx context.Context, u *model.User) error
Create(ctx context.Context, u *model.UserCreateDTO) (*model.User, error)
Exist(ctx context.Context, u *model.UserExistDTO) error
}
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
encrypted, err := bcrypt.GenerateFromPassword([]byte(u.Password), 12)
if err != nil {
@ -82,7 +82,12 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.User) (*model.User,
data, err := uuc.dbRepo.Transaction(
ctx,
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 {
match, _ := regexp.MatchString("SQLSTATE 23505", err.Error())
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),
)
return u, err
return created, err
},
)
if err != nil {
@ -113,7 +118,7 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.User) (*model.User,
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)
// Any query error?
if err != nil {

View File

@ -29,30 +29,42 @@ import (
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/model"
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/usecase/usecase/repomock"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/bcrypt"
)
func TestCreateUser(t *testing.T) {
t.Run("normal create", func(t *testing.T) {
ctx := context.Background()
userUsecase := NewUserUsecase(&repomock.TestUserRepository{}, &repomock.TestDBRepository{})
input := &model.User{
input := &model.UserCreateDTO{
Email: "a@b.c",
FirstName: "James",
LastName: "Bond",
Password: "verystrong",
}
want := input
want.ID = 123
want := &model.User{
ID: 123,
Email: input.Email,
FirstName: input.FirstName,
LastName: input.LastName,
// Password is hashed
Password: "verystrong",
}
got, err := userUsecase.Create(ctx, input)
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) {
ctx := context.Background()
userUsecase := NewUserUsecase(&repomock.TestUserRepository{}, &repomock.TestDBRepository{})
input := &model.User{
input := &model.UserCreateDTO{
Email: "duplicate@error.com",
FirstName: "James",
LastName: "Bond",
@ -67,22 +79,22 @@ func TestCreateUser(t *testing.T) {
func TestUserExist(t *testing.T) {
testCases := []struct {
Name string
User *model.User
User *model.UserExistDTO
ExpErr error
}{
{"user exists", &model.User{
{"user exists", &model.UserExistDTO{
Email: "a@b.c",
Password: "strongHashed",
}, nil},
{"query error", &model.User{
{"query error", &model.UserExistDTO{
Email: "query@error.com",
Password: "strongHashed",
}, repomock.UserTestDummyErr},
{"user doesn not exist", &model.User{
{"user doesn not exist", &model.UserExistDTO{
Email: "inexist@error.com",
Password: "strongHashed",
}, UserNotExist},
{"wrong password", &model.User{
{"wrong password", &model.UserExistDTO{
Email: "a@b.c",
Password: "wrongHashed",
}, UserWrongPassword},