You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
8.2 KiB
206 lines
8.2 KiB
8 years ago
|
// Copyright 2017 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.
|
||
|
|
||
2 years ago
|
package integration
|
||
8 years ago
|
|
||
|
import (
|
||
7 years ago
|
"fmt"
|
||
8 years ago
|
"net/http"
|
||
5 years ago
|
"net/url"
|
||
8 years ago
|
"testing"
|
||
|
|
||
2 years ago
|
issues_model "code.gitea.io/gitea/models/issues"
|
||
3 years ago
|
repo_model "code.gitea.io/gitea/models/repo"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unittest"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/convert"
|
||
6 years ago
|
api "code.gitea.io/gitea/modules/structs"
|
||
2 years ago
|
"code.gitea.io/gitea/tests"
|
||
8 years ago
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
7 years ago
|
func TestAPIListRepoComments(t *testing.T) {
|
||
2 years ago
|
defer tests.PrepareTestEnv(t)()
|
||
7 years ago
|
|
||
2 years ago
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{},
|
||
2 years ago
|
unittest.Cond("type = ?", issues_model.CommentTypeComment))
|
||
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID})
|
||
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
|
||
|
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
||
7 years ago
|
|
||
|
session := loginUser(t, repoOwner.Name)
|
||
5 years ago
|
link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments", repoOwner.Name, repo.Name))
|
||
|
req := NewRequest(t, "GET", link.String())
|
||
7 years ago
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||
|
|
||
|
var apiComments []*api.Comment
|
||
|
DecodeJSON(t, resp, &apiComments)
|
||
5 years ago
|
assert.Len(t, apiComments, 2)
|
||
7 years ago
|
for _, apiComment := range apiComments {
|
||
2 years ago
|
c := &issues_model.Comment{ID: apiComment.ID}
|
||
3 years ago
|
unittest.AssertExistsAndLoadBean(t, c,
|
||
2 years ago
|
unittest.Cond("type = ?", issues_model.CommentTypeComment))
|
||
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: c.IssueID, RepoID: repo.ID})
|
||
7 years ago
|
}
|
||
5 years ago
|
|
||
3 years ago
|
// test before and since filters
|
||
5 years ago
|
query := url.Values{}
|
||
3 years ago
|
before := "2000-01-01T00:00:11+00:00" // unix: 946684811
|
||
|
since := "2000-01-01T00:00:12+00:00" // unix: 946684812
|
||
5 years ago
|
query.Add("before", before)
|
||
|
link.RawQuery = query.Encode()
|
||
|
req = NewRequest(t, "GET", link.String())
|
||
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
||
|
DecodeJSON(t, resp, &apiComments)
|
||
|
assert.Len(t, apiComments, 1)
|
||
|
assert.EqualValues(t, 2, apiComments[0].ID)
|
||
|
|
||
|
query.Del("before")
|
||
|
query.Add("since", since)
|
||
|
link.RawQuery = query.Encode()
|
||
|
req = NewRequest(t, "GET", link.String())
|
||
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
||
|
DecodeJSON(t, resp, &apiComments)
|
||
|
assert.Len(t, apiComments, 1)
|
||
|
assert.EqualValues(t, 3, apiComments[0].ID)
|
||
7 years ago
|
}
|
||
|
|
||
|
func TestAPIListIssueComments(t *testing.T) {
|
||
2 years ago
|
defer tests.PrepareTestEnv(t)()
|
||
8 years ago
|
|
||
2 years ago
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{},
|
||
2 years ago
|
unittest.Cond("type = ?", issues_model.CommentTypeComment))
|
||
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID})
|
||
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
|
||
|
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
||
8 years ago
|
|
||
|
session := loginUser(t, repoOwner.Name)
|
||
8 years ago
|
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/comments",
|
||
8 years ago
|
repoOwner.Name, repo.Name, issue.Index)
|
||
7 years ago
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||
8 years ago
|
|
||
|
var comments []*api.Comment
|
||
|
DecodeJSON(t, resp, &comments)
|
||
2 years ago
|
expectedCount := unittest.GetCount(t, &issues_model.Comment{IssueID: issue.ID},
|
||
|
unittest.Cond("type = ?", issues_model.CommentTypeComment))
|
||
8 years ago
|
assert.EqualValues(t, expectedCount, len(comments))
|
||
|
}
|
||
7 years ago
|
|
||
|
func TestAPICreateComment(t *testing.T) {
|
||
2 years ago
|
defer tests.PrepareTestEnv(t)()
|
||
7 years ago
|
const commentBody = "Comment body"
|
||
|
|
||
2 years ago
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{})
|
||
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
|
||
|
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
||
7 years ago
|
|
||
|
session := loginUser(t, repoOwner.Name)
|
||
6 years ago
|
token := getTokenForLoggedInUser(t, session)
|
||
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments?token=%s",
|
||
|
repoOwner.Name, repo.Name, issue.Index, token)
|
||
7 years ago
|
req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
|
||
|
"body": commentBody,
|
||
|
})
|
||
|
resp := session.MakeRequest(t, req, http.StatusCreated)
|
||
|
|
||
|
var updatedComment api.Comment
|
||
|
DecodeJSON(t, resp, &updatedComment)
|
||
|
assert.EqualValues(t, commentBody, updatedComment.Body)
|
||
2 years ago
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: updatedComment.ID, IssueID: issue.ID, Content: commentBody})
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
func TestAPIGetComment(t *testing.T) {
|
||
2 years ago
|
defer tests.PrepareTestEnv(t)()
|
||
5 years ago
|
|
||
2 years ago
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2})
|
||
5 years ago
|
assert.NoError(t, comment.LoadIssue())
|
||
2 years ago
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: comment.Issue.RepoID})
|
||
|
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
||
5 years ago
|
|
||
|
session := loginUser(t, repoOwner.Name)
|
||
|
token := getTokenForLoggedInUser(t, session)
|
||
|
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID)
|
||
3 years ago
|
session.MakeRequest(t, req, http.StatusOK)
|
||
5 years ago
|
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d?token=%s", repoOwner.Name, repo.Name, comment.ID, token)
|
||
3 years ago
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||
5 years ago
|
|
||
|
var apiComment api.Comment
|
||
|
DecodeJSON(t, resp, &apiComment)
|
||
|
|
||
|
assert.NoError(t, comment.LoadPoster())
|
||
4 years ago
|
expect := convert.ToComment(comment)
|
||
5 years ago
|
|
||
|
assert.Equal(t, expect.ID, apiComment.ID)
|
||
|
assert.Equal(t, expect.Poster.FullName, apiComment.Poster.FullName)
|
||
|
assert.Equal(t, expect.Body, apiComment.Body)
|
||
|
assert.Equal(t, expect.Created.Unix(), apiComment.Created.Unix())
|
||
|
}
|
||
|
|
||
7 years ago
|
func TestAPIEditComment(t *testing.T) {
|
||
2 years ago
|
defer tests.PrepareTestEnv(t)()
|
||
7 years ago
|
const newCommentBody = "This is the new comment body"
|
||
|
|
||
2 years ago
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{},
|
||
2 years ago
|
unittest.Cond("type = ?", issues_model.CommentTypeComment))
|
||
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID})
|
||
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
|
||
|
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
||
7 years ago
|
|
||
|
session := loginUser(t, repoOwner.Name)
|
||
6 years ago
|
token := getTokenForLoggedInUser(t, session)
|
||
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d?token=%s",
|
||
|
repoOwner.Name, repo.Name, comment.ID, token)
|
||
7 years ago
|
req := NewRequestWithValues(t, "PATCH", urlStr, map[string]string{
|
||
|
"body": newCommentBody,
|
||
|
})
|
||
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||
|
|
||
|
var updatedComment api.Comment
|
||
|
DecodeJSON(t, resp, &updatedComment)
|
||
|
assert.EqualValues(t, comment.ID, updatedComment.ID)
|
||
|
assert.EqualValues(t, newCommentBody, updatedComment.Body)
|
||
2 years ago
|
unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: comment.ID, IssueID: issue.ID, Content: newCommentBody})
|
||
7 years ago
|
}
|
||
|
|
||
|
func TestAPIDeleteComment(t *testing.T) {
|
||
2 years ago
|
defer tests.PrepareTestEnv(t)()
|
||
7 years ago
|
|
||
2 years ago
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{},
|
||
2 years ago
|
unittest.Cond("type = ?", issues_model.CommentTypeComment))
|
||
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID})
|
||
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
|
||
|
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
||
7 years ago
|
|
||
|
session := loginUser(t, repoOwner.Name)
|
||
6 years ago
|
token := getTokenForLoggedInUser(t, session)
|
||
|
req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/comments/%d?token=%s",
|
||
|
repoOwner.Name, repo.Name, comment.ID, token)
|
||
7 years ago
|
session.MakeRequest(t, req, http.StatusNoContent)
|
||
|
|
||
2 years ago
|
unittest.AssertNotExistsBean(t, &issues_model.Comment{ID: comment.ID})
|
||
7 years ago
|
}
|
||
3 years ago
|
|
||
|
func TestAPIListIssueTimeline(t *testing.T) {
|
||
2 years ago
|
defer tests.PrepareTestEnv(t)()
|
||
3 years ago
|
|
||
|
// load comment
|
||
2 years ago
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
||
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
|
||
|
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
||
3 years ago
|
|
||
|
// make request
|
||
|
session := loginUser(t, repoOwner.Name)
|
||
|
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/timeline",
|
||
|
repoOwner.Name, repo.Name, issue.Index)
|
||
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||
|
|
||
|
// check if lens of list returned by API and
|
||
|
// lists extracted directly from DB are the same
|
||
|
var comments []*api.TimelineComment
|
||
|
DecodeJSON(t, resp, &comments)
|
||
2 years ago
|
expectedCount := unittest.GetCount(t, &issues_model.Comment{IssueID: issue.ID})
|
||
3 years ago
|
assert.EqualValues(t, expectedCount, len(comments))
|
||
|
}
|