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.
145 lines
4.6 KiB
145 lines
4.6 KiB
5 years ago
|
// 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.
|
||
|
|
||
2 years ago
|
package integration
|
||
5 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
issues_model "code.gitea.io/gitea/models/issues"
|
||
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"
|
||
5 years ago
|
api "code.gitea.io/gitea/modules/structs"
|
||
2 years ago
|
"code.gitea.io/gitea/tests"
|
||
5 years ago
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestAPIIssuesReactions(t *testing.T) {
|
||
2 years ago
|
defer tests.PrepareTestEnv(t)()
|
||
5 years ago
|
|
||
2 years ago
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
||
3 years ago
|
_ = issue.LoadRepo(db.DefaultContext)
|
||
2 years ago
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
|
||
5 years ago
|
|
||
|
session := loginUser(t, owner.Name)
|
||
|
token := getTokenForLoggedInUser(t, session)
|
||
|
|
||
2 years ago
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||
5 years ago
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/reactions?token=%s",
|
||
|
owner.Name, issue.Repo.Name, issue.Index, token)
|
||
|
|
||
3 years ago
|
// Try to add not allowed reaction
|
||
5 years ago
|
req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
||
|
Reaction: "wrong",
|
||
|
})
|
||
3 years ago
|
session.MakeRequest(t, req, http.StatusForbidden)
|
||
5 years ago
|
|
||
3 years ago
|
// Delete not allowed reaction
|
||
5 years ago
|
req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
|
||
|
Reaction: "zzz",
|
||
|
})
|
||
3 years ago
|
session.MakeRequest(t, req, http.StatusOK)
|
||
5 years ago
|
|
||
3 years ago
|
// Add allowed reaction
|
||
5 years ago
|
req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
||
|
Reaction: "rocket",
|
||
|
})
|
||
3 years ago
|
resp := session.MakeRequest(t, req, http.StatusCreated)
|
||
5 years ago
|
var apiNewReaction api.Reaction
|
||
5 years ago
|
DecodeJSON(t, resp, &apiNewReaction)
|
||
|
|
||
3 years ago
|
// Add existing reaction
|
||
3 years ago
|
session.MakeRequest(t, req, http.StatusForbidden)
|
||
5 years ago
|
|
||
3 years ago
|
// Get end result of reaction list of issue #1
|
||
5 years ago
|
req = NewRequestf(t, "GET", urlStr)
|
||
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
||
5 years ago
|
var apiReactions []*api.Reaction
|
||
5 years ago
|
DecodeJSON(t, resp, &apiReactions)
|
||
5 years ago
|
expectResponse := make(map[int]api.Reaction)
|
||
|
expectResponse[0] = api.Reaction{
|
||
4 years ago
|
User: convert.ToUser(user2, user2),
|
||
5 years ago
|
Reaction: "eyes",
|
||
|
Created: time.Unix(1573248003, 0),
|
||
|
}
|
||
5 years ago
|
expectResponse[1] = apiNewReaction
|
||
|
assert.Len(t, apiReactions, 2)
|
||
5 years ago
|
for i, r := range apiReactions {
|
||
|
assert.Equal(t, expectResponse[i].Reaction, r.Reaction)
|
||
|
assert.Equal(t, expectResponse[i].Created.Unix(), r.Created.Unix())
|
||
|
assert.Equal(t, expectResponse[i].User.ID, r.User.ID)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestAPICommentReactions(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
|
_ = comment.LoadIssue()
|
||
|
issue := comment.Issue
|
||
3 years ago
|
_ = issue.LoadRepo(db.DefaultContext)
|
||
2 years ago
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
|
||
5 years ago
|
|
||
|
session := loginUser(t, owner.Name)
|
||
|
token := getTokenForLoggedInUser(t, session)
|
||
|
|
||
2 years ago
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||
5 years ago
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/reactions?token=%s",
|
||
|
owner.Name, issue.Repo.Name, comment.ID, token)
|
||
|
|
||
3 years ago
|
// Try to add not allowed reaction
|
||
5 years ago
|
req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
||
|
Reaction: "wrong",
|
||
|
})
|
||
3 years ago
|
session.MakeRequest(t, req, http.StatusForbidden)
|
||
5 years ago
|
|
||
3 years ago
|
// Delete none existing reaction
|
||
5 years ago
|
req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
|
||
|
Reaction: "eyes",
|
||
|
})
|
||
3 years ago
|
session.MakeRequest(t, req, http.StatusOK)
|
||
5 years ago
|
|
||
3 years ago
|
// Add allowed reaction
|
||
5 years ago
|
req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
||
|
Reaction: "+1",
|
||
|
})
|
||
3 years ago
|
resp := session.MakeRequest(t, req, http.StatusCreated)
|
||
5 years ago
|
var apiNewReaction api.Reaction
|
||
5 years ago
|
DecodeJSON(t, resp, &apiNewReaction)
|
||
|
|
||
3 years ago
|
// Add existing reaction
|
||
3 years ago
|
session.MakeRequest(t, req, http.StatusForbidden)
|
||
5 years ago
|
|
||
3 years ago
|
// Get end result of reaction list of issue #1
|
||
5 years ago
|
req = NewRequestf(t, "GET", urlStr)
|
||
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
||
5 years ago
|
var apiReactions []*api.Reaction
|
||
5 years ago
|
DecodeJSON(t, resp, &apiReactions)
|
||
5 years ago
|
expectResponse := make(map[int]api.Reaction)
|
||
|
expectResponse[0] = api.Reaction{
|
||
4 years ago
|
User: convert.ToUser(user2, user2),
|
||
5 years ago
|
Reaction: "laugh",
|
||
|
Created: time.Unix(1573248004, 0),
|
||
|
}
|
||
5 years ago
|
expectResponse[1] = api.Reaction{
|
||
4 years ago
|
User: convert.ToUser(user1, user1),
|
||
5 years ago
|
Reaction: "laugh",
|
||
|
Created: time.Unix(1573248005, 0),
|
||
|
}
|
||
|
expectResponse[2] = apiNewReaction
|
||
|
assert.Len(t, apiReactions, 3)
|
||
|
for i, r := range apiReactions {
|
||
|
assert.Equal(t, expectResponse[i].Reaction, r.Reaction)
|
||
|
assert.Equal(t, expectResponse[i].Created.Unix(), r.Created.Unix())
|
||
|
assert.Equal(t, expectResponse[i].User.ID, r.User.ID)
|
||
|
}
|
||
|
}
|