diff options
author | 6543 <6543@obermui.de> | 2019-12-27 21:30:58 +0100 |
---|---|---|
committer | zeripath <art27@cantab.net> | 2019-12-27 20:30:58 +0000 |
commit | f2d03cda96eb5febbf9801f6b6cf5daa37220bc9 (patch) | |
tree | 8906922ca58d47634ec2b6237f00a87389dd8316 /integrations/api_issue_tracked_time_test.go | |
parent | 0bcf644da4c3d21fad3ce8f33ccc26f8110568d6 (diff) | |
download | gitea-f2d03cda96eb5febbf9801f6b6cf5daa37220bc9.tar.gz gitea-f2d03cda96eb5febbf9801f6b6cf5daa37220bc9.zip |
[API] Extend times API (#9200)
Extensively extend the times API.
close #8833; close #8513; close #8559
Diffstat (limited to 'integrations/api_issue_tracked_time_test.go')
-rw-r--r-- | integrations/api_issue_tracked_time_test.go | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/integrations/api_issue_tracked_time_test.go b/integrations/api_issue_tracked_time_test.go new file mode 100644 index 0000000000..ed6c036db6 --- /dev/null +++ b/integrations/api_issue_tracked_time_test.go @@ -0,0 +1,109 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integrations + +import ( + "fmt" + "net/http" + "testing" + "time" + + "code.gitea.io/gitea/models" + api "code.gitea.io/gitea/modules/structs" + + "github.com/stretchr/testify/assert" +) + +func TestAPIGetTrackedTimes(t *testing.T) { + defer prepareTestEnv(t)() + + user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + issue2 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) + assert.NoError(t, issue2.LoadRepo()) + + session := loginUser(t, user2.Name) + token := getTokenForLoggedInUser(t, session) + + req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token) + resp := session.MakeRequest(t, req, http.StatusOK) + var apiTimes api.TrackedTimeList + DecodeJSON(t, resp, &apiTimes) + expect, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{IssueID: issue2.ID}) + assert.NoError(t, err) + assert.Len(t, apiTimes, 3) + + for i, time := range expect { + assert.Equal(t, time.ID, apiTimes[i].ID) + assert.EqualValues(t, issue2.Title, apiTimes[i].Issue.Title) + assert.EqualValues(t, issue2.ID, apiTimes[i].IssueID) + assert.Equal(t, time.Created.Unix(), apiTimes[i].Created.Unix()) + assert.Equal(t, time.Time, apiTimes[i].Time) + user, err := models.GetUserByID(time.UserID) + assert.NoError(t, err) + assert.Equal(t, user.Name, apiTimes[i].UserName) + } +} + +func TestAPIDeleteTrackedTime(t *testing.T) { + defer prepareTestEnv(t)() + + time6 := models.AssertExistsAndLoadBean(t, &models.TrackedTime{ID: 6}).(*models.TrackedTime) + issue2 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) + assert.NoError(t, issue2.LoadRepo()) + user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + + session := loginUser(t, user2.Name) + token := getTokenForLoggedInUser(t, session) + + //Deletion not allowed + req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times/%d?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, time6.ID, token) + session.MakeRequest(t, req, http.StatusForbidden) + /* Delete own time <-- ToDo: timout without reason + time3 := models.AssertExistsAndLoadBean(t, &models.TrackedTime{ID: 3}).(*models.TrackedTime) + req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times/%d?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, time3.ID, token) + session.MakeRequest(t, req, http.StatusNoContent) + //Delete non existing time + session.MakeRequest(t, req, http.StatusInternalServerError) */ + + //Reset time of user 2 on issue 2 + trackedSeconds, err := models.GetTrackedSeconds(models.FindTrackedTimesOptions{IssueID: 2, UserID: 2}) + assert.NoError(t, err) + assert.Equal(t, int64(3662), trackedSeconds) + + req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token) + session.MakeRequest(t, req, http.StatusNoContent) + session.MakeRequest(t, req, http.StatusNotFound) + + trackedSeconds, err = models.GetTrackedSeconds(models.FindTrackedTimesOptions{IssueID: 2, UserID: 2}) + assert.NoError(t, err) + assert.Equal(t, int64(0), trackedSeconds) +} + +func TestAPIAddTrackedTimes(t *testing.T) { + defer prepareTestEnv(t)() + + issue2 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) + assert.NoError(t, issue2.LoadRepo()) + user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + admin := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + + session := loginUser(t, admin.Name) + token := getTokenForLoggedInUser(t, session) + + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token) + + req := NewRequestWithJSON(t, "POST", urlStr, &api.AddTimeOption{ + Time: 33, + User: user2.Name, + Created: time.Unix(947688818, 0), + }) + resp := session.MakeRequest(t, req, http.StatusOK) + var apiNewTime api.TrackedTime + DecodeJSON(t, resp, &apiNewTime) + + assert.EqualValues(t, 33, apiNewTime.Time) + assert.EqualValues(t, user2.ID, apiNewTime.UserID) + assert.EqualValues(t, 947688818, apiNewTime.Created.Unix()) +} |