| 1 | package token |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/smallstep/cli/jose" |
| 9 | ) |
| 10 | |
| 11 | func TestOptions(t *testing.T) { |
| 12 | empty := new(Claims) |
| 13 | now := time.Now() |
| 14 | tests := []struct { |
| 15 | name string |
| 16 | option Options |
| 17 | want *Claims |
| 18 | wantErr bool |
| 19 | }{ |
| 20 | {"WithClaim ok", WithClaim("name", "foo"), &Claims{ExtraClaims: map[string]interface{}{"name": "foo"}}, false}, |
| 21 | {"WithClaim fail", WithClaim("", "foo"), empty, true}, |
| 22 | {"WithRootCA ok", WithRootCA("../crypto/pemutil/testdata/ca.crt"), &Claims{ExtraClaims: map[string]interface{}{"sha": "6908751f68290d4573ae0be39a98c8b9b7b7d4e8b2a6694b7509946626adfe98"}}, false}, |
| 23 | {"WithRootCA fail", WithRootCA("not-exists"), empty, true}, |
| 24 | {"WithValidity ok", WithValidity(now, now.Add(5*time.Minute)), &Claims{Claims: jose.Claims{NotBefore: jose.NewNumericDate(now), Expiry: jose.NewNumericDate(now.Add(5 * time.Minute))}}, false}, |
| 25 | {"WithRootCA expired", WithValidity(now, now.Add(-1*time.Second)), empty, true}, |
| 26 | {"WithRootCA long delay", WithValidity(now.Add(MaxValidityDelay+time.Minute), now.Add(MaxValidityDelay+10*time.Minute)), empty, true}, |
| 27 | {"WithRootCA min validity ok", WithValidity(now, now.Add(MinValidity)), &Claims{Claims: jose.Claims{NotBefore: jose.NewNumericDate(now), Expiry: jose.NewNumericDate(now.Add(MinValidity))}}, false}, |
| 28 | {"WithRootCA min validity fail", WithValidity(now, now.Add(MinValidity-time.Second)), empty, true}, |
| 29 | {"WithRootCA max validity ok", WithValidity(now, now.Add(MaxValidity)), &Claims{Claims: jose.Claims{NotBefore: jose.NewNumericDate(now), Expiry: jose.NewNumericDate(now.Add(MaxValidity))}}, false}, |
| 30 | {"WithRootCA max validity fail", WithValidity(now, now.Add(MaxValidity+time.Second)), empty, true}, |
| 31 | {"WithIssuer ok", WithIssuer("value"), &Claims{Claims: jose.Claims{Issuer: "value"}}, false}, |
| 32 | {"WithIssuer fail", WithIssuer(""), empty, true}, |
| 33 | {"WithSubject ok", WithSubject("value"), &Claims{Claims: jose.Claims{Subject: "value"}}, false}, |
| 34 | {"WithSubject fail", WithSubject(""), empty, true}, |
| 35 | {"WithAudience ok", WithAudience("value"), &Claims{Claims: jose.Claims{Audience: jose.Audience{"value"}}}, false}, |
| 36 | {"WithAudience fail", WithAudience(""), empty, true}, |
| 37 | {"WithJWTID ok", WithJWTID("value"), &Claims{Claims: jose.Claims{ID: "value"}}, false}, |
| 38 | {"WithJWTID fail", WithJWTID(""), empty, true}, |
| 39 | {"WithKid ok", WithKid("value"), &Claims{ExtraHeaders: map[string]interface{}{"kid": "value"}}, false}, |
| 40 | {"WithKid fail", WithKid(""), empty, true}, |
| 41 | {"WithSHA ok", WithSHA("6908751f68290d4573ae0be39a98c8b9b7b7d4e8b2a6694b7509946626adfe98"), &Claims{ExtraClaims: map[string]interface{}{"sha": "6908751f68290d4573ae0be39a98c8b9b7b7d4e8b2a6694b7509946626adfe98"}}, false}, |
| 42 | } |
| 43 | |
| 44 | for _, tt := range tests { |
| 45 | t.Run(tt.name, func(t *testing.T) { |
| 46 | claim := new(Claims) |
| 47 | err := tt.option(claim) |
| 48 | if (err != nil) != tt.wantErr { |
| 49 | t.Errorf("Options error = %v, wantErr %v", err, tt.wantErr) |
| 50 | return |
| 51 | } |
| 52 | if !reflect.DeepEqual(claim, tt.want) { |
| 53 | t.Errorf("Options claims = %v, want %v", claim, tt.want) |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | } |