Completed
Pull Request — master (#93)
by Johan
03:20
created

testRevokeUserFromRoleWithRealId_()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace App\Containers\Authorization\UI\API\Tests\Functional;
4
5
use App\Containers\Authorization\Models\Role;
6
use App\Containers\User\Models\User;
7
use App\Containers\Authorization\Tests\TestCase;
8
9
/**
10
 * Class RevokeUserFromRoleTest.
11
 *
12
 * @author  Mahmoud Zalt <[email protected]>
13
 */
14
class RevokeUserFromRoleTest extends TestCase
15
{
16
17
    protected $endpoint = '/roles/revoke';
18
19
    protected $access = [
20
        'roles'       => 'admin',
21
        'permissions' => '',
22
    ];
23
24
    public function setUp()
25
    {
26
        putenv('HASH_ID=true');
27
        parent::setup();
28
    }
29
30
    public function testRevokeUserFromRole_()
31
    {
32
        $admin = $this->getTestingAdmin();
33
34
        $data = [
35
            'roles_names' => 'admin',
36
            'user_id'     => $admin->getHashedKey(),
37
        ];
38
39
        // send the HTTP request
40
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
41
42
        // assert response status is correct
43
        $this->assertEquals('200', $response->getStatusCode());
44
45
        $responseObject = $this->getResponseObject($response);
46
47
        $this->assertEquals($data['user_id'], $responseObject->data->id);
48
49
        $this->missingFromDatabase('user_has_roles', [
50
            'user_id' => $admin->id,
51
            'role_id' => 2, // for admin, manually setting it now
52
        ]);
53
    }
54
55
    public function testRevokeUserFromRoleWithRealId_()
56
    {
57
        $admin = $this->getTestingAdmin();
58
59
        $data = [
60
            'roles_names' => 'admin',
61
            'user_id'     => $admin->id,
62
        ];
63
64
        // send the HTTP request
65
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
66
67
        // assert response status is correct. Note: this will return 200 if `HASH_ID=false` in the .env
68
        $this->assertEquals('400', $response->getStatusCode());
69
70
        $this->assertResponseContainKeyValue([
71
            'message' => 'Only Hashed ID\'s allowed to be passed.',
72
        ], $response);
73
    }
74
75
    public function testRevokeUserFromManyRoles_()
76
    {
77
        $this->getTestingUser();
78
79
        $randomUser = factory(User::class)->create();
80
81
        $roleA = Role::create([
82
            'name'         => 'role-A',
83
            'description'  => 'AA',
84
            'display_name' => 'A',
85
        ]);
86
87
        $roleB = Role::create([
88
            'name'         => 'role-B',
89
            'description'  => 'BB',
90
            'display_name' => 'B',
91
        ]);
92
93
        $randomUser->assignRole($roleA);
94
        $randomUser->assignRole($roleB);
95
96
        $data = [
97
            'roles_names' => ['role-A', 'role-B'],
98
            'user_id'     => $randomUser->getHashedKey(),
99
        ];
100
101
        // send the HTTP request
102
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
103
104
        // assert response status is correct
105
        $this->assertEquals('200', $response->getStatusCode());
106
107
        $this->missingFromDatabase('user_has_roles', [
108
            'user_id' => $randomUser->id,
109
            'role_id' => $roleB->id,
110
            'role_id' => $roleA->id,
111
        ]);
112
113
    }
114
115
}
116