Skip to content

Commit

Permalink
test: FromPrivateKey, FromPublicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Lodek committed Jun 11, 2024
1 parent 9308ad2 commit d9cf7d6
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions acp/identity/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,47 @@ func Test_DIDFromPublicKey_ProducesDIDForPublicKey(t *testing.T) {
}

func Test_DIDFromPublicKey_ReturnsErrorWhenProducerFails(t *testing.T) {
// pre: replace the producer function
didProducer = func(kt crypto.KeyType, publicKey []byte) (*key.DIDKey, error) {
return nil, fmt.Errorf("some did generation error")
}
execTestWithMockecProducer(
func() {
pubKey := &secp256k1.PublicKey{}
did, err := DIDFromPublicKey(pubKey)

pubKey := &secp256k1.PublicKey{}
require.Empty(t, did)
require.ErrorIs(t, err, ErrDIDCreation)
},
)
}

did, err := DIDFromPublicKey(pubKey)
func Test_FromPublicKey_ProducerFailureCausesError(t *testing.T) {
execTestWithMockecProducer(
func() {
pubKey := &secp256k1.PublicKey{}
identity, err := FromPublicKey(pubKey)

require.Empty(t, did)
require.ErrorIs(t, err, ErrDIDCreation)
require.Equal(t, None, identity)
require.ErrorIs(t, err, ErrDIDCreation)
},
)
}

func Test_FromPrivateKey_ProducerFailureCausesError(t *testing.T) {
execTestWithMockecProducer(
func() {
key := &secp256k1.PrivateKey{}
identity, err := FromPrivateKey(key)

require.Equal(t, None, identity)
require.ErrorIs(t, err, ErrDIDCreation)
},
)
}

func execTestWithMockecProducer(test func()) {
// pre: replace the producer function
didProducer = func(kt crypto.KeyType, publicKey []byte) (*key.DIDKey, error) {
return nil, fmt.Errorf("some did generation error")
}
test()
// post: restore producer function
didProducer = key.CreateDIDKey
}

0 comments on commit d9cf7d6

Please sign in to comment.