You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

userlist_test.go 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "testing"
  8. "code.gitea.io/gitea/models/db"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestUserListIsPublicMember(t *testing.T) {
  12. assert.NoError(t, db.PrepareTestDatabase())
  13. tt := []struct {
  14. orgid int64
  15. expected map[int64]bool
  16. }{
  17. {3, map[int64]bool{2: true, 4: false, 28: true}},
  18. {6, map[int64]bool{5: true, 28: true}},
  19. {7, map[int64]bool{5: false}},
  20. {25, map[int64]bool{24: true}},
  21. {22, map[int64]bool{}},
  22. }
  23. for _, v := range tt {
  24. t.Run(fmt.Sprintf("IsPublicMemberOfOrdIg%d", v.orgid), func(t *testing.T) {
  25. testUserListIsPublicMember(t, v.orgid, v.expected)
  26. })
  27. }
  28. }
  29. func testUserListIsPublicMember(t *testing.T, orgID int64, expected map[int64]bool) {
  30. org, err := GetUserByID(orgID)
  31. assert.NoError(t, err)
  32. assert.NoError(t, org.GetMembers())
  33. assert.Equal(t, expected, org.MembersIsPublic)
  34. }
  35. func TestUserListIsUserOrgOwner(t *testing.T) {
  36. assert.NoError(t, db.PrepareTestDatabase())
  37. tt := []struct {
  38. orgid int64
  39. expected map[int64]bool
  40. }{
  41. {3, map[int64]bool{2: true, 4: false, 28: false}},
  42. {6, map[int64]bool{5: true, 28: false}},
  43. {7, map[int64]bool{5: true}},
  44. {25, map[int64]bool{24: false}}, // ErrTeamNotExist
  45. {22, map[int64]bool{}}, // No member
  46. }
  47. for _, v := range tt {
  48. t.Run(fmt.Sprintf("IsUserOrgOwnerOfOrdIg%d", v.orgid), func(t *testing.T) {
  49. testUserListIsUserOrgOwner(t, v.orgid, v.expected)
  50. })
  51. }
  52. }
  53. func testUserListIsUserOrgOwner(t *testing.T, orgID int64, expected map[int64]bool) {
  54. org, err := GetUserByID(orgID)
  55. assert.NoError(t, err)
  56. assert.NoError(t, org.GetMembers())
  57. assert.Equal(t, expected, org.Members.IsUserOrgOwner(orgID))
  58. }
  59. func TestUserListIsTwoFaEnrolled(t *testing.T) {
  60. assert.NoError(t, db.PrepareTestDatabase())
  61. tt := []struct {
  62. orgid int64
  63. expected map[int64]bool
  64. }{
  65. {3, map[int64]bool{2: false, 4: false, 28: false}},
  66. {6, map[int64]bool{5: false, 28: false}},
  67. {7, map[int64]bool{5: false}},
  68. {25, map[int64]bool{24: true}},
  69. {22, map[int64]bool{}},
  70. }
  71. for _, v := range tt {
  72. t.Run(fmt.Sprintf("IsTwoFaEnrolledOfOrdIg%d", v.orgid), func(t *testing.T) {
  73. testUserListIsTwoFaEnrolled(t, v.orgid, v.expected)
  74. })
  75. }
  76. }
  77. func testUserListIsTwoFaEnrolled(t *testing.T, orgID int64, expected map[int64]bool) {
  78. org, err := GetUserByID(orgID)
  79. assert.NoError(t, err)
  80. assert.NoError(t, org.GetMembers())
  81. assert.Equal(t, expected, org.Members.GetTwoFaStatus())
  82. }