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.6 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. "net/http"
  7. "path"
  8. "testing"
  9. api "code.gitea.io/sdk/gitea"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestRepoCommits(t *testing.T) {
  13. prepareTestEnv(t)
  14. session := loginUser(t, "user2")
  15. // Request repository commits page
  16. req := NewRequest(t, "GET", "/user2/repo1/commits/master")
  17. resp := session.MakeRequest(t, req)
  18. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  19. doc := NewHTMLParser(t, resp.Body)
  20. commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href")
  21. assert.True(t, exists)
  22. assert.NotEmpty(t, commitURL)
  23. }
  24. func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
  25. prepareTestEnv(t)
  26. session := loginUser(t, "user2")
  27. // Request repository commits page
  28. req := NewRequest(t, "GET", "/user2/repo1/commits/master")
  29. resp := session.MakeRequest(t, req)
  30. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  31. doc := NewHTMLParser(t, resp.Body)
  32. // Get first commit URL
  33. commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href")
  34. assert.True(t, exists)
  35. assert.NotEmpty(t, commitURL)
  36. // Call API to add status for commit
  37. req = NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/statuses/"+path.Base(commitURL),
  38. api.CreateStatusOption{
  39. State: api.StatusState(state),
  40. TargetURL: "http://test.ci/",
  41. Description: "",
  42. Context: "testci",
  43. },
  44. )
  45. resp = session.MakeRequest(t, req)
  46. assert.EqualValues(t, http.StatusCreated, resp.HeaderCode)
  47. req = NewRequest(t, "GET", "/user2/repo1/commits/master")
  48. resp = session.MakeRequest(t, req)
  49. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  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. }
  58. func TestRepoCommitsWithStatusPending(t *testing.T) {
  59. doTestRepoCommitWithStatus(t, "pending", "circle", "yellow")
  60. }
  61. func TestRepoCommitsWithStatusSuccess(t *testing.T) {
  62. doTestRepoCommitWithStatus(t, "success", "check", "green")
  63. }
  64. func TestRepoCommitsWithStatusError(t *testing.T) {
  65. doTestRepoCommitWithStatus(t, "error", "warning", "red")
  66. }
  67. func TestRepoCommitsWithStatusFailure(t *testing.T) {
  68. doTestRepoCommitWithStatus(t, "failure", "remove", "red")
  69. }
  70. func TestRepoCommitsWithStatusWarning(t *testing.T) {
  71. doTestRepoCommitWithStatus(t, "warning", "warning", "sign", "yellow")
  72. }