code.gitea.io/gitea@v1.21.7/tests/integration/timetracking_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"net/http"
     8  	"path"
     9  	"testing"
    10  	"time"
    11  
    12  	"code.gitea.io/gitea/modules/test"
    13  	"code.gitea.io/gitea/tests"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestViewTimetrackingControls(t *testing.T) {
    19  	defer tests.PrepareTestEnv(t)()
    20  	session := loginUser(t, "user2")
    21  	testViewTimetrackingControls(t, session, "user2", "repo1", "1", true)
    22  	// user2/repo1
    23  }
    24  
    25  func TestNotViewTimetrackingControls(t *testing.T) {
    26  	defer tests.PrepareTestEnv(t)()
    27  	session := loginUser(t, "user5")
    28  	testViewTimetrackingControls(t, session, "user2", "repo1", "1", false)
    29  	// user2/repo1
    30  }
    31  
    32  func TestViewTimetrackingControlsDisabled(t *testing.T) {
    33  	defer tests.PrepareTestEnv(t)()
    34  	session := loginUser(t, "user2")
    35  	testViewTimetrackingControls(t, session, "org3", "repo3", "1", false)
    36  }
    37  
    38  func testViewTimetrackingControls(t *testing.T, session *TestSession, user, repo, issue string, canTrackTime bool) {
    39  	req := NewRequest(t, "GET", path.Join(user, repo, "issues", issue))
    40  	resp := session.MakeRequest(t, req, http.StatusOK)
    41  
    42  	htmlDoc := NewHTMLParser(t, resp.Body)
    43  
    44  	htmlDoc.AssertElement(t, ".timetrack .issue-start-time", canTrackTime)
    45  	htmlDoc.AssertElement(t, ".timetrack .issue-add-time", canTrackTime)
    46  
    47  	req = NewRequestWithValues(t, "POST", path.Join(user, repo, "issues", issue, "times", "stopwatch", "toggle"), map[string]string{
    48  		"_csrf": htmlDoc.GetCSRF(),
    49  	})
    50  	if canTrackTime {
    51  		resp = session.MakeRequest(t, req, http.StatusSeeOther)
    52  
    53  		req = NewRequest(t, "GET", test.RedirectURL(resp))
    54  		resp = session.MakeRequest(t, req, http.StatusOK)
    55  		htmlDoc = NewHTMLParser(t, resp.Body)
    56  
    57  		events := htmlDoc.doc.Find(".event > span.text")
    58  		assert.Contains(t, events.Last().Text(), "started working")
    59  
    60  		htmlDoc.AssertElement(t, ".timetrack .issue-stop-time", true)
    61  		htmlDoc.AssertElement(t, ".timetrack .issue-cancel-time", true)
    62  
    63  		// Sleep for 1 second to not get wrong order for stopping timer
    64  		time.Sleep(time.Second)
    65  
    66  		req = NewRequestWithValues(t, "POST", path.Join(user, repo, "issues", issue, "times", "stopwatch", "toggle"), map[string]string{
    67  			"_csrf": htmlDoc.GetCSRF(),
    68  		})
    69  		resp = session.MakeRequest(t, req, http.StatusSeeOther)
    70  
    71  		req = NewRequest(t, "GET", test.RedirectURL(resp))
    72  		resp = session.MakeRequest(t, req, http.StatusOK)
    73  		htmlDoc = NewHTMLParser(t, resp.Body)
    74  
    75  		events = htmlDoc.doc.Find(".event > span.text")
    76  		assert.Contains(t, events.Last().Text(), "stopped working")
    77  		htmlDoc.AssertElement(t, ".event .detail .octicon-clock", true)
    78  	} else {
    79  		session.MakeRequest(t, req, http.StatusNotFound)
    80  	}
    81  }