[chore] improved router logging, recovery and error handling (#705)

* move panic recovery to logging middleware, improve logging + panic recovery logic

Signed-off-by: kim <grufwub@gmail.com>

* remove dead code

Signed-off-by: kim <grufwub@gmail.com>

* remove skip paths code

Signed-off-by: kim <grufwub@gmail.com>

* re-enable log quoting

Signed-off-by: kim <grufwub@gmail.com>

* use human-friendly bytesize in logging body size

Signed-off-by: kim <grufwub@gmail.com>

* only disable quoting in debug builds

Signed-off-by: kim <grufwub@gmail.com>

* use logrus level instead of debug.DEBUG() to enable/disable quoting

Signed-off-by: kim <grufwub@gmail.com>

* shutup linter

Signed-off-by: kim <grufwub@gmail.com>

* fix instance tests

Signed-off-by: kim <grufwub@gmail.com>

* fix gin test contexts created with missing engine HTML renderer

Signed-off-by: kim <grufwub@gmail.com>

* add note regarding not logging query parameters

Signed-off-by: kim <grufwub@gmail.com>

* better explain 'DisableQuoting' logic

Signed-off-by: kim <grufwub@gmail.com>

* add license text

Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
kim 2022-07-12 08:32:20 +01:00 committed by GitHub
commit 6934ae378a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 526 additions and 190 deletions

View file

@ -98,27 +98,29 @@ func (suite *InstanceStandardTestSuite) TearDownTest() {
testrig.StandardStorageTeardown(suite.storage)
}
func (suite *InstanceStandardTestSuite) newContext(recorder *httptest.ResponseRecorder, requestMethod string, requestBody []byte, requestPath string, bodyContentType string) *gin.Context {
ctx, _ := gin.CreateTestContext(recorder)
ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["admin_account"])
ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["admin_account"]))
ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["admin_account"])
ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["admin_account"])
func (suite *InstanceStandardTestSuite) newContext(recorder *httptest.ResponseRecorder, method string, path string, body []byte, contentType string, auth bool) *gin.Context {
protocol := config.GetProtocol()
host := config.GetHost()
baseURI := fmt.Sprintf("%s://%s", protocol, host)
requestURI := fmt.Sprintf("%s/%s", baseURI, requestPath)
requestURI := fmt.Sprintf("%s/%s", baseURI, path)
ctx.Request = httptest.NewRequest(requestMethod, requestURI, bytes.NewReader(requestBody)) // the endpoint we're hitting
req := httptest.NewRequest(method, requestURI, bytes.NewReader(body)) // the endpoint we're hitting
if bodyContentType != "" {
ctx.Request.Header.Set("Content-Type", bodyContentType)
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
ctx.Request.Header.Set("accept", "application/json")
req.Header.Set("accept", "application/json")
ctx, _ := testrig.CreateGinTestContext(recorder, req)
if auth {
ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["admin_account"])
ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["admin_account"]))
ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["admin_account"])
ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["admin_account"])
}
return ctx
}

View file

@ -49,7 +49,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch1() {
// set up the request
recorder := httptest.NewRecorder()
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType())
ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true)
// call the handler
suite.instanceModule.InstanceUpdatePATCHHandler(ctx)
@ -79,7 +79,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch2() {
// set up the request
recorder := httptest.NewRecorder()
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType())
ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true)
// call the handler
suite.instanceModule.InstanceUpdatePATCHHandler(ctx)
@ -109,7 +109,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch3() {
// set up the request
recorder := httptest.NewRecorder()
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType())
ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true)
// call the handler
suite.instanceModule.InstanceUpdatePATCHHandler(ctx)
@ -137,7 +137,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch4() {
// set up the request
recorder := httptest.NewRecorder()
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType())
ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true)
// call the handler
suite.instanceModule.InstanceUpdatePATCHHandler(ctx)
@ -166,7 +166,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch5() {
// set up the request
recorder := httptest.NewRecorder()
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType())
ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true)
ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"])
ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["local_account_1"]))
@ -200,7 +200,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch6() {
// set up the request
recorder := httptest.NewRecorder()
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType())
ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true)
// call the handler
suite.instanceModule.InstanceUpdatePATCHHandler(ctx)
@ -230,7 +230,7 @@ func (suite *InstancePatchTestSuite) TestInstancePatch7() {
// set up the request
recorder := httptest.NewRecorder()
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType())
ctx := suite.newContext(recorder, http.MethodPatch, instance.InstanceInformationPath, bodyBytes, w.FormDataContentType(), true)
// call the handler
suite.instanceModule.InstanceUpdatePATCHHandler(ctx)

