-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # go.sum # internal/cmd/serve.go # internal/http/http.go
- Loading branch information
Showing
19 changed files
with
758 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package signer | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"strings" | ||
) | ||
|
||
type Signer interface { | ||
Sign(data io.Reader) ([]byte, error) | ||
GetPublicKey(format string) ([]byte, error) | ||
} | ||
|
||
type LocalSigner struct { | ||
Signer | ||
} | ||
|
||
func (s *LocalSigner) Sign(ctx context.Context, data string) (string, error) { | ||
signed, err := s.Signer.Sign(strings.NewReader(data)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(signed), nil | ||
} | ||
|
||
func (s *LocalSigner) GetPublicKey(ctx context.Context, format string) (string, error) { | ||
publicKey, err := s.Signer.GetPublicKey(format) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(publicKey), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package signer | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type SignerMock struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *SignerMock) Sign(data io.Reader) ([]byte, error) { | ||
args := m.Called(data) | ||
var result []byte | ||
if casted, ok := args.Get(0).([]byte); ok { | ||
result = casted | ||
} | ||
|
||
return result, args.Error(1) | ||
} | ||
|
||
func (m *SignerMock) GetPublicKey(format string) ([]byte, error) { | ||
args := m.Called(format) | ||
var result []byte | ||
if casted, ok := args.Get(0).([]byte); ok { | ||
result = casted | ||
} | ||
|
||
return result, args.Error(1) | ||
} | ||
|
||
type LocalSignerServiceTestSuite struct { | ||
suite.Suite | ||
|
||
Service *LocalSigner | ||
|
||
Signer *SignerMock | ||
} | ||
|
||
func (t *LocalSignerServiceTestSuite) SetupSubTest() { | ||
t.Signer = &SignerMock{} | ||
|
||
t.Service = &LocalSigner{ | ||
Signer: t.Signer, | ||
} | ||
} | ||
|
||
func (t *LocalSignerServiceTestSuite) TearDownSubTest() { | ||
t.Signer.AssertExpectations(t.T()) | ||
} | ||
|
||
func (t *LocalSignerServiceTestSuite) TestSign() { | ||
t.Run("successfully sign", func() { | ||
signature := []byte("mock signature") | ||
t.Signer.On("Sign", mock.Anything).Return(signature, nil).Run(func(args mock.Arguments) { | ||
r, _ := io.ReadAll(args.Get(0).(io.Reader)) | ||
t.Equal([]byte("mock body to sign"), r) | ||
}) | ||
|
||
result, err := t.Service.Sign(context.Background(), "mock body to sign") | ||
t.NoError(err) | ||
t.Equal(string(signature), result) | ||
}) | ||
|
||
t.Run("handle error during sign", func() { | ||
expectedErr := errors.New("mock error") | ||
t.Signer.On("Sign", mock.Anything).Return(nil, expectedErr) | ||
|
||
result, err := t.Service.Sign(context.Background(), "mock body to sign") | ||
t.Error(err) | ||
t.Same(expectedErr, err) | ||
t.Empty(result) | ||
}) | ||
} | ||
|
||
func (t *LocalSignerServiceTestSuite) TestGetPublicKey() { | ||
t.Run("successfully get", func() { | ||
publicKey := []byte("mock public key") | ||
t.Signer.On("GetPublicKey", "pem").Return(publicKey, nil) | ||
|
||
result, err := t.Service.GetPublicKey(context.Background(), "pem") | ||
t.NoError(err) | ||
t.Equal(string(publicKey), result) | ||
}) | ||
|
||
t.Run("handle error", func() { | ||
expectedErr := errors.New("mock error") | ||
t.Signer.On("GetPublicKey", "pem").Return(nil, expectedErr) | ||
|
||
result, err := t.Service.GetPublicKey(context.Background(), "pem") | ||
t.Error(err) | ||
t.Same(expectedErr, err) | ||
t.Empty(result) | ||
}) | ||
} | ||
|
||
func TestLocalSignerService(t *testing.T) { | ||
suite.Run(t, new(LocalSignerServiceTestSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.