Skip to content

Commit 3d264c7

Browse files
committed
fix prepair logic! + add Tests
1 parent c38418c commit 3d264c7

File tree

4 files changed

+93
-6
lines changed

4 files changed

+93
-6
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"net/http"
9+
"testing"
10+
11+
"code.gitea.io/gitea/models"
12+
api "code.gitea.io/gitea/modules/structs"
13+
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func TestAPIListStopWatches(t *testing.T) {
18+
defer prepareTestEnv(t)()
19+
20+
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
21+
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
22+
23+
session := loginUser(t, owner.Name)
24+
token := getTokenForLoggedInUser(t, session)
25+
req := NewRequestf(t, "GET", "/api/v1/user/stopwatches?token=%s", token)
26+
resp := session.MakeRequest(t, req, http.StatusOK)
27+
var apiWatches []*api.StopWatch
28+
DecodeJSON(t, resp, &apiWatches)
29+
expect := models.AssertExistsAndLoadBean(t, &models.Stopwatch{UserID: owner.ID}).(*models.Stopwatch)
30+
expectApi, _ := expect.APIFormat()
31+
assert.Len(t, apiWatches, 1)
32+
33+
assert.EqualValues(t, expectApi.IssueIndex, apiWatches[0].IssueIndex)
34+
assert.EqualValues(t, expectApi.Created, apiWatches[0].Created)
35+
}
36+
37+
func TestAPIStopStopWatches(t *testing.T) {
38+
defer prepareTestEnv(t)()
39+
40+
issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
41+
_ = issue.LoadRepo()
42+
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User)
43+
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
44+
45+
session := loginUser(t, user.Name)
46+
token := getTokenForLoggedInUser(t, session)
47+
48+
req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/issues/%d/stopwatch/stop?token=%s", owner.Name, issue.Repo.Name, issue.Index, token)
49+
session.MakeRequest(t, req, http.StatusCreated)
50+
session.MakeRequest(t, req, http.StatusConflict)
51+
}
52+
53+
func TestAPICancelStopWatches(t *testing.T) {
54+
defer prepareTestEnv(t)()
55+
56+
issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 1}).(*models.Issue)
57+
_ = issue.LoadRepo()
58+
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User)
59+
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
60+
61+
session := loginUser(t, user.Name)
62+
token := getTokenForLoggedInUser(t, session)
63+
64+
req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/stopwatch/delete?token=%s", owner.Name, issue.Repo.Name, issue.Index, token)
65+
session.MakeRequest(t, req, http.StatusAccepted)
66+
session.MakeRequest(t, req, http.StatusConflict)
67+
}
68+
69+
func TestAPIStartStopWatches(t *testing.T) {
70+
defer prepareTestEnv(t)()
71+
72+
issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue)
73+
_ = issue.LoadRepo()
74+
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: issue.Repo.OwnerID}).(*models.User)
75+
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
76+
77+
session := loginUser(t, user.Name)
78+
token := getTokenForLoggedInUser(t, session)
79+
80+
req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/issues/%d/stopwatch/start?token=%s", owner.Name, issue.Repo.Name, issue.Index, token)
81+
session.MakeRequest(t, req, http.StatusCreated)
82+
session.MakeRequest(t, req, http.StatusConflict)
83+
}

models/fixtures/stopwatch.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
id: 1
33
user_id: 1
44
issue_id: 1
5-
created_unix: 1500988502
5+
created_unix: 1500988001
66

77
-
88
id: 2
99
user_id: 2
1010
issue_id: 2
11-
created_unix: 1500988502
11+
created_unix: 1500988002

routers/api/v1/repo/issue_stopwatch.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func DeleteIssueStopwatch(ctx *context.APIContext) {
139139
// "404":
140140
// description: Issue not found
141141
// "409":
142-
// description: Cannot stop a non existent stopwatch
142+
// description: Cannot cancel a non existent stopwatch
143143
issue, err := prepareIssueStopwatch(ctx, true)
144144
if err != nil {
145145
return
@@ -175,8 +175,12 @@ func prepareIssueStopwatch(ctx *context.APIContext, shouldExist bool) (*models.I
175175
return nil, err
176176
}
177177

178-
if models.StopwatchExists(ctx.User.ID, issue.ID) == shouldExist {
179-
ctx.Error(409, "StopwatchExists", "cannot stop a non existent stopwatch")
178+
if models.StopwatchExists(ctx.User.ID, issue.ID) != shouldExist {
179+
if shouldExist {
180+
ctx.Error(409, "StopwatchExists", "cannot stop/cancel a non existent stopwatch")
181+
} else {
182+
ctx.Error(409, "StopwatchExists", "cannot start a stopwatch again if it already exists")
183+
}
180184
return nil, err
181185
}
182186

templates/swagger/v1_json.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4020,7 +4020,7 @@
40204020
"description": "Issue not found"
40214021
},
40224022
"409": {
4023-
"description": "Cannot stop a non existent stopwatch"
4023+
"description": "Cannot cancel a non existent stopwatch"
40244024
}
40254025
}
40264026
}

0 commit comments

Comments
 (0)