101 lines
3.0 KiB
Go
101 lines
3.0 KiB
Go
// 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 usecase
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/model"
|
|
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/usecase/repo"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCreateUser(t *testing.T) {
|
|
t.Run("normal create", func(t *testing.T) {
|
|
ctx := context.Background()
|
|
userUsecase := NewUserUsecase(&repo.TestUserRepository{}, &repo.TestDBRepository{})
|
|
input := &model.User{
|
|
Email: "a@b.c",
|
|
FirstName: "James",
|
|
LastName: "Bond",
|
|
Password: "verystrong",
|
|
}
|
|
want := input
|
|
want.ID = 123
|
|
|
|
got, err := userUsecase.Create(ctx, input)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, want, got)
|
|
})
|
|
|
|
t.Run("duplicate create", func(t *testing.T) {
|
|
ctx := context.Background()
|
|
userUsecase := NewUserUsecase(&repo.TestUserRepository{}, &repo.TestDBRepository{})
|
|
input := &model.User{
|
|
Email: "duplicate@error.com",
|
|
FirstName: "James",
|
|
LastName: "Bond",
|
|
Password: "verystrong",
|
|
}
|
|
|
|
_, err := userUsecase.Create(ctx, input)
|
|
assert.EqualError(t, err, UserExisted.Error())
|
|
})
|
|
}
|
|
|
|
func TestUserExist(t *testing.T) {
|
|
testCases := []struct {
|
|
Name string
|
|
User *model.User
|
|
ExpErr error
|
|
ExpRes bool
|
|
}{
|
|
{"user exists", &model.User{
|
|
Email: "a@b.c",
|
|
Password: "strongHashed",
|
|
}, nil, true},
|
|
{"query error", &model.User{
|
|
Email: "query@error.com",
|
|
Password: "strongHashed",
|
|
}, repo.UserTestDummyErr, false},
|
|
{"user doesn not exist", &model.User{
|
|
Email: "inexist@error.com",
|
|
Password: "strongHashed",
|
|
}, UserNotExist, false},
|
|
{"wrong password", &model.User{
|
|
Email: "a@b.c",
|
|
Password: "wrongHashed",
|
|
}, UserWrongPassword, false},
|
|
}
|
|
|
|
for _, tst := range testCases {
|
|
ctx := context.Background()
|
|
userUsecase := NewUserUsecase(&repo.TestUserRepository{}, &repo.TestDBRepository{})
|
|
|
|
got, err := userUsecase.Exist(ctx, tst.User)
|
|
assert.ErrorIs(t, err, tst.ExpErr)
|
|
assert.Equal(t, tst.ExpRes, got)
|
|
}
|
|
}
|