Skip to content

Commit dadbd1e

Browse files
committedApr 24, 2021
test: added user role names test
1 parent 9ba5321 commit dadbd1e

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed
 

‎entities/user.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type User struct {
2222
}
2323

2424
func (u *User) RoleNames() []string {
25-
roles := []string{}
25+
var roles []string
2626
for _, r := range u.Roles {
2727
roles = append(roles, r.Name)
2828
}

‎entities/user_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package entities
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestUserRoleNames(t *testing.T) {
10+
u := &User{}
11+
12+
roles := u.RoleNames()
13+
assert.Empty(t, roles)
14+
15+
u.Roles = []Role{
16+
{Name: "admin"},
17+
{Name: "engineering"},
18+
}
19+
20+
roles = u.RoleNames()
21+
assert.NotEmpty(t, roles)
22+
assert.Len(t, roles, 2)
23+
assert.Equal(t, roles[0], "admin")
24+
assert.Equal(t, roles[1], "engineering")
25+
26+
}

‎opa/rbac_authz.rego

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ role_permissions := {
66
"engineering": [{"method": "GET", "path": "/api/campaigns"},
77
{"method": "GET", "path": "/api/campaigns/:id"}],
88
"webdev": [{"method": "GET", "path": "/api/campaigns"},
9-
{"method": "PUT", "path": "/api/campaigns/:id"}],
9+
{"method": "PUT", "path": "/api/campaigns/:id"}]
1010
}
1111

1212
default allow = false

0 commit comments

Comments
 (0)
Please sign in to comment.