feat: user service
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
package entitlement
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"galaxy/user/internal/domain/common"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPeriodRecordValidate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
startsAt := time.Unix(1_775_240_000, 0).UTC()
|
||||
endsAt := startsAt.Add(30 * 24 * time.Hour)
|
||||
createdAt := startsAt.Add(-time.Hour)
|
||||
closedAt := startsAt.Add(12 * time.Hour)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
record PeriodRecord
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid open record",
|
||||
record: PeriodRecord{
|
||||
RecordID: EntitlementRecordID("entitlement-123"),
|
||||
UserID: common.UserID("user-123"),
|
||||
PlanCode: PlanCodePaidMonthly,
|
||||
Source: common.Source("admin"),
|
||||
Actor: common.ActorRef{Type: common.ActorType("admin"), ID: common.ActorID("admin-1")},
|
||||
ReasonCode: common.ReasonCode("manual_grant"),
|
||||
StartsAt: startsAt,
|
||||
EndsAt: &endsAt,
|
||||
CreatedAt: createdAt,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid closed record",
|
||||
record: PeriodRecord{
|
||||
RecordID: EntitlementRecordID("entitlement-123"),
|
||||
UserID: common.UserID("user-123"),
|
||||
PlanCode: PlanCodePaidMonthly,
|
||||
Source: common.Source("admin"),
|
||||
Actor: common.ActorRef{Type: common.ActorType("admin"), ID: common.ActorID("admin-1")},
|
||||
ReasonCode: common.ReasonCode("manual_grant"),
|
||||
StartsAt: startsAt,
|
||||
EndsAt: &endsAt,
|
||||
CreatedAt: createdAt,
|
||||
ClosedAt: &closedAt,
|
||||
ClosedBy: common.ActorRef{Type: common.ActorType("admin"), ID: common.ActorID("admin-2")},
|
||||
ClosedReasonCode: common.ReasonCode("manual_revoke"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "close metadata without closed at",
|
||||
record: PeriodRecord{
|
||||
RecordID: EntitlementRecordID("entitlement-123"),
|
||||
UserID: common.UserID("user-123"),
|
||||
PlanCode: PlanCodePaidMonthly,
|
||||
Source: common.Source("admin"),
|
||||
Actor: common.ActorRef{Type: common.ActorType("admin"), ID: common.ActorID("admin-1")},
|
||||
ReasonCode: common.ReasonCode("manual_grant"),
|
||||
StartsAt: startsAt,
|
||||
CreatedAt: createdAt,
|
||||
ClosedReasonCode: common.ReasonCode("manual_revoke"),
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
err := tt.record.Validate()
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurrentSnapshotValidate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
startsAt := time.Unix(1_775_240_000, 0).UTC()
|
||||
endsAt := startsAt.Add(30 * 24 * time.Hour)
|
||||
updatedAt := startsAt.Add(2 * time.Hour)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
record CurrentSnapshot
|
||||
wantErr bool
|
||||
wantFinite bool
|
||||
}{
|
||||
{
|
||||
name: "valid finite paid snapshot",
|
||||
record: CurrentSnapshot{
|
||||
UserID: common.UserID("user-123"),
|
||||
PlanCode: PlanCodePaidMonthly,
|
||||
IsPaid: true,
|
||||
StartsAt: startsAt,
|
||||
EndsAt: &endsAt,
|
||||
Source: common.Source("admin"),
|
||||
Actor: common.ActorRef{Type: common.ActorType("admin"), ID: common.ActorID("admin-1")},
|
||||
ReasonCode: common.ReasonCode("manual_grant"),
|
||||
UpdatedAt: updatedAt,
|
||||
},
|
||||
wantFinite: true,
|
||||
},
|
||||
{
|
||||
name: "valid free snapshot",
|
||||
record: CurrentSnapshot{
|
||||
UserID: common.UserID("user-123"),
|
||||
PlanCode: PlanCodeFree,
|
||||
IsPaid: false,
|
||||
StartsAt: startsAt,
|
||||
Source: common.Source("system"),
|
||||
Actor: common.ActorRef{Type: common.ActorType("service")},
|
||||
ReasonCode: common.ReasonCode("default_free_plan"),
|
||||
UpdatedAt: updatedAt,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "paid flag mismatch",
|
||||
record: CurrentSnapshot{
|
||||
UserID: common.UserID("user-123"),
|
||||
PlanCode: PlanCodeFree,
|
||||
IsPaid: true,
|
||||
StartsAt: startsAt,
|
||||
Source: common.Source("system"),
|
||||
Actor: common.ActorRef{Type: common.ActorType("service")},
|
||||
ReasonCode: common.ReasonCode("default_free_plan"),
|
||||
UpdatedAt: updatedAt,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
err := tt.record.Validate()
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.wantFinite, tt.record.HasFiniteExpiry())
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user