github.com/astaxie/beego@v1.12.3/plugins/authz/authz_test.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package authz
    16  
    17  import (
    18  	"net/http"
    19  	"net/http/httptest"
    20  	"testing"
    21  
    22  	"github.com/astaxie/beego"
    23  	"github.com/astaxie/beego/context"
    24  	"github.com/astaxie/beego/plugins/auth"
    25  	"github.com/casbin/casbin"
    26  )
    27  
    28  func testRequest(t *testing.T, handler *beego.ControllerRegister, user string, path string, method string, code int) {
    29  	r, _ := http.NewRequest(method, path, nil)
    30  	r.SetBasicAuth(user, "123")
    31  	w := httptest.NewRecorder()
    32  	handler.ServeHTTP(w, r)
    33  
    34  	if w.Code != code {
    35  		t.Errorf("%s, %s, %s: %d, supposed to be %d", user, path, method, w.Code, code)
    36  	}
    37  }
    38  
    39  func TestBasic(t *testing.T) {
    40  	handler := beego.NewControllerRegister()
    41  
    42  	handler.InsertFilter("*", beego.BeforeRouter, auth.Basic("alice", "123"))
    43  	handler.InsertFilter("*", beego.BeforeRouter, NewAuthorizer(casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")))
    44  
    45  	handler.Any("*", func(ctx *context.Context) {
    46  		ctx.Output.SetStatus(200)
    47  	})
    48  
    49  	testRequest(t, handler, "alice", "/dataset1/resource1", "GET", 200)
    50  	testRequest(t, handler, "alice", "/dataset1/resource1", "POST", 200)
    51  	testRequest(t, handler, "alice", "/dataset1/resource2", "GET", 200)
    52  	testRequest(t, handler, "alice", "/dataset1/resource2", "POST", 403)
    53  }
    54  
    55  func TestPathWildcard(t *testing.T) {
    56  	handler := beego.NewControllerRegister()
    57  
    58  	handler.InsertFilter("*", beego.BeforeRouter, auth.Basic("bob", "123"))
    59  	handler.InsertFilter("*", beego.BeforeRouter, NewAuthorizer(casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")))
    60  
    61  	handler.Any("*", func(ctx *context.Context) {
    62  		ctx.Output.SetStatus(200)
    63  	})
    64  
    65  	testRequest(t, handler, "bob", "/dataset2/resource1", "GET", 200)
    66  	testRequest(t, handler, "bob", "/dataset2/resource1", "POST", 200)
    67  	testRequest(t, handler, "bob", "/dataset2/resource1", "DELETE", 200)
    68  	testRequest(t, handler, "bob", "/dataset2/resource2", "GET", 200)
    69  	testRequest(t, handler, "bob", "/dataset2/resource2", "POST", 403)
    70  	testRequest(t, handler, "bob", "/dataset2/resource2", "DELETE", 403)
    71  
    72  	testRequest(t, handler, "bob", "/dataset2/folder1/item1", "GET", 403)
    73  	testRequest(t, handler, "bob", "/dataset2/folder1/item1", "POST", 200)
    74  	testRequest(t, handler, "bob", "/dataset2/folder1/item1", "DELETE", 403)
    75  	testRequest(t, handler, "bob", "/dataset2/folder1/item2", "GET", 403)
    76  	testRequest(t, handler, "bob", "/dataset2/folder1/item2", "POST", 200)
    77  	testRequest(t, handler, "bob", "/dataset2/folder1/item2", "DELETE", 403)
    78  }
    79  
    80  func TestRBAC(t *testing.T) {
    81  	handler := beego.NewControllerRegister()
    82  
    83  	handler.InsertFilter("*", beego.BeforeRouter, auth.Basic("cathy", "123"))
    84  	e := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")
    85  	handler.InsertFilter("*", beego.BeforeRouter, NewAuthorizer(e))
    86  
    87  	handler.Any("*", func(ctx *context.Context) {
    88  		ctx.Output.SetStatus(200)
    89  	})
    90  
    91  	// cathy can access all /dataset1/* resources via all methods because it has the dataset1_admin role.
    92  	testRequest(t, handler, "cathy", "/dataset1/item", "GET", 200)
    93  	testRequest(t, handler, "cathy", "/dataset1/item", "POST", 200)
    94  	testRequest(t, handler, "cathy", "/dataset1/item", "DELETE", 200)
    95  	testRequest(t, handler, "cathy", "/dataset2/item", "GET", 403)
    96  	testRequest(t, handler, "cathy", "/dataset2/item", "POST", 403)
    97  	testRequest(t, handler, "cathy", "/dataset2/item", "DELETE", 403)
    98  
    99  	// delete all roles on user cathy, so cathy cannot access any resources now.
   100  	e.DeleteRolesForUser("cathy")
   101  
   102  	testRequest(t, handler, "cathy", "/dataset1/item", "GET", 403)
   103  	testRequest(t, handler, "cathy", "/dataset1/item", "POST", 403)
   104  	testRequest(t, handler, "cathy", "/dataset1/item", "DELETE", 403)
   105  	testRequest(t, handler, "cathy", "/dataset2/item", "GET", 403)
   106  	testRequest(t, handler, "cathy", "/dataset2/item", "POST", 403)
   107  	testRequest(t, handler, "cathy", "/dataset2/item", "DELETE", 403)
   108  }