Add permissions test

This commit is contained in:
Eccentric 2024-02-09 23:56:06 +00:00
parent 86748fa32b
commit 04fa8f53e1
2 changed files with 121 additions and 2 deletions

89
main_test.go Normal file
View File

@ -0,0 +1,89 @@
package main
import (
_ "embed"
"testing"
"github.com/ectrc/snow/aid"
"github.com/ectrc/snow/fortnite"
p "github.com/ectrc/snow/person"
)
func TestPersonPermissions(t *testing.T) {
person := fortnite.NewFortnitePerson("helloWorld", false)
if person.DisplayName != "helloWorld" {
t.Errorf("Expected 'helloWorld', got %s", person.DisplayName)
}
aid.Print("intial permissions", person.Permissions)
if person.Permissions != 0 {
t.Errorf("Expected 0, got %d", person.Permissions)
}
person.AddPermission(p.PermissionAll)
aid.Print("permissions after adding all", person.Permissions)
if !person.HasPermission(p.PermissionAll) {
t.Errorf("Expected to have all permissions, got false")
}
aid.Print("has all", person.HasPermission(p.PermissionAll))
if !person.HasPermission(p.PermissionDonator) {
t.Errorf("Expected donator permission as a result of all permissions, got false")
}
aid.Print("has donator", person.HasPermission(p.PermissionDonator))
person.RemovePermission(p.PermissionAll)
aid.Print("permissions after removing all", person.Permissions)
if person.HasPermission(p.PermissionAll) {
t.Errorf("Expected no permissions, got true")
}
aid.Print("does not have all", !person.HasPermission(p.PermissionAll))
person.AddPermission(p.PermissionDonator)
aid.Print("permissions after adding donator", person.Permissions)
if !person.HasPermission(p.PermissionDonator) {
t.Errorf("Expected to have donator permission, got false")
}
aid.Print("has donator", person.HasPermission(p.PermissionDonator))
if person.HasPermission(p.PermissionAll) {
t.Errorf("Expected not to have all permission, got true")
}
aid.Print("does not have all", !person.HasPermission(p.PermissionAll))
if person.HasPermission(p.PermissionItemControl) {
t.Errorf("Expected not to have give permission, got true")
}
person.RemovePermission(p.PermissionAll)
aid.Print("permissions after removing all", person.Permissions)
if person.HasPermission(p.PermissionAll) {
t.Errorf("Expected not to have all permission, got true")
}
aid.Print("does not have all", !person.HasPermission(p.PermissionAll))
if person.HasPermission(p.PermissionDonator) {
t.Errorf("Expected not to have donator permission, got true")
}
aid.Print("does not have donator", !person.HasPermission(p.PermissionDonator))
person.AddPermission(p.PermissionDonator | p.PermissionItemControl)
aid.Print("permissions after adding donator and item control", person.Permissions)
if !person.HasPermission(p.PermissionDonator) {
t.Errorf("Expected to have donator permission, got false")
}
aid.Print("has donator", person.HasPermission(p.PermissionDonator))
if !person.HasPermission(p.PermissionItemControl) {
t.Errorf("Expected to have item control permission, got false")
}
aid.Print("has item control", person.HasPermission(p.PermissionItemControl))
person.Delete()
}

View File

@ -1,5 +1,7 @@
package person package person
import "github.com/ectrc/snow/aid"
type Permission int64 type Permission int64
// DO NOT MOVE THE ORDER OF THESE PERMISSIONS AS THEY ARE USED IN THE DATABASE // DO NOT MOVE THE ORDER OF THESE PERMISSIONS AS THEY ARE USED IN THE DATABASE
@ -14,5 +16,33 @@ const (
PermissionOwner PermissionOwner
PermissionDonator PermissionDonator
PermissionAll Permission = 1<<iota - 1 permissionRealAll = 1<<iota - 1
) // every permission except owner
PermissionAll Permission = permissionRealAll ^ PermissionOwner
)
func (p Permission) StringifyMe() string {
aid.Print(p)
switch p {
case PermissionLookup:
return "Lookup"
case PermissionBan:
return "Ban"
case PermissionInformation:
return "Information"
case PermissionItemControl:
return "ItemControl"
case PermissionLockerControl:
return "LockerControl"
case PermissionPermissionControl:
return "PermissionControl"
case PermissionOwner:
return "Owner"
case PermissionDonator:
return "Donator"
case PermissionAll:
return "All"
default:
return "Unknown"
}
}