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.

90 lines
2.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. "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 := NewRequest(t, "GET", "/user2/repo1/commits/master")
  17. resp := session.MakeRequest(t, req)
  18. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  19. doc, err := NewHtmlParser(resp.Body)
  20. assert.NoError(t, err)
  21. commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href")
  22. assert.True(t, exists)
  23. assert.NotEmpty(t, commitURL)
  24. }
  25. func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
  26. prepareTestEnv(t)
  27. session := loginUser(t, "user2", "password")
  28. // Request repository commits page
  29. req := NewRequest(t, "GET", "/user2/repo1/commits/master")
  30. resp := session.MakeRequest(t, req)
  31. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  32. doc, err := NewHtmlParser(resp.Body)
  33. assert.NoError(t, err)
  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 = NewRequestBody(t, "POST", "/api/v1/repos/user2/repo1/statuses/"+path.Base(commitURL),
  40. bytes.NewBufferString("{\"state\":\""+state+"\", \"target_url\": \"http://test.ci/\", \"description\": \"\", \"context\": \"testci\"}"))
  41. req.Header.Add("Content-Type", "application/json")
  42. resp = session.MakeRequest(t, req)
  43. assert.EqualValues(t, http.StatusCreated, resp.HeaderCode)
  44. req = NewRequest(t, "GET", "/user2/repo1/commits/master")
  45. resp = session.MakeRequest(t, req)
  46. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  47. doc, err = NewHtmlParser(resp.Body)
  48. assert.NoError(t, err)
  49. // Check if commit status is displayed in message column
  50. sel := doc.doc.Find("#commits-table tbody tr td.message i.commit-status")
  51. assert.Equal(t, sel.Length(), 1)
  52. for _, class := range classes {
  53. assert.True(t, sel.HasClass(class))
  54. }
  55. }
  56. func TestRepoCommitsWithStatusPending(t *testing.T) {
  57. doTestRepoCommitWithStatus(t, "pending", "circle", "yellow")
  58. }
  59. func TestRepoCommitsWithStatusSuccess(t *testing.T) {
  60. doTestRepoCommitWithStatus(t, "success", "check", "green")
  61. }
  62. func TestRepoCommitsWithStatusError(t *testing.T) {
  63. doTestRepoCommitWithStatus(t, "error", "warning", "red")
  64. }
  65. func TestRepoCommitsWithStatusFailure(t *testing.T) {
  66. doTestRepoCommitWithStatus(t, "failure", "remove", "red")
  67. }
  68. func TestRepoCommitsWithStatusWarning(t *testing.T) {
  69. doTestRepoCommitWithStatus(t, "warning", "warning", "sign", "yellow")
  70. }