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

testAssignUserToRoleWithRealId_()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
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\Containers\Authorization\Tests\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 setUp()
24
    {
25
        putenv('HASH_ID=true');
26
        parent::setup();
27
    }
28
29
    public function testAssignUserToRole_()
30
    {
31
        $this->getTestingAdmin();
32
33
        $randomUser = factory(User::class)->create();
34
35
        $data = [
36
            'roles_names' => 'admin',
37
            'user_id'     => $randomUser->getHashedKey(),
38
        ];
39
40
        // send the HTTP request
41
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
42
43
        // assert response status is correct
44
        $this->assertEquals('200', $response->getStatusCode());
45
46
        $responseObject = $this->getResponseObject($response);
47
48
        $this->assertEquals($data['user_id'], $responseObject->data->id);
49
50
        $this->assertEquals($data['roles_names'], $responseObject->data->roles->data[0]->name);
51
    }
52
53
    public function testAssignUserToRoleWithRealId_()
54
    {
55
        $this->getTestingAdmin();
56
57
        $randomUser = factory(User::class)->create();
58
59
        $data = [
60
            'roles_names' => 'admin',
61
            'user_id'     => $randomUser->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 testAssignUserToManyRoles_()
76
    {
77
        $this->getTestingUser();
78
79
        $randomUser = factory(User::class)->create();
80
81
        $data = [
82
            'roles_names' => ['admin', 'client'],
83
            'user_id'     => $randomUser->getHashedKey(),
84
        ];
85
86
        // send the HTTP request
87
        $response = $this->apiCall($this->endpoint, 'post', $data, true);
88
89
        // assert response status is correct
90
        $this->assertEquals('200', $response->getStatusCode());
91
92
        $responseObject = $this->getResponseObject($response);
93
94
        $this->assertTrue(count($responseObject->data->roles->data) > 1);
95
96
        $this->assertEquals($data['roles_names'][0], $responseObject->data->roles->data[0]->name);
97
98
        $this->assertEquals($data['roles_names'][1], $responseObject->data->roles->data[1]->name);
99
    }
100
101
}
102