dex/vendor/github.com/ericchiang/oidc/oidc_test.go

77 lines
1.4 KiB
Go
Raw Normal View History

2016-07-26 01:30:28 +05:30
package oidc
2016-08-09 00:15:51 +05:30
import (
"encoding/json"
"reflect"
"testing"
)
2016-07-26 01:30:28 +05:30
func TestClientVerifier(t *testing.T) {
tests := []struct {
clientID string
2016-08-09 00:15:51 +05:30
aud []string
2016-07-26 01:30:28 +05:30
wantErr bool
}{
{
clientID: "1",
2016-08-09 00:15:51 +05:30
aud: []string{"1"},
2016-07-26 01:30:28 +05:30
},
{
clientID: "1",
2016-08-09 00:15:51 +05:30
aud: []string{"2"},
2016-07-26 01:30:28 +05:30
wantErr: true,
},
{
clientID: "1",
2016-08-09 00:15:51 +05:30
aud: []string{"2", "1"},
2016-07-26 01:30:28 +05:30
},
{
clientID: "3",
2016-08-09 00:15:51 +05:30
aud: []string{"1", "2"},
2016-07-26 01:30:28 +05:30
wantErr: true,
},
}
for i, tc := range tests {
2016-08-09 00:15:51 +05:30
token := IDToken{Audience: tc.aud}
err := (clientVerifier{tc.clientID}).verifyIDToken(&token)
2016-07-26 01:30:28 +05:30
if err != nil && !tc.wantErr {
t.Errorf("case %d: %v", i)
}
if err == nil && tc.wantErr {
t.Errorf("case %d: expected error")
}
}
}
2016-08-09 00:15:51 +05:30
func TestUnmarshalAudience(t *testing.T) {
tests := []struct {
data string
want audience
wantErr bool
}{
{`"foo"`, audience{"foo"}, false},
{`["foo","bar"]`, audience{"foo", "bar"}, false},
{"foo", nil, true}, // invalid JSON
}
for _, tc := range tests {
var a audience
if err := json.Unmarshal([]byte(tc.data), &a); err != nil {
if !tc.wantErr {
t.Errorf("failed to unmarshal %q: %v", tc.data, err)
}
continue
}
if tc.wantErr {
t.Errorf("did not expected to be able to unmarshal %q", tc.data)
continue
}
if !reflect.DeepEqual(tc.want, a) {
t.Errorf("from %q expected %q got %q", tc.data, tc.want, a)
}
}
}