1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overwatch\UserBundle\Tests\Controller; |
4
|
|
|
|
5
|
|
|
use Overwatch\UserBundle\DataFixtures\ORM\UserFixtures; |
6
|
|
|
use Overwatch\UserBundle\Enum\AlertSetting; |
7
|
|
|
use Overwatch\UserBundle\Tests\Base\DatabaseAwareTestCase; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* ApiControllerTest |
12
|
|
|
* Functional test for API method provided by ApiController |
13
|
|
|
*/ |
14
|
|
|
class ApiControllerTest extends DatabaseAwareTestCase |
15
|
|
|
{ |
16
|
|
|
public function testCreateUser() |
17
|
|
|
{ |
18
|
|
|
$email = '[email protected]'; |
19
|
|
|
|
20
|
|
|
$this->loginAs(UserFixtures::$users['user-1'], 'overwatchApi'); |
21
|
|
|
$this->client = $this->makeClient(); //When using loginAs, we must re-create the client |
22
|
|
|
$this->client->enableProfiler(); |
23
|
|
|
$this->client->request('POST', '/api/users/' . $email); |
24
|
|
|
|
25
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
26
|
|
|
|
27
|
|
|
$user = $this->em->getRepository('Overwatch\UserBundle\Entity\User')->findOneBy([ |
28
|
|
|
'email' => $email |
29
|
|
|
]); |
30
|
|
|
|
31
|
|
|
$this->assertInstanceOf('Overwatch\UserBundle\Entity\User', $user); |
32
|
|
|
$this->assertEquals($email, $user->getUsername()); |
33
|
|
|
$this->assertEquals($email, $user->getEmail()); |
34
|
|
|
|
35
|
|
|
$mailCollector = $this->client->getProfile()->getCollector('swiftmailer'); |
36
|
|
|
$this->assertEquals(1, $mailCollector->getMessageCount()); |
37
|
|
|
|
38
|
|
|
$message = $mailCollector->getMessages()[0]; |
39
|
|
|
$this->assertInstanceOf('Swift_Message', $message); |
40
|
|
|
$this->assertEquals('You have been invited to Overwatch', $message->getSubject()); |
41
|
|
|
$this->assertEquals(UserFixtures::$users['user-1']->getEmail(), key($message->getFrom())); |
42
|
|
|
$this->assertEquals($email, key($message->getTo())); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testCreateUserInsufficentPerms() |
46
|
|
|
{ |
47
|
|
|
$this->logIn('ROLE_ADMIN'); |
48
|
|
|
$this->client->request('POST', '/api/users/[email protected]'); |
49
|
|
|
|
50
|
|
|
$this->assertForbidden($this->client->getResponse()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function testGetAlertSettings() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$this->logIn('ROLE_USER'); |
56
|
|
|
$this->client->request('GET', '/api/alertSettings'); |
57
|
|
|
|
58
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
59
|
|
|
$this->assertJsonStringEqualsJsonString( |
60
|
|
|
json_encode(AlertSetting::getAll()), |
61
|
|
|
$this->getResponseContent(true) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function testFindUser() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
68
|
|
|
$this->client->request('GET', '/api/users/' . UserFixtures::$users['user-1']); |
69
|
|
|
|
70
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
71
|
|
|
$this->assertJsonStringEqualsJsonString( |
72
|
|
|
json_encode($this->em->find( |
73
|
|
|
'Overwatch\UserBundle\Entity\User', |
74
|
|
|
UserFixtures::$users['user-1'] |
75
|
|
|
)), |
76
|
|
|
$this->getResponseContent(true) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function testFindUserInsufficentPerms() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$this->logIn('ROLE_ADMIN'); |
83
|
|
|
$this->client->request('GET', '/api/users/' . UserFixtures::$users['user-1']); |
84
|
|
|
|
85
|
|
|
$this->assertForbidden($this->client->getResponse()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function testFindUserInvalidUser() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
91
|
|
|
$this->client->request('GET', '/api/users/[email protected]'); |
92
|
|
|
|
93
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
public function testGetAllUsers() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
99
|
|
|
$this->client->request('GET', '/api/users'); |
100
|
|
|
|
101
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
102
|
|
|
$this->assertJsonStringEqualsJsonString( |
103
|
|
|
json_encode($this->em->getRepository( |
104
|
|
|
'Overwatch\UserBundle\Entity\User' |
105
|
|
|
)->findAll()), |
106
|
|
|
$this->getResponseContent(true) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testGetAllUsersInsufficentPerms() |
111
|
|
|
{ |
112
|
|
|
$this->logIn('ROLE_ADMIN'); |
113
|
|
|
$this->client->request('GET', '/api/users'); |
114
|
|
|
|
115
|
|
|
$this->assertForbidden($this->client->getResponse()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testUpdateUser() |
119
|
|
|
{ |
120
|
|
|
$this->loginAs( |
121
|
|
|
$this->em->find( |
122
|
|
|
'Overwatch\UserBundle\Entity\User', |
123
|
|
|
UserFixtures::$users['user-2']->getId() |
124
|
|
|
), |
125
|
|
|
'overwatchApi' |
126
|
|
|
); |
127
|
|
|
$this->client = $this->makeClient(); //When using loginAs, we must re-create the client |
128
|
|
|
$this->client->request( |
129
|
|
|
'PUT', |
130
|
|
|
'/api/users', |
131
|
|
|
[ |
132
|
|
|
'alertSetting' => 1, |
133
|
|
|
'telephoneNumber' => '+447981123456' |
134
|
|
|
] |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
138
|
|
|
|
139
|
|
|
$user = $this->em->find('Overwatch\UserBundle\Entity\User', UserFixtures::$users['user-2']->getId()); |
140
|
|
|
$this->assertEquals( |
141
|
|
|
1, |
142
|
|
|
$user->getAlertSetting() |
143
|
|
|
); |
144
|
|
|
$this->assertEquals( |
145
|
|
|
'+447981123456', |
146
|
|
|
$user->getTelephoneNumber() |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
View Code Duplication |
public function testToggleLockUser() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$this->loginAs(UserFixtures::$users['user-1'], 'overwatchApi'); |
153
|
|
|
$this->client = $this->makeClient(); //When using loginAs, we must re-create the client |
154
|
|
|
$this->client->request('POST', '/api/users/' . UserFixtures::$users['user-2']->getId() . '/lock'); |
155
|
|
|
|
156
|
|
|
$user = $this->em->find('Overwatch\UserBundle\Entity\User', UserFixtures::$users['user-2']->getId()); |
157
|
|
|
$this->assertTrue($user->isLocked()); |
158
|
|
|
|
159
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
160
|
|
|
$this->assertJsonStringEqualsJsonString( |
161
|
|
|
json_encode($user), |
162
|
|
|
$this->getResponseContent(true) |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
View Code Duplication |
public function testToggleLockUserInsufficentPerms() |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$this->logIn('ROLE_ADMIN'); |
169
|
|
|
$this->client->request('POST', '/api/users/' . UserFixtures::$users['user-1']->getId() . '/lock'); |
170
|
|
|
|
171
|
|
|
$this->assertForbidden($this->client->getResponse()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
View Code Duplication |
public function testToggleLockUserDisallowSelf() |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$this->loginAs(UserFixtures::$users['user-1'], 'overwatchApi'); |
177
|
|
|
$this->client = $this->makeClient(); //When using loginAs, we must re-create the client |
178
|
|
|
$this->client->request('POST', '/api/users/' . UserFixtures::$users['user-1']->getId() . '/lock'); |
179
|
|
|
|
180
|
|
|
$this->assertForbidden($this->client->getResponse()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
View Code Duplication |
public function testSetUserRole() |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
$this->loginAs(UserFixtures::$users['user-1'], 'overwatchApi'); |
186
|
|
|
$this->client = $this->makeClient(); //When using loginAs, we must re-create the client |
187
|
|
|
$this->client->request('POST', '/api/users/' . UserFixtures::$users['user-2']->getId() . '/role/ROLE_ADMIN'); |
188
|
|
|
|
189
|
|
|
$user = $this->em->find('Overwatch\UserBundle\Entity\User', UserFixtures::$users['user-2']->getId()); |
190
|
|
|
$this->assertTrue($user->hasRole('ROLE_ADMIN')); |
191
|
|
|
|
192
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
193
|
|
|
$this->assertJsonStringEqualsJsonString( |
194
|
|
|
json_encode($user), |
195
|
|
|
$this->getResponseContent(true) |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
View Code Duplication |
public function testSetUserRoleInsufficentPerms() |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
$this->logIn('ROLE_ADMIN'); |
202
|
|
|
$this->client->request('POST', '/api/users/' . UserFixtures::$users['user-1']->getId() . '/role/ROLE_USER'); |
203
|
|
|
|
204
|
|
|
$this->assertForbidden($this->client->getResponse()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
View Code Duplication |
public function testSetUserRoleDisallowSelf() |
|
|
|
|
208
|
|
|
{ |
209
|
|
|
$this->loginAs(UserFixtures::$users['user-1'], 'overwatchApi'); |
210
|
|
|
$this->client = $this->makeClient(); //When using loginAs, we must re-create the client |
211
|
|
|
$this->client->request('POST', '/api/users/' . UserFixtures::$users['user-1']->getId() . '/role/ROLE_USER'); |
212
|
|
|
|
213
|
|
|
$this->assertForbidden($this->client->getResponse()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testDeleteUser() |
217
|
|
|
{ |
218
|
|
|
$this->loginAs(UserFixtures::$users['user-1'], 'overwatchApi'); |
219
|
|
|
$this->client = $this->makeClient(); //When using loginAs, we must re-create the client |
220
|
|
|
$this->client->request('DELETE', '/api/users/' . UserFixtures::$users['user-2']->getId()); |
221
|
|
|
|
222
|
|
|
$user = $this->em->find('Overwatch\UserBundle\Entity\User', UserFixtures::$users['user-2']->getId()); |
223
|
|
|
$this->assertNull($user); |
224
|
|
|
|
225
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $this->client->getResponse()->getStatusCode()); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
View Code Duplication |
public function testDeleteUserInsufficentPerms() |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
$this->logIn('ROLE_ADMIN'); |
231
|
|
|
$this->client->request('DELETE', '/api/users/' . UserFixtures::$users['user-1']->getId()); |
232
|
|
|
|
233
|
|
|
$this->assertForbidden($this->client->getResponse()); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
View Code Duplication |
public function testDeleteUserDisallowSelf() |
|
|
|
|
237
|
|
|
{ |
238
|
|
|
$this->loginAs(UserFixtures::$users['user-1'], 'overwatchApi'); |
239
|
|
|
$this->client = $this->makeClient(); //When using loginAs, we must re-create the client |
240
|
|
|
$this->client->request('DELETE', '/api/users/' . UserFixtures::$users['user-1']->getId()); |
241
|
|
|
|
242
|
|
|
$this->assertForbidden($this->client->getResponse()); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
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.