From fc8579e4d57c85c075c6c16d33dbb5ec25ef9a50 Mon Sep 17 00:00:00 2001 From: clD11 <23483715+clD11@users.noreply.github.com> Date: Wed, 29 Jan 2025 21:30:26 +0000 Subject: [PATCH] feat: toggle tos version based on environments (#2750) --- services/rewards/cmd/rest_run.go | 9 ++- services/rewards/service.go | 20 +++++- services/rewards/service_test.go | 104 +++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 5 deletions(-) diff --git a/services/rewards/cmd/rest_run.go b/services/rewards/cmd/rest_run.go index 1b13891ac..cb8bc7cec 100644 --- a/services/rewards/cmd/rest_run.go +++ b/services/rewards/cmd/rest_run.go @@ -72,13 +72,16 @@ func RestRun(command *cobra.Command, args []string) { ctx = context.WithValue(ctx, appctx.DefaultACChoicesCTXKey, acChoices) } + env := os.Getenv("ENV") + if env == "" { + lg.Fatal().Err(err).Msg("error retrieving environment") + } + tosVersion, err := strconv.Atoi(os.Getenv("REWARDS_TOS_VERSION")) if err != nil { lg.Fatal().Err(err).Msg("error retrieving rewards terms of service version") } - tosEnabled, _ := strconv.ParseBool(os.Getenv("REWARDS_TOS_VERSION_ENABLED")) - // Get the bucket from the context and not os.Getenv so we don't diverge. GetParameters uses the context on // each request and this will need to be refactored before we can remove it. cardsBucket, ok := ctx.Value(appctx.ParametersMergeBucketCTXKey).(string) @@ -92,8 +95,8 @@ func RestRun(command *cobra.Command, args []string) { } cfg := &rewards.Config{ + Env: env, TOSVersion: tosVersion, - TOSEnabled: tosEnabled, Cards: &rewards.CardsConfig{ Bucket: cardsBucket, Key: cardsKey, diff --git a/services/rewards/service.go b/services/rewards/service.go index b96c87f42..30c0f48e7 100644 --- a/services/rewards/service.go +++ b/services/rewards/service.go @@ -33,8 +33,8 @@ type CardsConfig struct { } type Config struct { + Env string TOSVersion int - TOSEnabled bool Cards *CardsConfig } @@ -49,6 +49,22 @@ type Service struct { s3Svc s3Service } +func (c *Config) isDevelopment() bool { + if c == nil { + return false + } + + return c.Env == "development" +} + +func (c *Config) isStaging() bool { + if c == nil { + return false + } + + return c.Env == "staging" +} + func (s *Service) Jobs() []srv.Job { return s.jobs } @@ -159,7 +175,7 @@ func (s *Service) GetParameters(ctx context.Context, currency *BaseCurrency) (*P }, } - if s.cfg.TOSEnabled { + if s.cfg.isDevelopment() || s.cfg.isStaging() { params.TOSVersion = s.cfg.TOSVersion } diff --git a/services/rewards/service_test.go b/services/rewards/service_test.go index 4249b130a..9e75e24c0 100644 --- a/services/rewards/service_test.go +++ b/services/rewards/service_test.go @@ -101,3 +101,107 @@ func (m *mockS3Service) GetObject(ctx context.Context, params *s3.GetObjectInput return m.fnGetObject(ctx, params, optFns...) } + +func TestConfig_isDevelopment(t *testing.T) { + type tcGiven struct { + c *Config + } + + type tcExpected struct { + val bool + } + + type testCase struct { + name string + given tcGiven + exp tcExpected + } + + tests := []testCase{ + { + name: "empty", + }, + + { + name: "false", + given: tcGiven{ + c: &Config{ + Env: "env", + }, + }, + }, + + { + name: "true", + given: tcGiven{ + c: &Config{ + Env: "development", + }, + }, + exp: tcExpected{ + val: true, + }, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual := tc.given.c.isDevelopment() + assert.Equal(t, tc.exp.val, actual) + }) + } +} + +func TestConfig_isStaging(t *testing.T) { + type tcGiven struct { + c *Config + } + + type tcExpected struct { + val bool + } + + type testCase struct { + name string + given tcGiven + exp tcExpected + } + + tests := []testCase{ + { + name: "empty", + }, + + { + name: "false", + given: tcGiven{ + c: &Config{ + Env: "env", + }, + }, + }, + + { + name: "true", + given: tcGiven{ + c: &Config{ + Env: "staging", + }, + }, + exp: tcExpected{ + val: true, + }, + }, + } + + for i := range tests { + tc := tests[i] + + t.Run(tc.name, func(t *testing.T) { + actual := tc.given.c.isStaging() + assert.Equal(t, tc.exp.val, actual) + }) + } +}