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.

timetracking_test.go 2.6KB

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