forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserCreationServiceTest.php
More file actions
192 lines (163 loc) · 6.14 KB
/
UserCreationServiceTest.php
File metadata and controls
192 lines (163 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace Tests\Unit\Services;
use Mockery as m;
use Tests\TestCase;
use Tests\Traits\MocksUuids;
use Illuminate\Foundation\Application;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Notifications\ChannelManager;
use Pterodactyl\Notifications\AccountCreated;
use Pterodactyl\Services\Users\UserCreationService;
use Pterodactyl\Services\Helpers\TemporaryPasswordService;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class UserCreationServiceTest extends TestCase
{
use MocksUuids;
/**
* @var \Illuminate\Foundation\Application
*/
protected $appMock;
/**
* @var \Illuminate\Database\ConnectionInterface
*/
protected $database;
/**
* @var \Illuminate\Contracts\Hashing\Hasher
*/
protected $hasher;
/**
* @var \Illuminate\Notifications\ChannelManager
*/
protected $notification;
/**
* @var \Pterodactyl\Services\Helpers\TemporaryPasswordService
*/
protected $passwordService;
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Users\UserCreationService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->appMock = m::mock(Application::class);
$this->database = m::mock(ConnectionInterface::class);
$this->hasher = m::mock(Hasher::class);
$this->notification = m::mock(ChannelManager::class);
$this->passwordService = m::mock(TemporaryPasswordService::class);
$this->repository = m::mock(UserRepositoryInterface::class);
$this->service = new UserCreationService(
$this->appMock,
$this->notification,
$this->database,
$this->hasher,
$this->passwordService,
$this->repository
);
}
/**
* Test that a user is created when a password is passed.
*/
public function testUserIsCreatedWhenPasswordIsProvided()
{
$user = (object) [
'name_first' => 'FirstName',
'username' => 'user_name',
];
$this->hasher->shouldReceive('make')->with('raw-password')->once()->andReturn('enc-password');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with([
'password' => 'enc-password',
'uuid' => $this->getKnownUuid(),
])->once()->andReturn($user);
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->appMock->shouldReceive('makeWith')->with(AccountCreated::class, [
'user' => [
'name' => 'FirstName',
'username' => 'user_name',
'token' => null,
],
])->once()->andReturnNull();
$this->notification->shouldReceive('send')->with($user, null)->once()->andReturnNull();
$response = $this->service->handle([
'password' => 'raw-password',
]);
$this->assertNotNull($response);
$this->assertEquals($user->username, $response->username);
$this->assertEquals($user->name_first, 'FirstName');
}
/**
* Test that a UUID passed in the submission data is not used when
* creating the user.
*/
public function testUuidPassedInDataIsIgnored()
{
$user = (object) [
'name_first' => 'FirstName',
'username' => 'user_name',
];
$this->hasher->shouldReceive('make')->andReturn('enc-password');
$this->database->shouldReceive('beginTransaction')->andReturnNull();
$this->repository->shouldReceive('create')->with([
'password' => 'enc-password',
'uuid' => $this->getKnownUuid(),
])->once()->andReturn($user);
$this->database->shouldReceive('commit')->andReturnNull();
$this->appMock->shouldReceive('makeWith')->andReturnNull();
$this->notification->shouldReceive('send')->andReturnNull();
$response = $this->service->handle([
'password' => 'raw-password',
'uuid' => 'test-uuid',
]);
$this->assertNotNull($response);
$this->assertEquals($user->username, $response->username);
$this->assertEquals($user->name_first, 'FirstName');
}
/**
* Test that a user is created with a random password when no password is provided.
*/
public function testUserIsCreatedWhenNoPasswordIsProvided()
{
$user = (object) [
'name_first' => 'FirstName',
'username' => 'user_name',
'email' => 'user@example.com',
];
$this->hasher->shouldNotReceive('make');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->hasher->shouldReceive('make')->once()->andReturn('created-enc-password');
$this->passwordService->shouldReceive('handle')
->with('user@example.com')
->once()
->andReturn('random-token');
$this->repository->shouldReceive('create')->with([
'password' => 'created-enc-password',
'email' => 'user@example.com',
'uuid' => $this->getKnownUuid(),
])->once()->andReturn($user);
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->appMock->shouldReceive('makeWith')->with(AccountCreated::class, [
'user' => [
'name' => 'FirstName',
'username' => 'user_name',
'token' => 'random-token',
],
])->once()->andReturnNull();
$this->notification->shouldReceive('send')->with($user, null)->once()->andReturnNull();
$response = $this->service->handle([
'email' => 'user@example.com',
]);
$this->assertNotNull($response);
$this->assertEquals($user->username, $response->username);
$this->assertEquals($user->name_first, 'FirstName');
$this->assertEquals($user->email, $response->email);
}
}