Skip to content

Commit

Permalink
fix prepair logic! + add Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 committed Dec 9, 2019
1 parent c38418c commit 3d264c7
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
83 changes: 83 additions & 0 deletions integrations/api_issue_stopwatch_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions models/fixtures/stopwatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 7 additions & 3 deletions routers/api/v1/repo/issue_stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4020,7 +4020,7 @@
"description": "Issue not found"
},
"409": {
"description": "Cannot stop a non existent stopwatch"
"description": "Cannot cancel a non existent stopwatch"
}
}
}
Expand Down

0 comments on commit 3d264c7

Please sign in to comment.