2019-12-08 06:04:19 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-12-08 06:04:19 +08:00
|
|
|
|
2022-09-03 03:18:23 +08:00
|
|
|
package integration
|
2019-12-08 06:04:19 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-01-18 05:46:03 +08:00
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2022-04-08 17:11:15 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 17:37:59 +08:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-16 16:53:21 +08:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-12-08 06:04:19 +08:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2022-12-29 10:57:15 +08:00
|
|
|
"code.gitea.io/gitea/services/convert"
|
2022-09-03 03:18:23 +08:00
|
|
|
"code.gitea.io/gitea/tests"
|
2019-12-08 06:04:19 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPIIssuesReactions(t *testing.T) {
|
2022-09-03 03:18:23 +08:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-12-08 06:04:19 +08:00
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
|
2022-04-08 17:11:15 +08:00
|
|
|
_ = issue.LoadRepo(db.DefaultContext)
|
2022-08-16 10:22:25 +08:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
|
2019-12-08 06:04:19 +08:00
|
|
|
|
|
|
|
session := loginUser(t, owner.Name)
|
2023-01-18 05:46:03 +08:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeRepo)
|
2019-12-08 06:04:19 +08:00
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2019-12-08 06:04:19 +08:00
|
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/reactions?token=%s",
|
|
|
|
owner.Name, issue.Repo.Name, issue.Index, token)
|
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
// Try to add not allowed reaction
|
2019-12-08 06:04:19 +08:00
|
|
|
req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "wrong",
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusForbidden)
|
2019-12-08 06:04:19 +08:00
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
// Delete not allowed reaction
|
2019-12-08 06:04:19 +08:00
|
|
|
req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "zzz",
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusOK)
|
2019-12-08 06:04:19 +08:00
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
// Add allowed reaction
|
2019-12-08 06:04:19 +08:00
|
|
|
req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "rocket",
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
resp := MakeRequest(t, req, http.StatusCreated)
|
2019-12-31 16:21:21 +08:00
|
|
|
var apiNewReaction api.Reaction
|
2019-12-08 06:04:19 +08:00
|
|
|
DecodeJSON(t, resp, &apiNewReaction)
|
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
// Add existing reaction
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusForbidden)
|
2019-12-08 06:04:19 +08:00
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
// Get end result of reaction list of issue #1
|
2019-12-08 06:04:19 +08:00
|
|
|
req = NewRequestf(t, "GET", urlStr)
|
2022-12-02 11:39:42 +08:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2019-12-31 16:21:21 +08:00
|
|
|
var apiReactions []*api.Reaction
|
2019-12-08 06:04:19 +08:00
|
|
|
DecodeJSON(t, resp, &apiReactions)
|
2019-12-31 16:21:21 +08:00
|
|
|
expectResponse := make(map[int]api.Reaction)
|
|
|
|
expectResponse[0] = api.Reaction{
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
User: convert.ToUser(db.DefaultContext, user2, user2),
|
2019-12-08 06:04:19 +08:00
|
|
|
Reaction: "eyes",
|
|
|
|
Created: time.Unix(1573248003, 0),
|
|
|
|
}
|
2019-12-18 21:07:36 +08:00
|
|
|
expectResponse[1] = apiNewReaction
|
|
|
|
assert.Len(t, apiReactions, 2)
|
2019-12-08 06:04:19 +08:00
|
|
|
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 03:18:23 +08:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2019-12-08 06:04:19 +08:00
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2})
|
2022-11-19 16:12:33 +08:00
|
|
|
_ = comment.LoadIssue(db.DefaultContext)
|
2019-12-08 06:04:19 +08:00
|
|
|
issue := comment.Issue
|
2022-04-08 17:11:15 +08:00
|
|
|
_ = issue.LoadRepo(db.DefaultContext)
|
2022-08-16 10:22:25 +08:00
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
|
2019-12-08 06:04:19 +08:00
|
|
|
|
|
|
|
session := loginUser(t, owner.Name)
|
2023-01-18 05:46:03 +08:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeRepo)
|
2019-12-08 06:04:19 +08:00
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2019-12-08 06:04:19 +08:00
|
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/reactions?token=%s",
|
|
|
|
owner.Name, issue.Repo.Name, comment.ID, token)
|
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
// Try to add not allowed reaction
|
2019-12-08 06:04:19 +08:00
|
|
|
req := NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "wrong",
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusForbidden)
|
2019-12-08 06:04:19 +08:00
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
// Delete none existing reaction
|
2019-12-08 06:04:19 +08:00
|
|
|
req = NewRequestWithJSON(t, "DELETE", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "eyes",
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusOK)
|
2019-12-08 06:04:19 +08:00
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
// Add allowed reaction
|
2019-12-08 06:04:19 +08:00
|
|
|
req = NewRequestWithJSON(t, "POST", urlStr, &api.EditReactionOption{
|
|
|
|
Reaction: "+1",
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
resp := MakeRequest(t, req, http.StatusCreated)
|
2019-12-31 16:21:21 +08:00
|
|
|
var apiNewReaction api.Reaction
|
2019-12-08 06:04:19 +08:00
|
|
|
DecodeJSON(t, resp, &apiNewReaction)
|
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
// Add existing reaction
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusForbidden)
|
2019-12-08 06:04:19 +08:00
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
// Get end result of reaction list of issue #1
|
2019-12-08 06:04:19 +08:00
|
|
|
req = NewRequestf(t, "GET", urlStr)
|
2022-12-02 11:39:42 +08:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2019-12-31 16:21:21 +08:00
|
|
|
var apiReactions []*api.Reaction
|
2019-12-08 06:04:19 +08:00
|
|
|
DecodeJSON(t, resp, &apiReactions)
|
2019-12-31 16:21:21 +08:00
|
|
|
expectResponse := make(map[int]api.Reaction)
|
|
|
|
expectResponse[0] = api.Reaction{
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
User: convert.ToUser(db.DefaultContext, user2, user2),
|
2019-12-08 06:04:19 +08:00
|
|
|
Reaction: "laugh",
|
|
|
|
Created: time.Unix(1573248004, 0),
|
|
|
|
}
|
2019-12-31 16:21:21 +08:00
|
|
|
expectResponse[1] = api.Reaction{
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
User: convert.ToUser(db.DefaultContext, user1, user1),
|
2019-12-08 06:04:19 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|