daemonRepository = m::mock(ServerRepositoryInterface::class); $this->repository = m::mock(DaemonKeyRepositoryInterface::class); } /** * Test that keys can be successfully revoked. */ public function testSuccessfulKeyRevocation() { $user = factory(User::class)->make(); $server = factory(Server::class)->make(); $key = factory(DaemonKey::class)->make(['user_id' => $user->id]); $key->setRelation('server', $server); $this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key])); $this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf(); $this->daemonRepository->shouldReceive('revokeAccessKey')->with([$key->secret])->once()->andReturnNull(); $this->repository->shouldReceive('deleteKeys')->with([$key->id])->once()->andReturnNull(); $this->getService()->handle($user); $this->assertTrue(true); } /** * Test that an exception thrown by a call to the daemon is handled. * * @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException */ public function testExceptionThrownFromDaemonCallIsHandled() { $this->configureExceptionMock(); $user = factory(User::class)->make(); $server = factory(Server::class)->make(); $key = factory(DaemonKey::class)->make(['user_id' => $user->id]); $key->setRelation('server', $server); $this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key])); $this->daemonRepository->shouldReceive('setNode->revokeAccessKey')->with([$key->secret])->once()->andThrow($this->getExceptionMock()); $this->getService()->handle($user); } /** * Test that the behavior for handling exceptions that should not be thrown * immediately is working correctly and adds them to the array. */ public function testIgnoredExceptionsAreHandledProperly() { $this->configureExceptionMock(); $user = factory(User::class)->make(); $server = factory(Server::class)->make(); $key = factory(DaemonKey::class)->make(['user_id' => $user->id]); $key->setRelation('server', $server); $this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key])); $this->daemonRepository->shouldReceive('setNode->revokeAccessKey')->with([$key->secret])->once()->andThrow($this->getExceptionMock()); $this->repository->shouldReceive('deleteKeys')->with([$key->id])->once()->andReturnNull(); $service = $this->getService(); $service->handle($user, true); $this->assertNotEmpty($service->getExceptions()); $this->assertArrayHasKey($server->node_id, $service->getExceptions()); $this->assertSame(array_get($service->getExceptions(), $server->node_id), $this->getExceptionMock()); $this->assertTrue(true); } /** * Return an instance of the service for testing. * * @return \Pterodactyl\Services\DaemonKeys\RevokeMultipleDaemonKeysService */ private function getService(): RevokeMultipleDaemonKeysService { return new RevokeMultipleDaemonKeysService($this->repository, $this->daemonRepository); } }