Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — master ( 4a9886...024700 )
by Jérémiah
16:44
created

testNotAuthenticatedUserAccessToUserRoles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the OverblogGraphQLBundle package.
5
 *
6
 * (c) Overblog <http://github.com/overblog/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Overblog\GraphQLBundle\Tests\Functional\Security;
13
14
use Overblog\GraphQLBundle\Tests\Functional\app\Mutation\SimpleMutationWithThunkFieldsMutation;
15
use Overblog\GraphQLBundle\Tests\Functional\TestCase;
16
17
class AccessTest extends TestCase
18
{
19
    const USER_RYAN = 'ryan';
20
    const USER_ADMIN = 'admin';
21
    const ANONYMOUS_USER = null;
22
23
    private $userNameQuery = 'query MyQuery { user { name } }';
24
25
    private $userRolesQuery = 'query MyQuery { user { roles } }';
26
27
    private $userIsEnabledQuery = 'query MyQuery { user { isEnabled } }';
28
29
    private $userFriendsQuery = <<<EOF
30
query MyQuery {
31
  user {
32
    friends(first: 2) {
33
      edges {
34
        node {
35
          name
36
        }
37
      }
38
    }
39
  }
40
}
41
EOF;
42
43
    private $simpleMutationWithThunkQuery = <<<EOF
44
mutation M {
45
  simpleMutationWithThunkFields(input: {inputData: %d, clientMutationId: "bac"}) {
46
    result
47
    clientMutationId
48
  }
49
}
50
EOF;
51
52 View Code Duplication
    public function testNotAuthenticatedUserAccessToUserName()
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
        $expected = [
55
            'data' => [
56
                'user' => [
57
                    'name' => null,
58
                ],
59
            ],
60
            'extensions' => [
61
                'warnings' => [
62
                    [
63
                        'message' => 'Access denied to this field.',
64
                        'locations' => [['line' => 1, 'column' => 24]],
65
                    ],
66
                ],
67
            ],
68
        ];
69
70
        $this->assertResponse($this->userNameQuery, $expected, static::ANONYMOUS_USER);
71
    }
72
73
    public function testFullyAuthenticatedUserAccessToUserName()
74
    {
75
        $expected = [
76
            'data' => [
77
                'user' => [
78
                    'name' => 'Dan',
79
                ],
80
            ],
81
        ];
82
83
        $this->assertResponse($this->userNameQuery, $expected, static::USER_RYAN);
84
    }
85
86
    public function testNotAuthenticatedUserAccessToUserRoles()
87
    {
88
        $this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::ANONYMOUS_USER);
89
    }
90
91
    public function testAuthenticatedUserAccessToUserRolesWithoutEnoughRights()
92
    {
93
        $this->assertResponse($this->userRolesQuery, $this->expectedFailedUserRoles(), static::USER_RYAN);
94
    }
95
96
    public function testUserWithCorrectRightsAccessToUserRoles()
97
    {
98
        $expected = [
99
            'data' => [
100
                'user' => [
101
                    'roles' => ['ROLE_USER'],
102
                ],
103
            ],
104
        ];
105
106
        $this->assertResponse($this->userRolesQuery, $expected, static::USER_ADMIN);
107
    }
108
109
    public function testUserAccessToUserFriends()
110
    {
111
        $expected = [
112
            'data' => [
113
                'user' => [
114
                    'friends' => [
115
                        'edges' => [
116
                            ['node' => ['name' => 'Nick']],
117
                            ['node' => null],
118
                        ],
119
                    ],
120
                ],
121
            ],
122
        ];
123
124
        $this->assertResponse($this->userFriendsQuery, $expected, static::USER_ADMIN);
125
    }
126
127 View Code Duplication
    public function testUserAccessToUserIsEnabledWithExpressionLanguageEvaluationFailed()
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...
128
    {
129
        $expected = [
130
            'data' => [
131
                'user' => [
132
                    'isEnabled' => null,
133
                ],
134
            ],
135
            'extensions' => [
136
                'warnings' => [
137
                    [
138
                        'message' => 'Access denied to this field.',
139
                        'locations' => [['line' => 1, 'column' => 24]],
140
                    ],
141
                ],
142
            ],
143
        ];
144
145
        $this->assertResponse($this->userIsEnabledQuery, $expected, static::USER_ADMIN);
146
    }
147
148
    public function testMutationAllowedUser()
149
    {
150
        $result = 123;
151
152
        $expected = [
153
            'data' => [
154
                'simpleMutationWithThunkFields' => [
155
                    'result' => $result,
156
                    'clientMutationId' => 'bac',
157
                ],
158
            ],
159
        ];
160
161
        $this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, $result), $expected, static::USER_ADMIN);
162
        $this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true));
163
    }
164
165
    public function testMutationAllowedButNoRightsToDisplayPayload()
166
    {
167
        $expected = [
168
            'data' => [
169
                'simpleMutationWithThunkFields' => [
170
                    'result' => null,
171
                    'clientMutationId' => 'bac',
172
                ],
173
            ],
174
            'extensions' => [
175
                'warnings' => [
176
                    [
177
                        'message' => 'Access denied to this field.',
178
                        'locations' => [
179
                            [
180
                                'line' => 3,
181
                                'column' => 5,
182
                            ],
183
                        ],
184
                    ],
185
                ],
186
            ],
187
        ];
188
189
        $this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 321), $expected, static::USER_ADMIN);
190
        $this->assertTrue(SimpleMutationWithThunkFieldsMutation::hasMutate(true));
191
    }
192
193
    public function testMutationNotAllowedUser()
194
    {
195
        $expected = [
196
            'data' => [
197
                'simpleMutationWithThunkFields' => null,
198
            ],
199
            'extensions' => [
200
                'warnings' => [
201
                    [
202
                        'message' => 'Access denied to this field.',
203
                        'locations' => [
204
                            [
205
                                'line' => 2,
206
                                'column' => 3,
207
                            ],
208
                        ],
209
                    ],
210
                ],
211
            ],
212
        ];
213
214
        $this->assertResponse(sprintf($this->simpleMutationWithThunkQuery, 123), $expected, static::USER_RYAN);
215
        $this->assertFalse(SimpleMutationWithThunkFieldsMutation::hasMutate(true));
216
    }
217
218
    private function expectedFailedUserRoles()
219
    {
220
        return [
221
            'data' => [
222
                'user' => [
223
                    'roles' => [0 => null],
224
                ],
225
            ],
226
        ];
227
    }
228
229 View Code Duplication
    private static function assertResponse($query, array $expected, $username)
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...
230
    {
231
        $client = self::createClientAuthenticated($username);
232
        $client->request('GET', '/', ['query' => $query]);
233
234
        $result = $client->getResponse()->getContent();
235
236
        static::assertEquals($expected, json_decode($result, true), $result);
237
238
        return $client;
239
    }
240
241
    private static function createClientAuthenticated($username)
242
    {
243
        $client = static::createClient(['test_case' => 'access']);
244
245
        if ($username) {
246
            $client->setServerParameters([
247
                'PHP_AUTH_USER' => $username,
248
                'PHP_AUTH_PW' => '123',
249
            ]);
250
        }
251
252
        return $client;
253
    }
254
}
255