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

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"net/http"
     8  	"testing"
     9  
    10  	auth_model "code.gitea.io/gitea/models/auth"
    11  	"code.gitea.io/gitea/models/db"
    12  	issues_model "code.gitea.io/gitea/models/issues"
    13  	repo_model "code.gitea.io/gitea/models/repo"
    14  	"code.gitea.io/gitea/models/unittest"
    15  	user_model "code.gitea.io/gitea/models/user"
    16  	api "code.gitea.io/gitea/modules/structs"
    17  	"code.gitea.io/gitea/tests"
    18  
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  func TestAPIListStopWatches(t *testing.T) {
    23  	defer tests.PrepareTestEnv(t)()
    24  
    25  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
    26  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
    27  
    28  	session := loginUser(t, owner.Name)
    29  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository, auth_model.AccessTokenScopeReadUser)
    30  	req := NewRequestf(t, "GET", "/api/v1/user/stopwatches?token=%s", token)
    31  	resp := MakeRequest(t, req, http.StatusOK)
    32  	var apiWatches []*api.StopWatch
    33  	DecodeJSON(t, resp, &apiWatches)
    34  	stopwatch := unittest.AssertExistsAndLoadBean(t, &issues_model.Stopwatch{UserID: owner.ID})
    35  	issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: stopwatch.IssueID})
    36  	if assert.Len(t, apiWatches, 1) {
    37  		assert.EqualValues(t, stopwatch.CreatedUnix.AsTime().Unix(), apiWatches[0].Created.Unix())
    38  		assert.EqualValues(t, issue.Index, apiWatches[0].IssueIndex)
    39  		assert.EqualValues(t, issue.Title, apiWatches[0].IssueTitle)
    40  		assert.EqualValues(t, repo.Name, apiWatches[0].RepoName)
    41  		assert.EqualValues(t, repo.OwnerName, apiWatches[0].RepoOwnerName)
    42  		assert.Greater(t, apiWatches[0].Seconds, int64(0))
    43  	}
    44  }
    45  
    46  func TestAPIStopStopWatches(t *testing.T) {
    47  	defer tests.PrepareTestEnv(t)()
    48  
    49  	issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
    50  	_ = issue.LoadRepo(db.DefaultContext)
    51  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
    52  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    53  
    54  	session := loginUser(t, user.Name)
    55  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
    56  
    57  	req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/issues/%d/stopwatch/stop?token=%s", owner.Name, issue.Repo.Name, issue.Index, token)
    58  	MakeRequest(t, req, http.StatusCreated)
    59  	MakeRequest(t, req, http.StatusConflict)
    60  }
    61  
    62  func TestAPICancelStopWatches(t *testing.T) {
    63  	defer tests.PrepareTestEnv(t)()
    64  
    65  	issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
    66  	_ = issue.LoadRepo(db.DefaultContext)
    67  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
    68  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
    69  
    70  	session := loginUser(t, user.Name)
    71  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
    72  
    73  	req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/stopwatch/delete?token=%s", owner.Name, issue.Repo.Name, issue.Index, token)
    74  	MakeRequest(t, req, http.StatusNoContent)
    75  	MakeRequest(t, req, http.StatusConflict)
    76  }
    77  
    78  func TestAPIStartStopWatches(t *testing.T) {
    79  	defer tests.PrepareTestEnv(t)()
    80  
    81  	issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 3})
    82  	_ = issue.LoadRepo(db.DefaultContext)
    83  	owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
    84  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    85  
    86  	session := loginUser(t, user.Name)
    87  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue)
    88  
    89  	req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/issues/%d/stopwatch/start?token=%s", owner.Name, issue.Repo.Name, issue.Index, token)
    90  	MakeRequest(t, req, http.StatusCreated)
    91  	MakeRequest(t, req, http.StatusConflict)
    92  }