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.

85 lines
2.4 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/git"
  10. "code.gitea.io/gitea/models"
  11. api "code.gitea.io/sdk/gitea"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestAPICreateRelease(t *testing.T) {
  15. prepareTestEnv(t)
  16. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  17. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  18. session := loginUser(t, owner.LowerName)
  19. token := getTokenForLoggedInUser(t, session)
  20. gitRepo, err := git.OpenRepository(repo.RepoPath())
  21. assert.NoError(t, err)
  22. err = gitRepo.CreateTag("v0.0.1", "master")
  23. assert.NoError(t, err)
  24. commitID, err := gitRepo.GetTagCommitID("v0.0.1")
  25. assert.NoError(t, err)
  26. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/releases?token=%s",
  27. owner.Name, repo.Name, token)
  28. req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateReleaseOption{
  29. TagName: "v0.0.1",
  30. Title: "v0.0.1",
  31. Note: "test",
  32. IsDraft: false,
  33. IsPrerelease: false,
  34. Target: commitID,
  35. })
  36. resp := session.MakeRequest(t, req, http.StatusCreated)
  37. var newRelease api.Release
  38. DecodeJSON(t, resp, &newRelease)
  39. models.AssertExistsAndLoadBean(t, &models.Release{
  40. ID: newRelease.ID,
  41. TagName: newRelease.TagName,
  42. Title: newRelease.Title,
  43. Note: newRelease.Note,
  44. })
  45. urlStr = fmt.Sprintf("/api/v1/repos/%s/%s/releases/%d?token=%s",
  46. owner.Name, repo.Name, newRelease.ID, token)
  47. req = NewRequest(t, "GET", urlStr)
  48. resp = session.MakeRequest(t, req, http.StatusOK)
  49. var release api.Release
  50. DecodeJSON(t, resp, &release)
  51. assert.Equal(t, newRelease.TagName, release.TagName)
  52. assert.Equal(t, newRelease.Title, release.Title)
  53. assert.Equal(t, newRelease.Note, release.Note)
  54. req = NewRequestWithJSON(t, "PATCH", urlStr, &api.EditReleaseOption{
  55. TagName: release.TagName,
  56. Title: release.Title,
  57. Note: "updated",
  58. IsDraft: &release.IsDraft,
  59. IsPrerelease: &release.IsPrerelease,
  60. Target: release.Target,
  61. })
  62. resp = session.MakeRequest(t, req, http.StatusOK)
  63. DecodeJSON(t, resp, &newRelease)
  64. models.AssertExistsAndLoadBean(t, &models.Release{
  65. ID: newRelease.ID,
  66. TagName: newRelease.TagName,
  67. Title: newRelease.Title,
  68. Note: newRelease.Note,
  69. })
  70. }