2019-12-28 05:30:58 +09:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-28 03:20:29 +09:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-12-28 05:30:58 +09:00
|
|
|
|
2022-09-03 04:18:23 +09:00
|
|
|
package integration
|
2019-12-28 05:30:58 +09:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-01-18 06:46:03 +09:00
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2022-04-08 18:11:15 +09:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 18:37:59 +09:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-16 17:53:21 +09:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 18:49:20 +09:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-12-28 05:30:58 +09:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2022-09-03 04:18:23 +09:00
|
|
|
"code.gitea.io/gitea/tests"
|
2019-12-28 05:30:58 +09:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPIGetTrackedTimes(t *testing.T) {
|
2022-09-03 04:18:23 +09:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-12-28 05:30:58 +09:00
|
|
|
|
2022-08-16 11:22:25 +09:00
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
issue2 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
|
2022-04-08 18:11:15 +09:00
|
|
|
assert.NoError(t, issue2.LoadRepo(db.DefaultContext))
|
2019-12-28 05:30:58 +09:00
|
|
|
|
|
|
|
session := loginUser(t, user2.Name)
|
2023-01-18 06:46:03 +09:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeRepo)
|
2019-12-28 05:30:58 +09:00
|
|
|
|
|
|
|
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token)
|
2022-12-02 12:39:42 +09:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2019-12-28 05:30:58 +09:00
|
|
|
var apiTimes api.TrackedTimeList
|
|
|
|
DecodeJSON(t, resp, &apiTimes)
|
2022-06-13 18:37:59 +09:00
|
|
|
expect, err := issues_model.GetTrackedTimes(db.DefaultContext, &issues_model.FindTrackedTimesOptions{IssueID: issue2.ID})
|
2019-12-28 05:30:58 +09:00
|
|
|
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)
|
2022-12-03 11:48:26 +09:00
|
|
|
user, err := user_model.GetUserByID(db.DefaultContext, time.UserID)
|
2019-12-28 05:30:58 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, user.Name, apiTimes[i].UserName)
|
|
|
|
}
|
2020-01-09 06:14:00 +09:00
|
|
|
|
|
|
|
// test filter
|
2022-01-21 02:46:10 +09:00
|
|
|
since := "2000-01-01T00%3A00%3A02%2B00%3A00" // 946684802
|
|
|
|
before := "2000-01-01T00%3A00%3A12%2B00%3A00" // 946684812
|
2020-01-09 06:14:00 +09:00
|
|
|
|
|
|
|
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/times?since=%s&before=%s&token=%s", user2.Name, issue2.Repo.Name, issue2.Index, since, before, token)
|
2022-12-02 12:39:42 +09:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-01-09 06:14:00 +09:00
|
|
|
var filterAPITimes api.TrackedTimeList
|
|
|
|
DecodeJSON(t, resp, &filterAPITimes)
|
|
|
|
assert.Len(t, filterAPITimes, 2)
|
|
|
|
assert.Equal(t, int64(3), filterAPITimes[0].ID)
|
|
|
|
assert.Equal(t, int64(6), filterAPITimes[1].ID)
|
2019-12-28 05:30:58 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPIDeleteTrackedTime(t *testing.T) {
|
2022-09-03 04:18:23 +09:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-12-28 05:30:58 +09:00
|
|
|
|
2022-08-16 11:22:25 +09:00
|
|
|
time6 := unittest.AssertExistsAndLoadBean(t, &issues_model.TrackedTime{ID: 6})
|
|
|
|
issue2 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
|
2022-04-08 18:11:15 +09:00
|
|
|
assert.NoError(t, issue2.LoadRepo(db.DefaultContext))
|
2022-08-16 11:22:25 +09:00
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2019-12-28 05:30:58 +09:00
|
|
|
|
|
|
|
session := loginUser(t, user2.Name)
|
2023-01-18 06:46:03 +09:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeRepo)
|
2019-12-28 05:30:58 +09:00
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
// Deletion not allowed
|
2019-12-28 05:30:58 +09:00
|
|
|
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)
|
2022-12-02 12:39:42 +09:00
|
|
|
MakeRequest(t, req, http.StatusForbidden)
|
2020-05-09 23:18:44 +09:00
|
|
|
|
2022-08-16 11:22:25 +09:00
|
|
|
time3 := unittest.AssertExistsAndLoadBean(t, &issues_model.TrackedTime{ID: 3})
|
2019-12-28 05:30:58 +09:00
|
|
|
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)
|
2022-12-02 12:39:42 +09:00
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
2022-01-21 02:46:10 +09:00
|
|
|
// Delete non existing time
|
2022-12-02 12:39:42 +09:00
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
2019-12-28 05:30:58 +09:00
|
|
|
|
2022-01-21 02:46:10 +09:00
|
|
|
// Reset time of user 2 on issue 2
|
2022-06-13 18:37:59 +09:00
|
|
|
trackedSeconds, err := issues_model.GetTrackedSeconds(db.DefaultContext, issues_model.FindTrackedTimesOptions{IssueID: 2, UserID: 2})
|
2019-12-28 05:30:58 +09:00
|
|
|
assert.NoError(t, err)
|
2020-05-09 23:18:44 +09:00
|
|
|
assert.Equal(t, int64(3661), trackedSeconds)
|
2019-12-28 05:30:58 +09:00
|
|
|
|
|
|
|
req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token)
|
2022-12-02 12:39:42 +09:00
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
2019-12-28 05:30:58 +09:00
|
|
|
|
2022-06-13 18:37:59 +09:00
|
|
|
trackedSeconds, err = issues_model.GetTrackedSeconds(db.DefaultContext, issues_model.FindTrackedTimesOptions{IssueID: 2, UserID: 2})
|
2019-12-28 05:30:58 +09:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(0), trackedSeconds)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPIAddTrackedTimes(t *testing.T) {
|
2022-09-03 04:18:23 +09:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-12-28 05:30:58 +09:00
|
|
|
|
2022-08-16 11:22:25 +09:00
|
|
|
issue2 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
|
2022-04-08 18:11:15 +09:00
|
|
|
assert.NoError(t, issue2.LoadRepo(db.DefaultContext))
|
2022-08-16 11:22:25 +09:00
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
2019-12-28 05:30:58 +09:00
|
|
|
|
|
|
|
session := loginUser(t, admin.Name)
|
2023-01-18 06:46:03 +09:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeRepo)
|
2019-12-28 05:30:58 +09:00
|
|
|
|
|
|
|
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),
|
|
|
|
})
|
2022-12-02 12:39:42 +09:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2019-12-28 05:30:58 +09:00
|
|
|
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())
|
|
|
|
}
|