Skip to content

Commit

Permalink
test: Unit tests for configuring rate limits
Browse files Browse the repository at this point in the history
Signed-off-by: Alf-Rune Siqveland <[email protected]>
  • Loading branch information
alfrunes committed Dec 16, 2024
1 parent aab7188 commit 9ec7b89
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions backend/services/deviceauth/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"fmt"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"

dconfig "github.com/mendersoftware/mender-server/services/deviceauth/config"
"github.com/mendersoftware/mender-server/services/deviceauth/devauth"
)

func TestSetupRateLimits(t *testing.T) {
t.Parallel()

type testCase struct {
Config *viper.Viper

Error error
}

for name, _tc := range map[string]testCase{
"ok/slice": {
Config: func() *viper.Viper {
cfg := viper.New()
cfg.Set(dconfig.SettingRatelimitsQuotas, "enterprise=1.5 professional=0.75 os=0.5")
return cfg
}(),
},
"ok/map": {
Config: func() *viper.Viper {
cfg := viper.New()
cfg.Set(dconfig.SettingRatelimitsQuotas, map[string]any{
"enterprise": float64(2.5),
"professional": int(2),
"os": uint64(1),
})
return cfg
}(),
},
"ok/no limits": {
Config: func() *viper.Viper {
cfg := viper.New()
return cfg
}(),
},
"error/negative value": {
Config: func() *viper.Viper {
cfg := viper.New()
cfg.Set(dconfig.SettingRatelimitsQuotas, map[string]any{"bad": -1.0})
return cfg
}(),
Error: fmt.Errorf("invalid config value %s[bad]: value must be a positive value",
dconfig.SettingRatelimitsQuotas),
},
"error/slice without separator": {
Config: func() *viper.Viper {
cfg := viper.New()
cfg.Set(dconfig.SettingRatelimitsQuotas, "foo bar baz")
return cfg
}(),
Error: fmt.Errorf("invalid config %s: value %v item #1: missing key/value separator '='",
dconfig.SettingRatelimitsQuotas, []string{"foo", "bar", "baz"}),
},
"error/not convertible to float": {
Config: func() *viper.Viper {
cfg := viper.New()
cfg.Set(dconfig.SettingRatelimitsQuotas, "enterprise=many")
return cfg
}(),
Error: fmt.Errorf("error parsing quota value"),
},
"error/unexpected config type": {
Config: func() *viper.Viper {
cfg := viper.New()
cfg.Set(dconfig.SettingRatelimitsQuotas, "")
return cfg
}(),
Error: fmt.Errorf("invalid config value %s: cannot be empty",
dconfig.SettingRatelimitsQuotas),
},
"error/unexpected map type": {
Config: func() *viper.Viper {
cfg := viper.New()
cfg.Set(dconfig.SettingRatelimitsQuotas, map[string]any{"foo": "123"})
return cfg
}(),
Error: fmt.Errorf("invalid config value %s[foo]: "+
"not a numeric value",
dconfig.SettingRatelimitsQuotas),
},
} {
tc := _tc
t.Run(name, func(t *testing.T) {
da := &devauth.DevAuth{}
err := setupRatelimits(tc.Config, da, "n/a", nil)
if tc.Error != nil {
assert.ErrorContains(t, err, tc.Error.Error())
} else {
assert.NoError(t, err)
}
})
}

}

0 comments on commit 9ec7b89

Please sign in to comment.