From 993396b60e96067d25ccafab94896661881cd852 Mon Sep 17 00:00:00 2001 From: clD11 <23483715+clD11@users.noreply.github.com> Date: Sat, 10 Aug 2024 08:51:08 +0100 Subject: [PATCH] test: add tests --- services/skus/radom/event.go | 14 +- services/skus/radom/event_test.go | 374 ++++++++++++++++++++++++++++++ 2 files changed, 381 insertions(+), 7 deletions(-) diff --git a/services/skus/radom/event.go b/services/skus/radom/event.go index 292d8a410..4b059d6f8 100644 --- a/services/skus/radom/event.go +++ b/services/skus/radom/event.go @@ -140,8 +140,8 @@ func ParseEvent(b []byte) (Event, error) { } type MessageAuthConfig struct { - token string - disabled bool + token string + enabled bool } type MessageAuthenticator struct { @@ -155,13 +155,13 @@ func NewMessageAuthenticator(cfg MessageAuthConfig) *MessageAuthenticator { } const ( - ErrDisabled = model.Error("radom: radom disabled") - ErrVerificationKeyEmpty = model.Error("radom: verification key is empty") - ErrWebhookInvalidKey = model.Error("radom: verification key is invalid") + ErrDisabled = model.Error("radom: radom disabled") + ErrVerificationKeyEmpty = model.Error("radom: verification key is empty") + ErrVerificationKeyInvalid = model.Error("radom: verification key is invalid") ) func (r *MessageAuthenticator) Authenticate(_ context.Context, token string) error { - if r.cfg.disabled { + if !r.cfg.enabled { return ErrDisabled } @@ -170,7 +170,7 @@ func (r *MessageAuthenticator) Authenticate(_ context.Context, token string) err } if token != r.cfg.token { - return ErrWebhookInvalidKey + return ErrVerificationKeyInvalid } return nil diff --git a/services/skus/radom/event_test.go b/services/skus/radom/event_test.go index d7d17d5e9..746fd6a02 100644 --- a/services/skus/radom/event_test.go +++ b/services/skus/radom/event_test.go @@ -1,6 +1,7 @@ package radom import ( + "context" "testing" uuid "github.com/satori/go.uuid" @@ -516,3 +517,376 @@ func TestEvent_SubID(t *testing.T) { }) } } + +func TestEvent_IsNewSub(t *testing.T) { + type tcGiven struct { + event Event + } + + type testCase struct { + name string + given tcGiven + exp bool + } + + tests := []testCase{ + { + name: "new_subscription", + given: tcGiven{ + event: Event{ + EventData: EventData{ + NewSubscription: &NewSubscription{}, + }, + }, + }, + exp: true, + }, + + { + name: "not_new_subscription", + exp: false, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual := tc.given.event.IsNewSub() + should.Equal(t, tc.exp, actual) + }) + } +} + +func TestEvent_ShouldRenew(t *testing.T) { + type tcGiven struct { + event Event + } + + type testCase struct { + name string + given tcGiven + exp bool + } + + tests := []testCase{ + { + name: "subscription_payment", + given: tcGiven{ + event: Event{ + EventData: EventData{ + SubscriptionPayment: &SubscriptionPayment{}, + }, + }, + }, + exp: true, + }, + + { + name: "not_subscription_payment", + exp: false, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual := tc.given.event.ShouldRenew() + should.Equal(t, tc.exp, actual) + }) + } +} + +func TestEvent_ShouldCancel(t *testing.T) { + type tcGiven struct { + event Event + } + + type testCase struct { + name string + given tcGiven + exp bool + } + + tests := []testCase{ + { + name: "subscription_cancelled", + given: tcGiven{ + event: Event{ + EventData: EventData{ + SubscriptionCancelled: &SubscriptionCancelled{}, + }, + }, + }, + exp: true, + }, + + { + name: "not_subscription_cancelled", + exp: false, + }, + + { + name: "subscription_expired", + given: tcGiven{ + event: Event{ + EventData: EventData{ + SubscriptionExpired: &SubscriptionExpired{}, + }, + }, + }, + exp: true, + }, + + { + name: "not_subscription_expired", + exp: false, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual := tc.given.event.ShouldCancel() + should.Equal(t, tc.exp, actual) + }) + } +} + +func TestEvent_ShouldProcess(t *testing.T) { + type tcGiven struct { + event Event + } + + type testCase struct { + name string + given tcGiven + exp bool + } + + tests := []testCase{ + { + name: "should_process_new_subscription", + given: tcGiven{ + event: Event{ + EventData: EventData{ + NewSubscription: &NewSubscription{}, + }, + }, + }, + exp: true, + }, + + { + name: "should_process_subscription_payment", + given: tcGiven{ + event: Event{ + EventData: EventData{ + SubscriptionPayment: &SubscriptionPayment{}, + }, + }, + }, + exp: true, + }, + + { + name: "should_process_subscription_cancelled", + given: tcGiven{ + event: Event{ + EventData: EventData{ + SubscriptionCancelled: &SubscriptionCancelled{}, + }, + }, + }, + exp: true, + }, + + { + name: "should_process_subscription_expired", + given: tcGiven{ + event: Event{ + EventData: EventData{ + SubscriptionExpired: &SubscriptionExpired{}, + }, + }, + }, + exp: true, + }, + + { + name: "not_should_process", + exp: false, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual := tc.given.event.ShouldProcess() + should.Equal(t, tc.exp, actual) + }) + } +} + +func TestEvent_Effect(t *testing.T) { + type tcGiven struct { + event Event + } + + type testCase struct { + name string + given tcGiven + exp string + } + + tests := []testCase{ + { + name: "new", + given: tcGiven{ + event: Event{ + EventData: EventData{ + NewSubscription: &NewSubscription{}, + }, + }, + }, + exp: "new", + }, + + { + name: "renew", + given: tcGiven{ + event: Event{ + EventData: EventData{ + SubscriptionPayment: &SubscriptionPayment{}, + }, + }, + }, + exp: "renew", + }, + + { + name: "cancel", + given: tcGiven{ + event: Event{ + EventData: EventData{ + SubscriptionCancelled: &SubscriptionCancelled{}, + }, + }, + }, + exp: "cancel", + }, + + { + name: "expired", + given: tcGiven{ + event: Event{ + EventData: EventData{ + SubscriptionExpired: &SubscriptionExpired{}, + }, + }, + }, + exp: "cancel", + }, + + { + name: "skip", + exp: "skip", + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual := tc.given.event.Effect() + should.Equal(t, tc.exp, actual) + }) + } +} + +func TestMessageAuthenticator_Authenticate(t *testing.T) { + type tcGiven struct { + mAuth MessageAuthenticator + token string + } + + type tcExpected struct { + err error + } + + type testCase struct { + name string + given tcGiven + exp tcExpected + } + + tests := []testCase{ + { + name: "disabled", + given: tcGiven{ + mAuth: MessageAuthenticator{}, + }, + exp: tcExpected{ + err: ErrDisabled, + }, + }, + + { + name: "verification_key_empty", + given: tcGiven{ + mAuth: MessageAuthenticator{ + cfg: MessageAuthConfig{ + enabled: true, + token: "token", + }, + }, + }, + exp: tcExpected{ + err: ErrVerificationKeyEmpty, + }, + }, + + { + name: "verification_key_invalid", + given: tcGiven{ + mAuth: MessageAuthenticator{ + cfg: MessageAuthConfig{ + enabled: true, + token: "token_1", + }, + }, + token: "token_2", + }, + exp: tcExpected{ + err: ErrVerificationKeyInvalid, + }, + }, + + { + name: "success", + given: tcGiven{ + mAuth: MessageAuthenticator{ + cfg: MessageAuthConfig{ + enabled: true, + token: "token_1", + }, + }, + token: "token_1", + }, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + ctx := context.Background() + + actual := tc.given.mAuth.Authenticate(ctx, tc.given.token) + should.ErrorIs(t, tc.exp.err, actual) + }) + } +}