Loan orders, paid-off letters, and repayments now include user, branch, type, and document names alongside foreign-key IDs. Co-authored-by: Cursor <cursoragent@cursor.com>
134 lines
3.2 KiB
Go
134 lines
3.2 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"online-back/internal/config"
|
|
"online-back/internal/identity/user"
|
|
"online-back/pkg/client/postgresql"
|
|
"online-back/pkg/logging"
|
|
)
|
|
|
|
func testRepo(t *testing.T) (user.Repository, func()) {
|
|
t.Helper()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
|
|
t.Cleanup(cancel)
|
|
|
|
pool, err := postgresql.NewClient(ctx, 2, config.StorageConfig{
|
|
Host: "localhost",
|
|
Port: "5424",
|
|
Database: "bank_db",
|
|
Username: "bank_admin",
|
|
Password: "bank@#23#@_password",
|
|
PgPoolMaxConn: 4,
|
|
})
|
|
if err != nil {
|
|
t.Skipf("postgres unavailable: %v", err)
|
|
}
|
|
|
|
return NewRepository(pool, logging.GetLogger()), func() { pool.Close() }
|
|
}
|
|
|
|
func assignedRole(t *testing.T, ctx context.Context, repo user.Repository, userID int64) (id int64, code string) {
|
|
t.Helper()
|
|
poolRepo, ok := repo.(*repository)
|
|
if !ok {
|
|
t.Fatal("unexpected repository type")
|
|
}
|
|
err := poolRepo.client.QueryRow(ctx, `
|
|
SELECT r.id, r.code
|
|
FROM role_user ru
|
|
JOIN roles r ON r.id = ru.role_id
|
|
WHERE ru.user_id = $1
|
|
ORDER BY ru.id
|
|
LIMIT 1`, userID).Scan(&id, &code)
|
|
if err != nil {
|
|
t.Fatalf("lookup role_user for user %d: %v", userID, err)
|
|
}
|
|
return id, code
|
|
}
|
|
|
|
func assertJoinedRole(t *testing.T, ctx context.Context, repo user.Repository, item user.UserDTO) {
|
|
t.Helper()
|
|
wantID, wantCode := assignedRole(t, ctx, repo, item.Id)
|
|
if item.Role != wantCode {
|
|
t.Errorf("user %d role=%q, want roles.code %q", item.Id, item.Role, wantCode)
|
|
}
|
|
if item.RoleId == nil {
|
|
t.Fatalf("user %d missing roleId", item.Id)
|
|
}
|
|
if *item.RoleId != wantID {
|
|
t.Errorf("user %d roleId=%d, want roles.id %d", item.Id, *item.RoleId, wantID)
|
|
}
|
|
}
|
|
|
|
func TestListUsersRoleComesFromJoin(t *testing.T) {
|
|
repo, closeRepo := testRepo(t)
|
|
defer closeRepo()
|
|
ctx := context.Background()
|
|
filter := user.ListFilter{Page: 1, Limit: 100}
|
|
|
|
list, err := repo.List(ctx, filter)
|
|
if err != nil {
|
|
t.Fatalf("List: %v", err)
|
|
}
|
|
if list.Count == 0 || len(list.Users) == 0 {
|
|
t.Fatal("List returned no users")
|
|
}
|
|
for _, item := range list.Users {
|
|
if item.Role != "user" {
|
|
t.Errorf("List user %d role=%q, want user", item.Id, item.Role)
|
|
}
|
|
assertJoinedRole(t, ctx, repo, item)
|
|
}
|
|
|
|
clients, err := repo.GetByRole(ctx, "user", filter)
|
|
if err != nil {
|
|
t.Fatalf("GetByRole: %v", err)
|
|
}
|
|
if clients.Count == 0 {
|
|
t.Fatal("clients list empty")
|
|
}
|
|
for _, item := range clients.Users {
|
|
assertJoinedRole(t, ctx, repo, item)
|
|
}
|
|
|
|
staff, err := repo.ListStaff(ctx, filter)
|
|
if err != nil {
|
|
t.Fatalf("ListStaff: %v", err)
|
|
}
|
|
if staff.Count == 0 {
|
|
t.Fatal("staff list empty")
|
|
}
|
|
for _, item := range staff.Users {
|
|
assertJoinedRole(t, ctx, repo, item)
|
|
if item.Branches == nil {
|
|
t.Errorf("staff user %d missing branches", item.Id)
|
|
}
|
|
}
|
|
|
|
got, err := repo.GetByID(ctx, staff.Users[0].Id)
|
|
if err != nil {
|
|
t.Fatalf("GetByID: %v", err)
|
|
}
|
|
assertJoinedRole(t, ctx, repo, got)
|
|
|
|
raw, err := json.Marshal(list.Users[0])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var payload map[string]any
|
|
if err := json.Unmarshal(raw, &payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := payload["role"]; !ok {
|
|
t.Errorf("json missing role: %s", raw)
|
|
}
|
|
if _, ok := payload["roleId"]; !ok {
|
|
t.Errorf("json missing roleId: %s", raw)
|
|
}
|
|
}
|