admin_service_rpm_status_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //go:build unit
  2. package service
  3. import (
  4. "context"
  5. "testing"
  6. "github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
  7. "github.com/stretchr/testify/require"
  8. )
  9. type rpmStatusUserRepoStub struct {
  10. UserRepository
  11. user *User
  12. }
  13. func (s *rpmStatusUserRepoStub) GetByID(_ context.Context, _ int64) (*User, error) {
  14. return s.user, nil
  15. }
  16. type rpmStatusAPIKeyRepoStub struct {
  17. APIKeyRepository
  18. keys []APIKey
  19. }
  20. func (s *rpmStatusAPIKeyRepoStub) ListByUserID(_ context.Context, _ int64, _ pagination.PaginationParams, _ APIKeyListFilters) ([]APIKey, *pagination.PaginationResult, error) {
  21. return s.keys, &pagination.PaginationResult{Total: int64(len(s.keys))}, nil
  22. }
  23. type rpmStatusGroupRepoStub struct {
  24. GroupRepository
  25. groups map[int64]*Group
  26. }
  27. func (s *rpmStatusGroupRepoStub) GetByIDLite(_ context.Context, id int64) (*Group, error) {
  28. return s.groups[id], nil
  29. }
  30. type rpmStatusRateRepoStub struct {
  31. UserGroupRateRepository
  32. overrides map[int64]*int
  33. }
  34. func (s *rpmStatusRateRepoStub) GetRPMOverrideByUserAndGroup(_ context.Context, _, groupID int64) (*int, error) {
  35. return s.overrides[groupID], nil
  36. }
  37. type rpmStatusCacheStub struct {
  38. UserRPMCache
  39. userUsed int
  40. groupUsed map[int64]int
  41. }
  42. func (s *rpmStatusCacheStub) IncrementUserGroupRPM(context.Context, int64, int64) (int, error) {
  43. return 0, nil
  44. }
  45. func (s *rpmStatusCacheStub) IncrementUserRPM(context.Context, int64) (int, error) {
  46. return 0, nil
  47. }
  48. func (s *rpmStatusCacheStub) GetUserGroupRPM(_ context.Context, _, groupID int64) (int, error) {
  49. return s.groupUsed[groupID], nil
  50. }
  51. func (s *rpmStatusCacheStub) GetUserRPM(context.Context, int64) (int, error) {
  52. return s.userUsed, nil
  53. }
  54. func TestAdminService_GetUserRPMStatus_AggregatesUserAndGroupLimits(t *testing.T) {
  55. groupOneID := int64(1)
  56. groupTwoID := int64(2)
  57. override := 7
  58. svc := &adminServiceImpl{
  59. userRepo: &rpmStatusUserRepoStub{user: &User{
  60. ID: 42,
  61. RPMLimit: 20,
  62. }},
  63. apiKeyRepo: &rpmStatusAPIKeyRepoStub{keys: []APIKey{
  64. {ID: 100, UserID: 42, GroupID: &groupTwoID},
  65. {ID: 101, UserID: 42, GroupID: &groupOneID},
  66. {ID: 102, UserID: 42, GroupID: &groupTwoID},
  67. {ID: 103, UserID: 42},
  68. }},
  69. groupRepo: &rpmStatusGroupRepoStub{groups: map[int64]*Group{
  70. groupOneID: {ID: groupOneID, Name: "group-one", RPMLimit: 10},
  71. groupTwoID: {ID: groupTwoID, Name: "group-two", RPMLimit: 60},
  72. }},
  73. userGroupRateRepo: &rpmStatusRateRepoStub{overrides: map[int64]*int{
  74. groupTwoID: &override,
  75. }},
  76. userRPMCache: &rpmStatusCacheStub{
  77. userUsed: 5,
  78. groupUsed: map[int64]int{
  79. groupOneID: 3,
  80. groupTwoID: 4,
  81. },
  82. },
  83. }
  84. status, err := svc.GetUserRPMStatus(context.Background(), 42)
  85. require.NoError(t, err)
  86. require.Equal(t, &UserRPMStatus{
  87. UserRPMUsed: 5,
  88. UserRPMLimit: 20,
  89. PerGroup: []UserGroupRPMStatus{
  90. {GroupID: groupOneID, GroupName: "group-one", Used: 3, Limit: 10, Source: "group"},
  91. {GroupID: groupTwoID, GroupName: "group-two", Used: 4, Limit: 7, Source: "override"},
  92. },
  93. }, status)
  94. }