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

timetracking_test.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "github.com/stretchr/testify/assert"
  10. )
  11. func TestViewTimetrackingControls(t *testing.T) {
  12. prepareTestEnv(t)
  13. session := loginUser(t, "user2")
  14. testViewTimetrackingControls(t, session, "user2", "repo1", "1", true)
  15. //user2/repo1
  16. }
  17. func TestNotViewTimetrackingControls(t *testing.T) {
  18. prepareTestEnv(t)
  19. session := loginUser(t, "user5")
  20. testViewTimetrackingControls(t, session, "user2", "repo1", "1", false)
  21. //user2/repo1
  22. }
  23. func TestViewTimetrackingControlsDisabled(t *testing.T) {
  24. prepareTestEnv(t)
  25. session := loginUser(t, "user2")
  26. testViewTimetrackingControls(t, session, "user3", "repo3", "1", false)
  27. }
  28. func testViewTimetrackingControls(t *testing.T, session *TestSession, user, repo, issue string, canTrackTime bool) {
  29. req := NewRequest(t, "GET", path.Join(user, repo, "issues", issue))
  30. resp := session.MakeRequest(t, req, http.StatusOK)
  31. htmlDoc := NewHTMLParser(t, resp.Body)
  32. htmlDoc.AssertElement(t, ".timetrack .start-add .start", canTrackTime)
  33. htmlDoc.AssertElement(t, ".timetrack .start-add .add-time", canTrackTime)
  34. req = NewRequestWithValues(t, "POST", path.Join(user, repo, "issues", issue, "times", "stopwatch", "toggle"), map[string]string{
  35. "_csrf": htmlDoc.GetCSRF(),
  36. })
  37. if canTrackTime {
  38. resp = session.MakeRequest(t, req, http.StatusSeeOther)
  39. req = NewRequest(t, "GET", RedirectURL(t, resp))
  40. resp = session.MakeRequest(t, req, http.StatusOK)
  41. htmlDoc = NewHTMLParser(t, resp.Body)
  42. events := htmlDoc.doc.Find(".event > span.text")
  43. assert.Contains(t, events.Last().Text(), "started working")
  44. htmlDoc.AssertElement(t, ".timetrack .stop-cancel .stop", true)
  45. htmlDoc.AssertElement(t, ".timetrack .stop-cancel .cancel", true)
  46. req = NewRequestWithValues(t, "POST", path.Join(user, repo, "issues", issue, "times", "stopwatch", "toggle"), map[string]string{
  47. "_csrf": htmlDoc.GetCSRF(),
  48. })
  49. resp = session.MakeRequest(t, req, http.StatusSeeOther)
  50. req = NewRequest(t, "GET", RedirectURL(t, resp))
  51. resp = session.MakeRequest(t, req, http.StatusOK)
  52. htmlDoc = NewHTMLParser(t, resp.Body)
  53. events = htmlDoc.doc.Find(".event > span.text")
  54. assert.Contains(t, events.Last().Text(), "stopped working")
  55. htmlDoc.AssertElement(t, ".event .detail .octicon-clock", true)
  56. } else {
  57. session.MakeRequest(t, req, http.StatusNotFound)
  58. }
  59. }