/** * @param User $user * @return User */ public function invoke(User $user) { $user->setEncodedPassword($this->passwordEncoder->encode($user->getPassword())); $this->userRepository->add($user); $this->entityManager->flush(); $user->setPassword(null); return $user; }
/** * @param Thread $thread * @param User $user * @return bool */ public function remove(Thread $thread, User $user) { $owner = $thread->getUser(); if (!$owner || $owner->getId() != $user->getId()) { return false; } $this->threadRepository->remove($thread); $this->entityManager->flush(); return true; }
/** * @test */ public function findUserByUsername() { $username = "******"; $user = new User(); $user->setUsername($username); $repoMock = $this->getMock("RepositoryMock", array("findOneBy")); $repoMock->expects($this->once())->method("findOneBy")->with($this->equalTo(["username" => $username]))->will($this->returnValue($user)); $emMock = $this->getEntityManagerMock(array("getRepository")); $emMock->expects($this->once())->method("getRepository")->with($this->equalTo(EntityConstant::USER))->will($this->returnValue($repoMock)); $passwordEncoderMock = $this->getPasswordEncoderMock(); /** @var \Doctrine\ORM\EntityManager $emMock */ /** @var \Kumatch\BBSAPI\Utility\PasswordEncoder $passwordEncoderMock */ $useCase = new UserRegistration($emMock, $passwordEncoderMock); $this->assertEquals($user, $useCase->findByUsername($username)); }
/** * @test */ public function succeedCreation() { $title = "foo"; $thread = new Thread(); $thread->setTitle($title); $user = new User(); $user->setUsername("*****@*****.**"); $threadRepo = $this->getMock("RepositoryMock", array("add")); $threadRepo->expects($this->once())->method("add")->with($this->logicalAnd($this->isInstanceOf('Kumatch\\BBSAPI\\Entity\\Thread'), $this->attributeEqualTo("title", $title), $this->attributeEqualTo("user", $user), $this->attributeEqualTo("lastPostedAt", null))); $em = $this->getEntityManagerMock(array("getRepository", "flush")); $em->expects($this->once())->method("getRepository")->with($this->equalTo(EntityConstant::THREAD))->will($this->returnValue($threadRepo)); $em->expects($this->once())->method("flush"); /** @var EntityManager $em */ $useCase = new ThreadManagement($em); $thread = $useCase->create($thread, $user); $this->assertInstanceOf('Kumatch\\BBSAPI\\Entity\\Thread', $thread); $this->assertEquals($title, $thread->getTitle()); $this->assertEquals($user, $thread->getUser()); $this->assertNull($thread->getLastPostedAt()); }
public function register(Application $app) { $app["bbsapi.user.registration"] = function (Application $app) { return new UserRegistration($app["entity_manager"], $app["bbsapi.utility.password_encoder"]); }; $app["bbsapi.user.authentication"] = function (Application $app) { return new UserAuthentication($app["entity_manager"], $app["bbsapi.utility.password_encoder"], $app["bbsapi.utility.token_generator"]); }; $app["bbsapi.user.token_authorization"] = function (Application $app) { return new UserTokenAuthorization($app["entity_manager"]); }; $app["bbsapi.spec.user_spec"] = function () { return new UserSpec(); }; $app->before(function (Request $req) use($app) { $userId = $req->headers->get(self::HEADER_AUTHORIZATION_USER_ID); $tokenString = $req->headers->get(self::HEADER_AUTHORIZATION_TOKEN); if (!$userId || !$tokenString) { return; } /** @var UserTokenAuthorization $service */ $service = $app["bbsapi.user.token_authorization"]; $user = $service->invoke($userId, $tokenString); if (!$user) { return; } $req->setUser($user); }); $app->post("/user/register", function (Application $app, Request $req) { /** @var UserSpec $spec */ $spec = $app["bbsapi.spec.user_spec"]; /** @var UserRegistration $service */ $service = $app["bbsapi.user.registration"]; $email = $req->request->get("email"); $username = $req->request->get("username"); $password = $req->request->get("password"); $user = new User(); $user->setEmail($email)->setUsername($username)->setPassword($password); $result = $spec->validate($user); if (!$result->isValid()) { return $app->json(["errors" => $result->getErrors()], 400); } $alreadyExistsErrors = ["user" => [sprintf("A username [%s] is already exists.", $user->getUsername())]]; if ($service->findByUsername($user->getUsername())) { return $app->json(["errors" => $alreadyExistsErrors], 400); } try { $user = $service->invoke($user); } catch (UniqueConstraintViolationException $e) { return $app->json(["errors" => $alreadyExistsErrors], 400); } return $app->json($spec->format($user), 201); }); $app->post("/user/authorize", function (Application $app, Request $req) { /** @var UserAuthentication $service */ $service = $app["bbsapi.user.authentication"]; $username = $req->request->get("username"); $password = $req->request->get("password"); $accessToken = $service->invoke($username, $password); if (!$accessToken) { return $app->json(null, 401); } return $app->json(["id" => $accessToken->getUser()->getId(), "token" => $accessToken->getToken(), "period" => $accessToken->getPeriod()]); }); $app->get("/users/{username}", function (Application $app, $username) { /** @var UserSpec $spec */ $spec = $app["bbsapi.spec.user_spec"]; /** @var UserRegistration $service */ $service = $app["bbsapi.user.registration"]; $user = $service->findByUsername($username); if (!$user) { return $app->json(null, 404); } return $app->json($spec->format($user)); })->assert('username', '^\\w+$'); }
/** * @param User $user * @return array */ public function format(User $user) { return ["email" => $user->getEmail(), "username" => $user->getUsername()]; }
/** * @test */ public function failIfPasswordIsInvalid() { $username = "******"; $password = "******"; $encodedPassword = "******"; $token = "oHyu823kfhdGFpVzLf/PiCoVbo5IqwuQSN0YeiJj"; $now = 1234567890; $user = new User(); $user->setUsername($username)->setEncodedPassword($encodedPassword); $userRepo = $this->getMock("RepositoryMock", array("findOneBy")); $userRepo->expects($this->once())->method("findOneBy")->with($this->equalTo(array("username" => $username)))->will($this->returnValue($user)); $accessTokenRepo = $this->getMock("RepositoryMock", array("add")); $accessTokenRepo->expects($this->never())->method("add"); $em = $this->getEntityManagerMock(array("getRepository", "flush")); $em->expects($this->at(0))->method("getRepository")->with($this->equalTo(EntityConstant::USER))->will($this->returnValue($userRepo)); $em->expects($this->at(1))->method("getRepository")->with($this->equalTo(EntityConstant::ACCESS_TOKEN))->will($this->returnValue($accessTokenRepo)); $em->expects($this->never())->method("flush"); $passwordEncoder = $this->getPasswordEncoderMock(array("encode")); $passwordEncoder->expects($this->once())->method("encode")->with($this->equalTo($password))->will($this->returnValue("invalid_password_string")); $tokenGenerator = $this->getTokenGeneratorMock(array("generate")); $tokenGenerator->expects($this->never())->method("generate"); /** @var \Doctrine\ORM\EntityManager $em */ /** @var \Kumatch\BBSAPI\Utility\PasswordEncoder $passwordEncoder */ /** @var \Kumatch\BBSAPI\Utility\TokenGenerator $tokenGenerator */ $useCase = new UserAuthentication($em, $passwordEncoder, $tokenGenerator); /** @var UserAuthentication $useCase */ $this->assertFalse($useCase->invoke($username, $password)); }
/** * @test * @dataProvider provideInvalidPassword * @param string $password * @param int $errorSize */ public function invalidPassword($password, $errorSize = 1) { $user = new User(); $user->setEmail("*****@*****.**")->setUsername("foo_user")->setPassword($password); $result = $this->spec->validate($user); $errors = $result->getErrors(); $this->assertFalse($result->isValid()); $this->assertCount(1, $errors); $this->assertCount($errorSize, $errors["password"]); }