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.

120 lines
3.7 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. 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. err = gitRepo.CreateTag("v0.0.1", "master")
  45. assert.NoError(t, err)
  46. target, err := gitRepo.GetTagCommitID("v0.0.1")
  47. assert.NoError(t, err)
  48. newRelease := createNewReleaseUsingAPI(t, session, token, owner, repo, "v0.0.1", target, "v0.0.1", "test")
  49. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/releases/%d?token=%s",
  50. owner.Name, repo.Name, newRelease.ID, token)
  51. req := NewRequest(t, "GET", urlStr)
  52. resp := session.MakeRequest(t, req, http.StatusOK)
  53. var release api.Release
  54. DecodeJSON(t, resp, &release)
  55. assert.Equal(t, newRelease.TagName, release.TagName)
  56. assert.Equal(t, newRelease.Title, release.Title)
  57. assert.Equal(t, newRelease.Note, release.Note)
  58. req = NewRequestWithJSON(t, "PATCH", urlStr, &api.EditReleaseOption{
  59. TagName: release.TagName,
  60. Title: release.Title,
  61. Note: "updated",
  62. IsDraft: &release.IsDraft,
  63. IsPrerelease: &release.IsPrerelease,
  64. Target: release.Target,
  65. })
  66. resp = session.MakeRequest(t, req, http.StatusOK)
  67. DecodeJSON(t, resp, &newRelease)
  68. models.AssertExistsAndLoadBean(t, &models.Release{
  69. ID: newRelease.ID,
  70. TagName: newRelease.TagName,
  71. Title: newRelease.Title,
  72. Note: newRelease.Note,
  73. })
  74. }
  75. func TestAPICreateReleaseToDefaultBranch(t *testing.T) {
  76. prepareTestEnv(t)
  77. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  78. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  79. session := loginUser(t, owner.LowerName)
  80. token := getTokenForLoggedInUser(t, session)
  81. createNewReleaseUsingAPI(t, session, token, owner, repo, "v0.0.1", "", "v0.0.1", "test")
  82. }
  83. func TestAPICreateReleaseToDefaultBranchOnExistingTag(t *testing.T) {
  84. prepareTestEnv(t)
  85. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  86. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  87. session := loginUser(t, owner.LowerName)
  88. token := getTokenForLoggedInUser(t, session)
  89. gitRepo, err := git.OpenRepository(repo.RepoPath())
  90. assert.NoError(t, err)
  91. err = gitRepo.CreateTag("v0.0.1", "master")
  92. assert.NoError(t, err)
  93. createNewReleaseUsingAPI(t, session, token, owner, repo, "v0.0.1", "", "v0.0.1", "test")
  94. }