github.com/hyperledger/aries-framework-go@v0.3.2/pkg/doc/verifiable/credential_jwt_test.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  SPDX-License-Identifier: Apache-2.0
     4  */
     5  
     6  package verifiable
     7  
     8  import (
     9  	"errors"
    10  	"testing"
    11  	"time"
    12  
    13  	josejwt "github.com/go-jose/go-jose/v3/jwt"
    14  	"github.com/stretchr/testify/require"
    15  
    16  	"github.com/hyperledger/aries-framework-go/pkg/doc/jwt"
    17  )
    18  
    19  func TestDecodeJWT(t *testing.T) {
    20  	vcBytes, err := decodeCredJWT("", func(string) (*JWTCredClaims, error) {
    21  		return nil, errors.New("cannot parse JWT claims")
    22  	})
    23  	require.Error(t, err)
    24  	require.Contains(t, err.Error(), "cannot parse JWT claims")
    25  	require.Nil(t, vcBytes)
    26  }
    27  
    28  func TestRefineVcFromJwtClaims(t *testing.T) {
    29  	issuerID := "did:example:76e12ec712ebc6f1c221ebfeb1f"
    30  	issued := time.Date(2019, time.August, 10, 0, 0, 0, 0, time.UTC)
    31  	vcID := "http://example.edu/credentials/3732"
    32  	expired := time.Date(2029, time.August, 10, 0, 0, 0, 0, time.UTC)
    33  
    34  	vcMap := map[string]interface{}{
    35  		"issuer": "unknown",
    36  	}
    37  	credClaims := &jwt.Claims{
    38  		Issuer:    issuerID,
    39  		NotBefore: josejwt.NewNumericDate(issued),
    40  		ID:        vcID,
    41  		IssuedAt:  josejwt.NewNumericDate(issued),
    42  		Expiry:    josejwt.NewNumericDate(expired),
    43  	}
    44  
    45  	jwtCredClaims := &JWTCredClaims{
    46  		Claims: credClaims,
    47  		VC:     vcMap,
    48  	}
    49  
    50  	jwtCredClaims.refineFromJWTClaims()
    51  
    52  	require.Equal(t, issuerID, vcMap["issuer"])
    53  	require.Equal(t, "2019-08-10T00:00:00Z", vcMap["issuanceDate"])
    54  	require.Equal(t, "2029-08-10T00:00:00Z", vcMap["expirationDate"])
    55  }