user_can_bind_group_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package service
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestUserCanBindGroup(t *testing.T) {
  7. const (
  8. publicGroupA int64 = 10
  9. publicGroupB int64 = 11
  10. exclusiveGroupA int64 = 20
  11. )
  12. for _, tc := range []struct {
  13. name string
  14. user User
  15. groupID int64
  16. isExclusive bool
  17. want bool
  18. }{
  19. {
  20. name: "public group is bindable by default",
  21. user: User{},
  22. groupID: publicGroupA,
  23. want: true,
  24. },
  25. {
  26. name: "exclusive group needs an explicit grant",
  27. user: User{},
  28. groupID: exclusiveGroupA,
  29. isExclusive: true,
  30. want: false,
  31. },
  32. {
  33. name: "granted exclusive group is bindable",
  34. user: User{AllowedGroups: []int64{exclusiveGroupA}},
  35. groupID: exclusiveGroupA,
  36. isExclusive: true,
  37. want: true,
  38. },
  39. {
  40. name: "an unrestricted user keeps every public group even with a grant list",
  41. user: User{AllowedGroups: []int64{exclusiveGroupA}},
  42. groupID: publicGroupA,
  43. want: true,
  44. },
  45. {
  46. name: "a restricted user keeps the public groups listed for them",
  47. user: User{RestrictPublicGroups: true, AllowedGroups: []int64{publicGroupA}},
  48. groupID: publicGroupA,
  49. want: true,
  50. },
  51. {
  52. name: "a restricted user loses the public groups not listed for them",
  53. user: User{RestrictPublicGroups: true, AllowedGroups: []int64{publicGroupA}},
  54. groupID: publicGroupB,
  55. want: false,
  56. },
  57. {
  58. name: "a restricted user with no list keeps no public group",
  59. user: User{RestrictPublicGroups: true},
  60. groupID: publicGroupA,
  61. want: false,
  62. },
  63. {
  64. name: "restricting public groups does not widen exclusive access",
  65. user: User{RestrictPublicGroups: true, AllowedGroups: []int64{publicGroupA}},
  66. groupID: exclusiveGroupA,
  67. isExclusive: true,
  68. want: false,
  69. },
  70. {
  71. name: "a restricted user can still hold both kinds of grant",
  72. user: User{RestrictPublicGroups: true, AllowedGroups: []int64{publicGroupA, exclusiveGroupA}},
  73. groupID: exclusiveGroupA,
  74. isExclusive: true,
  75. want: true,
  76. },
  77. } {
  78. t.Run(tc.name, func(t *testing.T) {
  79. user := tc.user
  80. require.Equal(t, tc.want, user.CanBindGroup(tc.groupID, tc.isExclusive))
  81. })
  82. }
  83. }