You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.8 KiB

  1. // Copyright 2018 The Gitea 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 integrations
  5. import (
  6. "fmt"
  7. "net/http"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/git"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func createNewReleaseUsingAPI(t *testing.T, session *TestSession, token string, owner *models.User, repo *models.Repository, name, target, title, desc string) *api.Release {
  15. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/releases?token=%s",
  16. owner.Name, repo.Name, token)
  17. req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateReleaseOption{
  18. TagName: name,
  19. Title: title,
  20. Note: desc,
  21. IsDraft: false,
  22. IsPrerelease: false,
  23. Target: target,
  24. })
  25. resp := session.MakeRequest(t, req, http.StatusCreated)
  26. var newRelease api.Release
  27. DecodeJSON(t, resp, &newRelease)
  28. models.AssertExistsAndLoadBean(t, &models.Release{
  29. ID: newRelease.ID,
  30. TagName: newRelease.TagName,
  31. Title: newRelease.Title,
  32. Note: newRelease.Note,
  33. })
  34. return &newRelease
  35. }
  36. func TestAPICreateAndUpdateRelease(t *testing.T) {
  37. defer prepareTestEnv(t)()
  38. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  39. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  40. session := loginUser(t, owner.LowerName)
  41. token := getTokenForLoggedInUser(t, session)
  42. gitRepo, err := git.OpenRepository(repo.RepoPath())
  43. assert.NoError(t, err)
  44. defer gitRepo.Close()
  45. err = gitRepo.CreateTag("v0.0.1", "master")
  46. assert.NoError(t, err)
  47. target, err := gitRepo.GetTagCommitID("v0.0.1")
  48. assert.NoError(t, err)
  49. newRelease := createNewReleaseUsingAPI(t, session, token, owner, repo, "v0.0.1", target, "v0.0.1", "test")
  50. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/releases/%d?token=%s",
  51. owner.Name, repo.Name, newRelease.ID, token)
  52. req := NewRequest(t, "GET", urlStr)
  53. resp := session.MakeRequest(t, req, http.StatusOK)
  54. var release api.Release
  55. DecodeJSON(t, resp, &release)
  56. assert.Equal(t, newRelease.TagName, release.TagName)
  57. assert.Equal(t, newRelease.Title, release.Title)
  58. assert.Equal(t, newRelease.Note, release.Note)
  59. req = NewRequestWithJSON(t, "PATCH", urlStr, &api.EditReleaseOption{
  60. TagName: release.TagName,
  61. Title: release.Title,
  62. Note: "updated",
  63. IsDraft: &release.IsDraft,
  64. IsPrerelease: &release.IsPrerelease,
  65. Target: release.Target,
  66. })
  67. resp = session.MakeRequest(t, req, http.StatusOK)
  68. DecodeJSON(t, resp, &newRelease)
  69. models.AssertExistsAndLoadBean(t, &models.Release{
  70. ID: newRelease.ID,
  71. TagName: newRelease.TagName,
  72. Title: newRelease.Title,
  73. Note: newRelease.Note,
  74. })
  75. }
  76. func TestAPICreateReleaseToDefaultBranch(t *testing.T) {
  77. defer prepareTestEnv(t)()
  78. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  79. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  80. session := loginUser(t, owner.LowerName)
  81. token := getTokenForLoggedInUser(t, session)
  82. createNewReleaseUsingAPI(t, session, token, owner, repo, "v0.0.1", "", "v0.0.1", "test")
  83. }
  84. func TestAPICreateReleaseToDefaultBranchOnExistingTag(t *testing.T) {
  85. defer prepareTestEnv(t)()
  86. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  87. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  88. session := loginUser(t, owner.LowerName)
  89. token := getTokenForLoggedInUser(t, session)
  90. gitRepo, err := git.OpenRepository(repo.RepoPath())
  91. assert.NoError(t, err)
  92. defer gitRepo.Close()
  93. err = gitRepo.CreateTag("v0.0.1", "master")
  94. assert.NoError(t, err)
  95. createNewReleaseUsingAPI(t, session, token, owner, repo, "v0.0.1", "", "v0.0.1", "test")
  96. }