Completed
Push — master ( 0476dc...911884 )
by Mahmoud
03:48
created

AssignUserToRoleTest::testAssignUserToRole_()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 12
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\Authorization\Tests\TestCase;
7
use App\Containers\User\Models\User;
8
9
/**
10
 * Class AssignUserToRoleTest.
11
 *
12
 * @author  Mahmoud Zalt <[email protected]>
13
 */
14
class AssignUserToRoleTest extends TestCase
15
{
16
17
    protected $endpoint = '/roles/assign';
18
19
    protected $access = [
20
        'roles'       => 'admin',
21
        'permissions' => '',
22
    ];
23
24
    public function testAssignUserToRole_()
25
    {
26
        $this->getTestingAdmin();
27
28
        $randomUser = factory(User::class)->create();
29
30
        $role = factory(Role::class)->create();
31
32
        $data = [
33
            'roles_ids' => $role->getHashedKey(),
34
            'user_id'   => $randomUser->getHashedKey(),
35
        ];
36
37
        // send the HTTP request
38
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
39
40
        // assert response status is correct
41
        $this->assertEquals('200', $response->getStatusCode());
42
43
        $responseObject = $this->getResponseObject($response);
44
45
        $this->assertEquals($data['user_id'], $responseObject->data->id);
46
47
        $this->assertEquals($data['roles_ids'], $responseObject->data->roles->data[0]->id);
48
    }
49
50
    public function testAssignUserToRoleWithRealId_()
51
    {
52
        $this->getTestingAdmin();
53
54
        $randomUser = factory(User::class)->create();
55
56
        $data = [
57
            'roles_ids' => 'admin',
58
            'user_id'   => $randomUser->id,
59
        ];
60
61
        // send the HTTP request
62
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
63
64
        // assert response status is correct. Note: this will return 200 if `HASH_ID=false` in the .env
65
        $this->assertEquals('400', $response->getStatusCode());
66
67
        $this->assertResponseContainKeyValue([
68
            'message' => 'Only Hashed ID\'s allowed to be passed.',
69
        ], $response);
70
    }
71
72
    public function testAssignUserToManyRoles_()
73
    {
74
        $this->getTestingUser();
75
76
        $randomUser = factory(User::class)->create();
77
78
        $data = [
79
            'roles_ids' => ['admin', 'client'],
80
            'user_id'   => $randomUser->getHashedKey(),
81
        ];
82
83
        // send the HTTP request
84
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
85
86
        // assert response status is correct
87
        $this->assertEquals('200', $response->getStatusCode());
88
89
        $responseObject = $this->getResponseObject($response);
90
91
        $this->assertTrue(count($responseObject->data->roles->data) > 1);
92
93
        $this->assertEquals($data['roles_ids'][0], $responseObject->data->roles->data[0]->name);
94
95
        $this->assertEquals($data['roles_ids'][1], $responseObject->data->roles->data[1]->name);
96
    }
97
98
}
99