Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

repo_commits_test.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }