| 1 |
package oidc |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/stretchr/testify/assert" |
| 8 |
"github.com/stretchr/testify/require" |
| 9 |
) |
| 10 |
|
| 11 |
type jsonErrorTest struct{} |
| 12 |
|
| 13 |
func (jsonErrorTest) MarshalJSON() ([]byte, error) { |
| 14 |
return nil, errors.New("test") |
| 15 |
} |
| 16 |
|
| 17 |
func Test_mergeAndMarshalClaims(t *testing.T) { |
| 18 |
type args struct { |
| 19 |
registered any |
| 20 |
claims map[string]any |
| 21 |
} |
| 22 |
tests := []struct { |
| 23 |
name string |
| 24 |
args args |
| 25 |
want string |
| 26 |
wantErr bool |
| 27 |
}{ |
| 28 |
{ |
| 29 |
name: "encoder error", |
| 30 |
args: args{ |
| 31 |
registered: jsonErrorTest{}, |
| 32 |
}, |
| 33 |
wantErr: true, |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: "no claims", |
| 37 |
args: args{ |
| 38 |
registered: struct { |
| 39 |
Foo string `json:"foo,omitempty"` |
| 40 |
}{ |
| 41 |
Foo: "bar", |
| 42 |
}, |
| 43 |
}, |
| 44 |
want: "{\"foo\":\"bar\"}\n", |
| 45 |
}, |
| 46 |
{ |
| 47 |
name: "with claims", |
| 48 |
args: args{ |
| 49 |
registered: struct { |
| 50 |
Foo string `json:"foo,omitempty"` |
| 51 |
}{ |
| 52 |
Foo: "bar", |
| 53 |
}, |
| 54 |
claims: map[string]any{ |
| 55 |
"bar": "foo", |
| 56 |
}, |
| 57 |
}, |
| 58 |
want: "{\"bar\":\"foo\",\"foo\":\"bar\"}\n", |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "registered overwrites custom", |
| 62 |
args: args{ |
| 63 |
registered: struct { |
| 64 |
Foo string `json:"foo,omitempty"` |
| 65 |
}{ |
| 66 |
Foo: "bar", |
| 67 |
}, |
| 68 |
claims: map[string]any{ |
| 69 |
"foo": "Hello, World!", |
| 70 |
}, |
| 71 |
}, |
| 72 |
want: "{\"foo\":\"bar\"}\n", |
| 73 |
}, |
| 74 |
} |
| 75 |
for _, tt := range tests { |
| 76 |
t.Run(tt.name, func(t *testing.T) { |
| 77 |
got, err := mergeAndMarshalClaims(tt.args.registered, tt.args.claims) |
| 78 |
if tt.wantErr { |
| 79 |
require.Error(t, err) |
| 80 |
} else { |
| 81 |
require.NoError(t, err) |
| 82 |
} |
| 83 |
assert.Equal(t, tt.want, string(got)) |
| 84 |
}) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func Test_unmarshalJSONMulti(t *testing.T) { |
| 89 |
type dst struct { |
| 90 |
Foo string `json:"foo,omitempty"` |
| 91 |
} |
| 92 |
|
| 93 |
type args struct { |
| 94 |
data string |
| 95 |
destinations []any |
| 96 |
} |
| 97 |
tests := []struct { |
| 98 |
name string |
| 99 |
args args |
| 100 |
want []any |
| 101 |
wantErr bool |
| 102 |
}{ |
| 103 |
{ |
| 104 |
name: "error", |
| 105 |
args: args{ |
| 106 |
data: "~!~~", |
| 107 |
destinations: []any{ |
| 108 |
&dst{}, |
| 109 |
&map[string]any{}, |
| 110 |
}, |
| 111 |
}, |
| 112 |
want: []any{ |
| 113 |
&dst{}, |
| 114 |
&map[string]any{}, |
| 115 |
}, |
| 116 |
wantErr: true, |
| 117 |
}, |
| 118 |
{ |
| 119 |
name: "success", |
| 120 |
args: args{ |
| 121 |
data: "{\"bar\":\"foo\",\"foo\":\"bar\"}\n", |
| 122 |
destinations: []any{ |
| 123 |
&dst{}, |
| 124 |
&map[string]any{}, |
| 125 |
}, |
| 126 |
}, |
| 127 |
want: []any{ |
| 128 |
&dst{Foo: "bar"}, |
| 129 |
&map[string]any{ |
| 130 |
"foo": "bar", |
| 131 |
"bar": "foo", |
| 132 |
}, |
| 133 |
}, |
| 134 |
}, |
| 135 |
} |
| 136 |
for _, tt := range tests { |
| 137 |
t.Run(tt.name, func(t *testing.T) { |
| 138 |
err := unmarshalJSONMulti([]byte(tt.args.data), tt.args.destinations...) |
| 139 |
if tt.wantErr { |
| 140 |
require.Error(t, err) |
| 141 |
} else { |
| 142 |
require.NoError(t, err) |
| 143 |
} |
| 144 |
assert.Equal(t, tt.want, tt.args.destinations) |
| 145 |
}) |
| 146 |
} |
| 147 |
} |