Completed
Push — master ( b6a9d1...1b7f7e )
by Mahmoud
03:41
created

testRevokeUserFromRoleWithRealId_()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 15

Duplication

Lines 28
Ratio 100 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 28
loc 28
rs 8.8571
cc 2
eloc 15
nc 2
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 RevokeUserFromRoleTest.
11
 *
12
 * @author  Mahmoud Zalt <[email protected]>
13
 */
14
class RevokeUserFromRoleTest extends TestCase
15
{
16
17
    protected $endpoint = 'post@roles/revoke';
18
19
    protected $access = [
20
        'roles'       => 'admin',
21
        'permissions' => '',
22
    ];
23
24 View Code Duplication
    public function testRevokeUserFromRole_()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26
        $roleA = factory(Role::class)->create();
27
28
        $randomUser = factory(User::class)->create();
29
        $randomUser->assignRole($roleA);
30
31
        $data = [
32
            'roles_ids' => [$roleA->getHashedKey()],
33
            'user_id'   => $randomUser->getHashedKey(),
34
        ];
35
36
        // send the HTTP request
37
        $response = $this->makeCall($data);
38
39
        // assert response status is correct
40
        $this->assertEquals('200', $response->getStatusCode());
41
42
        $responseContent = $this->getResponseContent($response);
43
44
        $this->assertEquals($data['user_id'], $responseContent->data->id);
45
46
        $this->missingFromDatabase('user_has_roles', [
47
            'user_id' => $randomUser->id,
48
            'role_id' => $roleA->id,
49
        ]);
50
    }
51
52 View Code Duplication
    public function testRevokeUserFromRoleWithRealId_()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $roleA = factory(Role::class)->create();
55
56
        $randomUser = factory(User::class)->create();
57
        $randomUser->assignRole($roleA);
58
59
        $data = [
60
            'roles_ids' => [$roleA->id],
61
            'user_id'   => $randomUser->id,
62
        ];
63
64
        // send the HTTP request
65
        $response = $this->makeCall($data);
66
67
68
        // assert response status is correct. Note: this will return 200 if `HASH_ID=false` in the .env
69
        if(\Config::get('hello.hash-id')){
70
            $this->assertEquals('400', $response->getStatusCode());
71
72
            $this->assertResponseContainKeyValue([
73
                'message' => 'Only Hashed ID\'s allowed (roles_ids.*).',
74
            ], $response);
75
        }else{
76
            $this->assertEquals('200', $response->getStatusCode());
77
        }
78
79
    }
80
81
    public function testRevokeUserFromManyRoles_()
82
    {
83
        $roleA = factory(Role::class)->create();
84
        $roleB = factory(Role::class)->create();
85
86
        $randomUser = factory(User::class)->create();
87
        $randomUser->assignRole($roleA);
88
        $randomUser->assignRole($roleB);
89
90
        $data = [
91
            'roles_ids' => [$roleA->getHashedKey(), $roleB->getHashedKey()],
92
            'user_id'   => $randomUser->getHashedKey(),
93
        ];
94
95
        // send the HTTP request
96
        $response = $this->makeCall($data);
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