github.com/nats-io/jwt/v2@v2.5.6/v1compat/genericclaims_test.go (about)

     1  /*
     2   * Copyright 2018 The NATS Authors
     3   * Licensed under the Apache License, Version 2.0 (the "License");
     4   * you may not use this file except in compliance with the License.
     5   * You may obtain a copy of the License at
     6   *
     7   * http://www.apache.org/licenses/LICENSE-2.0
     8   *
     9   * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15  
    16  package jwt
    17  
    18  import (
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  func TestNewGenericClaims(t *testing.T) {
    24  	akp := createAccountNKey(t)
    25  	apk := publicKey(akp, t)
    26  
    27  	uc := NewGenericClaims(apk)
    28  	uc.Expires = time.Now().Add(time.Duration(time.Hour)).UTC().Unix()
    29  	uc.Name = "alberto"
    30  	uc.Audience = "everyone"
    31  	uc.NotBefore = time.Now().UTC().Unix()
    32  	uc.Tags.Add("one")
    33  	uc.Tags.Add("one")
    34  	uc.Tags.Add("one")
    35  	uc.Tags.Add("TWO") // should become lower case
    36  	uc.Tags.Add("three")
    37  
    38  	uJwt := encode(uc, akp, t)
    39  
    40  	uc2, err := DecodeGeneric(uJwt)
    41  	if err != nil {
    42  		t.Fatal("failed to decode", err)
    43  	}
    44  
    45  	AssertEquals(uc.String(), uc2.String(), t)
    46  	AssertEquals(uc.Name, uc2.Name, t)
    47  	AssertEquals(uc.Audience, uc2.Audience, t)
    48  	AssertEquals(uc.Expires, uc2.Expires, t)
    49  	AssertEquals(uc.NotBefore, uc2.NotBefore, t)
    50  	AssertEquals(uc.Subject, uc2.Subject, t)
    51  
    52  	AssertEquals(3, len(uc2.Tags), t)
    53  	AssertEquals(true, uc2.Tags.Contains("two"), t)
    54  	AssertEquals("one", uc2.Tags[0], t)
    55  	AssertEquals("two", uc2.Tags[1], t)
    56  	AssertEquals("three", uc2.Tags[2], t)
    57  
    58  	AssertEquals(uc.Claims() != nil, true, t)
    59  	AssertEquals(uc.Payload() != nil, true, t)
    60  }