summaryrefslogtreecommitdiffstats
path: root/tests/integration/integration_test.go
diff options
context:
space:
mode:
authorMeisam <39205857+MFTabriz@users.noreply.github.com>2022-12-17 07:22:34 +0100
committerGitHub <noreply@github.com>2022-12-17 01:22:34 -0500
commitf3370eeaeefecdcc06940edd0377264a5c0c7212 (patch)
tree54369a301acb00acd7fb5dc77d466f563247e3eb /tests/integration/integration_test.go
parentc4c4151f7d7a6aa18b354ae45c6ed93570d5de77 (diff)
downloadgitea-f3370eeaeefecdcc06940edd0377264a5c0c7212.tar.gz
gitea-f3370eeaeefecdcc06940edd0377264a5c0c7212.zip
verify nodeinfo response by schema (#22137)
... using [github.com/xeipuuv/gojsonschema](https://github.com/xeipuuv/gojsonschema) Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'tests/integration/integration_test.go')
-rw-r--r--tests/integration/integration_test.go20
1 files changed, 20 insertions, 0 deletions
diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go
index 9bf4c38ee0..d5fbb393b5 100644
--- a/tests/integration/integration_test.go
+++ b/tests/integration/integration_test.go
@@ -33,6 +33,7 @@ import (
"github.com/PuerkitoBio/goquery"
"github.com/stretchr/testify/assert"
+ "github.com/xeipuuv/gojsonschema"
)
var c *web.Route
@@ -398,6 +399,25 @@ func DecodeJSON(t testing.TB, resp *httptest.ResponseRecorder, v interface{}) {
assert.NoError(t, decoder.Decode(v))
}
+func VerifyJSONSchema(t testing.TB, resp *httptest.ResponseRecorder, schemaFile string) {
+ t.Helper()
+
+ schemaFilePath := filepath.Join(filepath.Dir(setting.AppPath), "tests", "integration", "schemas", schemaFile)
+ _, schemaFileErr := os.Stat(schemaFilePath)
+ assert.Nil(t, schemaFileErr)
+
+ schema, schemaFileReadErr := os.ReadFile(schemaFilePath)
+ assert.Nil(t, schemaFileReadErr)
+ assert.True(t, len(schema) > 0)
+
+ nodeinfoSchema := gojsonschema.NewStringLoader(string(schema))
+ nodeinfoString := gojsonschema.NewStringLoader(resp.Body.String())
+ result, schemaValidationErr := gojsonschema.Validate(nodeinfoSchema, nodeinfoString)
+ assert.Nil(t, schemaValidationErr)
+ assert.Empty(t, result.Errors())
+ assert.True(t, result.Valid())
+}
+
func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
t.Helper()
req := NewRequest(t, "GET", urlStr)