Compare commits
No commits in common. "3b18a15494f2f0c669ee4704a29573a474921133" and "322b441c70a1f3860f2e61deb2cd4eb416985660" have entirely different histories.
3b18a15494
...
322b441c70
@ -64,14 +64,19 @@ 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 user model.UserExistDTO
|
||||
var params createParams
|
||||
|
||||
if err := ctx.Bind(&user); err != nil {
|
||||
if err := ctx.Bind(¶ms); 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)
|
||||
|
@ -24,9 +24,7 @@ package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
@ -34,43 +32,12 @@ 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
|
||||
@ -126,57 +93,3 @@ 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{}
|
||||
}
|
||||
|
||||
func (*testUserUsecase) Create(ctx context.Context, u *model.UserCreateDTO) (*model.User, error) {
|
||||
func (*testUserUsecase) Create(ctx context.Context, u *model.User) (*model.User, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (*testUserUsecase) Exist(ctx context.Context, u *model.UserExistDTO) error {
|
||||
func (*testUserUsecase) Exist(ctx context.Context, u *model.User) error {
|
||||
switch u.Email {
|
||||
case "a@b.c":
|
||||
if u.Password == "strong password" {
|
||||
|
@ -57,14 +57,26 @@ func NewUserController(us usecase.User) User {
|
||||
}
|
||||
|
||||
func (uc *UserController) Create(ctx core.Context) {
|
||||
var userDTO model.UserCreateDTO
|
||||
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"`
|
||||
}
|
||||
|
||||
if err := ctx.Bind(&userDTO); err != nil {
|
||||
if err := ctx.Bind(¶ms); err != nil {
|
||||
core.WriteResponse(ctx, UserParamsErr, nil)
|
||||
return
|
||||
}
|
||||
|
||||
_, err := uc.userUsecase.Create(ctx, &userDTO)
|
||||
user := model.User{
|
||||
Email: params.Email,
|
||||
FirstName: params.FirstName,
|
||||
LastName: params.LastName,
|
||||
Password: params.Password,
|
||||
}
|
||||
|
||||
_, err := uc.userUsecase.Create(ctx, &user)
|
||||
if err != nil {
|
||||
core.WriteResponse(ctx, err, nil)
|
||||
return
|
||||
|
@ -1,38 +0,0 @@
|
||||
// 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
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
// 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
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
// 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,18 +24,6 @@ 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
|
||||
|
@ -60,8 +60,8 @@ type userUsecase struct {
|
||||
}
|
||||
|
||||
type User interface {
|
||||
Create(ctx context.Context, u *model.UserCreateDTO) (*model.User, error)
|
||||
Exist(ctx context.Context, u *model.UserExistDTO) error
|
||||
Create(ctx context.Context, u *model.User) (*model.User, error)
|
||||
Exist(ctx context.Context, u *model.User) 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.UserCreateDTO) (*model.User, error) {
|
||||
func (uuc *userUsecase) Create(ctx context.Context, u *model.User) (*model.User, error) {
|
||||
// Hash the password
|
||||
encrypted, err := bcrypt.GenerateFromPassword([]byte(u.Password), 12)
|
||||
if err != nil {
|
||||
@ -82,12 +82,7 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.UserCreateDTO) (*mo
|
||||
data, err := uuc.dbRepo.Transaction(
|
||||
ctx,
|
||||
func(txCtx context.Context, tx interface{}) (interface{}, error) {
|
||||
created, err := uuc.userRepo.Create(txCtx, tx, &model.User{
|
||||
Email: u.Email,
|
||||
Password: u.Password,
|
||||
FirstName: u.FirstName,
|
||||
LastName: u.LastName,
|
||||
})
|
||||
u, err := uuc.userRepo.Create(txCtx, tx, u)
|
||||
if err != nil {
|
||||
match, _ := regexp.MatchString("SQLSTATE 23505", err.Error())
|
||||
if match {
|
||||
@ -105,7 +100,7 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.UserCreateDTO) (*mo
|
||||
fmt.Sprintf("%s %s", u.FirstName, u.LastName),
|
||||
)
|
||||
|
||||
return created, err
|
||||
return u, err
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
@ -118,7 +113,7 @@ func (uuc *userUsecase) Create(ctx context.Context, u *model.UserCreateDTO) (*mo
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (uuc *userUsecase) Exist(ctx context.Context, u *model.UserExistDTO) error {
|
||||
func (uuc *userUsecase) Exist(ctx context.Context, u *model.User) error {
|
||||
got, err := uuc.userRepo.GetByEmail(ctx, u.Email)
|
||||
// Any query error?
|
||||
if err != nil {
|
||||
|
@ -29,42 +29,30 @@ 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.UserCreateDTO{
|
||||
input := &model.User{
|
||||
Email: "a@b.c",
|
||||
FirstName: "James",
|
||||
LastName: "Bond",
|
||||
Password: "verystrong",
|
||||
}
|
||||
want := &model.User{
|
||||
ID: 123,
|
||||
Email: input.Email,
|
||||
FirstName: input.FirstName,
|
||||
LastName: input.LastName,
|
||||
// Password is hashed
|
||||
Password: "verystrong",
|
||||
}
|
||||
want := input
|
||||
want.ID = 123
|
||||
|
||||
got, err := userUsecase.Create(ctx, input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, want.ID, got.ID)
|
||||
|
||||
assert.NoError(
|
||||
t,
|
||||
bcrypt.CompareHashAndPassword([]byte(got.Password), []byte(want.Password)),
|
||||
)
|
||||
assert.Equal(t, want, got)
|
||||
})
|
||||
|
||||
t.Run("duplicate create", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
userUsecase := NewUserUsecase(&repomock.TestUserRepository{}, &repomock.TestDBRepository{})
|
||||
input := &model.UserCreateDTO{
|
||||
input := &model.User{
|
||||
Email: "duplicate@error.com",
|
||||
FirstName: "James",
|
||||
LastName: "Bond",
|
||||
@ -79,22 +67,22 @@ func TestCreateUser(t *testing.T) {
|
||||
func TestUserExist(t *testing.T) {
|
||||
testCases := []struct {
|
||||
Name string
|
||||
User *model.UserExistDTO
|
||||
User *model.User
|
||||
ExpErr error
|
||||
}{
|
||||
{"user exists", &model.UserExistDTO{
|
||||
{"user exists", &model.User{
|
||||
Email: "a@b.c",
|
||||
Password: "strongHashed",
|
||||
}, nil},
|
||||
{"query error", &model.UserExistDTO{
|
||||
{"query error", &model.User{
|
||||
Email: "query@error.com",
|
||||
Password: "strongHashed",
|
||||
}, repomock.UserTestDummyErr},
|
||||
{"user doesn not exist", &model.UserExistDTO{
|
||||
{"user doesn not exist", &model.User{
|
||||
Email: "inexist@error.com",
|
||||
Password: "strongHashed",
|
||||
}, UserNotExist},
|
||||
{"wrong password", &model.UserExistDTO{
|
||||
{"wrong password", &model.User{
|
||||
Email: "a@b.c",
|
||||
Password: "wrongHashed",
|
||||
}, UserWrongPassword},
|
||||
|
Loading…
x
Reference in New Issue
Block a user