organizations_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. "time"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. "gogs.io/gogs/internal/conf"
  14. "gogs.io/gogs/internal/dbtest"
  15. "gogs.io/gogs/internal/errutil"
  16. )
  17. func TestOrganizations(t *testing.T) {
  18. if testing.Short() {
  19. t.Skip()
  20. }
  21. t.Parallel()
  22. ctx := context.Background()
  23. tables := []any{new(User), new(EmailAddress), new(OrgUser), new(Team), new(TeamUser)}
  24. db := &organizations{
  25. DB: dbtest.NewDB(t, "orgs", tables...),
  26. }
  27. for _, tc := range []struct {
  28. name string
  29. test func(t *testing.T, ctx context.Context, db *organizations)
  30. }{
  31. {"Create", orgsCreate},
  32. {"List", orgsList},
  33. {"SearchByName", orgsSearchByName},
  34. {"CountByUser", orgsCountByUser},
  35. } {
  36. t.Run(tc.name, func(t *testing.T) {
  37. t.Cleanup(func() {
  38. err := clearTables(t, db.DB, tables...)
  39. require.NoError(t, err)
  40. })
  41. tc.test(t, ctx, db)
  42. })
  43. if t.Failed() {
  44. break
  45. }
  46. }
  47. }
  48. func orgsCreate(t *testing.T, ctx context.Context, db *organizations) {
  49. usersStore := NewUsersStore(db.DB)
  50. alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
  51. require.NoError(t, err)
  52. t.Run("name not allowed", func(t *testing.T) {
  53. _, err := db.Create(ctx, "-", alice.ID, CreateOrganizationOptions{})
  54. wantErr := ErrNameNotAllowed{
  55. args: errutil.Args{
  56. "reason": "reserved",
  57. "name": "-",
  58. },
  59. }
  60. assert.Equal(t, wantErr, err)
  61. })
  62. // Users and organizations share the same namespace for names.
  63. t.Run("name already exists", func(t *testing.T) {
  64. _, err := db.Create(ctx, alice.Name, alice.ID, CreateOrganizationOptions{})
  65. wantErr := ErrOrganizationAlreadyExist{
  66. args: errutil.Args{
  67. "name": alice.Name,
  68. },
  69. }
  70. assert.Equal(t, wantErr, err)
  71. })
  72. tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsCreate-tempPictureAvatarUploadPath")
  73. conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
  74. org, err := db.Create(
  75. ctx,
  76. "acme",
  77. alice.ID,
  78. CreateOrganizationOptions{
  79. FullName: "Acme Corp",
  80. Email: "admin@acme.com",
  81. Location: "Earth",
  82. Website: "acme.com",
  83. Description: "A popcorn company",
  84. },
  85. )
  86. require.NoError(t, err)
  87. got, err := db.GetByName(ctx, org.Name)
  88. require.NoError(t, err)
  89. assert.Equal(t, org.Name, got.Name)
  90. assert.Equal(t, org.FullName, got.FullName)
  91. assert.Equal(t, org.Email, got.Email)
  92. assert.Equal(t, org.Location, got.Location)
  93. assert.Equal(t, org.Website, got.Website)
  94. assert.Equal(t, org.Description, got.Description)
  95. assert.Equal(t, -1, got.MaxRepoCreation)
  96. assert.Equal(t, 1, got.NumTeams)
  97. assert.Equal(t, 1, got.NumMembers)
  98. assert.Equal(t, db.NowFunc().Format(time.RFC3339), got.Created.UTC().Format(time.RFC3339))
  99. assert.Equal(t, db.NowFunc().Format(time.RFC3339), got.Updated.UTC().Format(time.RFC3339))
  100. }
  101. func orgsList(t *testing.T, ctx context.Context, db *organizations) {
  102. usersStore := NewUsersStore(db.DB)
  103. alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
  104. require.NoError(t, err)
  105. bob, err := usersStore.Create(ctx, "bob", "bob@example.com", CreateUserOptions{})
  106. require.NoError(t, err)
  107. tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsList-tempPictureAvatarUploadPath")
  108. conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
  109. org1, err := db.Create(ctx, "org1", alice.ID, CreateOrganizationOptions{})
  110. require.NoError(t, err)
  111. org2, err := db.Create(ctx, "org2", alice.ID, CreateOrganizationOptions{})
  112. require.NoError(t, err)
  113. err = db.SetMemberVisibility(ctx, org2.ID, alice.ID, true)
  114. require.NoError(t, err)
  115. err = db.AddMember(ctx, org2.ID, bob.ID)
  116. require.NoError(t, err)
  117. err = db.SetMemberVisibility(ctx, org2.ID, alice.ID, true)
  118. require.NoError(t, err)
  119. tests := []struct {
  120. name string
  121. opts ListOrganizationsOptions
  122. wantOrgNames []string
  123. }{
  124. {
  125. name: "only public memberships for a user",
  126. opts: ListOrganizationsOptions{
  127. MemberID: alice.ID,
  128. IncludePrivateMembers: false,
  129. },
  130. wantOrgNames: []string{org2.Name},
  131. },
  132. {
  133. name: "all memberships for a user",
  134. opts: ListOrganizationsOptions{
  135. MemberID: alice.ID,
  136. IncludePrivateMembers: true,
  137. },
  138. wantOrgNames: []string{org1.Name, org2.Name},
  139. },
  140. {
  141. name: "no membership for a non-existent user",
  142. opts: ListOrganizationsOptions{
  143. MemberID: 404,
  144. IncludePrivateMembers: true,
  145. },
  146. wantOrgNames: []string{},
  147. },
  148. }
  149. for _, test := range tests {
  150. t.Run(test.name, func(t *testing.T) {
  151. got, err := db.List(ctx, test.opts)
  152. require.NoError(t, err)
  153. gotOrgNames := make([]string, len(got))
  154. for i := range got {
  155. gotOrgNames[i] = got[i].Name
  156. }
  157. assert.Equal(t, test.wantOrgNames, gotOrgNames)
  158. })
  159. }
  160. }
  161. func orgsSearchByName(t *testing.T, ctx context.Context, db *organizations) {
  162. tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsSearchByName-tempPictureAvatarUploadPath")
  163. conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
  164. org1, err := db.Create(ctx, "org1", 1, CreateOrganizationOptions{FullName: "Acme Corp"})
  165. require.NoError(t, err)
  166. org2, err := db.Create(ctx, "org2", 1, CreateOrganizationOptions{FullName: "Acme Corp 2"})
  167. require.NoError(t, err)
  168. t.Run("search for username org1", func(t *testing.T) {
  169. orgs, count, err := db.SearchByName(ctx, "G1", 1, 1, "")
  170. require.NoError(t, err)
  171. require.Len(t, orgs, int(count))
  172. assert.Equal(t, int64(1), count)
  173. assert.Equal(t, org1.ID, orgs[0].ID)
  174. })
  175. t.Run("search for username org2", func(t *testing.T) {
  176. orgs, count, err := db.SearchByName(ctx, "G2", 1, 1, "")
  177. require.NoError(t, err)
  178. require.Len(t, orgs, int(count))
  179. assert.Equal(t, int64(1), count)
  180. assert.Equal(t, org2.ID, orgs[0].ID)
  181. })
  182. t.Run("search for full name acme", func(t *testing.T) {
  183. orgs, count, err := db.SearchByName(ctx, "ACME", 1, 10, "")
  184. require.NoError(t, err)
  185. require.Len(t, orgs, int(count))
  186. assert.Equal(t, int64(2), count)
  187. })
  188. t.Run("search for full name acme ORDER BY id DESC LIMIT 1", func(t *testing.T) {
  189. orgs, count, err := db.SearchByName(ctx, "ACME", 1, 1, "id DESC")
  190. require.NoError(t, err)
  191. require.Len(t, orgs, 1)
  192. assert.Equal(t, int64(2), count)
  193. assert.Equal(t, org2.ID, orgs[0].ID)
  194. })
  195. }
  196. func orgsCountByUser(t *testing.T, ctx context.Context, db *organizations) {
  197. usersStore := NewUsersStore(db.DB)
  198. alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
  199. require.NoError(t, err)
  200. bob, err := usersStore.Create(ctx, "bob", "bob@example.com", CreateUserOptions{})
  201. require.NoError(t, err)
  202. tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsCountByUser-tempPictureAvatarUploadPath")
  203. conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
  204. org1, err := db.Create(ctx, "org1", alice.ID, CreateOrganizationOptions{})
  205. require.NoError(t, err)
  206. err = db.AddMember(ctx, org1.ID, bob.ID)
  207. require.NoError(t, err)
  208. got, err := db.CountByUser(ctx, alice.ID)
  209. require.NoError(t, err)
  210. assert.Equal(t, int64(1), got)
  211. got, err = db.CountByUser(ctx, 404)
  212. require.NoError(t, err)
  213. assert.Equal(t, int64(0), got)
  214. }