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.

94 lines
2.9 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. "bytes"
  7. "net/http"
  8. "path"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestRepoCommits(t *testing.T) {
  13. prepareTestEnv(t)
  14. session := loginUser(t, "user2", "password")
  15. // Request repository commits page
  16. req, err := http.NewRequest("GET", "/user2/repo1/commits/master", nil)
  17. assert.NoError(t, err)
  18. resp := session.MakeRequest(t, req)
  19. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  20. doc, err := NewHtmlParser(resp.Body)
  21. assert.NoError(t, err)
  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. prepareTestEnv(t)
  28. session := loginUser(t, "user2", "password")
  29. // Request repository commits page
  30. req, err := http.NewRequest("GET", "/user2/repo1/commits/master", nil)
  31. assert.NoError(t, err)
  32. resp := session.MakeRequest(t, req)
  33. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  34. doc, err := NewHtmlParser(resp.Body)
  35. assert.NoError(t, err)
  36. // Get first commit URL
  37. commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href")
  38. assert.True(t, exists)
  39. assert.NotEmpty(t, commitURL)
  40. // Call API to add status for commit
  41. req, err = http.NewRequest("POST", "/api/v1/repos/user2/repo1/statuses/"+path.Base(commitURL),
  42. bytes.NewBufferString("{\"state\":\""+state+"\", \"target_url\": \"http://test.ci/\", \"description\": \"\", \"context\": \"testci\"}"))
  43. assert.NoError(t, err)
  44. req.Header.Add("Content-Type", "application/json")
  45. resp = session.MakeRequest(t, req)
  46. assert.EqualValues(t, http.StatusCreated, resp.HeaderCode)
  47. req, err = http.NewRequest("GET", "/user2/repo1/commits/master", nil)
  48. assert.NoError(t, err)
  49. resp = session.MakeRequest(t, req)
  50. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  51. doc, err = NewHtmlParser(resp.Body)
  52. assert.NoError(t, err)
  53. // Check if commit status is displayed in message column
  54. sel := doc.doc.Find("#commits-table tbody tr td.message i.commit-status")
  55. assert.Equal(t, sel.Length(), 1)
  56. for _, class := range classes {
  57. assert.True(t, sel.HasClass(class))
  58. }
  59. }
  60. func TestRepoCommitsWithStatusPending(t *testing.T) {
  61. doTestRepoCommitWithStatus(t, "pending", "circle", "yellow")
  62. }
  63. func TestRepoCommitsWithStatusSuccess(t *testing.T) {
  64. doTestRepoCommitWithStatus(t, "success", "check", "green")
  65. }
  66. func TestRepoCommitsWithStatusError(t *testing.T) {
  67. doTestRepoCommitWithStatus(t, "error", "warning", "red")
  68. }
  69. func TestRepoCommitsWithStatusFailure(t *testing.T) {
  70. doTestRepoCommitWithStatus(t, "failure", "remove", "red")
  71. }
  72. func TestRepoCommitsWithStatusWarning(t *testing.T) {
  73. doTestRepoCommitWithStatus(t, "warning", "warning", "sign", "yellow")
  74. }