generated from VLADIMIR/template
up
This commit is contained in:
@@ -203,14 +203,14 @@ func (s *server) DeleteUserRole(ctx context.Context, req *proto.DeleteUserRoleRe
|
||||
}
|
||||
|
||||
func (s *server) GetPermissions(ctx context.Context, _ *proto.GetPermissionsReq) (*proto.GetPermissionsRsp, error) {
|
||||
claims, _ := processor_jwt.ClaimsFromContext(ctx)
|
||||
if claims == nil {
|
||||
return &proto.GetPermissionsRsp{}, nil
|
||||
rsp := &proto.GetPermissionsRsp{
|
||||
Environment: s.uiService.Environment,
|
||||
}
|
||||
permissions := s.uiService.GetPermissions(claims.Roles)
|
||||
return &proto.GetPermissionsRsp{
|
||||
Permissions: permissions,
|
||||
}, nil
|
||||
claims, _ := processor_jwt.ClaimsFromContext(ctx)
|
||||
if claims != nil {
|
||||
rsp.Permissions = s.uiService.GetPermissions(claims.Roles)
|
||||
}
|
||||
return rsp, nil
|
||||
}
|
||||
|
||||
func (s *server) ChangeUserPassword(ctx context.Context, req *proto.ChangeUserPasswordReq) (*proto.ChangeUserPasswordRsp, error) {
|
||||
|
||||
@@ -9,32 +9,29 @@ import (
|
||||
proto "evening_detective_server/proto"
|
||||
)
|
||||
|
||||
func TestGetPermissionsAllowsAnonymousUsers(t *testing.T) {
|
||||
s := &server{uiService: ui_service.NewUIService()}
|
||||
|
||||
rsp, err := s.GetPermissions(context.Background(), &proto.GetPermissionsReq{})
|
||||
if err != nil {
|
||||
t.Fatalf("GetPermissions() error = %v", err)
|
||||
}
|
||||
if len(rsp.GetPermissions()) != 0 {
|
||||
t.Fatalf("anonymous permissions = %v, want empty", rsp.GetPermissions())
|
||||
}
|
||||
|
||||
ctx := processor_jwt.ContextWithClaims(context.Background(), &processor_jwt.JWTClaims{Roles: []string{"user"}})
|
||||
rsp, err = s.GetPermissions(ctx, &proto.GetPermissionsReq{})
|
||||
if err != nil {
|
||||
t.Fatalf("GetPermissions() with claims error = %v", err)
|
||||
}
|
||||
if len(rsp.GetPermissions()) == 0 {
|
||||
t.Fatal("authenticated permissions are empty")
|
||||
}
|
||||
|
||||
ctx = processor_jwt.ContextWithClaims(context.Background(), &processor_jwt.JWTClaims{Roles: []string{"admin"}})
|
||||
rsp, err = s.GetPermissions(ctx, &proto.GetPermissionsReq{})
|
||||
if err != nil {
|
||||
t.Fatalf("GetPermissions() for admin error = %v", err)
|
||||
}
|
||||
if len(rsp.GetPermissions()) == 0 {
|
||||
t.Fatal("admin permissions are empty")
|
||||
func TestGetPermissionsEnvironment(t *testing.T) {
|
||||
for _, mode := range []struct {
|
||||
name string
|
||||
venue bool
|
||||
}{{"prod", false}, {"venue", true}} {
|
||||
for _, role := range []string{"", "user", "admin"} {
|
||||
t.Run(mode.name+"/"+role, func(t *testing.T) {
|
||||
s := &server{uiService: ui_service.NewUIService(mode.venue)}
|
||||
ctx := context.Background()
|
||||
if role != "" {
|
||||
ctx = processor_jwt.ContextWithClaims(ctx, &processor_jwt.JWTClaims{Roles: []string{role}})
|
||||
}
|
||||
rsp, err := s.GetPermissions(ctx, &proto.GetPermissionsReq{})
|
||||
if err != nil {
|
||||
t.Fatalf("GetPermissions() error = %v", err)
|
||||
}
|
||||
if rsp.GetEnvironment() != mode.name {
|
||||
t.Fatalf("environment = %q, want %q", rsp.GetEnvironment(), mode.name)
|
||||
}
|
||||
if (len(rsp.GetPermissions()) == 0) != (role == "") {
|
||||
t.Fatalf("unexpected permissions for role %q: %v", role, rsp.GetPermissions())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,11 +32,17 @@ var (
|
||||
)
|
||||
|
||||
// UIService provides user interface permissions.
|
||||
type UIService struct{}
|
||||
type UIService struct {
|
||||
Environment string
|
||||
}
|
||||
|
||||
// NewUIService creates a UI service.
|
||||
func NewUIService() *UIService {
|
||||
return &UIService{}
|
||||
func NewUIService(venueMode bool) *UIService {
|
||||
environment := "prod"
|
||||
if venueMode {
|
||||
environment = "venue"
|
||||
}
|
||||
return &UIService{Environment: environment}
|
||||
}
|
||||
|
||||
// GetPermissions returns permissions for the supplied roles.
|
||||
|
||||
@@ -13,7 +13,7 @@ func TestTasksPermission(t *testing.T) {
|
||||
{"developer", true}, {"admin", true}, {"user", false}, {"author", false}, {"organizer", false},
|
||||
} {
|
||||
t.Run(tc.role, func(t *testing.T) {
|
||||
got := NewUIService().GetPermissions([]string{tc.role})
|
||||
got := NewUIService(false).GetPermissions([]string{tc.role})
|
||||
if slices.Contains(got, "tasks_page") != tc.allowed {
|
||||
t.Fatalf("permissions=%v", got)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user