vitess.io/vitess@v0.16.2/go/acl/acl_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package acl 18 19 import ( 20 "errors" 21 "net/http" 22 "testing" 23 ) 24 25 type TestPolicy struct{} 26 27 func (tp TestPolicy) CheckAccessActor(actor, role string) error { 28 if role == ADMIN { 29 return errors.New("not allowed") 30 } 31 return nil 32 } 33 34 func (tp TestPolicy) CheckAccessHTTP(req *http.Request, role string) error { 35 if role == ADMIN { 36 return errors.New("not allowed") 37 } 38 return nil 39 } 40 41 func init() { 42 RegisterPolicy("test", TestPolicy{}) 43 44 // Run the `once` so it doesn't run during testing, 45 // since we need to override the currentPolicy. 46 once.Do(savePolicy) 47 } 48 49 func TestSimplePolicy(t *testing.T) { 50 currentPolicy = policies["test"] 51 err := CheckAccessActor("", ADMIN) 52 want := "not allowed" 53 if err == nil || err.Error() != want { 54 t.Errorf("got %v, want %s", err, want) 55 } 56 err = CheckAccessActor("", DEBUGGING) 57 if err != nil { 58 t.Errorf("got %v, want no error", err) 59 } 60 61 err = CheckAccessHTTP(nil, ADMIN) 62 if err == nil || err.Error() != want { 63 t.Errorf("got %v, want %s", err, want) 64 } 65 err = CheckAccessHTTP(nil, DEBUGGING) 66 if err != nil { 67 t.Errorf("got %v, want no error", err) 68 } 69 } 70 71 func TestEmptyPolicy(t *testing.T) { 72 currentPolicy = nil 73 err := CheckAccessActor("", ADMIN) 74 if err != nil { 75 t.Errorf("got %v, want no error", err) 76 } 77 err = CheckAccessActor("", DEBUGGING) 78 if err != nil { 79 t.Errorf("got %v, want no error", err) 80 } 81 82 err = CheckAccessHTTP(nil, ADMIN) 83 if err != nil { 84 t.Errorf("got %v, want no error", err) 85 } 86 err = CheckAccessHTTP(nil, DEBUGGING) 87 if err != nil { 88 t.Errorf("got %v, want no error", err) 89 } 90 }