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.

64 lines
2.0 KiB

  1. // Copyright 2017 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. api "code.gitea.io/gitea/modules/structs"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestAPIListIssues(t *testing.T) {
  14. prepareTestEnv(t)
  15. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  16. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  17. session := loginUser(t, owner.Name)
  18. token := getTokenForLoggedInUser(t, session)
  19. req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues?state=all&token=%s",
  20. owner.Name, repo.Name, token)
  21. resp := session.MakeRequest(t, req, http.StatusOK)
  22. var apiIssues []*api.Issue
  23. DecodeJSON(t, resp, &apiIssues)
  24. assert.Len(t, apiIssues, models.GetCount(t, &models.Issue{RepoID: repo.ID}))
  25. for _, apiIssue := range apiIssues {
  26. models.AssertExistsAndLoadBean(t, &models.Issue{ID: apiIssue.ID, RepoID: repo.ID})
  27. }
  28. }
  29. func TestAPICreateIssue(t *testing.T) {
  30. prepareTestEnv(t)
  31. const body, title = "apiTestBody", "apiTestTitle"
  32. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  33. owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  34. session := loginUser(t, owner.Name)
  35. token := getTokenForLoggedInUser(t, session)
  36. urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues?state=all&token=%s", owner.Name, repo.Name, token)
  37. req := NewRequestWithJSON(t, "POST", urlStr, &api.CreateIssueOption{
  38. Body: body,
  39. Title: title,
  40. Assignee: owner.Name,
  41. })
  42. resp := session.MakeRequest(t, req, http.StatusCreated)
  43. var apiIssue api.Issue
  44. DecodeJSON(t, resp, &apiIssue)
  45. assert.Equal(t, apiIssue.Body, body)
  46. assert.Equal(t, apiIssue.Title, title)
  47. models.AssertExistsAndLoadBean(t, &models.Issue{
  48. RepoID: repo.ID,
  49. AssigneeID: owner.ID,
  50. Content: body,
  51. Title: title,
  52. })
  53. }