Skip to content

Commit

Permalink
feat(receiver): make type immutable (#152)
Browse files Browse the repository at this point in the history
* feat(receiver): make type immutable

* chore: update proto
  • Loading branch information
mabdh authored Nov 21, 2022
1 parent a4fdf9f commit 193787a
Show file tree
Hide file tree
Showing 9 changed files with 613 additions and 584 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NAME="github.com/odpf/siren"
LAST_COMMIT := $(shell git rev-parse --short HEAD)
LAST_TAG := "$(shell git rev-list --tags --max-count=1)"
APP_VERSION := "$(shell git describe --tags ${LAST_TAG})-next"
PROTON_COMMIT := "45cc543ff18d6efd6d8a39947018c828599b476b"
PROTON_COMMIT := "f21015688d165bec2d859c6fca284754dd81755f"

.PHONY: all build test clean dist vet proto install

Expand Down
8 changes: 8 additions & 0 deletions core/receiver/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ func (s *Service) Get(ctx context.Context, id uint64) (*Receiver, error) {
}

func (s *Service) Update(ctx context.Context, rcv *Receiver) error {
rcv, err := s.repository.Get(ctx, rcv.ID)
if err != nil {
if errors.As(err, new(NotFoundError)) {
return errors.ErrNotFound.WithMsgf(err.Error())
}
return err
}

receiverPlugin, err := s.getReceiverPlugin(rcv.Type)
if err != nil {
return err
Expand Down
46 changes: 44 additions & 2 deletions core/receiver/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,17 +396,38 @@ func TestService_UpdateReceiver(t *testing.T) {
var (
ctx = context.TODO()
testCases = []testCase{
{
Description: "should return error if get receiver return error",
Setup: func(rr *mocks.ReceiverRepository, ss *mocks.ConfigResolver) {
rr.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(nil, errors.New("some error"))
},
Rcv: &receiver.Receiver{
ID: 123,
},
Err: errors.New("some error"),
},
{
Description: "should return error if type unknown",
Setup: func(rr *mocks.ReceiverRepository, ss *mocks.ConfigResolver) {},
Setup: func(rr *mocks.ReceiverRepository, ss *mocks.ConfigResolver) {
rr.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&receiver.Receiver{
Type: "random",
}, nil)
},
Rcv: &receiver.Receiver{
Type: "random",
ID: 123,
},
Err: errors.New("unsupported receiver type: \"random\""),
},
{
Description: "should return error if PreHookDBTransformConfigs return error",
Setup: func(rr *mocks.ReceiverRepository, ss *mocks.ConfigResolver) {
rr.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&receiver.Receiver{
ID: 123,
Type: receiver.TypeSlack,
Configurations: map[string]interface{}{
"token": "key",
},
}, nil)
ss.EXPECT().PreHookDBTransformConfigs(mock.AnythingOfType("*context.emptyCtx"), map[string]interface{}{"token": "key"}).Return(nil, errors.New("some error"))
},
Rcv: &receiver.Receiver{
Expand All @@ -421,6 +442,13 @@ func TestService_UpdateReceiver(t *testing.T) {
{
Description: "should return error if Update repository return error",
Setup: func(rr *mocks.ReceiverRepository, ss *mocks.ConfigResolver) {
rr.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&receiver.Receiver{
ID: 123,
Type: receiver.TypeSlack,
Configurations: map[string]interface{}{
"token": "key",
},
}, nil)
ss.EXPECT().PreHookDBTransformConfigs(mock.AnythingOfType("*context.emptyCtx"), map[string]interface{}{"token": "key"}).Return(map[string]interface{}{
"token": "encrypted_key",
}, nil)
Expand All @@ -444,6 +472,13 @@ func TestService_UpdateReceiver(t *testing.T) {
{
Description: "should return nil error if no error returned",
Setup: func(rr *mocks.ReceiverRepository, ss *mocks.ConfigResolver) {
rr.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&receiver.Receiver{
ID: 123,
Type: receiver.TypeSlack,
Configurations: map[string]interface{}{
"token": "key",
},
}, nil)
ss.EXPECT().PreHookDBTransformConfigs(mock.AnythingOfType("*context.emptyCtx"), map[string]interface{}{"token": "key"}).Return(map[string]interface{}{
"token": "encrypted_key",
}, nil)
Expand All @@ -466,6 +501,13 @@ func TestService_UpdateReceiver(t *testing.T) {
}, {
Description: "should return error not found if repository return not found error",
Setup: func(rr *mocks.ReceiverRepository, ss *mocks.ConfigResolver) {
rr.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&receiver.Receiver{
ID: 123,
Type: receiver.TypeSlack,
Configurations: map[string]interface{}{
"token": "key",
},
}, nil)
ss.EXPECT().PreHookDBTransformConfigs(mock.AnythingOfType("*context.emptyCtx"), map[string]interface{}{"token": "key"}).Return(map[string]interface{}{
"token": "encrypted_key",
}, nil)
Expand Down
5 changes: 0 additions & 5 deletions internal/api/v1beta1/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,9 @@ func (s *GRPCServer) GetReceiver(ctx context.Context, req *sirenv1beta1.GetRecei
}

func (s *GRPCServer) UpdateReceiver(ctx context.Context, req *sirenv1beta1.UpdateReceiverRequest) (*sirenv1beta1.UpdateReceiverResponse, error) {
if !receiver.IsTypeSupported(req.GetType()) {
return nil, s.generateRPCErr(errors.ErrInvalid.WithMsgf("unsupported type %s", req.GetType()))
}

rcv := &receiver.Receiver{
ID: req.GetId(),
Name: req.GetName(),
Type: req.GetType(),
Labels: req.GetLabels(),
Configurations: req.GetConfigurations().AsMap(),
}
Expand Down
2 changes: 0 additions & 2 deletions internal/api/v1beta1/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,12 @@ func TestGRPCServer_UpdateReceiver(t *testing.T) {
dummyReq := &sirenv1beta1.UpdateReceiverRequest{
Id: uint64(22),
Name: "foo",
Type: "slack",
Labels: labels,
Configurations: configurationsData,
}
payload := &receiver.Receiver{
ID: uint64(22),
Name: "foo",
Type: "slack",
Labels: labels,
Configurations: configurations,
}
Expand Down
3 changes: 1 addition & 2 deletions internal/store/postgres/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RETURNING *
`

const receiverUpdateQuery = `
UPDATE receivers SET name=$2, type=$3, labels=$4, configurations=$5, updated_at=now()
UPDATE receivers SET name=$2, labels=$3, configurations=$4, updated_at=now()
WHERE id = $1
RETURNING *
`
Expand Down Expand Up @@ -132,7 +132,6 @@ func (r ReceiverRepository) Update(ctx context.Context, rcv *receiver.Receiver)
if err := r.client.db.QueryRowxContext(ctx, receiverUpdateQuery,
receiverModel.ID,
receiverModel.Name,
receiverModel.Type,
receiverModel.Labels,
receiverModel.Configurations,
).StructScan(&updatedReceiver); err != nil {
Expand Down
Loading

0 comments on commit 193787a

Please sign in to comment.