Skip to content

Commit

Permalink
test(backend): add test for authz
Browse files Browse the repository at this point in the history
- add tests for getting lower roles
- add tests for getting authorized roles from scope
  • Loading branch information
detj committed Dec 5, 2023
1 parent 059e51d commit 6bafebe
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions measure-backend/measure-go/authz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"reflect"
"testing"
)

func TestLowerRoles(t *testing.T) {
{
expected := []rank{owner, admin, developer, viewer}
result := owner.getLower()

if !reflect.DeepEqual(expected, result) {
t.Errorf("Expected %v but got %v", expected, result)
}
}
{
expected := []rank{admin, developer, viewer}
result := admin.getLower()

if !reflect.DeepEqual(expected, result) {
t.Errorf("Expected %v but got %v", expected, result)
}
}
{
expected := []rank{developer, viewer}
result := developer.getLower()

if !reflect.DeepEqual(expected, result) {
t.Errorf("Expected %v but got %v", expected, result)
}
}
{
expected := []rank{viewer}
result := viewer.getLower()

if !reflect.DeepEqual(expected, result) {
t.Errorf("Expected %v but got %v", expected, result)
}
}
}
func TestSameOrLowerRoleFromScope(t *testing.T) {
{
// get owner roles for TeamInviteSameOrLower scope
expected := []rank{owner, admin, developer, viewer}
result := ScopeTeamInviteSameOrLower.getRolesSameOrLower(owner)

if !reflect.DeepEqual(expected, result) {
t.Errorf("Expected %v but got %v", expected, result)
}
}
{
// get owner roles for TeamChangeRoleSameOrLower scope
expected := []rank{owner, admin, developer, viewer}
result := ScopeTeamChangeRoleSameOrLower.getRolesSameOrLower(owner)

if !reflect.DeepEqual(expected, result) {
t.Errorf("Expected %v but got %v", expected, result)
}
}
{
// get admin roles for TeamInviteSameOrLower scope
expected := []rank{admin, developer, viewer}
result := ScopeTeamInviteSameOrLower.getRolesSameOrLower(admin)

if !reflect.DeepEqual(expected, result) {
t.Errorf("Expected %v but got %v", expected, result)
}
}
{
// get admin roles for TeamChangeRoleSameOrLower scope
expected := []rank{admin, developer, viewer}
result := ScopeTeamChangeRoleSameOrLower.getRolesSameOrLower(admin)

if !reflect.DeepEqual(expected, result) {
t.Errorf("Expected %v but got %v", expected, result)
}
}
{
// get developer roles for TeamInviteSameOrLower scope
expected := []rank{developer, viewer}
result := ScopeTeamInviteSameOrLower.getRolesSameOrLower(developer)

if !reflect.DeepEqual(expected, result) {
t.Errorf("Expected %v but got %v", expected, result)
}
}
{
// get developer roles for TeamChangeRoleSameOrLower scope
expected := []rank{developer, viewer}
result := ScopeTeamChangeRoleSameOrLower.getRolesSameOrLower(developer)

if !reflect.DeepEqual(expected, result) {
t.Errorf("Expected %v but got %v", expected, result)
}
}
{
// get viewer roles for TeamInviteSameOrLower scope
expected := []rank{viewer}
result := ScopeTeamInviteSameOrLower.getRolesSameOrLower(viewer)

if !reflect.DeepEqual(expected, result) {
t.Errorf("Expected %v but got %v", expected, result)
}
}
{
// get viewer roles for TeamChangeRoleSameOrLower scope
expected := []rank{}
result := ScopeTeamChangeRoleSameOrLower.getRolesSameOrLower(viewer)

if len(expected) != len(result) {
t.Errorf("Expected %v but got %v", expected, result)
}
}

}

0 comments on commit 6bafebe

Please sign in to comment.