github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/stone/jwt_test.go (about) 1 package stone 2 3 import ( 4 "crypto/rand" 5 "crypto/rsa" 6 "crypto/x509" 7 "encoding/pem" 8 "fmt" 9 "testing" 10 "time" 11 12 "github.com/mundipagg/boleto-api/certificate" 13 "github.com/mundipagg/boleto-api/config" 14 "github.com/mundipagg/boleto-api/mock" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func Test_generateJWT(t *testing.T) { 19 mock.StartMockService("9048") 20 21 pkByte, err := generateTestPK() 22 assert.Nil(t, err) 23 certificate.SetCertificateOnStore(config.Get().AzureStorageOpenBankSkName, pkByte) 24 25 tests := []struct { 26 name string 27 wantErr bool 28 }{ 29 { 30 name: "generate jwt successfully", 31 wantErr: false, 32 }, 33 } 34 for _, tt := range tests { 35 t.Run(tt.name, func(t *testing.T) { 36 got, err := generateJWT() 37 assert.Nil(t, err) 38 assert.Equal(t, (err != nil), tt.wantErr) 39 assert.NotEmpty(t, got) 40 }) 41 } 42 } 43 44 func Test_generateJTIFromTime(t *testing.T) { 45 layout := "2006-01-02T15:04:05.000Z" 46 tStr := "2021-06-24T19:54:26.371Z" 47 expTime, _ := time.Parse(layout, tStr) 48 type args struct { 49 t time.Time 50 } 51 tests := []struct { 52 name string 53 args args 54 want string 55 }{ 56 { 57 name: "jwt generations", 58 args: args{expTime}, 59 want: "20210624195426371", 60 }, 61 } 62 for _, tt := range tests { 63 t.Run(tt.name, func(t *testing.T) { 64 assert.Contains(t, generateJTIFromTime(tt.args.t), tt.want) 65 }) 66 } 67 } 68 69 func generateTestPK() ([]byte, error) { 70 // generate key 71 privatekey, err := rsa.GenerateKey(rand.Reader, 2048) 72 if err != nil { 73 return []byte(""), fmt.Errorf("Cannot generate RSA key") 74 } 75 76 privateKeyBlock := &pem.Block{ 77 Type: "RSA PRIVATE KEY", 78 Bytes: x509.MarshalPKCS1PrivateKey(privatekey), 79 } 80 81 return pem.EncodeToMemory(privateKeyBlock), nil 82 }