ms-auth/internal/users/repository/pg_repository_test.go

245 lines
5.6 KiB
Go
Raw Normal View History

2024-10-09 17:07:38 +00:00
package repository
import (
"context"
"database/sql/driver"
2024-12-30 15:04:26 +00:00
"git.sch9.ru/new_gate/ms-auth/internal/models"
"git.sch9.ru/new_gate/ms-auth/pkg"
2024-10-09 17:07:38 +00:00
"github.com/DATA-DOG/go-sqlmock"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/require"
"testing"
"time"
)
2024-12-30 15:04:26 +00:00
func TestCaller_CreateUser(t *testing.T) {
2024-10-09 17:07:38 +00:00
t.Parallel()
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
require.NoError(t, err)
defer db.Close()
sqlxDB := sqlx.NewDb(db, "sqlmock")
defer sqlxDB.Close()
2024-12-30 15:04:26 +00:00
userRepo := NewUserRepository(sqlxDB)
2024-10-09 17:07:38 +00:00
t.Run("valid user creation", func(t *testing.T) {
rows := sqlmock.NewRows([]string{"id"}).AddRow(1)
2024-12-30 15:04:26 +00:00
username := "testuser"
password := "testpassword"
role := models.RoleAdmin
mock.ExpectQuery(sqlxDB.Rebind(createUser)).WithArgs(
username,
AnyString{},
role,
).WillReturnRows(rows)
_, err = userRepo.C().CreateUser(context.Background(), username, password, role)
require.NoError(t, err)
})
t.Run("invalid user creation (invalid username)", func(t *testing.T) {
username := "test"
password := "testpassword"
role := models.RoleAdmin
_, err = userRepo.C().CreateUser(context.Background(), username, password, role)
require.ErrorIs(t, err, pkg.ErrBadInput)
})
t.Run("invalid user creation (invalid password)", func(t *testing.T) {
username := "testuser"
password := "test"
role := models.RoleAdmin
_, err = userRepo.C().CreateUser(context.Background(), username, password, role)
require.ErrorIs(t, err, pkg.ErrBadInput)
})
t.Run("invalid user creation (invalid role)", func(t *testing.T) {
username := "testuser"
password := "testpassword"
_, err = userRepo.C().CreateUser(context.Background(), username, password, 123)
require.ErrorIs(t, err, pkg.ErrBadInput)
})
}
func TestCaller_ReadUserByUsername(t *testing.T) {
t.Parallel()
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
require.NoError(t, err)
defer db.Close()
sqlxDB := sqlx.NewDb(db, "sqlmock")
defer sqlxDB.Close()
userRepo := NewUserRepository(sqlxDB)
t.Run("valid user read", func(t *testing.T) {
2024-10-09 17:07:38 +00:00
user := &models.User{
2024-12-30 15:04:26 +00:00
Id: 1,
Username: "testuser",
HashedPassword: "hashedtestpassword",
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Role: models.RoleAdmin,
2024-10-09 17:07:38 +00:00
}
2024-12-30 15:04:26 +00:00
rows := sqlmock.NewRows(
[]string{
"id",
"username",
"hashed_pwd",
"created_at",
"modified_at",
"role",
}).AddRow(
user.Id,
2024-10-09 17:07:38 +00:00
user.Username,
2024-12-30 15:04:26 +00:00
user.HashedPassword,
user.CreatedAt,
user.ModifiedAt,
2024-10-09 17:07:38 +00:00
user.Role,
2024-12-30 15:04:26 +00:00
)
mock.ExpectQuery(sqlxDB.Rebind(readUserByUsername)).WithArgs(
user.Username,
2024-10-09 17:07:38 +00:00
).WillReturnRows(rows)
2024-12-30 15:04:26 +00:00
readUser, err := userRepo.C().ReadUserByUsername(context.Background(), user.Username)
2024-10-09 17:07:38 +00:00
require.NoError(t, err)
2024-12-30 15:04:26 +00:00
require.Equal(t, user, readUser)
2024-10-09 17:07:38 +00:00
})
}
2024-10-13 17:45:39 +00:00
func TestUsersRepository_ReadUserById(t *testing.T) {
t.Parallel()
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
require.NoError(t, err)
defer db.Close()
sqlxDB := sqlx.NewDb(db, "sqlmock")
defer sqlxDB.Close()
2024-12-30 15:04:26 +00:00
userRepo := NewUserRepository(sqlxDB)
2024-10-13 17:45:39 +00:00
t.Run("valid user read", func(t *testing.T) {
user := &models.User{
2024-12-30 15:04:26 +00:00
Id: 1,
Username: "testuser",
HashedPassword: "hashedtestpassword",
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Role: models.RoleAdmin,
2024-10-13 17:45:39 +00:00
}
rows := sqlmock.NewRows(
[]string{
"id",
"username",
"hashed_pwd",
"created_at",
2024-12-30 15:04:26 +00:00
"modified_at",
2024-10-13 17:45:39 +00:00
"role",
}).AddRow(
2024-12-30 15:04:26 +00:00
user.Id,
user.Username,
user.HashedPassword,
user.CreatedAt,
user.ModifiedAt,
user.Role,
2024-10-13 17:45:39 +00:00
)
mock.ExpectQuery(sqlxDB.Rebind(readUserById)).WithArgs(
2024-12-30 15:04:26 +00:00
user.Id,
2024-10-13 17:45:39 +00:00
).WillReturnRows(rows)
2024-12-30 15:04:26 +00:00
readUser, err := userRepo.C().ReadUserById(context.Background(), user.Id)
2024-10-13 17:45:39 +00:00
require.NoError(t, err)
require.Equal(t, user, readUser)
})
}
2024-10-09 17:07:38 +00:00
func TestUsersRepository_UpdateUser(t *testing.T) {
t.Parallel()
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
require.NoError(t, err)
defer db.Close()
sqlxDB := sqlx.NewDb(db, "sqlmock")
defer sqlxDB.Close()
2024-12-30 15:04:26 +00:00
userRepo := NewUserRepository(sqlxDB)
2024-10-09 17:07:38 +00:00
t.Run("valid user update", func(t *testing.T) {
user := &models.User{
2024-12-30 15:04:26 +00:00
Id: 1,
Username: "testuser",
HashedPassword: "hashedtestpassword",
CreatedAt: time.Now(),
ModifiedAt: time.Now(),
Role: models.RoleAdmin,
2024-10-09 17:07:38 +00:00
}
2024-12-30 15:04:26 +00:00
mock.ExpectExec(sqlxDB.Rebind(updateUser)).WithArgs(
user.Username,
user.Role,
user.Id,
).WillReturnResult(sqlmock.NewResult(1, 1))
2024-10-09 17:07:38 +00:00
2024-12-30 15:04:26 +00:00
err := userRepo.C().UpdateUser(context.Background(), user.Id, AsStringP(user.Username), AsRoleP(user.Role))
2024-10-09 17:07:38 +00:00
require.NoError(t, err)
})
}
2024-12-30 15:04:26 +00:00
func TestCaller_DeleteUser(t *testing.T) {
2024-10-09 17:07:38 +00:00
t.Parallel()
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
require.NoError(t, err)
defer db.Close()
sqlxDB := sqlx.NewDb(db, "sqlmock")
defer sqlxDB.Close()
2024-12-30 15:04:26 +00:00
userRepo := NewUserRepository(sqlxDB)
2024-10-09 17:07:38 +00:00
2024-12-30 15:04:26 +00:00
t.Run("valid user delete", func(t *testing.T) {
mock.ExpectExec(sqlxDB.Rebind(deleteUser)).WithArgs(
1,
).WillReturnResult(sqlmock.NewResult(1, 1))
2024-10-09 17:07:38 +00:00
2024-12-30 15:04:26 +00:00
err := userRepo.C().DeleteUser(context.Background(), 1)
2024-10-09 17:07:38 +00:00
require.NoError(t, err)
})
}
func AsRoleP(r models.Role) *models.Role {
return &r
}
type AnyTime struct{}
// Match satisfies sqlmock.Argument interface
func (a AnyTime) Match(v driver.Value) bool {
_, ok := v.(time.Time)
return ok
}
type AnyString struct{}
func (a AnyString) Match(v driver.Value) bool {
_, ok := v.(string)
return ok
}
2024-12-30 15:04:26 +00:00
func AsStringP(str string) *string {
return &str
}