File: identity_test.go

package info (click to toggle)
golang-github-coreos-go-oidc 0.0~git20160926.0.16c5ecc-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 436 kB
  • sloc: sh: 40; makefile: 5
file content (113 lines) | stat: -rw-r--r-- 2,309 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package oidc

import (
	"reflect"
	"testing"
	"time"

	"github.com/coreos/go-oidc/jose"
)

func TestIdentityFromClaims(t *testing.T) {
	tests := []struct {
		claims jose.Claims
		want   Identity
	}{
		{
			claims: jose.Claims{
				"sub":   "123850281",
				"name":  "Elroy",
				"email": "elroy@example.com",
				"exp":   float64(1.416935146e+09),
			},
			want: Identity{
				ID:        "123850281",
				Name:      "",
				Email:     "elroy@example.com",
				ExpiresAt: time.Date(2014, time.November, 25, 17, 05, 46, 0, time.UTC),
			},
		},
		{
			claims: jose.Claims{
				"sub":  "123850281",
				"name": "Elroy",
				"exp":  float64(1.416935146e+09),
			},
			want: Identity{
				ID:        "123850281",
				Name:      "",
				Email:     "",
				ExpiresAt: time.Date(2014, time.November, 25, 17, 05, 46, 0, time.UTC),
			},
		},
		{
			claims: jose.Claims{
				"sub":   "123850281",
				"name":  "Elroy",
				"email": "elroy@example.com",
				"exp":   int64(1416935146),
			},
			want: Identity{
				ID:        "123850281",
				Name:      "",
				Email:     "elroy@example.com",
				ExpiresAt: time.Date(2014, time.November, 25, 17, 05, 46, 0, time.UTC),
			},
		},
		{
			claims: jose.Claims{
				"sub":   "123850281",
				"name":  "Elroy",
				"email": "elroy@example.com",
			},
			want: Identity{
				ID:        "123850281",
				Name:      "",
				Email:     "elroy@example.com",
				ExpiresAt: time.Time{},
			},
		},
	}

	for i, tt := range tests {
		got, err := IdentityFromClaims(tt.claims)
		if err != nil {
			t.Errorf("case %d: unexpected error: %v", i, err)
			continue
		}
		if !reflect.DeepEqual(tt.want, *got) {
			t.Errorf("case %d: want=%#v got=%#v", i, tt.want, *got)
		}
	}
}

func TestIdentityFromClaimsFail(t *testing.T) {
	tests := []jose.Claims{
		// sub incorrect type
		jose.Claims{
			"sub":   123,
			"name":  "foo",
			"email": "elroy@example.com",
		},
		// email incorrect type
		jose.Claims{
			"sub":   "123850281",
			"name":  "Elroy",
			"email": false,
		},
		// exp incorrect type
		jose.Claims{
			"sub":   "123850281",
			"name":  "Elroy",
			"email": "elroy@example.com",
			"exp":   "2014-11-25 18:05:46 +0000 UTC",
		},
	}

	for i, tt := range tests {
		_, err := IdentityFromClaims(tt)
		if err == nil {
			t.Errorf("case %d: expected non-nil error", i)
		}
	}
}