gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "encoding/base64"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "testing"
  10. "time"
  11. auth_model "code.gitea.io/gitea/models/auth"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/util"
  14. "code.gitea.io/gitea/routers/web/shared/user"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. func getCreateProfileReadmeFileOptions(content string) api.CreateFileOptions {
  18. contentEncoded := base64.StdEncoding.EncodeToString([]byte(content))
  19. return api.CreateFileOptions{
  20. FileOptions: api.FileOptions{
  21. BranchName: "main",
  22. NewBranchName: "main",
  23. Message: "create the profile README.md",
  24. Dates: api.CommitDateOptions{
  25. Author: time.Unix(946684810, 0),
  26. Committer: time.Unix(978307190, 0),
  27. },
  28. },
  29. ContentBase64: contentEncoded,
  30. }
  31. }
  32. func createTestProfile(t *testing.T, orgName, profileRepoName, readmeContent string) {
  33. isPrivate := profileRepoName == user.RepoNameProfilePrivate
  34. ctx := NewAPITestContext(t, "user1", profileRepoName, auth_model.AccessTokenScopeAll)
  35. session := loginUser(t, "user1")
  36. tokenAdmin := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAll)
  37. // create repo
  38. doAPICreateOrganizationRepository(ctx, orgName, &api.CreateRepoOption{Name: profileRepoName, Private: isPrivate})(t)
  39. // create readme
  40. createFileOptions := getCreateProfileReadmeFileOptions(readmeContent)
  41. req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", orgName, profileRepoName, "README.md"), &createFileOptions).
  42. AddTokenAuth(tokenAdmin)
  43. MakeRequest(t, req, http.StatusCreated)
  44. }
  45. func TestOrgProfile(t *testing.T) {
  46. onGiteaRun(t, testOrgProfile)
  47. }
  48. func testOrgProfile(t *testing.T, u *url.URL) {
  49. const contentPublicReadme = "Public Readme Content"
  50. const contentPrivateReadme = "Private Readme Content"
  51. // HTML: "#org-home-view-as-dropdown" (indicate whether the view as dropdown menu is present)
  52. // PART 1: Test Both Private and Public
  53. createTestProfile(t, "org3", user.RepoNameProfile, contentPublicReadme)
  54. createTestProfile(t, "org3", user.RepoNameProfilePrivate, contentPrivateReadme)
  55. // Anonymous User
  56. req := NewRequest(t, "GET", "org3")
  57. resp := MakeRequest(t, req, http.StatusOK)
  58. bodyString := util.UnsafeBytesToString(resp.Body.Bytes())
  59. assert.Contains(t, bodyString, contentPublicReadme)
  60. assert.NotContains(t, bodyString, `id="org-home-view-as-dropdown"`)
  61. // Logged in but not member
  62. session := loginUser(t, "user24")
  63. req = NewRequest(t, "GET", "org3")
  64. resp = session.MakeRequest(t, req, http.StatusOK)
  65. bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
  66. assert.Contains(t, bodyString, contentPublicReadme)
  67. assert.NotContains(t, bodyString, `id="org-home-view-as-dropdown"`)
  68. // Site Admin
  69. session = loginUser(t, "user1")
  70. req = NewRequest(t, "GET", "/org3")
  71. resp = session.MakeRequest(t, req, http.StatusOK)
  72. bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
  73. assert.Contains(t, bodyString, contentPrivateReadme) // as an org member, default to show the private profile
  74. assert.Contains(t, bodyString, `id="org-home-view-as-dropdown"`)
  75. req = NewRequest(t, "GET", "/org3?view_as=member")
  76. resp = session.MakeRequest(t, req, http.StatusOK)
  77. bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
  78. assert.Contains(t, bodyString, contentPrivateReadme)
  79. assert.Contains(t, bodyString, `id="org-home-view-as-dropdown"`)
  80. req = NewRequest(t, "GET", "/org3?view_as=public")
  81. resp = session.MakeRequest(t, req, http.StatusOK)
  82. bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
  83. assert.Contains(t, bodyString, contentPublicReadme)
  84. assert.Contains(t, bodyString, `id="org-home-view-as-dropdown"`)
  85. // PART 2: Each org has either one of private pr public profile
  86. createTestProfile(t, "org41", user.RepoNameProfile, contentPublicReadme)
  87. createTestProfile(t, "org42", user.RepoNameProfilePrivate, contentPrivateReadme)
  88. // Anonymous User
  89. req = NewRequest(t, "GET", "/org41")
  90. resp = MakeRequest(t, req, http.StatusOK)
  91. bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
  92. assert.Contains(t, bodyString, contentPublicReadme)
  93. assert.NotContains(t, bodyString, `id="org-home-view-as-dropdown"`)
  94. req = NewRequest(t, "GET", "/org42")
  95. resp = MakeRequest(t, req, http.StatusOK)
  96. bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
  97. assert.NotContains(t, bodyString, contentPrivateReadme)
  98. assert.NotContains(t, bodyString, `id="org-home-view-as-dropdown"`)
  99. // Site Admin
  100. req = NewRequest(t, "GET", "/org41")
  101. resp = session.MakeRequest(t, req, http.StatusOK)
  102. bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
  103. assert.Contains(t, bodyString, contentPublicReadme)
  104. assert.NotContains(t, bodyString, `id="org-home-view-as-dropdown"`)
  105. req = NewRequest(t, "GET", "/org42")
  106. resp = session.MakeRequest(t, req, http.StatusOK)
  107. bodyString = util.UnsafeBytesToString(resp.Body.Bytes())
  108. assert.Contains(t, bodyString, contentPrivateReadme)
  109. assert.NotContains(t, bodyString, `id="org-home-view-as-dropdown"`)
  110. }