1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Modules\Security\Integration; |
4
|
|
|
|
5
|
|
|
use App\Modules\Security\Api\Command\ChangeUserPasswordCommand; |
6
|
|
|
use App\Modules\Security\Api\Command\CreateUserCommand; |
7
|
|
|
use App\Modules\Security\Api\Command\RenameUserCommand; |
8
|
|
|
use App\Modules\Security\Api\Event\Inbound\CommentsCountUpdatedSecurityIEvent; |
9
|
|
|
use App\Modules\Security\Api\Event\Inbound\PostCreatedSecurityIEvent; |
10
|
|
|
use App\Modules\Security\Api\Event\Inbound\PostUpdatedSecurityIEvent; |
11
|
|
|
use App\Modules\Security\Api\Query\FindPostsByUserIdQuery; |
12
|
|
|
use App\Modules\Security\Api\Query\Response\FindPostsByUserIdQueryResponse; |
13
|
|
|
use App\Modules\Security\Api\SecurityApiInterface; |
14
|
|
|
use App\Modules\Security\Domain\Event\Outbound\UserRenamedOEvent; |
15
|
|
|
use App\Tests\Modules\Security\Integration\Http\SecurityHttpTrait; |
16
|
|
|
use App\Tests\TestUtils\Contracts\ApplicationEventContractLoader; |
17
|
|
|
use App\Tests\TestUtils\Events\InMemoryEventPublisher; |
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
19
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
20
|
|
|
use Symfony\Component\Uid\Ulid; |
21
|
|
|
|
22
|
|
|
class SecurityIT extends SecurityIntegrationTest |
23
|
|
|
{ |
24
|
|
|
use SecurityHttpTrait; |
25
|
|
|
use ApplicationEventContractLoader; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @test |
29
|
|
|
*/ |
30
|
|
|
public function shouldUpdateCommentsCountUponCommentsCountUpdatedIEvent(): void |
31
|
|
|
{ |
32
|
|
|
//given: there was a PostCreatedIEvent to be published |
33
|
|
|
$event = new PostCreatedSecurityIEvent($this->getInboundEvent("Security/PostCreatedSecurityIEvent")); |
34
|
|
|
|
35
|
|
|
//and: the Event was already published |
36
|
|
|
$this->getSecurityApi()->onPostCreated($event); |
37
|
|
|
|
38
|
|
|
//and: the Header was to be updated |
39
|
|
|
$event = new PostUpdatedSecurityIEvent($this->getInboundEvent("Security/PostUpdatedSecurityIEvent")); |
40
|
|
|
|
41
|
|
|
//when: the Event was published |
42
|
|
|
$this->getSecurityApi()->onPostUpdated($event); |
43
|
|
|
|
44
|
|
|
//and: the Header was to be updated |
45
|
|
|
$event = new CommentsCountUpdatedSecurityIEvent($this->getInboundEvent("Security/CommentsCountUpdatedSecurityIEvent")); |
46
|
|
|
|
47
|
|
|
//when: the Event was published |
48
|
|
|
$this->getSecurityApi()->onCommentsCountUpdated($event); |
49
|
|
|
|
50
|
|
|
//then: Post Header was created - no Exception was thrown |
51
|
|
|
$headers = $this->findPostsByUserId(new FindPostsByUserIdQuery($this->getUserId(), 1)); |
52
|
|
|
self::assertNotNull($headers); |
53
|
|
|
self::assertCount(1, $headers->getData()); |
54
|
|
|
self::assertEquals(1, $headers->getCount()); |
55
|
|
|
self::assertTrue(isset($headers->getData()[0])); |
56
|
|
|
$post = $this->convert($headers->getData()[0], FindPostsByUserIdQueryResponse::class); |
57
|
|
|
self::assertEquals($event->getPostId(), $post->getId()); |
58
|
|
|
self::assertEquals($event->getCommentsCount(), $post->getCommentsCount()); |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function getSecurityApi(): SecurityApiInterface |
63
|
|
|
{ |
64
|
|
|
return $this->getContainer()->get(SecurityApiInterface::class); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function getUserId(): Ulid |
68
|
|
|
{ |
69
|
|
|
$tokenParts = explode(".", $this->token); |
|
|
|
|
70
|
|
|
$tokenPayload = base64_decode($tokenParts[1]); |
71
|
|
|
$jwtPayload = json_decode($tokenPayload, true); |
72
|
|
|
return new Ulid($jwtPayload['id']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @test |
77
|
|
|
*/ |
78
|
|
|
public function shouldCreateNewUserAndUpdateIt(): void |
79
|
|
|
{ |
80
|
|
|
//given: There is a new user to be created |
81
|
|
|
$userName = "[email protected]"; |
82
|
|
|
$password = "itPassword"; |
83
|
|
|
$roles = "ROLE_ADMIN"; |
84
|
|
|
|
85
|
|
|
//and: the CLI application is bootable |
86
|
|
|
$application = $this->setupKernel(); |
87
|
|
|
|
88
|
|
|
//when: the user is created using CLI Command |
89
|
|
|
$command = $application->find('app:security:create-user'); |
90
|
|
|
$commandTester = new CommandTester($command); |
91
|
|
|
$commandTester->execute(array( |
92
|
|
|
'command' => $command->getName(), |
93
|
|
|
'login' => $userName, |
94
|
|
|
'password' => $password, |
95
|
|
|
'roles' => $roles, |
96
|
|
|
)); |
97
|
|
|
|
98
|
|
|
//then: the user was created successfully |
99
|
|
|
$output = $commandTester->getDisplay(); |
100
|
|
|
$this->assertStringStartsWith("Successfully created user '$userName' with id", $output); |
101
|
|
|
|
102
|
|
|
//when: the user is created using CLI Command |
103
|
|
|
$newUserName = "[email protected]"; |
104
|
|
|
$command = $application->find('app:security:rename-user'); |
105
|
|
|
$commandTester = new CommandTester($command); |
106
|
|
|
$commandTester->execute(array( |
107
|
|
|
'command' => $command->getName(), |
108
|
|
|
'login' => $userName, |
109
|
|
|
'newLogin' => $newUserName, |
110
|
|
|
)); |
111
|
|
|
|
112
|
|
|
//then: the user was renamed successfully |
113
|
|
|
$output = $commandTester->getDisplay(); |
114
|
|
|
$this->assertStringStartsWith("Successfully renamed user '$userName' to '$newUserName'", $output); |
115
|
|
|
|
116
|
|
|
//and: The RenamedUserOEvent was Published |
117
|
|
|
$events = InMemoryEventPublisher::get(UserRenamedOEvent::class); |
118
|
|
|
self::assertCount(1, $events); |
119
|
|
|
self::assertEquals($userName, $events[0]->getData()['oldLogin']); |
120
|
|
|
self::assertEquals($newUserName, $events[0]->getData()['newLogin']); |
121
|
|
|
|
122
|
|
|
//when: the user is created using CLI Command |
123
|
|
|
$newPassword = "itPasswordUpdated"; |
124
|
|
|
$command = $application->find('app:security:change-password'); |
125
|
|
|
$commandTester = new CommandTester($command); |
126
|
|
|
$commandTester->execute(array( |
127
|
|
|
'command' => $command->getName(), |
128
|
|
|
'login' => $newUserName, |
129
|
|
|
'password' => $newPassword |
130
|
|
|
)); |
131
|
|
|
|
132
|
|
|
//then: the password was changed successfully |
133
|
|
|
$output = $commandTester->getDisplay(); |
134
|
|
|
$this->assertStringStartsWith("Successfully changed password for user '$newUserName'.", $output); |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @test |
141
|
|
|
*/ |
142
|
|
|
public function shouldNotCreateNewUserWithInvalidName(): void |
143
|
|
|
{ |
144
|
|
|
//given: There is a new user to be created |
145
|
|
|
$userName = "it"; |
146
|
|
|
$password = "itPassword"; |
147
|
|
|
$roles = "ROLE_ADMIN"; |
148
|
|
|
|
149
|
|
|
//and: the CLI application is bootable |
150
|
|
|
$application = $this->setupKernel(); |
151
|
|
|
|
152
|
|
|
//expect: |
153
|
|
|
$this->expectException(\InvalidArgumentException::class); |
154
|
|
|
|
155
|
|
|
//when: the user is created using CLI Command |
156
|
|
|
$command = $application->find('app:security:create-user'); |
157
|
|
|
$commandTester = new CommandTester($command); |
158
|
|
|
$commandTester->execute(array( |
159
|
|
|
'command' => $command->getName(), |
160
|
|
|
'login' => $userName, |
161
|
|
|
'password' => $password, |
162
|
|
|
'roles' => $roles, |
163
|
|
|
)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return Application |
168
|
|
|
*/ |
169
|
|
|
protected function setupKernel(): Application |
170
|
|
|
{ |
171
|
|
|
$kernel = static::createKernel(); |
172
|
|
|
$kernel->boot(); |
173
|
|
|
$application = new Application($kernel); |
174
|
|
|
$application->add(new CreateUserCommand( |
175
|
|
|
$this->getContainer()->get(SecurityApiInterface::class) |
|
|
|
|
176
|
|
|
)); |
177
|
|
|
$application->add(new ChangeUserPasswordCommand( |
178
|
|
|
$this->getContainer()->get(SecurityApiInterface::class) |
|
|
|
|
179
|
|
|
)); |
180
|
|
|
$application->add(new RenameUserCommand( |
181
|
|
|
$this->getContainer()->get(SecurityApiInterface::class) |
|
|
|
|
182
|
|
|
)); |
183
|
|
|
return $application; |
184
|
|
|
} |
185
|
|
|
} |