diff --git a/acp/identity/identity_test.go b/acp/identity/identity_test.go index bef501948f..508a1bbc6c 100644 --- a/acp/identity/identity_test.go +++ b/acp/identity/identity_test.go @@ -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 }