commit b3b4bc79dd6dfc279e7ccd63dcf1086c9ed33e82
parent ff29041ab5acdb241745dc6fb67a246a01a384bf
Author: Felicitus <felicitus@felicitus.org>
Date: Tue, 1 Sep 2015 14:01:40 +0200
Reworked unit tests for the auth bundle, removed legacy login action
Diffstat:
3 files changed, 22 insertions(+), 136 deletions(-)
diff --git a/src/PartKeepr/AuthBundle/Controller/DefaultController.php b/src/PartKeepr/AuthBundle/Controller/DefaultController.php
@@ -6,62 +6,15 @@ use FOS\RestBundle\Controller\Annotations\RequestParam;
use FOS\RestBundle\Controller\Annotations\View;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Request\ParamFetcher;
-use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use PartKeepr\AuthBundle\Entity\User;
use PartKeepr\AuthBundle\Entity\User\Exceptions\InvalidLoginDataException;
-use PartKeepr\AuthBundle\Entity\UserManager;
use PartKeepr\AuthBundle\Response\LoginResponse;
-use PartKeepr\AuthBundle\Validator\Constraints\PasswordMD5Hash;
use PartKeepr\AuthBundle\Validator\Constraints\Username;
-use PartKeepr\Session\SessionManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration as Routing;
-use Symfony\Component\HttpKernel\Exception\HttpException;
class DefaultController extends FOSRestController
{
/**
- * Logs the user in using a given username and password.
- *
- * @Routing\Route("/auth/login", defaults={"method" = "get","_format" = "json"})
- * @Routing\Method({"POST"})
- * @RequestParam(name="username", strict=true, description="The username, 3-50 characters. Allowed characters: a-z, A-Z, 0-9, an underscore (_), a backslash (\), a slash (/), a dot (.) or a dash (-)", requirements=@Username, allowBlank=false)
- * @RequestParam(name="password", strict=true, description="The password in MD5 format", requirements=@PasswordMD5Hash, allowBlank=false)
- * @ApiDoc(section="auth",output="PartKeepr\AuthBundle\Response\LoginResponse")
- * @View()
- *
- * @param ParamFetcher $paramFetcher
- *
- * @return LoginResponse
- * @throws InvalidLoginDataException
- */
- public function loginAction(ParamFetcher $paramFetcher)
- {
- /* Build a temporary user */
- $user = new User();
- $user->setRawUsername($paramFetcher->get("username"));
- $user->setHashedPassword($paramFetcher->get("password"));
-
- try {
- $authenticatedUser = UserManager::getInstance()->authenticate($user);
- } catch (InvalidLoginDataException $e) {
- throw new HttpException(401, "Username or password invalid.");
- }
-
- /* Start Session */
- $session = SessionManager::getInstance()->startSession($authenticatedUser);
-
- $session->getUser()->updateSeen();
-
- $loginResponse = new LoginResponse();
- $loginResponse->sessionId = $session->getSessionID();
- $loginResponse->username = $paramFetcher->get("username");
- $loginResponse->isAdmin = $session->getUser()->isAdmin();
- //$loginResponse->userPreferences = $session->getUser()->getPreferences();
-
- return $loginResponse;
- }
-
- /**
* Retrieves the salt for a given user
*
* @Routing\Route("/auth/getSalt", defaults={"method" = "get","_format" = "json"})
diff --git a/src/PartKeepr/AuthBundle/DataFixtures/LoadUserData.php b/src/PartKeepr/AuthBundle/DataFixtures/LoadUserData.php
@@ -1,17 +1,20 @@
<?php
namespace PartKeepr\AuthBundle\DataFixtures;
-use Doctrine\Common\DataFixtures\FixtureInterface;
+use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use PartKeepr\AuthBundle\Entity\User;
-class LoadUserData implements FixtureInterface {
+class LoadUserData extends AbstractFixture {
public function load (ObjectManager $manager) {
$admin = new User();
$admin->setUsername("admin");
$admin->setPassword("admin");
+ $admin->setEmail("foo@bar.com");
$manager->persist($admin);
$manager->flush();
+
+ $this->addReference("user.admin", $admin);
}
}
\ No newline at end of file
diff --git a/src/PartKeepr/AuthBundle/Tests/Controller/DefaultControllerTest.php b/src/PartKeepr/AuthBundle/Tests/Controller/DefaultControllerTest.php
@@ -2,28 +2,35 @@
namespace PartKeepr\AuthBundle\Tests\Controller;
+use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
use Liip\FunctionalTestBundle\Test\WebTestCase;
+use PartKeepr\AuthBundle\Entity\User;
class DefaultControllerTest extends WebTestCase
{
+ /**
+ * @var ProxyReferenceRepository
+ */
+ private $fixtures;
+
public function setUp()
{
- $this->loadFixtures(
+ $this->fixtures = $this->loadFixtures(
array(
'PartKeepr\AuthBundle\DataFixtures\LoadUserData',
)
- );
+ )->getReferenceRepository();
}
- public function testLogin()
+ public function testGetSalt()
{
$client = static::createClient();
- $request = array("username" => "admin", "password" => md5("admin"));
+ $request = array("username" => "admin");
$client->request(
'POST',
- '/auth/login',
+ '/auth/getSalt',
array(),
array(),
array('CONTENT_TYPE' => 'application/json'),
@@ -32,88 +39,11 @@ class DefaultControllerTest extends WebTestCase
$response = json_decode($client->getResponse()->getContent());
- $this->assertObjectHasAttribute("sessionId", $response);
- $this->assertObjectHasAttribute("username", $response);
-
- }
-
- public function testMD5Constraint()
- {
- $client = static::createClient();
-
- $request = array("username" => "IDONOTEXIST", "password" => "IAMANINVALIDMD5HASH");
-
- $client->request(
- 'POST',
- '/auth/login',
- array(),
- array(),
- array('CONTENT_TYPE' => 'application/json'),
- json_encode($request)
- );
+ $admin = $this->fixtures->getReference("user.admin");
- $this->assertEquals($client->getResponse()->getStatusCode(), 400);
+ /**
+ * @var User $admin
+ */
+ $this->assertEquals($admin->getSalt(), $response);
}
-
- public function testFailedLogin()
- {
- $client = static::createClient();
-
- $request = array("username" => "IDONOTEXIST", "password" => md5("I AM NOT EXISTANT"));
-
- $client->request(
- 'POST',
- '/auth/login',
- array(),
- array(),
- array('CONTENT_TYPE' => 'application/json'),
- json_encode($request)
- );
-
- $this->assertEquals($client->getResponse()->getStatusCode(), 401);
- }
-
- public function testUsernameConstraints () {
- $this->_testInvalidUsernameConstraint("a"); // Username must be longer than 3 characters
- $this->_testInvalidUsernameConstraint(str_repeat("a",51)); // Username must be shorter than 50 characters
- $this->_testInvalidUsernameConstraint("BÄH"); // Username must not contain umlauts
-
- $this->_testValidUsernameConstraint("void_bar\\foo/test.ding-dong");
- }
- public function _testInvalidUsernameConstraint($username)
- {
- $client = static::createClient();
-
- $request = array("username" => $username, "password" => md5("I AM NOT EXISTANT"));
-
- $client->request(
- 'POST',
- '/auth/login',
- array(),
- array(),
- array('CONTENT_TYPE' => 'application/json'),
- json_encode($request)
- );
-
- $this->assertEquals($client->getResponse()->getStatusCode(), 400);
- }
-
- public function _testValidUsernameConstraint($username)
- {
- $client = static::createClient();
-
- $request = array("username" => $username, "password" => md5("I AM NOT EXISTANT"));
-
- $client->request(
- 'POST',
- '/auth/login',
- array(),
- array(),
- array('CONTENT_TYPE' => 'application/json'),
- json_encode($request)
- );
-
- $this->assertNotEquals($client->getResponse()->getStatusCode(), 400);
- }
-
}