2019-12-08 03:34:19 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-12-08 03:34:19 +05:30
|
|
|
|
2022-09-03 00:48:23 +05:30
|
|
|
package integration
|
2019-12-08 03:34:19 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-01-18 03:16:03 +05:30
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2022-04-08 14:41:15 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-16 14:23:21 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-12-08 03:34:19 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2022-12-29 08:27:15 +05:30
|
|
|
"code.gitea.io/gitea/services/convert"
|
2022-09-03 00:48:23 +05:30
|
|
|
"code.gitea.io/gitea/tests"
|
2019-12-08 03:34:19 +05:30
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPIIssuesReactions(t *testing.T) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-12-08 03:34:19 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
2022-04-08 14:41:15 +05:30
|
|
|
_ = issue.LoadRepo(db.DefaultContext)
|
2022-08-16 07:52:25 +05:30
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
|
2019-12-08 03:34:19 +05:30
|
|
|
|
|
|
|
session := loginUser(t, owner.Name)
|
2023-01-18 03:16:03 +05:30
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeRepo)
|
2019-12-08 03:34:19 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2019-12-08 03:34:19 +05:30
|
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/reactions?token=%s",
|
|
|
|
owner.Name, issue.Repo.Name, issue.Index, token)
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// Try to add not allowed reaction
|
2019-12-08 03:34:19 +05:30
|
|
|
req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "wrong",
|
|
|
|
})
|
2022-12-02 09:09:42 +05:30
|
|
|
MakeRequest(t, req, http.StatusForbidden)
|
2019-12-08 03:34:19 +05:30
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// Delete not allowed reaction
|
2019-12-08 03:34:19 +05:30
|
|
|
req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "zzz",
|
|
|
|
})
|
2022-12-02 09:09:42 +05:30
|
|
|
MakeRequest(t, req, http.StatusOK)
|
2019-12-08 03:34:19 +05:30
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// Add allowed reaction
|
2019-12-08 03:34:19 +05:30
|
|
|
req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "rocket",
|
|
|
|
})
|
2022-12-02 09:09:42 +05:30
|
|
|
resp := MakeRequest(t, req, http.StatusCreated)
|
2019-12-31 13:51:21 +05:30
|
|
|
var apiNewReaction api.Reaction
|
2019-12-08 03:34:19 +05:30
|
|
|
DecodeJSON(t, resp, &apiNewReaction)
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// Add existing reaction
|
2022-12-02 09:09:42 +05:30
|
|
|
MakeRequest(t, req, http.StatusForbidden)
|
2019-12-08 03:34:19 +05:30
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// Get end result of reaction list of issue #1
|
2019-12-08 03:34:19 +05:30
|
|
|
req = NewRequestf(t, "GET", urlStr)
|
2022-12-02 09:09:42 +05:30
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2019-12-31 13:51:21 +05:30
|
|
|
var apiReactions []*api.Reaction
|
2019-12-08 03:34:19 +05:30
|
|
|
DecodeJSON(t, resp, &apiReactions)
|
2019-12-31 13:51:21 +05:30
|
|
|
expectResponse := make(map[int]api.Reaction)
|
|
|
|
expectResponse[0] = api.Reaction{
|
2021-03-27 22:15:26 +05:30
|
|
|
User: convert.ToUser(user2, user2),
|
2019-12-08 03:34:19 +05:30
|
|
|
Reaction: "eyes",
|
|
|
|
Created: time.Unix(1573248003, 0),
|
|
|
|
}
|
2019-12-18 18:37:36 +05:30
|
|
|
expectResponse[1] = apiNewReaction
|
|
|
|
assert.Len(t, apiReactions, 2)
|
2019-12-08 03:34:19 +05:30
|
|
|
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) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-12-08 03:34:19 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2})
|
2022-11-19 13:42:33 +05:30
|
|
|
_ = comment.LoadIssue(db.DefaultContext)
|
2019-12-08 03:34:19 +05:30
|
|
|
issue := comment.Issue
|
2022-04-08 14:41:15 +05:30
|
|
|
_ = issue.LoadRepo(db.DefaultContext)
|
2022-08-16 07:52:25 +05:30
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
|
2019-12-08 03:34:19 +05:30
|
|
|
|
|
|
|
session := loginUser(t, owner.Name)
|
2023-01-18 03:16:03 +05:30
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeRepo)
|
2019-12-08 03:34:19 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2019-12-08 03:34:19 +05:30
|
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/reactions?token=%s",
|
|
|
|
owner.Name, issue.Repo.Name, comment.ID, token)
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// Try to add not allowed reaction
|
2019-12-08 03:34:19 +05:30
|
|
|
req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "wrong",
|
|
|
|
})
|
2022-12-02 09:09:42 +05:30
|
|
|
MakeRequest(t, req, http.StatusForbidden)
|
2019-12-08 03:34:19 +05:30
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// Delete none existing reaction
|
2019-12-08 03:34:19 +05:30
|
|
|
req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "eyes",
|
|
|
|
})
|
2022-12-02 09:09:42 +05:30
|
|
|
MakeRequest(t, req, http.StatusOK)
|
2019-12-08 03:34:19 +05:30
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// Add allowed reaction
|
2019-12-08 03:34:19 +05:30
|
|
|
req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "+1",
|
|
|
|
})
|
2022-12-02 09:09:42 +05:30
|
|
|
resp := MakeRequest(t, req, http.StatusCreated)
|
2019-12-31 13:51:21 +05:30
|
|
|
var apiNewReaction api.Reaction
|
2019-12-08 03:34:19 +05:30
|
|
|
DecodeJSON(t, resp, &apiNewReaction)
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// Add existing reaction
|
2022-12-02 09:09:42 +05:30
|
|
|
MakeRequest(t, req, http.StatusForbidden)
|
2019-12-08 03:34:19 +05:30
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// Get end result of reaction list of issue #1
|
2019-12-08 03:34:19 +05:30
|
|
|
req = NewRequestf(t, "GET", urlStr)
|
2022-12-02 09:09:42 +05:30
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2019-12-31 13:51:21 +05:30
|
|
|
var apiReactions []*api.Reaction
|
2019-12-08 03:34:19 +05:30
|
|
|
DecodeJSON(t, resp, &apiReactions)
|
2019-12-31 13:51:21 +05:30
|
|
|
expectResponse := make(map[int]api.Reaction)
|
|
|
|
expectResponse[0] = api.Reaction{
|
2021-03-27 22:15:26 +05:30
|
|
|
User: convert.ToUser(user2, user2),
|
2019-12-08 03:34:19 +05:30
|
|
|
Reaction: "laugh",
|
|
|
|
Created: time.Unix(1573248004, 0),
|
|
|
|
}
|
2019-12-31 13:51:21 +05:30
|
|
|
expectResponse[1] = api.Reaction{
|
2021-03-27 22:15:26 +05:30
|
|
|
User: convert.ToUser(user1, user1),
|
2019-12-08 03:34:19 +05:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|