hashids = m::mock(HashidsInterface::class); $this->repository = m::mock(SubuserRepositoryInterface::class); } /** * Test a successful middleware instance. */ public function testSuccessfulMiddleware() { $model = factory(Server::class)->make(); $subuser = factory(Subuser::class)->make([ 'server_id' => $model->id, ]); $this->setRequestAttribute('server', $model); $this->request->shouldReceive('route->parameter')->with('subuser', 0)->once()->andReturn('abc123'); $this->hashids->shouldReceive('decodeFirst')->with('abc123', 0)->once()->andReturn($subuser->id); $this->repository->shouldReceive('find')->with($subuser->id)->once()->andReturn($subuser); $this->request->shouldReceive('method')->withNoArgs()->once()->andReturn('GET'); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); $this->assertRequestHasAttribute('subuser'); $this->assertRequestAttributeEquals($subuser, 'subuser'); } /** * Test that a user can edit a user other than themselves. */ public function testSuccessfulMiddlewareWhenPatchRequest() { $this->setRequestUser(); $model = factory(Server::class)->make(); $subuser = factory(Subuser::class)->make([ 'server_id' => $model->id, ]); $this->setRequestAttribute('server', $model); $this->request->shouldReceive('route->parameter')->with('subuser', 0)->once()->andReturn('abc123'); $this->hashids->shouldReceive('decodeFirst')->with('abc123', 0)->once()->andReturn($subuser->id); $this->repository->shouldReceive('find')->with($subuser->id)->once()->andReturn($subuser); $this->request->shouldReceive('method')->withNoArgs()->once()->andReturn('PATCH'); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); $this->assertRequestHasAttribute('subuser'); $this->assertRequestAttributeEquals($subuser, 'subuser'); } /** * Test that an exception is thrown if a user attempts to edit themself. */ public function testExceptionIsThrownIfUserTriesToEditSelf() { $user = $this->setRequestUser(); $model = factory(Server::class)->make(); $subuser = factory(Subuser::class)->make([ 'server_id' => $model->id, 'user_id' => $user->id, ]); $this->setRequestAttribute('server', $model); $this->request->shouldReceive('route->parameter')->with('subuser', 0)->once()->andReturn('abc123'); $this->hashids->shouldReceive('decodeFirst')->with('abc123', 0)->once()->andReturn($subuser->id); $this->repository->shouldReceive('find')->with($subuser->id)->once()->andReturn($subuser); $this->request->shouldReceive('method')->withNoArgs()->once()->andReturn('PATCH'); try { $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); } catch (PterodactylException $exception) { $this->assertInstanceOf(DisplayException::class, $exception); $this->assertEquals(trans('exceptions.subusers.editing_self'), $exception->getMessage()); } } /** * Test that an exception is thrown if a subuser server does not match the * request server. * * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ public function testExceptionIsThrownIfSubuserServerDoesNotMatchRequestServer() { $model = factory(Server::class)->make(); $subuser = factory(Subuser::class)->make(); $this->setRequestAttribute('server', $model); $this->request->shouldReceive('route->parameter')->with('subuser', 0)->once()->andReturn('abc123'); $this->hashids->shouldReceive('decodeFirst')->with('abc123', 0)->once()->andReturn($subuser->id); $this->repository->shouldReceive('find')->with($subuser->id)->once()->andReturn($subuser); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); } /** * Test that an exception is thrown if no subuser is found. * * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ public function testExceptionIsThrownIfNoSubuserIsFound() { $model = factory(Server::class)->make(); $subuser = factory(Subuser::class)->make(); $this->setRequestAttribute('server', $model); $this->request->shouldReceive('route->parameter')->with('subuser', 0)->once()->andReturn('abc123'); $this->hashids->shouldReceive('decodeFirst')->with('abc123', 0)->once()->andReturn($subuser->id); $this->repository->shouldReceive('find')->with($subuser->id)->once()->andReturnNull(); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); } /** * Return an instance of the middleware using mocked dependencies. * * @return \Pterodactyl\Http\Middleware\Server\SubuserBelongsToServer */ private function getMiddleware(): SubuserBelongsToServer { return new SubuserBelongsToServer($this->hashids, $this->repository); } }