This repository has been archived on 2023-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
dex/vendor/github.com/ericchiang/oidc/oidc_test.go

77 lines
1.4 KiB
Go
Raw Normal View History

2016-07-25 20:00:28 +00:00
package oidc
2016-08-08 18:45:51 +00:00
import (
"encoding/json"
"reflect"
"testing"
)
2016-07-25 20:00:28 +00:00
func TestClientVerifier(t *testing.T) {
tests := []struct {
clientID string
2016-08-08 18:45:51 +00:00
aud []string
2016-07-25 20:00:28 +00:00
wantErr bool
}{
{
clientID: "1",
2016-08-08 18:45:51 +00:00
aud: []string{"1"},
2016-07-25 20:00:28 +00:00
},
{
clientID: "1",
2016-08-08 18:45:51 +00:00
aud: []string{"2"},
2016-07-25 20:00:28 +00:00
wantErr: true,
},
{
clientID: "1",
2016-08-08 18:45:51 +00:00
aud: []string{"2", "1"},
2016-07-25 20:00:28 +00:00
},
{
clientID: "3",
2016-08-08 18:45:51 +00:00
aud: []string{"1", "2"},
2016-07-25 20:00:28 +00:00
wantErr: true,
},
}
for i, tc := range tests {
2016-08-08 18:45:51 +00:00
token := IDToken{Audience: tc.aud}
err := (clientVerifier{tc.clientID}).verifyIDToken(&token)
2016-07-25 20:00:28 +00:00
if err != nil && !tc.wantErr {
t.Errorf("case %d: %v", i)
}
if err == nil && tc.wantErr {
t.Errorf("case %d: expected error")
}
}
}
2016-08-08 18:45:51 +00:00
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)
}
}
}