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 testGetUserWithoutAuthentication()
32 $client = static::createClient();
33 $client->request('GET', '/api/user.json');
34 $this->assertEquals(401, $client->getResponse()->getStatusCode());
36 $content = json_decode($client->getResponse()->getContent(), true);
38 $this->assertArrayHasKey('error', $content);
39 $this->assertArrayHasKey('error_description', $content);
41 $this->assertEquals('access_denied', $content['error']);
43 $this->assertEquals('application/json', $client->getResponse()->headers
->get('Content-Type'));
46 public function testCreateNewUser()
48 $this->client
->getContainer()->get('craue_config')->set('api_user_registration', 1);
49 $this->client
->request('PUT', '/api/user.json', [
50 'username' => 'google',
51 'password' => 'googlegoogle',
52 'email' => 'wallabag@google.com',
55 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
57 $content = json_decode($this->client
->getResponse()->getContent(), true);
59 $this->assertArrayHasKey('id', $content);
60 $this->assertArrayHasKey('email', $content);
61 $this->assertArrayHasKey('username', $content);
62 $this->assertArrayHasKey('created_at', $content);
63 $this->assertArrayHasKey('updated_at', $content);
65 $this->assertEquals('wallabag@google.com', $content['email']);
66 $this->assertEquals('google', $content['username']);
68 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
70 $this->client
->getContainer()->get('craue_config')->set('api_user_registration', 0);
73 public function testCreateNewUserWithoutAuthentication()
75 // create a new client instead of using $this->client to be sure client isn't authenticated
76 $client = static::createClient();
77 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
78 $client->request('PUT', '/api/user.json', [
79 'username' => 'google',
80 'password' => 'googlegoogle',
81 'email' => 'wallabag@google.com',
84 $this->assertEquals(200, $client->getResponse()->getStatusCode());
86 $content = json_decode($client->getResponse()->getContent(), true);
88 $this->assertArrayHasKey('id', $content);
89 $this->assertArrayHasKey('email', $content);
90 $this->assertArrayHasKey('username', $content);
91 $this->assertArrayHasKey('created_at', $content);
92 $this->assertArrayHasKey('updated_at', $content);
94 $this->assertEquals('wallabag@google.com', $content['email']);
95 $this->assertEquals('google', $content['username']);
97 $this->assertEquals('application/json', $client->getResponse()->headers
->get('Content-Type'));
99 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
102 public function testCreateNewUserWithExistingEmail()
104 $client = static::createClient();
105 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
106 $client->request('PUT', '/api/user.json', [
107 'username' => 'admin',
108 'password' => 'googlegoogle',
109 'email' => 'bigboss@wallabag.org',
112 $this->assertEquals(400, $client->getResponse()->getStatusCode());
114 $content = json_decode($client->getResponse()->getContent(), true);
116 $this->assertArrayHasKey('error', $content);
117 $this->assertArrayHasKey('username', $content['error']);
118 $this->assertArrayHasKey('email', $content['error']);
120 // $this->assertEquals('fos_user.username.already_used', $content['error']['username'][0]);
121 // $this->assertEquals('fos_user.email.already_used', $content['error']['email'][0]);
122 // This shouldn't be translated ...
123 $this->assertEquals('This value is already used.', $content['error']['username'][0]);
124 $this->assertEquals('This value is already used.', $content['error']['email'][0]);
126 $this->assertEquals('application/json', $client->getResponse()->headers
->get('Content-Type'));
128 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
131 public function testCreateNewUserWithTooShortPassword()
133 $client = static::createClient();
134 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
135 $client->request('PUT', '/api/user.json', [
136 'username' => 'facebook',
137 'password' => 'face',
138 'email' => 'facebook@wallabag.org',
141 $this->assertEquals(400, $client->getResponse()->getStatusCode());
143 $content = json_decode($client->getResponse()->getContent(), true);
145 $this->assertArrayHasKey('error', $content);
146 $this->assertArrayHasKey('password', $content['error']);
148 $this->assertEquals('validator.password_too_short', $content['error']['password'][0]);
150 $this->assertEquals('application/json', $client->getResponse()->headers
->get('Content-Type'));
152 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
155 public function testCreateNewUserWhenRegistrationIsDisabled()
157 $client = static::createClient();
158 $client->request('PUT', '/api/user.json', [
159 'username' => 'facebook',
160 'password' => 'face',
161 'email' => 'facebook@wallabag.org',
164 $this->assertEquals(403, $client->getResponse()->getStatusCode());
166 $content = json_decode($client->getResponse()->getContent(), true);
168 $this->assertArrayHasKey('error', $content);
170 $this->assertEquals('application/json', $client->getResponse()->headers
->get('Content-Type'));