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