forked from ory/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator_test.go
78 lines (71 loc) · 2.34 KB
/
validator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package schema
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/ory/jsonschema/v3/httploader"
"github.com/ory/x/httpx"
"github.com/julienschmidt/httprouter"
"github.com/stretchr/testify/require"
"github.com/ory/x/stringsx"
)
func TestSchemaValidator(t *testing.T) {
router := httprouter.New()
fs := http.StripPrefix("/schema", http.FileServer(http.Dir("stub/validator")))
router.GET("/schema/:name", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fs.ServeHTTP(w, r)
})
ts := httptest.NewServer(router)
defer ts.Close()
ctx := context.WithValue(ctx, httploader.ContextKey, httpx.NewResilientClient())
for k, tc := range []struct {
i json.RawMessage
err string
u string
}{
{
i: json.RawMessage(`{ "firstName": "first-name", "lastName": "last-name", "age": 1 }`),
},
{
i: json.RawMessage(`{ "firstName": "first-name", "lastName": "last-name", "age": -1 }`),
err: "I[#/age] S[#/properties/age/minimum] must be >= 1 but found -1",
},
{
i: json.RawMessage(`{ "whatever": "first-name", "lastName": "last-name", "age": 1 }`),
err: `I[#] S[#/additionalProperties] additionalProperties "whatever" not allowed`,
},
{
u: ts.URL + "/schema/whatever.schema.json",
i: json.RawMessage(`{ "whatever": "first-name", "lastName": "last-name", "age": 1 }`),
},
{
u: ts.URL + "/schema/whatever.schema.json",
i: json.RawMessage(`{ "firstName": "first-name", "lastName": "last-name", "age": 1 }`),
err: `I[#] S[#/additionalProperties] additionalProperties "firstName" not allowed`,
},
{
u: ts.URL,
i: json.RawMessage(`{ "firstName": "first-name", "lastName": "last-name", "age": 1 }`),
err: "An internal server error occurred, please contact the system administrator",
},
{
u: "not-a-url",
i: json.RawMessage(`{ "firstName": "first-name", "lastName": "last-name", "age": 1 }`),
err: "An internal server error occurred, please contact the system administrator",
},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
err := NewValidator().Validate(ctx, stringsx.Coalesce(tc.u, ts.URL+"/schema/firstName.schema.json"), tc.i)
if tc.err == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, tc.err)
}
})
}
}