|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Contracts\Auth\Access\Gate; |
| 4 | +use Illuminate\Foundation\Testing\DatabaseMigrations; |
| 5 | +use Lauthz\Facades\Enforcer; |
| 6 | +use Lauthz\Tests\TestCase; |
| 7 | + |
| 8 | +class EnforcerLocalizerTest extends TestCase |
| 9 | +{ |
| 10 | + use DatabaseMigrations; |
| 11 | + |
| 12 | + public function testRegisterAtGates() |
| 13 | + { |
| 14 | + $user = $this->user('alice'); |
| 15 | + $this->assertTrue($user->can('data1,read')); |
| 16 | + $this->assertFalse($user->can('data1,write')); |
| 17 | + $this->assertFalse($user->cannot('data2,read')); |
| 18 | + |
| 19 | + Enforcer::guard('second')->addPolicy('alice', 'data1', 'read'); |
| 20 | + $this->assertTrue($user->can('data1,read', 'second')); |
| 21 | + $this->assertFalse($user->can('data3,read', 'second')); |
| 22 | + } |
| 23 | + |
| 24 | + public function testNotLogin() |
| 25 | + { |
| 26 | + $this->assertFalse(app(Gate::class)->allows('data1,read')); |
| 27 | + $this->assertTrue(app(Gate::class)->forUser($this->user('alice'))->allows('data1,read')); |
| 28 | + $this->assertFalse(app(Gate::class)->forUser($this->user('bob'))->allows('data1,read')); |
| 29 | + } |
| 30 | + |
| 31 | + public function testAfterLogin() |
| 32 | + { |
| 33 | + $this->login('alice'); |
| 34 | + $this->assertTrue(app(Gate::class)->allows('data1,read')); |
| 35 | + $this->assertTrue(app(Gate::class)->allows('data2,read')); |
| 36 | + $this->assertTrue(app(Gate::class)->allows('data2,write')); |
| 37 | + |
| 38 | + $this->login('bob'); |
| 39 | + $this->assertFalse(app(Gate::class)->allows('data1,read')); |
| 40 | + $this->assertTrue(app(Gate::class)->allows('data2,write')); |
| 41 | + } |
| 42 | + |
| 43 | + public function initConfig() |
| 44 | + { |
| 45 | + parent::initConfig(); |
| 46 | + $this->app['config']->set('lauthz.second.model.config_type', 'text'); |
| 47 | + $this->app['config']->set( |
| 48 | + 'lauthz.second.model.config_text', |
| 49 | + $this->getModelText() |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + protected function getModelText(): string |
| 54 | + { |
| 55 | + return <<<EOT |
| 56 | +[request_definition] |
| 57 | +r = sub, obj, act |
| 58 | +
|
| 59 | +[policy_definition] |
| 60 | +p = sub, obj, act |
| 61 | +
|
| 62 | +[policy_effect] |
| 63 | +e = some(where (p.eft == allow)) |
| 64 | +
|
| 65 | +[matchers] |
| 66 | +m = r.sub == p.sub && r.obj == p.obj && r.act == p.act |
| 67 | +EOT; |
| 68 | + } |
| 69 | +} |
0 commit comments