organizations_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2022 The Gogs 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 db
  5. import (
  6. "context"
  7. "os"
  8. "path/filepath"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "gogs.io/gogs/internal/conf"
  13. "gogs.io/gogs/internal/dbtest"
  14. )
  15. func TestOrgs(t *testing.T) {
  16. if testing.Short() {
  17. t.Skip()
  18. }
  19. t.Parallel()
  20. ctx := context.Background()
  21. tables := []any{new(User), new(EmailAddress), new(OrgUser), new(Team), new(TeamUser)}
  22. db := &organizations{
  23. DB: dbtest.NewDB(t, "orgs", tables...),
  24. }
  25. for _, tc := range []struct {
  26. name string
  27. test func(t *testing.T, ctx context.Context, db *organizations)
  28. }{
  29. {"List", orgsList},
  30. {"SearchByName", orgsSearchByName},
  31. {"CountByUser", orgsCountByUser},
  32. } {
  33. t.Run(tc.name, func(t *testing.T) {
  34. t.Cleanup(func() {
  35. err := clearTables(t, db.DB, tables...)
  36. require.NoError(t, err)
  37. })
  38. tc.test(t, ctx, db)
  39. })
  40. if t.Failed() {
  41. break
  42. }
  43. }
  44. }
  45. func orgsList(t *testing.T, ctx context.Context, db *organizations) {
  46. usersStore := NewUsersStore(db.DB)
  47. alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
  48. require.NoError(t, err)
  49. bob, err := usersStore.Create(ctx, "bob", "bob@example.com", CreateUserOptions{})
  50. require.NoError(t, err)
  51. tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
  52. conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
  53. org1, err := db.Create(ctx, "org1", alice.ID, CreateOrganizationOptions{})
  54. require.NoError(t, err)
  55. org2, err := db.Create(ctx, "org2", alice.ID, CreateOrganizationOptions{})
  56. require.NoError(t, err)
  57. err = db.SetMemberVisibility(ctx, org2.ID, alice.ID, true)
  58. require.NoError(t, err)
  59. err = db.AddMember(ctx, org2.ID, bob.ID)
  60. require.NoError(t, err)
  61. err = db.SetMemberVisibility(ctx, org2.ID, alice.ID, true)
  62. require.NoError(t, err)
  63. tests := []struct {
  64. name string
  65. opts ListOrganizationsOptions
  66. wantOrgNames []string
  67. }{
  68. {
  69. name: "only public memberships for a user",
  70. opts: ListOrganizationsOptions{
  71. MemberID: alice.ID,
  72. IncludePrivateMembers: false,
  73. },
  74. wantOrgNames: []string{org2.Name},
  75. },
  76. {
  77. name: "all memberships for a user",
  78. opts: ListOrganizationsOptions{
  79. MemberID: alice.ID,
  80. IncludePrivateMembers: true,
  81. },
  82. wantOrgNames: []string{org1.Name, org2.Name},
  83. },
  84. {
  85. name: "no membership for a non-existent user",
  86. opts: ListOrganizationsOptions{
  87. MemberID: 404,
  88. IncludePrivateMembers: true,
  89. },
  90. wantOrgNames: []string{},
  91. },
  92. }
  93. for _, test := range tests {
  94. t.Run(test.name, func(t *testing.T) {
  95. got, err := db.List(ctx, test.opts)
  96. require.NoError(t, err)
  97. gotOrgNames := make([]string, len(got))
  98. for i := range got {
  99. gotOrgNames[i] = got[i].Name
  100. }
  101. assert.Equal(t, test.wantOrgNames, gotOrgNames)
  102. })
  103. }
  104. }
  105. func orgsSearchByName(t *testing.T, ctx context.Context, db *organizations) {
  106. tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
  107. conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
  108. org1, err := db.Create(ctx, "org1", 1, CreateOrganizationOptions{FullName: "Acme Corp"})
  109. require.NoError(t, err)
  110. org2, err := db.Create(ctx, "org2", 1, CreateOrganizationOptions{FullName: "Acme Corp 2"})
  111. require.NoError(t, err)
  112. t.Run("search for username org1", func(t *testing.T) {
  113. orgs, count, err := db.SearchByName(ctx, "G1", 1, 1, "")
  114. require.NoError(t, err)
  115. require.Len(t, orgs, int(count))
  116. assert.Equal(t, int64(1), count)
  117. assert.Equal(t, org1.ID, orgs[0].ID)
  118. })
  119. t.Run("search for username org2", func(t *testing.T) {
  120. orgs, count, err := db.SearchByName(ctx, "G2", 1, 1, "")
  121. require.NoError(t, err)
  122. require.Len(t, orgs, int(count))
  123. assert.Equal(t, int64(1), count)
  124. assert.Equal(t, org2.ID, orgs[0].ID)
  125. })
  126. t.Run("search for full name acme", func(t *testing.T) {
  127. orgs, count, err := db.SearchByName(ctx, "ACME", 1, 10, "")
  128. require.NoError(t, err)
  129. require.Len(t, orgs, int(count))
  130. assert.Equal(t, int64(2), count)
  131. })
  132. t.Run("search for full name acme ORDER BY id DESC LIMIT 1", func(t *testing.T) {
  133. orgs, count, err := db.SearchByName(ctx, "ACME", 1, 1, "id DESC")
  134. require.NoError(t, err)
  135. require.Len(t, orgs, 1)
  136. assert.Equal(t, int64(2), count)
  137. assert.Equal(t, org2.ID, orgs[0].ID)
  138. })
  139. }
  140. func orgsCountByUser(t *testing.T, ctx context.Context, db *organizations) {
  141. usersStore := NewUsersStore(db.DB)
  142. alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
  143. require.NoError(t, err)
  144. bob, err := usersStore.Create(ctx, "bob", "bob@example.com", CreateUserOptions{})
  145. require.NoError(t, err)
  146. tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
  147. conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
  148. org1, err := db.Create(ctx, "org1", alice.ID, CreateOrganizationOptions{})
  149. require.NoError(t, err)
  150. err = db.AddMember(ctx, org1.ID, bob.ID)
  151. require.NoError(t, err)
  152. got, err := db.CountByUser(ctx, alice.ID)
  153. require.NoError(t, err)
  154. assert.Equal(t, int64(1), got)
  155. got, err = db.CountByUser(ctx, 404)
  156. require.NoError(t, err)
  157. assert.Equal(t, int64(0), got)
  158. }