]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / UserRestControllerTest.php
index 3f4969a534bd7e6f4b2bb3701251a273e2b6301c..51fac2bd33194ce5dedd1d7210a69fae97d95a70 100644 (file)
@@ -9,7 +9,7 @@ class UserRestControllerTest extends WallabagApiTestCase
     public function testGetUser()
     {
         $this->client->request('GET', '/api/user.json');
-        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
+        $this->assertSame(200, $this->client->getResponse()->getStatusCode());
 
         $content = json_decode($this->client->getResponse()->getContent(), true);
 
@@ -20,22 +20,39 @@ class UserRestControllerTest extends WallabagApiTestCase
         $this->assertArrayHasKey('created_at', $content);
         $this->assertArrayHasKey('updated_at', $content);
 
-        $this->assertEquals('bigboss@wallabag.org', $content['email']);
-        $this->assertEquals('Big boss', $content['name']);
-        $this->assertEquals('admin', $content['username']);
+        $this->assertSame('bigboss@wallabag.org', $content['email']);
+        $this->assertSame('Big boss', $content['name']);
+        $this->assertSame('admin', $content['username']);
 
-        $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
+        $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
+    }
+
+    public function testGetUserWithoutAuthentication()
+    {
+        $client = static::createClient();
+        $client->request('GET', '/api/user.json');
+        $this->assertSame(401, $client->getResponse()->getStatusCode());
+
+        $content = json_decode($client->getResponse()->getContent(), true);
+
+        $this->assertArrayHasKey('error', $content);
+        $this->assertArrayHasKey('error_description', $content);
+
+        $this->assertSame('access_denied', $content['error']);
+
+        $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
     }
 
     public function testCreateNewUser()
     {
+        $this->client->getContainer()->get('craue_config')->set('api_user_registration', 1);
         $this->client->request('PUT', '/api/user.json', [
             'username' => 'google',
             'password' => 'googlegoogle',
             'email' => 'wallabag@google.com',
         ]);
 
-        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
+        $this->assertSame(201, $this->client->getResponse()->getStatusCode());
 
         $content = json_decode($this->client->getResponse()->getContent(), true);
 
@@ -44,36 +61,70 @@ class UserRestControllerTest extends WallabagApiTestCase
         $this->assertArrayHasKey('username', $content);
         $this->assertArrayHasKey('created_at', $content);
         $this->assertArrayHasKey('updated_at', $content);
+        $this->assertArrayHasKey('default_client', $content);
+
+        $this->assertSame('wallabag@google.com', $content['email']);
+        $this->assertSame('google', $content['username']);
+
+        $this->assertArrayHasKey('client_secret', $content['default_client']);
+        $this->assertArrayHasKey('client_id', $content['default_client']);
+
+        $this->assertSame('Default client', $content['default_client']['name']);
+
+        $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
+
+        $this->client->getContainer()->get('craue_config')->set('api_user_registration', 0);
+    }
+
+    public function testCreateNewUserWithoutAuthentication()
+    {
+        // create a new client instead of using $this->client to be sure client isn't authenticated
+        $client = static::createClient();
+        $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
+        $client->request('PUT', '/api/user.json', [
+            'username' => 'google',
+            'password' => 'googlegoogle',
+            'email' => 'wallabag@google.com',
+            'client_name' => 'My client name !!',
+        ]);
+
+        $this->assertSame(201, $client->getResponse()->getStatusCode());
+
+        $content = json_decode($client->getResponse()->getContent(), true);
+
+        $this->assertArrayHasKey('id', $content);
+        $this->assertArrayHasKey('email', $content);
+        $this->assertArrayHasKey('username', $content);
+        $this->assertArrayHasKey('created_at', $content);
+        $this->assertArrayHasKey('updated_at', $content);
+        $this->assertArrayHasKey('default_client', $content);
 
-        $this->assertEquals('wallabag@google.com', $content['email']);
-        $this->assertEquals('google', $content['username']);
+        $this->assertSame('wallabag@google.com', $content['email']);
+        $this->assertSame('google', $content['username']);
 
-        $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
+        $this->assertArrayHasKey('client_secret', $content['default_client']);
+        $this->assertArrayHasKey('client_id', $content['default_client']);
 
-        // remove the created user to avoid side effect on other tests
-        // @todo remove these lines when test will be isolated
-        $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
+        $this->assertSame('My client name !!', $content['default_client']['name']);
 
-        $query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Config c WHERE c.user = :user_id');
-        $query->setParameter('user_id', $content['id']);
-        $query->execute();
+        $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
 
-        $query = $em->createQuery('DELETE FROM Wallabag\UserBundle\Entity\User u WHERE u.id = :id');
-        $query->setParameter('id', $content['id']);
-        $query->execute();
+        $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
     }
 
     public function testCreateNewUserWithExistingEmail()
     {
-        $this->client->request('PUT', '/api/user.json', [
+        $client = static::createClient();
+        $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
+        $client->request('PUT', '/api/user.json', [
             'username' => 'admin',
             'password' => 'googlegoogle',
             'email' => 'bigboss@wallabag.org',
         ]);
 
-        $this->assertEquals(400, $this->client->getResponse()->getStatusCode());
+        $this->assertSame(400, $client->getResponse()->getStatusCode());
 
-        $content = json_decode($this->client->getResponse()->getContent(), true);
+        $content = json_decode($client->getResponse()->getContent(), true);
 
         $this->assertArrayHasKey('error', $content);
         $this->assertArrayHasKey('username', $content['error']);
@@ -82,29 +133,53 @@ class UserRestControllerTest extends WallabagApiTestCase
         // $this->assertEquals('fos_user.username.already_used', $content['error']['username'][0]);
         // $this->assertEquals('fos_user.email.already_used', $content['error']['email'][0]);
         // This shouldn't be translated ...
-        $this->assertEquals('This value is already used.', $content['error']['username'][0]);
-        $this->assertEquals('This value is already used.', $content['error']['email'][0]);
+        $this->assertSame('This value is already used.', $content['error']['username'][0]);
+        $this->assertSame('This value is already used.', $content['error']['email'][0]);
+
+        $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
 
-        $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
+        $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
     }
 
     public function testCreateNewUserWithTooShortPassword()
     {
-        $this->client->request('PUT', '/api/user.json', [
+        $client = static::createClient();
+        $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
+        $client->request('PUT', '/api/user.json', [
             'username' => 'facebook',
             'password' => 'face',
             'email' => 'facebook@wallabag.org',
         ]);
 
-        $this->assertEquals(400, $this->client->getResponse()->getStatusCode());
+        $this->assertSame(400, $client->getResponse()->getStatusCode());
 
-        $content = json_decode($this->client->getResponse()->getContent(), true);
+        $content = json_decode($client->getResponse()->getContent(), true);
 
         $this->assertArrayHasKey('error', $content);
         $this->assertArrayHasKey('password', $content['error']);
 
-        $this->assertEquals('validator.password_too_short', $content['error']['password'][0]);
+        $this->assertSame('validator.password_too_short', $content['error']['password'][0]);
+
+        $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
+
+        $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
+    }
+
+    public function testCreateNewUserWhenRegistrationIsDisabled()
+    {
+        $client = static::createClient();
+        $client->request('PUT', '/api/user.json', [
+            'username' => 'facebook',
+            'password' => 'face',
+            'email' => 'facebook@wallabag.org',
+        ]);
+
+        $this->assertSame(403, $client->getResponse()->getStatusCode());
+
+        $content = json_decode($client->getResponse()->getContent(), true);
+
+        $this->assertArrayHasKey('error', $content);
 
-        $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
+        $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
     }
 }