Completed
Push — master ( 730998...f24d78 )
by Mahmoud
03:24
created

RevokeUserFromRoleTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 96
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B testRevokeUserFromRole_() 0 24 1
A testRevokeUserFromRoleWithRealId_() 0 19 1
B testRevokeUserFromManyRoles_() 0 39 1
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\Port\Test\PHPUnit\Abstracts\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 testRevokeUserFromRole_()
25
    {
26
        $admin = $this->getTestingAdmin();
27
28
        $data = [
29
            'roles_names' => 'admin',
30
            'user_id'     => $admin->getHashedKey(),
31
        ];
32
33
        // send the HTTP request
34
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
35
36
        // assert response status is correct
37
        $this->assertEquals('200', $response->getStatusCode());
38
39
        $responseObject = $this->getResponseObject($response);
40
41
        $this->assertEquals($data['user_id'], $responseObject->data->id);
42
43
        $this->missingFromDatabase('user_has_roles', [
44
            'user_id' => $admin->id,
45
            'role_id' => 2, // for admin, manually setting it now
46
        ]);
47
    }
48
49
    public function testRevokeUserFromRoleWithRealId_()
50
    {
51
        $admin = $this->getTestingAdmin();
52
53
        $data = [
54
            'roles_names' => 'admin',
55
            'user_id'     => $admin->id,
56
        ];
57
58
        // send the HTTP request
59
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
60
61
        // assert response status is correct. Note: this will return 200 if `HASH_ID=false` in the .env
62
        $this->assertEquals('400', $response->getStatusCode());
63
64
        $this->assertResponseContainKeyValue([
65
            'message' => 'Only Hashed ID\'s allowed to be passed.',
66
        ], $response);
67
    }
68
69
    public function testRevokeUserFromManyRoles_()
70
    {
71
        $this->getTestingUser();
72
73
        $randomUser = factory(User::class)->create();
74
75
        $roleA = Role::create([
76
            'name'         => 'role-A',
77
            'description'  => 'AA',
78
            'display_name' => 'A',
79
        ]);
80
81
        $roleB = Role::create([
82
            'name'         => 'role-B',
83
            'description'  => 'BB',
84
            'display_name' => 'B',
85
        ]);
86
87
        $randomUser->assignRole($roleA);
88
        $randomUser->assignRole($roleB);
89
90
        $data = [
91
            'roles_names' => ['role-A', 'role-B'],
92
            'user_id'     => $randomUser->getHashedKey(),
93
        ];
94
95
        // send the HTTP request
96
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
97
98
        // assert response status is correct
99
        $this->assertEquals('200', $response->getStatusCode());
100
101
        $this->missingFromDatabase('user_has_roles', [
102
            'user_id' => $randomUser->id,
103
            'role_id' => $roleB->id,
104
            'role_id' => $roleA->id,
105
        ]);
106
107
    }
108
109
}
110