organizations_test.go 10 KB

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