@@ -32,28 +32,44 @@ func (s *userRepoStubForGroupUpdate) AddGroupToAllowedGroups(_ context.Context,
3232 return s .addGroupErr
3333}
3434
35- func (s * userRepoStubForGroupUpdate ) Create (context.Context , * User ) error { panic ("unexpected" ) }
36- func (s * userRepoStubForGroupUpdate ) GetByID (context.Context , int64 ) (* User , error ) { panic ("unexpected" ) }
37- func (s * userRepoStubForGroupUpdate ) GetByEmail (context.Context , string ) (* User , error ) { panic ("unexpected" ) }
38- func (s * userRepoStubForGroupUpdate ) GetFirstAdmin (context.Context ) (* User , error ) { panic ("unexpected" ) }
39- func (s * userRepoStubForGroupUpdate ) Update (context.Context , * User ) error { panic ("unexpected" ) }
40- func (s * userRepoStubForGroupUpdate ) Delete (context.Context , int64 ) error { panic ("unexpected" ) }
35+ func (s * userRepoStubForGroupUpdate ) Create (context.Context , * User ) error { panic ("unexpected" ) }
36+ func (s * userRepoStubForGroupUpdate ) GetByID (context.Context , int64 ) (* User , error ) {
37+ panic ("unexpected" )
38+ }
39+ func (s * userRepoStubForGroupUpdate ) GetByEmail (context.Context , string ) (* User , error ) {
40+ panic ("unexpected" )
41+ }
42+ func (s * userRepoStubForGroupUpdate ) GetFirstAdmin (context.Context ) (* User , error ) {
43+ panic ("unexpected" )
44+ }
45+ func (s * userRepoStubForGroupUpdate ) Update (context.Context , * User ) error { panic ("unexpected" ) }
46+ func (s * userRepoStubForGroupUpdate ) Delete (context.Context , int64 ) error { panic ("unexpected" ) }
4147func (s * userRepoStubForGroupUpdate ) List (context.Context , pagination.PaginationParams ) ([]User , * pagination.PaginationResult , error ) {
4248 panic ("unexpected" )
4349}
4450func (s * userRepoStubForGroupUpdate ) ListWithFilters (context.Context , pagination.PaginationParams , UserListFilters ) ([]User , * pagination.PaginationResult , error ) {
4551 panic ("unexpected" )
4652}
47- func (s * userRepoStubForGroupUpdate ) UpdateBalance (context.Context , int64 , float64 ) error { panic ("unexpected" ) }
48- func (s * userRepoStubForGroupUpdate ) DeductBalance (context.Context , int64 , float64 ) error { panic ("unexpected" ) }
49- func (s * userRepoStubForGroupUpdate ) UpdateConcurrency (context.Context , int64 , int ) error { panic ("unexpected" ) }
50- func (s * userRepoStubForGroupUpdate ) ExistsByEmail (context.Context , string ) (bool , error ) { panic ("unexpected" ) }
53+ func (s * userRepoStubForGroupUpdate ) UpdateBalance (context.Context , int64 , float64 ) error {
54+ panic ("unexpected" )
55+ }
56+ func (s * userRepoStubForGroupUpdate ) DeductBalance (context.Context , int64 , float64 ) error {
57+ panic ("unexpected" )
58+ }
59+ func (s * userRepoStubForGroupUpdate ) UpdateConcurrency (context.Context , int64 , int ) error {
60+ panic ("unexpected" )
61+ }
62+ func (s * userRepoStubForGroupUpdate ) ExistsByEmail (context.Context , string ) (bool , error ) {
63+ panic ("unexpected" )
64+ }
5165func (s * userRepoStubForGroupUpdate ) RemoveGroupFromAllowedGroups (context.Context , int64 ) (int64 , error ) {
5266 panic ("unexpected" )
5367}
54- func (s * userRepoStubForGroupUpdate ) UpdateTotpSecret (context.Context , int64 , * string ) error { panic ("unexpected" ) }
55- func (s * userRepoStubForGroupUpdate ) EnableTotp (context.Context , int64 ) error { panic ("unexpected" ) }
56- func (s * userRepoStubForGroupUpdate ) DisableTotp (context.Context , int64 ) error { panic ("unexpected" ) }
68+ func (s * userRepoStubForGroupUpdate ) UpdateTotpSecret (context.Context , int64 , * string ) error {
69+ panic ("unexpected" )
70+ }
71+ func (s * userRepoStubForGroupUpdate ) EnableTotp (context.Context , int64 ) error { panic ("unexpected" ) }
72+ func (s * userRepoStubForGroupUpdate ) DisableTotp (context.Context , int64 ) error { panic ("unexpected" ) }
5773
5874// apiKeyRepoStubForGroupUpdate implements APIKeyRepository for AdminUpdateAPIKeyGroupID tests.
5975type apiKeyRepoStubForGroupUpdate struct {
@@ -194,6 +210,29 @@ func (s *groupRepoStubForGroupUpdate) UpdateSortOrders(context.Context, []GroupS
194210 panic ("unexpected" )
195211}
196212
213+ type userSubRepoStubForGroupUpdate struct {
214+ userSubRepoNoop
215+ getActiveSub * UserSubscription
216+ getActiveErr error
217+ called bool
218+ calledUserID int64
219+ calledGroupID int64
220+ }
221+
222+ func (s * userSubRepoStubForGroupUpdate ) GetActiveByUserIDAndGroupID (_ context.Context , userID , groupID int64 ) (* UserSubscription , error ) {
223+ s .called = true
224+ s .calledUserID = userID
225+ s .calledGroupID = groupID
226+ if s .getActiveErr != nil {
227+ return nil , s .getActiveErr
228+ }
229+ if s .getActiveSub == nil {
230+ return nil , ErrSubscriptionNotFound
231+ }
232+ clone := * s .getActiveSub
233+ return & clone , nil
234+ }
235+
197236// ---------------------------------------------------------------------------
198237// Tests
199238// ---------------------------------------------------------------------------
@@ -386,14 +425,49 @@ func TestAdminService_AdminUpdateAPIKeyGroupID_NonExclusiveGroup_NoAllowedGroupU
386425func TestAdminService_AdminUpdateAPIKeyGroupID_SubscriptionGroup_Blocked (t * testing.T ) {
387426 existing := & APIKey {ID : 1 , UserID : 42 , Key : "sk-test" , GroupID : nil }
388427 apiKeyRepo := & apiKeyRepoStubForGroupUpdate {key : existing }
389- groupRepo := & groupRepoStubForGroupUpdate {group : & Group {ID : 10 , Name : "Sub" , Status : StatusActive , IsExclusive : true , SubscriptionType : SubscriptionTypeSubscription }}
428+ groupRepo := & groupRepoStubForGroupUpdate {group : & Group {ID : 10 , Name : "Sub" , Status : StatusActive , IsExclusive : false , SubscriptionType : SubscriptionTypeSubscription }}
429+ userRepo := & userRepoStubForGroupUpdate {}
430+ userSubRepo := & userSubRepoStubForGroupUpdate {getActiveErr : ErrSubscriptionNotFound }
431+ svc := & adminServiceImpl {apiKeyRepo : apiKeyRepo , groupRepo : groupRepo , userRepo : userRepo , userSubRepo : userSubRepo }
432+
433+ // 无有效订阅时应拒绝绑定
434+ _ , err := svc .AdminUpdateAPIKeyGroupID (context .Background (), 1 , int64Ptr (10 ))
435+ require .Error (t , err )
436+ require .Equal (t , "SUBSCRIPTION_REQUIRED" , infraerrors .Reason (err ))
437+ require .True (t , userSubRepo .called )
438+ require .Equal (t , int64 (42 ), userSubRepo .calledUserID )
439+ require .Equal (t , int64 (10 ), userSubRepo .calledGroupID )
440+ require .False (t , userRepo .addGroupCalled )
441+ }
442+
443+ func TestAdminService_AdminUpdateAPIKeyGroupID_SubscriptionGroup_RequiresRepo (t * testing.T ) {
444+ existing := & APIKey {ID : 1 , UserID : 42 , Key : "sk-test" , GroupID : nil }
445+ apiKeyRepo := & apiKeyRepoStubForGroupUpdate {key : existing }
446+ groupRepo := & groupRepoStubForGroupUpdate {group : & Group {ID : 10 , Name : "Sub" , Status : StatusActive , IsExclusive : false , SubscriptionType : SubscriptionTypeSubscription }}
390447 userRepo := & userRepoStubForGroupUpdate {}
391448 svc := & adminServiceImpl {apiKeyRepo : apiKeyRepo , groupRepo : groupRepo , userRepo : userRepo }
392449
393- // 订阅类型分组应被阻止绑定
394450 _ , err := svc .AdminUpdateAPIKeyGroupID (context .Background (), 1 , int64Ptr (10 ))
395451 require .Error (t , err )
396- require .Equal (t , "SUBSCRIPTION_GROUP_NOT_ALLOWED" , infraerrors .Reason (err ))
452+ require .Equal (t , "SUBSCRIPTION_REPOSITORY_UNAVAILABLE" , infraerrors .Reason (err ))
453+ require .False (t , userRepo .addGroupCalled )
454+ }
455+
456+ func TestAdminService_AdminUpdateAPIKeyGroupID_SubscriptionGroup_AllowsActiveSubscription (t * testing.T ) {
457+ existing := & APIKey {ID : 1 , UserID : 42 , Key : "sk-test" , GroupID : nil }
458+ apiKeyRepo := & apiKeyRepoStubForGroupUpdate {key : existing }
459+ groupRepo := & groupRepoStubForGroupUpdate {group : & Group {ID : 10 , Name : "Sub" , Status : StatusActive , IsExclusive : true , SubscriptionType : SubscriptionTypeSubscription }}
460+ userRepo := & userRepoStubForGroupUpdate {}
461+ userSubRepo := & userSubRepoStubForGroupUpdate {
462+ getActiveSub : & UserSubscription {ID : 99 , UserID : 42 , GroupID : 10 },
463+ }
464+ svc := & adminServiceImpl {apiKeyRepo : apiKeyRepo , groupRepo : groupRepo , userRepo : userRepo , userSubRepo : userSubRepo }
465+
466+ got , err := svc .AdminUpdateAPIKeyGroupID (context .Background (), 1 , int64Ptr (10 ))
467+ require .NoError (t , err )
468+ require .True (t , userSubRepo .called )
469+ require .NotNil (t , got .APIKey .GroupID )
470+ require .Equal (t , int64 (10 ), * got .APIKey .GroupID )
397471 require .False (t , userRepo .addGroupCalled )
398472}
399473
0 commit comments