Skip to content

Commit

Permalink
Add tests for permissions validation
Browse files Browse the repository at this point in the history
  • Loading branch information
RichDom2185 committed Aug 18, 2023
1 parent 5dc2084 commit 80f757f
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions internal/permissions/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package permissions

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

type test_alwaysTrue struct {
PermissionGroup
}

func (t test_alwaysTrue) IsAuthorized(r *http.Request) bool {
return true
}

type test_alwaysFalse struct {
PermissionGroup
}

func (t test_alwaysFalse) IsAuthorized(r *http.Request) bool {
return false
}

func TestValidateAllOf(t *testing.T) {
t.Run("should return true in the trivial case without any permissions", func(t *testing.T) {
groups := []PermissionGroup{}
allOf := AllOf{Groups: groups}
assert.True(t, allOf.IsAuthorized(nil))
})
t.Run("should return true if all groups return true", func(t *testing.T) {
groups := []PermissionGroup{
test_alwaysTrue{},
test_alwaysTrue{},
test_alwaysTrue{},
}
allOf := AllOf{Groups: groups}
assert.True(t, allOf.IsAuthorized(nil))
})
t.Run("should return false if any group returns false", func(t *testing.T) {
groups := []PermissionGroup{
test_alwaysTrue{},
test_alwaysFalse{},
test_alwaysTrue{},
}
allOf := AllOf{Groups: groups}
assert.False(t, allOf.IsAuthorized(nil))
})
}

func TestValidateAnyOf(t *testing.T) {
t.Run("should return true in the trivial case without any permissions", func(t *testing.T) {
groups := []PermissionGroup{}
anyOf := AnyOf{Groups: groups}
assert.True(t, anyOf.IsAuthorized(nil))
})
t.Run("should return false if all groups return false", func(t *testing.T) {
groups := []PermissionGroup{
test_alwaysFalse{},
test_alwaysFalse{},
test_alwaysFalse{},
}
anyOf := AnyOf{Groups: groups}
assert.False(t, anyOf.IsAuthorized(nil))
})
t.Run("should return true if any group returns true", func(t *testing.T) {
groups := []PermissionGroup{
test_alwaysFalse{},
test_alwaysTrue{},
test_alwaysFalse{},
}
anyOf := AnyOf{Groups: groups}
assert.True(t, anyOf.IsAuthorized(nil))
})

}

0 comments on commit 80f757f

Please sign in to comment.