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

timetracking_test.go 2.6KB

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