-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute_test.go
More file actions
75 lines (53 loc) · 1.77 KB
/
attribute_test.go
File metadata and controls
75 lines (53 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2026 Hyperscale. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package security_test
import (
"context"
"testing"
"github.com/hyperscale-stack/security"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRoleAttribute(t *testing.T) {
t.Parallel()
attr := security.Role("ADMIN")
role, ok := attr.(security.RoleAttribute)
require.True(t, ok)
// String() carries the Spring-style ROLE_ prefix.
assert.Equal(t, "ROLE_ADMIN", attr.String())
// Name() returns the bare role.
assert.Equal(t, "ADMIN", role.Name())
}
func TestScopeAttribute(t *testing.T) {
t.Parallel()
attr := security.Scope("read:mail")
scope, ok := attr.(security.ScopeAttribute)
require.True(t, ok)
assert.Equal(t, "scope:read:mail", attr.String())
assert.Equal(t, "read:mail", scope.Name())
}
func TestAuthorityAttribute(t *testing.T) {
t.Parallel()
attr := security.Authority("billing:export")
_, ok := attr.(security.AuthorityAttribute)
require.True(t, ok)
// Authority carries no convention — String() is the bare value.
assert.Equal(t, "billing:export", attr.String())
}
func TestPermissionAttribute(t *testing.T) {
t.Parallel()
called := false
predicate := func(context.Context, security.Authentication) bool {
called = true
return true
}
attr := security.Permission("owns-document", predicate)
perm, ok := attr.(security.PermissionAttribute)
require.True(t, ok)
assert.Equal(t, "permission:owns-document", attr.String())
assert.Equal(t, "owns-document", perm.Name)
require.NotNil(t, perm.Predicate)
assert.True(t, perm.Predicate(context.Background(), security.Anonymous()))
assert.True(t, called, "the constructed predicate must be the one supplied")
}