feat: authsession service
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
package sessionlimit
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestKindIsKnown(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
value Kind
|
||||
want bool
|
||||
}{
|
||||
{name: "disabled", value: KindDisabled, want: true},
|
||||
{name: "allowed", value: KindAllowed, want: true},
|
||||
{name: "exceeded", value: KindExceeded, want: true},
|
||||
{name: "unknown", value: Kind("unknown"), want: false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if got := tt.value.IsKnown(); got != tt.want {
|
||||
require.Failf(t, "test failed", "IsKnown() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecisionValidate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
limitTwo := 2
|
||||
limitThree := 3
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
value Decision
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "disabled valid",
|
||||
value: Decision{
|
||||
Kind: KindDisabled,
|
||||
ActiveSessionCount: 0,
|
||||
NextSessionCount: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "allowed valid",
|
||||
value: Decision{
|
||||
Kind: KindAllowed,
|
||||
ConfiguredLimit: &limitThree,
|
||||
ActiveSessionCount: 1,
|
||||
NextSessionCount: 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "exceeded valid",
|
||||
value: Decision{
|
||||
Kind: KindExceeded,
|
||||
ConfiguredLimit: &limitTwo,
|
||||
ActiveSessionCount: 2,
|
||||
NextSessionCount: 3,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "disabled rejects limit",
|
||||
value: Decision{
|
||||
Kind: KindDisabled,
|
||||
ConfiguredLimit: &limitTwo,
|
||||
ActiveSessionCount: 0,
|
||||
NextSessionCount: 1,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "allowed requires limit",
|
||||
value: Decision{
|
||||
Kind: KindAllowed,
|
||||
ActiveSessionCount: 0,
|
||||
NextSessionCount: 1,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "allowed rejects overflow",
|
||||
value: Decision{
|
||||
Kind: KindAllowed,
|
||||
ConfiguredLimit: &limitTwo,
|
||||
ActiveSessionCount: 2,
|
||||
NextSessionCount: 3,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "next count must be active plus one",
|
||||
value: Decision{
|
||||
Kind: KindDisabled,
|
||||
ActiveSessionCount: 2,
|
||||
NextSessionCount: 2,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
err := tt.value.Validate()
|
||||
if tt.wantErr && err == nil {
|
||||
require.FailNow(t, "Validate() returned nil error")
|
||||
}
|
||||
if !tt.wantErr && err != nil {
|
||||
require.Failf(t, "test failed", "Validate() returned error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user