organizations_test.go 8.3 KB

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