gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. "time"
  10. auth_model "code.gitea.io/gitea/models/auth"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "code.gitea.io/gitea/modules/test"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. var testLicenseContent = `
  16. Copyright (c) 2024 Gitea
  17. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  18. 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  19. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. `
  22. func TestAPIRepoLicense(t *testing.T) {
  23. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  24. session := loginUser(t, "user2")
  25. // Request editor page
  26. req := NewRequest(t, "GET", "/user2/repo1/_new/master/")
  27. resp := session.MakeRequest(t, req, http.StatusOK)
  28. doc := NewHTMLParser(t, resp.Body)
  29. lastCommit := doc.GetInputValueByName("last_commit")
  30. assert.NotEmpty(t, lastCommit)
  31. // Save new file to master branch
  32. req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{
  33. "_csrf": doc.GetCSRF(),
  34. "last_commit": lastCommit,
  35. "tree_path": "LICENSE",
  36. "content": testLicenseContent,
  37. "commit_choice": "direct",
  38. })
  39. resp = session.MakeRequest(t, req, http.StatusOK)
  40. assert.NotEmpty(t, test.RedirectURL(resp))
  41. // let gitea update repo license
  42. time.Sleep(time.Second)
  43. checkRepoLicense(t, "user2", "repo1", []string{"BSD-2-Clause"})
  44. // Change default branch
  45. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  46. branchName := "DefaultBranch"
  47. req = NewRequestWithJSON(t, "PATCH", "/api/v1/repos/user2/repo1", api.EditRepoOption{
  48. DefaultBranch: &branchName,
  49. }).AddTokenAuth(token)
  50. session.MakeRequest(t, req, http.StatusOK)
  51. // let gitea update repo license
  52. time.Sleep(time.Second)
  53. checkRepoLicense(t, "user2", "repo1", []string{"MIT"})
  54. })
  55. }
  56. func checkRepoLicense(t *testing.T, owner, repo string, expected []string) {
  57. reqURL := fmt.Sprintf("/api/v1/repos/%s/%s/licenses", owner, repo)
  58. req := NewRequest(t, "GET", reqURL)
  59. resp := MakeRequest(t, req, http.StatusOK)
  60. var licenses []string
  61. DecodeJSON(t, resp, &licenses)
  62. assert.ElementsMatch(t, expected, licenses, 0)
  63. }