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