5
5
package integrations
6
6
7
7
import (
8
+ "fmt"
8
9
"net/http"
9
10
"testing"
10
11
@@ -14,7 +15,31 @@ import (
14
15
"github.com/stretchr/testify/assert"
15
16
)
16
17
17
- func TestAPIListComments (t * testing.T ) {
18
+ func TestAPIListRepoComments (t * testing.T ) {
19
+ prepareTestEnv (t )
20
+
21
+ comment := models .AssertExistsAndLoadBean (t , & models.Comment {},
22
+ models .Cond ("type = ?" , models .CommentTypeComment )).(* models.Comment )
23
+ issue := models .AssertExistsAndLoadBean (t , & models.Issue {ID : comment .IssueID }).(* models.Issue )
24
+ repo := models .AssertExistsAndLoadBean (t , & models.Repository {ID : issue .RepoID }).(* models.Repository )
25
+ repoOwner := models .AssertExistsAndLoadBean (t , & models.User {ID : repo .OwnerID }).(* models.User )
26
+
27
+ session := loginUser (t , repoOwner .Name )
28
+ req := NewRequestf (t , "GET" , "/api/v1/repos/%s/%s/issues/comments" ,
29
+ repoOwner .Name , repo .Name )
30
+ resp := session .MakeRequest (t , req , http .StatusOK )
31
+
32
+ var apiComments []* api.Comment
33
+ DecodeJSON (t , resp , & apiComments )
34
+ for _ , apiComment := range apiComments {
35
+ c := & models.Comment {ID : apiComment .ID }
36
+ models .AssertExistsAndLoadBean (t , c ,
37
+ models .Cond ("type = ?" , models .CommentTypeComment ))
38
+ models .AssertExistsAndLoadBean (t , & models.Issue {ID : c .IssueID , RepoID : repo .ID })
39
+ }
40
+ }
41
+
42
+ func TestAPIListIssueComments (t * testing.T ) {
18
43
prepareTestEnv (t )
19
44
20
45
comment := models .AssertExistsAndLoadBean (t , & models.Comment {},
@@ -34,3 +59,67 @@ func TestAPIListComments(t *testing.T) {
34
59
models .Cond ("type = ?" , models .CommentTypeComment ))
35
60
assert .EqualValues (t , expectedCount , len (comments ))
36
61
}
62
+
63
+ func TestAPICreateComment (t * testing.T ) {
64
+ prepareTestEnv (t )
65
+ const commentBody = "Comment body"
66
+
67
+ issue := models .AssertExistsAndLoadBean (t , & models.Issue {}).(* models.Issue )
68
+ repo := models .AssertExistsAndLoadBean (t , & models.Repository {ID : issue .RepoID }).(* models.Repository )
69
+ repoOwner := models .AssertExistsAndLoadBean (t , & models.User {ID : repo .OwnerID }).(* models.User )
70
+
71
+ session := loginUser (t , repoOwner .Name )
72
+ urlStr := fmt .Sprintf ("/api/v1/repos/%s/%s/issues/%d/comments" ,
73
+ repoOwner .Name , repo .Name , issue .Index )
74
+ req := NewRequestWithValues (t , "POST" , urlStr , map [string ]string {
75
+ "body" : commentBody ,
76
+ })
77
+ resp := session .MakeRequest (t , req , http .StatusCreated )
78
+
79
+ var updatedComment api.Comment
80
+ DecodeJSON (t , resp , & updatedComment )
81
+ assert .EqualValues (t , commentBody , updatedComment .Body )
82
+ models .AssertExistsAndLoadBean (t , & models.Comment {ID : updatedComment .ID , IssueID : issue .ID , Content : commentBody })
83
+ }
84
+
85
+ func TestAPIEditComment (t * testing.T ) {
86
+ prepareTestEnv (t )
87
+ const newCommentBody = "This is the new comment body"
88
+
89
+ comment := models .AssertExistsAndLoadBean (t , & models.Comment {},
90
+ models .Cond ("type = ?" , models .CommentTypeComment )).(* models.Comment )
91
+ issue := models .AssertExistsAndLoadBean (t , & models.Issue {ID : comment .IssueID }).(* models.Issue )
92
+ repo := models .AssertExistsAndLoadBean (t , & models.Repository {ID : issue .RepoID }).(* models.Repository )
93
+ repoOwner := models .AssertExistsAndLoadBean (t , & models.User {ID : repo .OwnerID }).(* models.User )
94
+
95
+ session := loginUser (t , repoOwner .Name )
96
+ urlStr := fmt .Sprintf ("/api/v1/repos/%s/%s/issues/%d/comments/%d" ,
97
+ repoOwner .Name , repo .Name , issue .Index , comment .ID )
98
+ req := NewRequestWithValues (t , "PATCH" , urlStr , map [string ]string {
99
+ "body" : newCommentBody ,
100
+ })
101
+ resp := session .MakeRequest (t , req , http .StatusOK )
102
+
103
+ var updatedComment api.Comment
104
+ DecodeJSON (t , resp , & updatedComment )
105
+ assert .EqualValues (t , comment .ID , updatedComment .ID )
106
+ assert .EqualValues (t , newCommentBody , updatedComment .Body )
107
+ models .AssertExistsAndLoadBean (t , & models.Comment {ID : comment .ID , IssueID : issue .ID , Content : newCommentBody })
108
+ }
109
+
110
+ func TestAPIDeleteComment (t * testing.T ) {
111
+ prepareTestEnv (t )
112
+
113
+ comment := models .AssertExistsAndLoadBean (t , & models.Comment {},
114
+ models .Cond ("type = ?" , models .CommentTypeComment )).(* models.Comment )
115
+ issue := models .AssertExistsAndLoadBean (t , & models.Issue {ID : comment .IssueID }).(* models.Issue )
116
+ repo := models .AssertExistsAndLoadBean (t , & models.Repository {ID : issue .RepoID }).(* models.Repository )
117
+ repoOwner := models .AssertExistsAndLoadBean (t , & models.User {ID : repo .OwnerID }).(* models.User )
118
+
119
+ session := loginUser (t , repoOwner .Name )
120
+ req := NewRequestf (t , "DELETE" , "/api/v1/repos/%s/%s/issues/%d/comments/%d" ,
121
+ repoOwner .Name , repo .Name , issue .Index , comment .ID )
122
+ session .MakeRequest (t , req , http .StatusNoContent )
123
+
124
+ models .AssertNotExistsBean (t , & models.Comment {ID : comment .ID })
125
+ }
0 commit comments