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.

repo_commits_test.go 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. req.Header.Add("Content-Type", "application/json")
  46. resp = session.MakeRequest(t, req)
  47. assert.EqualValues(t, http.StatusCreated, resp.HeaderCode)
  48. req = NewRequest(t, "GET", "/user2/repo1/commits/master")
  49. resp = session.MakeRequest(t, req)
  50. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  51. doc = NewHTMLParser(t, resp.Body)
  52. // Check if commit status is displayed in message column
  53. sel := doc.doc.Find("#commits-table tbody tr td.message i.commit-status")
  54. assert.Equal(t, sel.Length(), 1)
  55. for _, class := range classes {
  56. assert.True(t, sel.HasClass(class))
  57. }
  58. }
  59. func TestRepoCommitsWithStatusPending(t *testing.T) {
  60. doTestRepoCommitWithStatus(t, "pending", "circle", "yellow")
  61. }
  62. func TestRepoCommitsWithStatusSuccess(t *testing.T) {
  63. doTestRepoCommitWithStatus(t, "success", "check", "green")
  64. }
  65. func TestRepoCommitsWithStatusError(t *testing.T) {
  66. doTestRepoCommitWithStatus(t, "error", "warning", "red")
  67. }
  68. func TestRepoCommitsWithStatusFailure(t *testing.T) {
  69. doTestRepoCommitWithStatus(t, "failure", "remove", "red")
  70. }
  71. func TestRepoCommitsWithStatusWarning(t *testing.T) {
  72. doTestRepoCommitWithStatus(t, "warning", "warning", "sign", "yellow")
  73. }