33namespace Tests \Unit \Http \Middleware \Server ;
44
55use Mockery as m ;
6- use Illuminate \Http \Response ;
76use Pterodactyl \Models \Server ;
87use Illuminate \Contracts \Session \Session ;
98use Illuminate \Contracts \Config \Repository ;
9+ use Illuminate \Contracts \Routing \ResponseFactory ;
1010use Tests \Unit \Http \Middleware \MiddlewareTestCase ;
1111use Pterodactyl \Http \Middleware \AccessingValidServer ;
1212use Pterodactyl \Contracts \Repository \ServerRepositoryInterface ;
@@ -23,6 +23,11 @@ class AccessingValidServerTest extends MiddlewareTestCase
2323 */
2424 private $ repository ;
2525
26+ /**
27+ * @var \Illuminate\Contracts\Routing\ResponseFactory|\Mockery\Mock
28+ */
29+ private $ response ;
30+
2631 /**
2732 * @var \Illuminate\Contracts\Session\Session|\Mockery\Mock
2833 */
@@ -37,30 +42,15 @@ public function setUp()
3742
3843 $ this ->config = m::mock (Repository::class);
3944 $ this ->repository = m::mock (ServerRepositoryInterface::class);
45+ $ this ->response = m::mock (ResponseFactory::class);
4046 $ this ->session = m::mock (Session::class);
4147 }
4248
43- /**
44- * Test that an exception is thrown if the request is an API request and no server is found.
45- *
46- * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
47- * @expectedExceptionMessage The requested server was not found on the system.
48- */
49- public function testExceptionIsThrownIfNoServerIsFoundAndIsAPIRequest ()
50- {
51- $ this ->request ->shouldReceive ('route->parameter ' )->with ('server ' )->once ()->andReturn ('123456 ' );
52- $ this ->request ->shouldReceive ('expectsJson ' )->withNoArgs ()->once ()->andReturn (true );
53-
54- $ this ->repository ->shouldReceive ('getByUuid ' )->with ('123456 ' )->once ()->andReturnNull ();
55-
56- $ this ->getMiddleware ()->handle ($ this ->request , $ this ->getClosureAssertions ());
57- }
58-
5949 /**
6050 * Test that an exception is thrown if the request is an API request and the server is suspended.
6151 *
6252 * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
63- * @expectedExceptionMessage Server is suspended.
53+ * @expectedExceptionMessage Server is suspended and cannot be accessed .
6454 */
6555 public function testExceptionIsThrownIfServerIsSuspended ()
6656 {
@@ -77,8 +67,8 @@ public function testExceptionIsThrownIfServerIsSuspended()
7767 /**
7868 * Test that an exception is thrown if the request is an API request and the server is not installed.
7969 *
80- * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
81- * @expectedExceptionMessage Server is not marked as installed .
70+ * @expectedException \Symfony\Component\HttpKernel\Exception\ConflictHttpException
71+ * @expectedExceptionMessage Server is still completing the installation process .
8272 */
8373 public function testExceptionIsThrownIfServerIsNotInstalled ()
8474 {
@@ -97,19 +87,18 @@ public function testExceptionIsThrownIfServerIsNotInstalled()
9787 *
9888 * @dataProvider viewDataProvider
9989 */
100- public function testCorrectErrorPagesAreRendered (Server $ model = null , string $ page , int $ httpCode )
90+ public function testCorrectErrorPagesAreRendered (Server $ model , string $ page , int $ httpCode )
10191 {
10292 $ this ->request ->shouldReceive ('route->parameter ' )->with ('server ' )->once ()->andReturn ('123456 ' );
10393 $ this ->request ->shouldReceive ('expectsJson ' )->withNoArgs ()->once ()->andReturn (false );
10494 $ this ->config ->shouldReceive ('get ' )->with ('pterodactyl.json_routes ' , [])->once ()->andReturn ([]);
10595 $ this ->request ->shouldReceive ('is ' )->with (...[])->once ()->andReturn (false );
10696
10797 $ this ->repository ->shouldReceive ('getByUuid ' )->with ('123456 ' )->once ()->andReturn ($ model );
98+ $ this ->response ->shouldReceive ('view ' )->with ($ page , [], $ httpCode )->once ()->andReturn (true );
10899
109100 $ response = $ this ->getMiddleware ()->handle ($ this ->request , $ this ->getClosureAssertions ());
110- $ this ->assertInstanceOf (Response::class, $ response );
111- $ this ->assertEquals ($ page , $ response ->getOriginalContent ()->getName (), 'Assert that the correct view is returned. ' );
112- $ this ->assertEquals ($ httpCode , $ response ->getStatusCode (), 'Assert that the correct HTTP code is returned. ' );
101+ $ this ->assertTrue ($ response );
113102 }
114103
115104 /**
@@ -143,10 +132,9 @@ public function viewDataProvider(): array
143132 $ this ->refreshApplication ();
144133
145134 return [
146- [null , 'errors.404 ' , 404 ],
147135 [factory (Server::class)->make (['suspended ' => 1 ]), 'errors.suspended ' , 403 ],
148- [factory (Server::class)->make (['installed ' => 0 ]), 'errors.installing ' , 403 ],
149- [factory (Server::class)->make (['installed ' => 2 ]), 'errors.installing ' , 403 ],
136+ [factory (Server::class)->make (['installed ' => 0 ]), 'errors.installing ' , 409 ],
137+ [factory (Server::class)->make (['installed ' => 2 ]), 'errors.installing ' , 409 ],
150138 ];
151139 }
152140
@@ -157,6 +145,6 @@ public function viewDataProvider(): array
157145 */
158146 private function getMiddleware (): AccessingValidServer
159147 {
160- return new AccessingValidServer ($ this ->config , $ this ->repository , $ this ->session );
148+ return new AccessingValidServer ($ this ->config , $ this ->response , $ this -> repository , $ this ->session );
161149 }
162150}
0 commit comments