diff --git a/integrations/api_issue_stopwatch_test.go b/integrations/api_issue_stopwatch_test.go new file mode 100644 index 0000000000000..f8b4fc3df26d2 --- /dev/null +++ b/integrations/api_issue_stopwatch_test.go @@ -0,0 +1,83 @@ +// 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 ( + "net/http" + "testing" + + "code.gitea.io/gitea/models" + api "code.gitea.io/gitea/modules/structs" + + "github.com/stretchr/testify/assert" +) + +func TestAPIListStopWatches(t *testing.T) { + defer prepareTestEnv(t)() + + repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) + + session := loginUser(t, owner.Name) + token := getTokenForLoggedInUser(t, session) + req := NewRequestf(t, "GET", "/api/v1/user/stopwatches?token=%s", token) + resp := session.MakeRequest(t, req, http.StatusOK) + var apiWatches []*api.StopWatch + DecodeJSON(t, resp, &apiWatches) + expect := models.AssertExistsAndLoadBean(t, &models.Stopwatch{UserID: owner.ID}).(*models.Stopwatch) + expectApi, _ := expect.APIFormat() + assert.Len(t, apiWatches, 1) + + assert.EqualValues(t, expectApi.IssueIndex, apiWatches[0].IssueIndex) + assert.EqualValues(t, expectApi.Created, apiWatches[0].Created) +} + +func TestAPIStopStopWatches(t *testing.T) { + defer prepareTestEnv(t)() + + issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue) + _ = issue.LoadRepo() + owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) + user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + + session := loginUser(t, user.Name) + token := getTokenForLoggedInUser(t, session) + + req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/issues/%d/stopwatch/stop?token=%s", owner.Name, issue.Repo.Name, issue.Index, token) + session.MakeRequest(t, req, http.StatusCreated) + session.MakeRequest(t, req, http.StatusConflict) +} + +func TestAPICancelStopWatches(t *testing.T) { + defer prepareTestEnv(t)() + + issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1}).(*models.Issue) + _ = issue.LoadRepo() + owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) + user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + + session := loginUser(t, user.Name) + token := getTokenForLoggedInUser(t, session) + + req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/stopwatch/delete?token=%s", owner.Name, issue.Repo.Name, issue.Index, token) + session.MakeRequest(t, req, http.StatusAccepted) + session.MakeRequest(t, req, http.StatusConflict) +} + +func TestAPIStartStopWatches(t *testing.T) { + defer prepareTestEnv(t)() + + issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue) + _ = issue.LoadRepo() + owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User) + user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + + session := loginUser(t, user.Name) + token := getTokenForLoggedInUser(t, session) + + req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/issues/%d/stopwatch/start?token=%s", owner.Name, issue.Repo.Name, issue.Index, token) + session.MakeRequest(t, req, http.StatusCreated) + session.MakeRequest(t, req, http.StatusConflict) +} diff --git a/models/fixtures/stopwatch.yml b/models/fixtures/stopwatch.yml index 397a8214d4570..b7919d6fbbd6e 100644 --- a/models/fixtures/stopwatch.yml +++ b/models/fixtures/stopwatch.yml @@ -2,10 +2,10 @@ id: 1 user_id: 1 issue_id: 1 - created_unix: 1500988502 + created_unix: 1500988001 - id: 2 user_id: 2 issue_id: 2 - created_unix: 1500988502 + created_unix: 1500988002 diff --git a/routers/api/v1/repo/issue_stopwatch.go b/routers/api/v1/repo/issue_stopwatch.go index 5b321b5a53eb8..8372252af50b8 100644 --- a/routers/api/v1/repo/issue_stopwatch.go +++ b/routers/api/v1/repo/issue_stopwatch.go @@ -139,7 +139,7 @@ func DeleteIssueStopwatch(ctx *context.APIContext) { // "404": // description: Issue not found // "409": - // description: Cannot stop a non existent stopwatch + // description: Cannot cancel a non existent stopwatch issue, err := prepareIssueStopwatch(ctx, true) if err != nil { return @@ -175,8 +175,12 @@ func prepareIssueStopwatch(ctx *context.APIContext, shouldExist bool) (*models.I return nil, err } - if models.StopwatchExists(ctx.User.ID, issue.ID) == shouldExist { - ctx.Error(409, "StopwatchExists", "cannot stop a non existent stopwatch") + if models.StopwatchExists(ctx.User.ID, issue.ID) != shouldExist { + if shouldExist { + ctx.Error(409, "StopwatchExists", "cannot stop/cancel a non existent stopwatch") + } else { + ctx.Error(409, "StopwatchExists", "cannot start a stopwatch again if it already exists") + } return nil, err } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index e09b91bb40bd9..45a7c15ceed55 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -4020,7 +4020,7 @@ "description": "Issue not found" }, "409": { - "description": "Cannot stop a non existent stopwatch" + "description": "Cannot cancel a non existent stopwatch" } } }