Skip to content

Commit

Permalink
feat: toggle tos version based on environments (#2750)
Browse files Browse the repository at this point in the history
  • Loading branch information
clD11 authored Jan 29, 2025
1 parent 4862f46 commit fc8579e
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 5 deletions.
9 changes: 6 additions & 3 deletions services/rewards/cmd/rest_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
20 changes: 18 additions & 2 deletions services/rewards/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type CardsConfig struct {
}

type Config struct {
Env string
TOSVersion int
TOSEnabled bool
Cards *CardsConfig
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
104 changes: 104 additions & 0 deletions services/rewards/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit fc8579e

Please sign in to comment.