1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMG\ManagerBundle\Tests\Controller; |
4
|
|
|
|
5
|
|
|
use SMG\UserBundle\Entity\User; |
6
|
|
|
use SMG\OauthBundle\Entity\Client; |
7
|
|
|
use SMG\UserBundle\Tests\TestHelpersTrait; |
8
|
|
|
use Liip\FunctionalTestBundle\Test\WebTestCase; |
9
|
|
|
|
10
|
|
|
class ManagersControllerTest extends WebTestCase |
11
|
|
|
{ |
12
|
|
|
const ADMIN_USER_NAME = 'admin'; |
13
|
|
|
const ADMIN_USER_PW = 'admin'; |
14
|
|
|
const COMMON_USER_NAME = 'Bobthesponge'; |
15
|
|
|
const COMMON_USER_PW = 'plop'; |
16
|
|
|
|
17
|
|
|
use TestHelpersTrait; |
18
|
|
|
|
19
|
|
|
private $user; |
20
|
|
|
private $oauthClient; |
21
|
|
|
|
22
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$this->client = static::createClient(); |
25
|
|
|
$fixtures = [ |
26
|
|
|
'SMG\UserBundle\DataFixtures\ORM\LoadUserData', |
27
|
|
|
'SMG\UserBundle\DataFixtures\ORM\LoadClientData', |
28
|
|
|
]; |
29
|
|
|
$fixtureExecutor = $this->loadFixtures($fixtures); |
30
|
|
|
$this->em = $fixtureExecutor->getObjectManager(); |
31
|
|
|
$this->fixtures = $fixtureExecutor->getReferenceRepository(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// Tests for PATCH /admin/users/{id}/password |
35
|
|
|
|
36
|
|
|
public function testPatchUserPasswordAsAdminUpdatePassword() |
37
|
|
|
{ |
38
|
|
|
$this->givenUser('new-user'); |
39
|
|
|
$this->givenClient('new-client'); |
40
|
|
|
|
41
|
|
|
$this->loginClientAsAdmin(); |
42
|
|
|
|
43
|
|
|
$payload = [ |
44
|
|
|
'new_password' => 'toto', |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
$this->performPatchUser( |
48
|
|
|
'/password', |
49
|
|
|
$payload |
50
|
|
|
); |
51
|
|
|
$this->assertNoContentResponse(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testPatchUserPasswordAsCommonUserReturn403() |
55
|
|
|
{ |
56
|
|
|
$this->givenUser('new-user'); |
57
|
|
|
$this->givenClient('new-client'); |
58
|
|
|
|
59
|
|
|
$this->loginAsCommonUser(); |
60
|
|
|
|
61
|
|
|
$payload = [ |
62
|
|
|
'new_password' => 'toto', |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
$this->performPatchUser( |
66
|
|
|
'/password', |
67
|
|
|
$payload |
68
|
|
|
); |
69
|
|
|
$this->assertPermissionDenied(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// conveniency methods |
73
|
|
|
|
74
|
|
|
private function performPatchUser($endpoint, array $userPayload) |
75
|
|
|
{ |
76
|
|
|
$this->performJsonClientRequest( |
77
|
|
|
'PUT', |
78
|
|
|
'/admin/users/'.$this->user->getId().$endpoint, |
79
|
|
|
$userPayload |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
private function performJsonClientRequest( |
|
|
|
|
84
|
|
|
$method, |
85
|
|
|
$endpoint, |
86
|
|
|
$payload |
87
|
|
|
) { |
88
|
|
|
$jsonHeaders = ['CONTENT_TYPE' => 'application/json']; |
89
|
|
|
$jsonBody = json_encode($payload); |
90
|
|
|
|
91
|
|
|
$this->response = $this->performClientRequest( |
92
|
|
|
$method, |
93
|
|
|
$endpoint, |
94
|
|
|
$jsonHeaders, |
95
|
|
|
$jsonBody |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
private function loginClientAsAdmin() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$link = '/oauth/v2/token?'. |
102
|
|
|
'client_id='.$this->oauthClient->getPublicId(). |
103
|
|
|
'&client_secret='.$this->oauthClient->getSecret(). |
104
|
|
|
'&grant_type=password'. |
105
|
|
|
'&username='.self::ADMIN_USER_NAME. |
106
|
|
|
'&password='.self::ADMIN_USER_PW |
107
|
|
|
; |
108
|
|
|
|
109
|
|
|
$this->createLoginClient($link); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
private function loginAsCommonUser() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$link = '/oauth/v2/token?'. |
115
|
|
|
'client_id='.$this->oauthClient->getPublicId(). |
116
|
|
|
'&client_secret='.$this->oauthClient->getSecret(). |
117
|
|
|
'&grant_type=password'. |
118
|
|
|
'&username='.self::COMMON_USER_NAME. |
119
|
|
|
'&password='.self::COMMON_USER_PW |
120
|
|
|
; |
121
|
|
|
|
122
|
|
|
$this->createLoginClient($link); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function createLoginClient($link) |
126
|
|
|
{ |
127
|
|
|
$this->client->request('GET', $link); |
128
|
|
|
|
129
|
|
|
$response = $this->client->getResponse()->getContent(); |
130
|
|
|
|
131
|
|
|
$decode = json_decode($response, true); |
132
|
|
|
|
133
|
|
|
$this->client = static::createClient( |
134
|
|
|
array(), |
135
|
|
|
array('HTTP_Authorization' => "Bearer {$decode['access_token']}") |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function givenUser($fixtureName) |
140
|
|
|
{ |
141
|
|
|
$this->user = $this->fixtures->getReference($fixtureName); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private function givenClient($fixtureName) |
145
|
|
|
{ |
146
|
|
|
$this->oauthClient = $this->fixtures->getReference($fixtureName); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
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.