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\SecurityApiInterface; |
9
|
|
|
use App\Modules\Security\Domain\Event\Outbound\UserRenamedOEvent; |
10
|
|
|
use App\Tests\Modules\Security\Integration\Http\SecurityHttpTrait; |
11
|
|
|
use App\Tests\TestUtils\Contracts\ApplicationEventContractLoader; |
12
|
|
|
use App\Tests\TestUtils\Events\InMemoryEventPublisher; |
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
14
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
15
|
|
|
use Symfony\Component\Uid\Ulid; |
16
|
|
|
|
17
|
|
|
class SecurityIT extends SecurityIntegrationTest |
18
|
|
|
{ |
19
|
|
|
use SecurityHttpTrait; |
20
|
|
|
use ApplicationEventContractLoader; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @test |
24
|
|
|
*/ |
25
|
|
|
public function shouldCreateNewUserAndUpdateIt(): void |
26
|
|
|
{ |
27
|
|
|
//given: There is a new user to be created |
28
|
|
|
$userName = "[email protected]"; |
29
|
|
|
$password = "itPassword"; |
30
|
|
|
$roles = "ROLE_ADMIN"; |
31
|
|
|
|
32
|
|
|
//and: the CLI application is bootable |
33
|
|
|
$application = $this->setupKernel(); |
34
|
|
|
|
35
|
|
|
//when: the user is created using CLI Command |
36
|
|
|
$command = $application->find('app:security:create-user'); |
37
|
|
|
$commandTester = new CommandTester($command); |
38
|
|
|
$commandTester->execute(array( |
39
|
|
|
'command' => $command->getName(), |
40
|
|
|
'login' => $userName, |
41
|
|
|
'password' => $password, |
42
|
|
|
'roles' => $roles, |
43
|
|
|
)); |
44
|
|
|
|
45
|
|
|
//then: the user was created successfully |
46
|
|
|
$output = $commandTester->getDisplay(); |
47
|
|
|
$this->assertStringStartsWith("Successfully created user '$userName' with id", $output); |
48
|
|
|
|
49
|
|
|
//when: the user is created using CLI Command |
50
|
|
|
$newUserName = "[email protected]"; |
51
|
|
|
$command = $application->find('app:security:rename-user'); |
52
|
|
|
$commandTester = new CommandTester($command); |
53
|
|
|
$commandTester->execute(array( |
54
|
|
|
'command' => $command->getName(), |
55
|
|
|
'login' => $userName, |
56
|
|
|
'newLogin' => $newUserName, |
57
|
|
|
)); |
58
|
|
|
|
59
|
|
|
//then: the user was renamed successfully |
60
|
|
|
$output = $commandTester->getDisplay(); |
61
|
|
|
$this->assertStringStartsWith("Successfully renamed user '$userName' to '$newUserName'", $output); |
62
|
|
|
|
63
|
|
|
//and: The RenamedUserOEvent was Published |
64
|
|
|
$events = InMemoryEventPublisher::get(UserRenamedOEvent::class); |
65
|
|
|
self::assertCount(1, $events); |
66
|
|
|
self::assertEquals($userName, $events[0]->getData()['oldLogin']); |
67
|
|
|
self::assertEquals($newUserName, $events[0]->getData()['newLogin']); |
68
|
|
|
|
69
|
|
|
//when: the user is created using CLI Command |
70
|
|
|
$newPassword = "itPasswordUpdated"; |
71
|
|
|
$command = $application->find('app:security:change-password'); |
72
|
|
|
$commandTester = new CommandTester($command); |
73
|
|
|
$commandTester->execute(array( |
74
|
|
|
'command' => $command->getName(), |
75
|
|
|
'login' => $newUserName, |
76
|
|
|
'password' => $newPassword |
77
|
|
|
)); |
78
|
|
|
|
79
|
|
|
//then: the password was changed successfully |
80
|
|
|
$output = $commandTester->getDisplay(); |
81
|
|
|
$this->assertStringStartsWith("Successfully changed password for user '$newUserName'.", $output); |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @test |
88
|
|
|
*/ |
89
|
|
|
public function shouldNotCreateNewUserWithInvalidName(): void |
90
|
|
|
{ |
91
|
|
|
//given: There is a new user to be created |
92
|
|
|
$userName = "it"; |
93
|
|
|
$password = "itPassword"; |
94
|
|
|
$roles = "ROLE_ADMIN"; |
95
|
|
|
|
96
|
|
|
//and: the CLI application is bootable |
97
|
|
|
$application = $this->setupKernel(); |
98
|
|
|
|
99
|
|
|
//expect: |
100
|
|
|
$this->expectException(\InvalidArgumentException::class); |
101
|
|
|
|
102
|
|
|
//when: the user is created using CLI Command |
103
|
|
|
$command = $application->find('app:security:create-user'); |
104
|
|
|
$commandTester = new CommandTester($command); |
105
|
|
|
$commandTester->execute(array( |
106
|
|
|
'command' => $command->getName(), |
107
|
|
|
'login' => $userName, |
108
|
|
|
'password' => $password, |
109
|
|
|
'roles' => $roles, |
110
|
|
|
)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return Application |
115
|
|
|
*/ |
116
|
|
|
protected function setupKernel(): Application |
117
|
|
|
{ |
118
|
|
|
$kernel = static::createKernel(); |
119
|
|
|
$kernel->boot(); |
120
|
|
|
$application = new Application($kernel); |
121
|
|
|
$application->add(new CreateUserCommand( |
122
|
|
|
$this->getContainer()->get(SecurityApiInterface::class) |
123
|
|
|
)); |
124
|
|
|
$application->add(new ChangeUserPasswordCommand( |
125
|
|
|
$this->getContainer()->get(SecurityApiInterface::class) |
126
|
|
|
)); |
127
|
|
|
$application->add(new RenameUserCommand( |
128
|
|
|
$this->getContainer()->get(SecurityApiInterface::class) |
129
|
|
|
)); |
130
|
|
|
return $application; |
131
|
|
|
} |
132
|
|
|
} |