|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package rbac_test |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "github.com/go-chassis/cari/rbac" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | +) |
| 27 | + |
| 28 | +func TestFromContext(t *testing.T) { |
| 29 | + ctx := rbac.NewContext(context.TODO(), map[string]interface{}{ |
| 30 | + rbac.ClaimsUser: "root", |
| 31 | + rbac.ClaimsRoles: []interface{}{}, |
| 32 | + }) |
| 33 | + |
| 34 | + claims, _ := rbac.FromContext(ctx) |
| 35 | + u := claims[rbac.ClaimsUser] |
| 36 | + r := claims[rbac.ClaimsRoles] |
| 37 | + assert.Equal(t, "root", u) |
| 38 | + assert.Equal(t, []interface{}{}, r) |
| 39 | + |
| 40 | + a, err := rbac.AccountFromContext(ctx) |
| 41 | + assert.NoError(t, err) |
| 42 | + assert.Equal(t, "root", a.Name) |
| 43 | + assert.Equal(t, []string{}, a.Roles) |
| 44 | +} |
| 45 | + |
| 46 | +func TestMustAuth(t *testing.T) { |
| 47 | + rbac.Add2WhiteAPIList("/test") |
| 48 | + rbac.Add2WhiteAPIList("/test1") |
| 49 | + assert.False(t, rbac.MustAuth("/test")) |
| 50 | + assert.False(t, rbac.MustAuth("/test1")) |
| 51 | + assert.True(t, rbac.MustAuth("/auth")) |
| 52 | + assert.True(t, rbac.MustAuth("/version")) |
| 53 | + assert.True(t, rbac.MustAuth("/v4/a/registry/version")) |
| 54 | + assert.True(t, rbac.MustAuth("/health")) |
| 55 | + assert.True(t, rbac.MustAuth("/v4/a/registry/health")) |
| 56 | +} |
0 commit comments