bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/web/roles_test.go (about)

     1  package web
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/captncraig/easyauth"
     7  )
     8  
     9  func TestWriterRole(t *testing.T) {
    10  	if roleWriter&canManageTokens != 0 {
    11  		t.Error("Writer should not be able to manage tokens")
    12  	}
    13  	if roleWriter&canCreateAnnotations != canCreateAnnotations {
    14  		t.Error("Writer should be able to create annotations")
    15  	}
    16  }
    17  
    18  func TestRoleParse(t *testing.T) {
    19  	tests := []struct {
    20  		s      string
    21  		expect easyauth.Role
    22  		errors bool
    23  	}{
    24  		{"ViewDashboard", canViewDash, false},
    25  		{"ViewDasHBoard", canViewDash, false},
    26  		{"ViewDashboardzzzz", 0, true},
    27  		{"Admin", roleAdmin, false},
    28  		{"ViewDashboard,PutData", canViewDash | canPutData, false},
    29  		{"ViewDashboard,Thisdoesnotexist", 0, true},
    30  	}
    31  	for i, test := range tests {
    32  		found, err := parseRole(test.s)
    33  		if err != nil && !test.errors {
    34  			t.Errorf("%d: Unexpected error for %s", i, test.s)
    35  			continue
    36  		}
    37  		if err == nil && test.errors {
    38  			t.Errorf("%d: Expected error not found for %s", i, test.s)
    39  			continue
    40  		}
    41  		if found != test.expect {
    42  			t.Errorf("%d: Expected %d but got %d", i, test.expect, found)
    43  		}
    44  	}
    45  }