github.com/goravel/framework@v1.13.9/auth/access/gate_test.go (about) 1 package access 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/suite" 10 11 "github.com/goravel/framework/contracts/auth/access" 12 ) 13 14 type contextKey int 15 16 const key contextKey = 0 17 18 type GateTestSuite struct { 19 suite.Suite 20 } 21 22 func TestGateTestSuite(t *testing.T) { 23 suite.Run(t, new(GateTestSuite)) 24 } 25 26 func (s *GateTestSuite) SetupTest() { 27 28 } 29 30 func (s *GateTestSuite) TestWithContext() { 31 ctx := context.WithValue(context.Background(), key, "goravel") 32 33 gate := NewGate(ctx) 34 gate.Define("create", func(ctx context.Context, arguments map[string]any) access.Response { 35 user := arguments["user"].(string) 36 if user == "1" { 37 return NewAllowResponse() 38 } else { 39 return NewDenyResponse(ctx.Value(key).(string)) 40 } 41 }) 42 43 assert.Equal(s.T(), NewDenyResponse("goravel"), gate.Inspect("create", map[string]any{ 44 "user": "2", 45 })) 46 } 47 48 func (s *GateTestSuite) TestAllows() { 49 gate := initGate() 50 assert.True(s.T(), gate.Allows("create", map[string]any{ 51 "user": "1", 52 })) 53 assert.False(s.T(), gate.Allows("create", map[string]any{ 54 "user": "2", 55 })) 56 assert.False(s.T(), gate.Allows("update", map[string]any{ 57 "user": "1", 58 })) 59 } 60 61 func (s *GateTestSuite) TestDenies() { 62 gate := initGate() 63 assert.False(s.T(), gate.Denies("create", map[string]any{ 64 "user": "1", 65 })) 66 assert.True(s.T(), gate.Denies("create", map[string]any{ 67 "user": "2", 68 })) 69 assert.True(s.T(), gate.Denies("update", map[string]any{ 70 "user": "1", 71 })) 72 } 73 74 func (s *GateTestSuite) TestInspect() { 75 gate := initGate() 76 assert.Equal(s.T(), NewAllowResponse(), gate.Inspect("create", map[string]any{ 77 "user": "1", 78 })) 79 assert.True(s.T(), gate.Inspect("create", map[string]any{ 80 "user": "1", 81 }).Allowed()) 82 assert.Equal(s.T(), NewDenyResponse("create error"), gate.Inspect("create", map[string]any{ 83 "user": "2", 84 })) 85 assert.Equal(s.T(), "create error", gate.Inspect("create", map[string]any{ 86 "user": "2", 87 }).Message()) 88 assert.Equal(s.T(), NewDenyResponse(fmt.Sprintf("ability doesn't exist: %s", "delete")), gate.Inspect("delete", map[string]any{ 89 "user": "1", 90 })) 91 } 92 93 func (s *GateTestSuite) TestAny() { 94 gate := initGate() 95 assert.True(s.T(), gate.Any([]string{"create", "update"}, map[string]any{ 96 "user": "1", 97 })) 98 assert.True(s.T(), gate.Any([]string{"create", "update"}, map[string]any{ 99 "user": "2", 100 })) 101 assert.False(s.T(), gate.Any([]string{"create", "update"}, map[string]any{ 102 "user": "3", 103 })) 104 } 105 106 func (s *GateTestSuite) TestNone() { 107 gate := initGate() 108 assert.False(s.T(), gate.None([]string{"create", "update"}, map[string]any{ 109 "user": "1", 110 })) 111 assert.False(s.T(), gate.None([]string{"create", "update"}, map[string]any{ 112 "user": "2", 113 })) 114 assert.True(s.T(), gate.None([]string{"create", "update"}, map[string]any{ 115 "user": "3", 116 })) 117 } 118 119 func (s *GateTestSuite) TestBefore() { 120 gate := initGate() 121 gate.Before(func(ctx context.Context, ability string, arguments map[string]any) access.Response { 122 user := arguments["user"].(string) 123 if user == "3" { 124 return NewAllowResponse() 125 } 126 127 return nil 128 }) 129 assert.True(s.T(), gate.Allows("create", map[string]any{ 130 "user": "3", 131 })) 132 assert.False(s.T(), gate.Allows("create", map[string]any{ 133 "user": "4", 134 })) 135 } 136 137 func (s *GateTestSuite) TestAfter() { 138 gate := initGate() 139 gate.Define("delete", func(ctx context.Context, arguments map[string]any) access.Response { 140 user := arguments["user"].(string) 141 if user == "3" { 142 return nil 143 } else { 144 return NewAllowResponse() 145 } 146 }) 147 gate.After(func(ctx context.Context, ability string, arguments map[string]any, result access.Response) access.Response { 148 user := arguments["user"].(string) 149 if user == "3" { 150 return NewAllowResponse() 151 } 152 153 return nil 154 }) 155 assert.True(s.T(), gate.Allows("delete", map[string]any{ 156 "user": "1", 157 })) 158 assert.True(s.T(), gate.Allows("delete", map[string]any{ 159 "user": "3", 160 })) 161 } 162 163 func initGate() *Gate { 164 gate := NewGate(context.Background()) 165 gate.Define("create", func(ctx context.Context, arguments map[string]any) access.Response { 166 user := arguments["user"].(string) 167 if user == "1" { 168 return NewAllowResponse() 169 } else { 170 return NewDenyResponse("create error") 171 } 172 }) 173 gate.Define("update", func(ctx context.Context, arguments map[string]any) access.Response { 174 user := arguments["user"].(string) 175 if user == "2" { 176 return NewAllowResponse() 177 } else { 178 return NewDenyResponse(" update error") 179 } 180 }) 181 182 return gate 183 }