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

AssignUserToRoleTest::testAssignUserToRole_()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace App\Containers\Authorization\UI\API\Tests\Functional;
4
5
use App\Containers\User\Models\User;
6
use App\Port\Test\PHPUnit\Abstracts\TestCase;
7
8
/**
9
 * Class AssignUserToRoleTest.
10
 *
11
 * @author  Mahmoud Zalt <[email protected]>
12
 */
13
class AssignUserToRoleTest extends TestCase
14
{
15
16
    protected $endpoint = '/roles/assign';
17
18
    protected $access = [
19
        'roles'       => 'admin',
20
        'permissions' => '',
21
    ];
22
23
    public function testAssignUserToRole_()
24
    {
25
        $this->getTestingAdmin();
26
27
        $randomUser = factory(User::class)->create();
28
29
        $data = [
30
            'roles_names' => 'admin',
31
            'user_id'     => $randomUser->getHashedKey(),
32
        ];
33
34
        // send the HTTP request
35
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
36
37
        // assert response status is correct
38
        $this->assertEquals('200', $response->getStatusCode());
39
40
        $responseObject = $this->getResponseObject($response);
41
42
        $this->assertEquals($data['user_id'], $responseObject->data->id);
43
44
        $this->assertEquals($data['roles_names'], $responseObject->data->roles->data[0]->name);
45
    }
46
47
    public function testAssignUserToRoleWithRealId_()
48
    {
49
        $this->getTestingAdmin();
50
51
        $randomUser = factory(User::class)->create();
52
53
        $data = [
54
            'roles_names' => 'admin',
55
            'user_id'     => $randomUser->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 testAssignUserToManyRoles_()
70
    {
71
        $this->getTestingUser();
72
73
        $randomUser = factory(User::class)->create();
74
75
        $data = [
76
            'roles_names' => ['admin', 'client'],
77
            'user_id'     => $randomUser->getHashedKey(),
78
        ];
79
80
        // send the HTTP request
81
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
82
83
        // assert response status is correct
84
        $this->assertEquals('200', $response->getStatusCode());
85
86
        $responseObject = $this->getResponseObject($response);
87
88
        $this->assertTrue(count($responseObject->data->roles->data) > 1);
89
90
        $this->assertEquals($data['roles_names'][0], $responseObject->data->roles->data[0]->name);
91
92
        $this->assertEquals($data['roles_names'][1], $responseObject->data->roles->data[1]->name);
93
    }
94
95
}
96