44
55use Mockery as m ;
66use Tests \TestCase ;
7- use Pterodactyl \Models \Node ;
7+ use Pterodactyl \Models \Egg ;
88use Pterodactyl \Models \User ;
99use Tests \Traits \MocksUuids ;
1010use Pterodactyl \Models \Server ;
11+ use Pterodactyl \Models \Allocation ;
1112use Tests \Traits \MocksRequestException ;
1213use Illuminate \Database \ConnectionInterface ;
14+ use Pterodactyl \Models \Objects \DeploymentObject ;
1315use Pterodactyl \Services \Servers \ServerCreationService ;
1416use Pterodactyl \Services \Servers \VariableValidatorService ;
1517use Pterodactyl \Services \Deployment \FindViableNodesService ;
@@ -35,7 +37,7 @@ class ServerCreationServiceTest extends TestCase
3537 private $ allocationRepository ;
3638
3739 /**
38- * @var \Pterodactyl\Services\Deployment\AllocationSelectionService
40+ * @var \Pterodactyl\Services\Deployment\AllocationSelectionService|\Mockery\Mock
3941 */
4042 private $ allocationSelectionService ;
4143
@@ -55,12 +57,12 @@ class ServerCreationServiceTest extends TestCase
5557 private $ daemonServerRepository ;
5658
5759 /**
58- * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
60+ * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
5961 */
6062 private $ eggRepository ;
6163
6264 /**
63- * @var \Pterodactyl\Services\Deployment\FindViableNodesService
65+ * @var \Pterodactyl\Services\Deployment\FindViableNodesService|\Mockery\Mock
6466 */
6567 private $ findViableNodesService ;
6668
@@ -117,6 +119,7 @@ public function testCreateShouldHitAllOfTheNecessaryServicesAndStoreTheServer()
117119 $ this ->repository ->shouldReceive ('create ' )->with (m::subset ([
118120 'uuid ' => $ this ->getKnownUuid (),
119121 'node_id ' => $ model ->node_id ,
122+ 'allocation_id ' => $ model ->allocation_id ,
120123 'owner_id ' => $ model ->owner_id ,
121124 'nest_id ' => $ model ->nest_id ,
122125 'egg_id ' => $ model ->egg_id ,
@@ -147,6 +150,86 @@ public function testCreateShouldHitAllOfTheNecessaryServicesAndStoreTheServer()
147150 $ this ->assertSame ($ model , $ response );
148151 }
149152
153+ /**
154+ * Test that optional parameters get auto-filled correctly on the model.
155+ */
156+ public function testDataIsAutoFilled ()
157+ {
158+ $ model = factory (Server::class)->make (['uuid ' => $ this ->getKnownUuid ()]);
159+ $ allocationModel = factory (Allocation::class)->make (['node_id ' => $ model ->node_id ]);
160+ $ eggModel = factory (Egg::class)->make (['nest_id ' => $ model ->nest_id ]);
161+
162+ $ this ->connection ->shouldReceive ('beginTransaction ' )->once ()->withNoArgs ();
163+ $ this ->allocationRepository ->shouldReceive ('setColumns->find ' )->once ()->with ($ model ->allocation_id )->andReturn ($ allocationModel );
164+ $ this ->eggRepository ->shouldReceive ('setColumns->find ' )->once ()->with ($ model ->egg_id )->andReturn ($ eggModel );
165+
166+ $ this ->validatorService ->shouldReceive ('setUserLevel->handle ' )->once ()->andReturn (collect ([]));
167+ $ this ->repository ->shouldReceive ('create ' )->once ()->with (m::subset ([
168+ 'uuid ' => $ this ->getKnownUuid (),
169+ 'node_id ' => $ model ->node_id ,
170+ 'allocation_id ' => $ model ->allocation_id ,
171+ 'nest_id ' => $ model ->nest_id ,
172+ 'egg_id ' => $ model ->egg_id ,
173+ ]))->andReturn ($ model );
174+
175+ $ this ->allocationRepository ->shouldReceive ('assignAllocationsToServer ' )->once ()->with ($ model ->id , [$ model ->allocation_id ]);
176+ $ this ->configurationStructureService ->shouldReceive ('handle ' )->once ()->with ($ model )->andReturn ([]);
177+
178+ $ this ->daemonServerRepository ->shouldReceive ('setServer->create ' )->once ();
179+ $ this ->connection ->shouldReceive ('commit ' )->once ()->withNoArgs ()->andReturnNull ();
180+
181+ $ this ->getService ()->handle (
182+ collect ($ model ->toArray ())->except (['node_id ' , 'nest_id ' ])->toArray ()
183+ );
184+ }
185+
186+ /**
187+ * Test that an auto-deployment object is used correctly if passed.
188+ */
189+ public function testAutoDeploymentObject ()
190+ {
191+ $ model = factory (Server::class)->make (['uuid ' => $ this ->getKnownUuid ()]);
192+ $ deploymentObject = new DeploymentObject ();
193+ $ deploymentObject ->setPorts (['25565 ' ]);
194+ $ deploymentObject ->setDedicated (false );
195+ $ deploymentObject ->setLocations ([1 ]);
196+
197+ $ this ->connection ->shouldReceive ('beginTransaction ' )->once ()->withNoArgs ();
198+
199+ $ this ->findViableNodesService ->shouldReceive ('setLocations ' )->once ()->with ($ deploymentObject ->getLocations ())->andReturnSelf ();
200+ $ this ->findViableNodesService ->shouldReceive ('setDisk ' )->once ()->with ($ model ->disk )->andReturnSelf ();
201+ $ this ->findViableNodesService ->shouldReceive ('setMemory ' )->once ()->with ($ model ->memory )->andReturnSelf ();
202+ $ this ->findViableNodesService ->shouldReceive ('handle ' )->once ()->withNoArgs ()->andReturn ([1 , 2 ]);
203+
204+ $ allocationModel = factory (Allocation::class)->make ([
205+ 'id ' => $ model ->allocation_id ,
206+ 'node_id ' => $ model ->node_id ,
207+ ]);
208+ $ this ->allocationSelectionService ->shouldReceive ('setDedicated ' )->once ()->with ($ deploymentObject ->isDedicated ())->andReturnSelf ();
209+ $ this ->allocationSelectionService ->shouldReceive ('setNodes ' )->once ()->with ([1 , 2 ])->andReturnSelf ();
210+ $ this ->allocationSelectionService ->shouldReceive ('setPorts ' )->once ()->with ($ deploymentObject ->getPorts ())->andReturnSelf ();
211+ $ this ->allocationSelectionService ->shouldReceive ('handle ' )->once ()->withNoArgs ()->andReturn ($ allocationModel );
212+
213+ $ this ->validatorService ->shouldReceive ('setUserLevel->handle ' )->once ()->andReturn (collect ([]));
214+ $ this ->repository ->shouldReceive ('create ' )->once ()->with (m::subset ([
215+ 'uuid ' => $ this ->getKnownUuid (),
216+ 'node_id ' => $ model ->node_id ,
217+ 'allocation_id ' => $ model ->allocation_id ,
218+ 'nest_id ' => $ model ->nest_id ,
219+ 'egg_id ' => $ model ->egg_id ,
220+ ]))->andReturn ($ model );
221+
222+ $ this ->allocationRepository ->shouldReceive ('assignAllocationsToServer ' )->once ()->with ($ model ->id , [$ model ->allocation_id ]);
223+ $ this ->configurationStructureService ->shouldReceive ('handle ' )->once ()->with ($ model )->andReturn ([]);
224+
225+ $ this ->daemonServerRepository ->shouldReceive ('setServer->create ' )->once ();
226+ $ this ->connection ->shouldReceive ('commit ' )->once ()->withNoArgs ()->andReturnNull ();
227+
228+ $ this ->getService ()->handle (
229+ collect ($ model ->toArray ())->except (['allocation_id ' , 'node_id ' ])->toArray (), $ deploymentObject
230+ );
231+ }
232+
150233 /**
151234 * Test handling of node timeout or other daemon error.
152235 *
0 commit comments