From f701e65f7520ae0e1437dd841b8345c0d33125fc Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 31 Jul 2016 13:14:06 +0200 Subject: [PATCH] Created RestTokenServiceTest --- .../test/Service/RestTokenServiceTest.php | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 module/Rest/test/Service/RestTokenServiceTest.php diff --git a/module/Rest/test/Service/RestTokenServiceTest.php b/module/Rest/test/Service/RestTokenServiceTest.php new file mode 100644 index 00000000..d4487ff1 --- /dev/null +++ b/module/Rest/test/Service/RestTokenServiceTest.php @@ -0,0 +1,93 @@ +em = $this->prophesize(EntityManager::class); + $this->service = new RestTokenService($this->em->reveal(), [ + 'username' => 'foo', + 'password' => 'bar', + ]); + } + + /** + * @test + */ + public function tokenIsCreatedIfCredentialsAreCorrect() + { + $this->em->persist(Argument::type(RestToken::class))->shouldBeCalledTimes(1); + $this->em->flush()->shouldBeCalledTimes(1); + + $token = $this->service->createToken('foo', 'bar'); + $this->assertInstanceOf(RestToken::class, $token); + $this->assertFalse($token->isExpired()); + } + + /** + * @test + * @expectedException \Shlinkio\Shlink\Rest\Exception\AuthenticationException + */ + public function exceptionIsThrownWhileCreatingTokenWithWrongCredentials() + { + $this->service->createToken('foo', 'wrong'); + } + + /** + * @test + */ + public function restTokenIsReturnedFromTokenString() + { + $authToken = 'ABC-abc'; + $theToken = new RestToken(); + $repo = $this->prophesize(EntityRepository::class); + $repo->findOneBy(['token' => $authToken])->willReturn($theToken)->shouldBeCalledTimes(1); + $this->em->getRepository(RestToken::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1); + + $this->assertSame($theToken, $this->service->getByToken($authToken)); + } + + /** + * @test + * @expectedException \Shlinkio\Shlink\Common\Exception\InvalidArgumentException + */ + public function exceptionIsThrownWhenRequestingWrongToken() + { + $authToken = 'ABC-abc'; + $repo = $this->prophesize(EntityRepository::class); + $repo->findOneBy(['token' => $authToken])->willReturn(null)->shouldBeCalledTimes(1); + $this->em->getRepository(RestToken::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1); + + $this->service->getByToken($authToken); + } + + /** + * @test + */ + public function updateExpirationFlushesEntityManager() + { + $token = $this->prophesize(RestToken::class); + $token->updateExpiration()->shouldBeCalledTimes(1); + $this->em->flush()->shouldBeCalledTimes(1); + + $this->service->updateExpiration($token->reveal()); + } +}