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.

116 lines
3.7 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. "encoding/json"
  7. "net/http"
  8. "net/http/httptest"
  9. "path"
  10. "testing"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestRepoCommits(t *testing.T) {
  16. defer prepareTestEnv(t)()
  17. session := loginUser(t, "user2")
  18. // Request repository commits page
  19. req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master")
  20. resp := session.MakeRequest(t, req, http.StatusOK)
  21. doc := NewHTMLParser(t, resp.Body)
  22. commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href")
  23. assert.True(t, exists)
  24. assert.NotEmpty(t, commitURL)
  25. }
  26. func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
  27. defer prepareTestEnv(t)()
  28. session := loginUser(t, "user2")
  29. token := getTokenForLoggedInUser(t, session)
  30. // Request repository commits page
  31. req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master")
  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").Attr("href")
  36. assert.True(t, exists)
  37. assert.NotEmpty(t, commitURL)
  38. // Call API to add status for commit
  39. req = NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/statuses/"+path.Base(commitURL)+"?token="+token,
  40. api.CreateStatusOption{
  41. State: api.StatusState(state),
  42. TargetURL: "http://test.ci/",
  43. Description: "",
  44. Context: "testci",
  45. },
  46. )
  47. resp = session.MakeRequest(t, req, http.StatusCreated)
  48. req = NewRequest(t, "GET", "/user2/repo1/commits/branch/master")
  49. resp = session.MakeRequest(t, req, http.StatusOK)
  50. doc = NewHTMLParser(t, resp.Body)
  51. // Check if commit status is displayed in message column
  52. sel := doc.doc.Find("#commits-table tbody tr td.message i.commit-status")
  53. assert.Equal(t, sel.Length(), 1)
  54. for _, class := range classes {
  55. assert.True(t, sel.HasClass(class))
  56. }
  57. //By SHA
  58. req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)+"/statuses")
  59. testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), state)
  60. //By Ref
  61. req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/master/statuses")
  62. testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), state)
  63. req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/v1.1/statuses")
  64. testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), state)
  65. }
  66. func testRepoCommitsWithStatus(t *testing.T, resp *httptest.ResponseRecorder, state string) {
  67. decoder := json.NewDecoder(resp.Body)
  68. statuses := []*api.Status{}
  69. assert.NoError(t, decoder.Decode(&statuses))
  70. assert.Len(t, statuses, 1)
  71. for _, s := range statuses {
  72. assert.Equal(t, api.StatusState(state), s.State)
  73. assert.Equal(t, setting.AppURL+"api/v1/repos/user2/repo1/statuses/65f1bf27bc3bf70f64657658635e66094edbcb4d", s.URL)
  74. assert.Equal(t, "http://test.ci/", s.TargetURL)
  75. assert.Equal(t, "", s.Description)
  76. assert.Equal(t, "testci", s.Context)
  77. }
  78. }
  79. func TestRepoCommitsWithStatusPending(t *testing.T) {
  80. doTestRepoCommitWithStatus(t, "pending", "circle", "yellow")
  81. }
  82. func TestRepoCommitsWithStatusSuccess(t *testing.T) {
  83. doTestRepoCommitWithStatus(t, "success", "check", "green")
  84. }
  85. func TestRepoCommitsWithStatusError(t *testing.T) {
  86. doTestRepoCommitWithStatus(t, "error", "warning", "red")
  87. }
  88. func TestRepoCommitsWithStatusFailure(t *testing.T) {
  89. doTestRepoCommitWithStatus(t, "failure", "remove", "red")
  90. }
  91. func TestRepoCommitsWithStatusWarning(t *testing.T) {
  92. doTestRepoCommitWithStatus(t, "warning", "warning", "sign", "yellow")
  93. }