| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package spec3_test |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/google/go-cmp/cmp" |
| 24 |
"github.com/stretchr/testify/require" |
| 25 |
|
| 26 |
"k8s.io/kube-openapi/pkg/spec3" |
| 27 |
jsontesting "k8s.io/kube-openapi/pkg/util/jsontesting" |
| 28 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 29 |
) |
| 30 |
|
| 31 |
func TestSecuritySchemeRoundTrip(t *testing.T) { |
| 32 |
cases := []jsontesting.RoundTripTestCase{ |
| 33 |
{ |
| 34 |
Name: "Basic Roundtrip", |
| 35 |
Object: &spec3.SecurityScheme{ |
| 36 |
spec.Refable{Ref: spec.MustCreateRef("Dog")}, |
| 37 |
spec3.SecuritySchemeProps{ |
| 38 |
Description: "foo", |
| 39 |
}, |
| 40 |
spec.VendorExtensible{Extensions: spec.Extensions{ |
| 41 |
"x-framework": "go-swagger", |
| 42 |
}}, |
| 43 |
}, |
| 44 |
}, |
| 45 |
} |
| 46 |
|
| 47 |
for _, tcase := range cases { |
| 48 |
t.Run(tcase.Name, func(t *testing.T) { |
| 49 |
require.NoError(t, tcase.RoundTripTest(&spec3.SecurityScheme{})) |
| 50 |
}) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func TestSecuritySchemaJSONSerialization(t *testing.T) { |
| 55 |
cases := []struct { |
| 56 |
name string |
| 57 |
target *spec3.SecurityScheme |
| 58 |
expectedOutput string |
| 59 |
}{ |
| 60 |
|
| 61 |
{ |
| 62 |
name: "scenario1: basic authentication", |
| 63 |
target: &spec3.SecurityScheme{ |
| 64 |
SecuritySchemeProps: spec3.SecuritySchemeProps{ |
| 65 |
Type: "http", |
| 66 |
Scheme: "basic", |
| 67 |
}, |
| 68 |
}, |
| 69 |
expectedOutput: `{"type":"http","scheme":"basic"}`, |
| 70 |
}, |
| 71 |
|
| 72 |
|
| 73 |
{ |
| 74 |
name: "scenario2: JWT Bearer", |
| 75 |
target: &spec3.SecurityScheme{ |
| 76 |
SecuritySchemeProps: spec3.SecuritySchemeProps{ |
| 77 |
Type: "http", |
| 78 |
Scheme: "basic", |
| 79 |
BearerFormat: "JWT", |
| 80 |
}, |
| 81 |
}, |
| 82 |
expectedOutput: `{"type":"http","scheme":"basic","bearerFormat":"JWT"}`, |
| 83 |
}, |
| 84 |
|
| 85 |
|
| 86 |
{ |
| 87 |
name: "scenario3: implicit OAuth2", |
| 88 |
target: &spec3.SecurityScheme{ |
| 89 |
SecuritySchemeProps: spec3.SecuritySchemeProps{ |
| 90 |
Type: "oauth2", |
| 91 |
Flows: map[string]*spec3.OAuthFlow{ |
| 92 |
"implicit": { |
| 93 |
OAuthFlowProps: spec3.OAuthFlowProps{ |
| 94 |
AuthorizationUrl: "https://example.com/api/oauth/dialog", |
| 95 |
Scopes: map[string]string{ |
| 96 |
"write:pets": "modify pets in your account", |
| 97 |
"read:pets": "read your pets", |
| 98 |
}, |
| 99 |
}, |
| 100 |
}, |
| 101 |
}, |
| 102 |
}, |
| 103 |
}, |
| 104 |
expectedOutput: `{"type":"oauth2","flows":{"implicit":{"authorizationUrl":"https://example.com/api/oauth/dialog","scopes":{"read:pets":"read your pets","write:pets":"modify pets in your account"}}}}`, |
| 105 |
}, |
| 106 |
|
| 107 |
|
| 108 |
{ |
| 109 |
name: "scenario4: reference Object", |
| 110 |
target: &spec3.SecurityScheme{ |
| 111 |
Refable: spec.Refable{Ref: spec.MustCreateRef("k8s.io/api/foo/v1beta1b.bar")}, |
| 112 |
}, |
| 113 |
expectedOutput: `{"$ref":"k8s.io/api/foo/v1beta1b.bar"}`, |
| 114 |
}, |
| 115 |
} |
| 116 |
for _, tc := range cases { |
| 117 |
t.Run(tc.name, func(t *testing.T) { |
| 118 |
rawTarget, err := json.Marshal(tc.target) |
| 119 |
if err != nil { |
| 120 |
t.Fatal(err) |
| 121 |
} |
| 122 |
serializedTarget := string(rawTarget) |
| 123 |
if !cmp.Equal(serializedTarget, tc.expectedOutput) { |
| 124 |
t.Fatalf("diff %s", cmp.Diff(serializedTarget, tc.expectedOutput)) |
| 125 |
} |
| 126 |
}) |
| 127 |
} |
| 128 |
} |