3 namespace Tests\Wallabag\ApiBundle\Controller
;
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase
;
7 class UserRestControllerTest
extends WallabagApiTestCase
9 public function testGetUser()
11 $this->client
->request('GET', '/api/user.json');
12 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
14 $content = json_decode($this->client
->getResponse()->getContent(), true);
16 $this->assertArrayHasKey('id', $content);
17 $this->assertArrayHasKey('email', $content);
18 $this->assertArrayHasKey('name', $content);
19 $this->assertArrayHasKey('username', $content);
20 $this->assertArrayHasKey('created_at', $content);
21 $this->assertArrayHasKey('updated_at', $content);
23 $this->assertEquals('bigboss@wallabag.org', $content['email']);
24 $this->assertEquals('Big boss', $content['name']);
25 $this->assertEquals('admin', $content['username']);
27 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
30 public function testCreateNewUser()
32 $this->client
->request('PUT', '/api/user.json', [
33 'username' => 'google',
34 'password' => 'googlegoogle',
35 'email' => 'wallabag@google.com',
38 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
40 $content = json_decode($this->client
->getResponse()->getContent(), true);
42 $this->assertArrayHasKey('id', $content);
43 $this->assertArrayHasKey('email', $content);
44 $this->assertArrayHasKey('username', $content);
45 $this->assertArrayHasKey('created_at', $content);
46 $this->assertArrayHasKey('updated_at', $content);
48 $this->assertEquals('wallabag@google.com', $content['email']);
49 $this->assertEquals('google', $content['username']);
51 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
53 // remove the created user to avoid side effect on other tests
54 // @todo remove these lines when test will be isolated
55 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
57 $query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Config c WHERE c.user = :user_id');
58 $query->setParameter('user_id', $content['id']);
61 $query = $em->createQuery('DELETE FROM Wallabag\UserBundle\Entity\User u WHERE u.id = :id');
62 $query->setParameter('id', $content['id']);
66 public function testCreateNewUserWithExistingEmail()
68 $this->client
->request('PUT', '/api/user.json', [
69 'username' => 'admin',
70 'password' => 'googlegoogle',
71 'email' => 'bigboss@wallabag.org',
74 $this->assertEquals(400, $this->client
->getResponse()->getStatusCode());
76 $content = json_decode($this->client
->getResponse()->getContent(), true);
78 $this->assertArrayHasKey('error', $content);
79 $this->assertArrayHasKey('username', $content['error']);
80 $this->assertArrayHasKey('email', $content['error']);
82 // $this->assertEquals('fos_user.username.already_used', $content['error']['username'][0]);
83 // $this->assertEquals('fos_user.email.already_used', $content['error']['email'][0]);
84 // This shouldn't be translated ...
85 $this->assertEquals('This value is already used.', $content['error']['username'][0]);
86 $this->assertEquals('This value is already used.', $content['error']['email'][0]);
88 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
91 public function testCreateNewUserWithTooShortPassword()
93 $this->client
->request('PUT', '/api/user.json', [
94 'username' => 'facebook',
96 'email' => 'facebook@wallabag.org',
99 $this->assertEquals(400, $this->client
->getResponse()->getStatusCode());
101 $content = json_decode($this->client
->getResponse()->getContent(), true);
103 $this->assertArrayHasKey('error', $content);
104 $this->assertArrayHasKey('password', $content['error']);
106 $this->assertEquals('validator.password_too_short', $content['error']['password'][0]);
108 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));