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.

93 lines
2.9 KiB

  1. // Copyright 2019 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. "path"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. api "code.gitea.io/sdk/gitea"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestPullCreate_CommitStatus(t *testing.T) {
  15. prepareTestEnv(t)
  16. session := loginUser(t, "user1")
  17. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  18. testEditFileToNewBranch(t, session, "user1", "repo1", "master", "status1", "README.md", "status1")
  19. url := path.Join("user1", "repo1", "compare", "master...status1")
  20. req := NewRequestWithValues(t, "POST", url,
  21. map[string]string{
  22. "_csrf": GetCSRF(t, session, url),
  23. "title": "pull request from status1",
  24. },
  25. )
  26. session.MakeRequest(t, req, http.StatusFound)
  27. req = NewRequest(t, "GET", "/user1/repo1/pulls")
  28. resp := session.MakeRequest(t, req, http.StatusOK)
  29. doc := NewHTMLParser(t, resp.Body)
  30. // Request repository commits page
  31. req = NewRequest(t, "GET", "/user1/repo1/pulls/1/commits")
  32. resp = session.MakeRequest(t, req, http.StatusOK)
  33. doc = NewHTMLParser(t, resp.Body)
  34. // Get first commit URL
  35. commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href")
  36. assert.True(t, exists)
  37. assert.NotEmpty(t, commitURL)
  38. commitID := path.Base(commitURL)
  39. statusList := []models.CommitStatusState{
  40. models.CommitStatusPending,
  41. models.CommitStatusError,
  42. models.CommitStatusFailure,
  43. models.CommitStatusWarning,
  44. models.CommitStatusSuccess,
  45. }
  46. statesIcons := map[models.CommitStatusState]string{
  47. models.CommitStatusPending: "circle icon yellow",
  48. models.CommitStatusSuccess: "check icon green",
  49. models.CommitStatusError: "warning icon red",
  50. models.CommitStatusFailure: "remove icon red",
  51. models.CommitStatusWarning: "warning sign icon yellow",
  52. }
  53. // Update commit status, and check if icon is updated as well
  54. for _, status := range statusList {
  55. // Call API to add status for commit
  56. token := getTokenForLoggedInUser(t, session)
  57. req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/user1/repo1/statuses/%s?token=%s", commitID, token),
  58. api.CreateStatusOption{
  59. State: api.StatusState(status),
  60. TargetURL: "http://test.ci/",
  61. Description: "",
  62. Context: "testci",
  63. },
  64. )
  65. session.MakeRequest(t, req, http.StatusCreated)
  66. req = NewRequestf(t, "GET", "/user1/repo1/pulls/1/commits")
  67. resp = session.MakeRequest(t, req, http.StatusOK)
  68. doc = NewHTMLParser(t, resp.Body)
  69. commitURL, exists = doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href")
  70. assert.True(t, exists)
  71. assert.NotEmpty(t, commitURL)
  72. assert.EqualValues(t, commitID, path.Base(commitURL))
  73. cls, ok := doc.doc.Find("#commits-table tbody tr td.message i.commit-status").Last().Attr("class")
  74. assert.True(t, ok)
  75. assert.EqualValues(t, "commit-status "+statesIcons[status], cls)
  76. }
  77. }