connection = m::mock(ConnectionInterface::class); $this->hasher = m::mock(Hasher::class); $this->passwordBroker = m::mock(PasswordBroker::class); $this->repository = m::mock(UserRepositoryInterface::class); } /** * Test that a user is created when a password is passed. */ public function testUserIsCreatedWhenPasswordIsProvided() { $user = factory(User::class)->make(); $this->hasher->shouldReceive('make')->with('raw-password')->once()->andReturn('enc-password'); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->repository->shouldReceive('create')->with([ 'password' => 'enc-password', 'uuid' => $this->getKnownUuid(), ], true, true)->once()->andReturn($user); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $response = $this->getService()->handle([ 'password' => 'raw-password', ]); $this->assertNotNull($response); Notification::assertSentTo($user, AccountCreated::class, function ($notification) use ($user) { $this->assertSame($user, $notification->user); $this->assertNull($notification->token); return true; }); } /** * Test that a UUID passed in the submission data is not used when * creating the user. */ public function testUuidPassedInDataIsIgnored() { $user = factory(User::class)->make(); $this->hasher->shouldReceive('make')->andReturn('enc-password'); $this->connection->shouldReceive('beginTransaction')->andReturnNull(); $this->repository->shouldReceive('create')->with([ 'password' => 'enc-password', 'uuid' => $this->getKnownUuid(), ], true, true)->once()->andReturn($user); $this->connection->shouldReceive('commit')->andReturnNull(); $response = $this->getService()->handle([ 'password' => 'raw-password', 'uuid' => 'test-uuid', ]); $this->assertNotNull($response); $this->assertInstanceOf(User::class, $response); Notification::assertSentTo($user, AccountCreated::class, function ($notification) use ($user) { $this->assertSame($user, $notification->user); $this->assertNull($notification->token); return true; }); } /** * Test that a user is created with a random password when no password is provided. */ public function testUserIsCreatedWhenNoPasswordIsProvided() { $user = factory(User::class)->make(); $this->hasher->shouldNotReceive('make'); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->hasher->shouldReceive('make')->once()->andReturn('created-enc-password'); $this->passwordBroker->shouldReceive('createToken')->with($user)->once()->andReturn('random-token'); $this->repository->shouldReceive('create')->with([ 'password' => 'created-enc-password', 'email' => $user->email, 'uuid' => $this->getKnownUuid(), ], true, true)->once()->andReturn($user); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $response = $this->getService()->handle([ 'email' => $user->email, ]); $this->assertNotNull($response); $this->assertInstanceOf(User::class, $response); Notification::assertSentTo($user, AccountCreated::class, function ($notification) use ($user) { $this->assertSame($user, $notification->user); $this->assertSame('random-token', $notification->token); return true; }); } /** * Return a new instance of the service using mocked dependencies. * * @return \Pterodactyl\Services\Users\UserCreationService */ private function getService(): UserCreationService { return new UserCreationService($this->connection, $this->hasher, $this->passwordBroker, $this->repository); } }