github.com/merlinepedra/gopphish-attack@v0.9.0/models/rbac_test.go (about)

     1  package models
     2  
     3  import (
     4  	"fmt"
     5  
     6  	check "gopkg.in/check.v1"
     7  )
     8  
     9  type PermissionCheck map[string]bool
    10  
    11  func (s *ModelsSuite) TestHasPermission(c *check.C) {
    12  
    13  	permissionTests := map[string]PermissionCheck{
    14  		RoleAdmin: PermissionCheck{
    15  			PermissionModifySystem:  true,
    16  			PermissionModifyObjects: true,
    17  			PermissionViewObjects:   true,
    18  		},
    19  		RoleUser: PermissionCheck{
    20  			PermissionModifySystem:  false,
    21  			PermissionModifyObjects: true,
    22  			PermissionViewObjects:   true,
    23  		},
    24  	}
    25  
    26  	for r, checks := range permissionTests {
    27  		// Create the user with the provided role
    28  		role, err := GetRoleBySlug(r)
    29  		c.Assert(err, check.Equals, nil)
    30  		user := User{
    31  			Username: fmt.Sprintf("test-%s", r),
    32  			Hash:     "12345",
    33  			ApiKey:   fmt.Sprintf("%s-key", r),
    34  			RoleID:   role.ID,
    35  		}
    36  		PutUser(&user)
    37  
    38  		// Perform the permission checks
    39  		for permission, expected := range checks {
    40  			access, err := user.HasPermission(permission)
    41  			fmt.Printf("Checking %s -> %s\n", r, permission)
    42  			c.Assert(err, check.Equals, nil)
    43  			c.Assert(access, check.Equals, expected)
    44  		}
    45  	}
    46  }
    47  
    48  func (s *ModelsSuite) TestGetRoleBySlug(c *check.C) {
    49  	roles := []string{RoleAdmin, RoleUser}
    50  	for _, role := range roles {
    51  		got, err := GetRoleBySlug(role)
    52  		c.Assert(err, check.Equals, nil)
    53  		c.Assert(got.Slug, check.Equals, role)
    54  	}
    55  	_, err := GetRoleBySlug("bogus")
    56  	c.Assert(err, check.NotNil)
    57  }