View file

@ -26,7 +26,7 @@ import (
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/render"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/api/client/instance"
"github.com/superseriousbusiness/gotosocial/internal/config"
@ -40,7 +40,8 @@ type InstancePeersGetTestSuite struct {
func (suite *InstancePeersGetTestSuite) TestInstancePeersGetNoParams() {
recorder := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(recorder)
ctx, r := testrig.CreateGinTestContext(recorder, nil)
r.HTMLRender = render.HTMLDebug{}
baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost())
requestURI := fmt.Sprintf("%s/%s", baseURI, instance.InstancePeersPath)
@ -63,11 +64,9 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetNoParamsUnauthorized
config.SetInstanceExposePeers(false)
recorder := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(recorder)
baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost())
requestURI := fmt.Sprintf("%s/%s", baseURI, instance.InstancePeersPath)
ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil)
ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false)
suite.instanceModule.InstancePeersGETHandler(ctx)
@ -88,7 +87,7 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetNoParamsAuthorized()
recorder := httptest.NewRecorder()
baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost())
requestURI := fmt.Sprintf("%s/%s", baseURI, instance.InstancePeersPath)
ctx := suite.newContext(recorder, http.MethodGet, []byte{}, requestURI, "")
ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", true)
suite.instanceModule.InstancePeersGETHandler(ctx)
@ -105,11 +104,9 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetNoParamsAuthorized()
func (suite *InstancePeersGetTestSuite) TestInstancePeersGetOnlySuspended() {
recorder := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(recorder)
baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost())
requestURI := fmt.Sprintf("%s/%s?filter=suspended", baseURI, instance.InstancePeersPath)
ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil)
ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false)
suite.instanceModule.InstancePeersGETHandler(ctx)
@ -128,11 +125,9 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetOnlySuspendedUnautho
config.SetInstanceExposeSuspended(false)
recorder := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(recorder)
baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost())
requestURI := fmt.Sprintf("%s/%s?filter=suspended", baseURI, instance.InstancePeersPath)
ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil)
ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false)
suite.instanceModule.InstancePeersGETHandler(ctx)
@ -153,7 +148,7 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetOnlySuspendedAuthori
recorder := httptest.NewRecorder()
baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost())
requestURI := fmt.Sprintf("%s/%s?filter=suspended", baseURI, instance.InstancePeersPath)
ctx := suite.newContext(recorder, http.MethodGet, []byte{}, requestURI, "")
ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", true)
suite.instanceModule.InstancePeersGETHandler(ctx)
@ -170,11 +165,9 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetOnlySuspendedAuthori
func (suite *InstancePeersGetTestSuite) TestInstancePeersGetAll() {
recorder := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(recorder)
baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost())
requestURI := fmt.Sprintf("%s/%s?filter=suspended,open", baseURI, instance.InstancePeersPath)
ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil)
ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false)
suite.instanceModule.InstancePeersGETHandler(ctx)
@ -202,11 +195,9 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetAllWithObfuscated()
suite.NoError(err)
recorder := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(recorder)
baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost())
requestURI := fmt.Sprintf("%s/%s?filter=suspended,open", baseURI, instance.InstancePeersPath)
ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil)
ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false)
suite.instanceModule.InstancePeersGETHandler(ctx)
@ -223,11 +214,9 @@ func (suite *InstancePeersGetTestSuite) TestInstancePeersGetAllWithObfuscated()
func (suite *InstancePeersGetTestSuite) TestInstancePeersGetFunkyParams() {
recorder := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(recorder)
baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost())
requestURI := fmt.Sprintf("%s/%s?filter=aaaaaaaaaaaaaaaaa,open", baseURI, instance.InstancePeersPath)
ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil)
ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", true)
suite.instanceModule.InstancePeersGETHandler(ctx)