]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php
Create a client when creating a user using the api
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / UserRestControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\ApiBundle\Controller;
4
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
6
7 class UserRestControllerTest extends WallabagApiTestCase
8 {
9 public function testGetUser()
10 {
11 $this->client->request('GET', '/api/user.json');
12 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
13
14 $content = json_decode($this->client->getResponse()->getContent(), true);
15
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);
22
23 $this->assertEquals('bigboss@wallabag.org', $content['email']);
24 $this->assertEquals('Big boss', $content['name']);
25 $this->assertEquals('admin', $content['username']);
26
27 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
28 }
29
30 public function testGetUserWithoutAuthentication()
31 {
32 $client = static::createClient();
33 $client->request('GET', '/api/user.json');
34 $this->assertEquals(401, $client->getResponse()->getStatusCode());
35
36 $content = json_decode($client->getResponse()->getContent(), true);
37
38 $this->assertArrayHasKey('error', $content);
39 $this->assertArrayHasKey('error_description', $content);
40
41 $this->assertEquals('access_denied', $content['error']);
42
43 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
44 }
45
46 public function testCreateNewUser()
47 {
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',
53 ]);
54
55 $this->assertEquals(201, $this->client->getResponse()->getStatusCode());
56
57 $content = json_decode($this->client->getResponse()->getContent(), true);
58
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);
64 $this->assertArrayHasKey('default_client', $content);
65
66 $this->assertEquals('wallabag@google.com', $content['email']);
67 $this->assertEquals('google', $content['username']);
68
69 $this->assertArrayHasKey('client_secret', $content['default_client']);
70 $this->assertArrayHasKey('client_id', $content['default_client']);
71
72 $this->assertEquals('Default client', $content['default_client']['name']);
73
74 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
75
76 $this->client->getContainer()->get('craue_config')->set('api_user_registration', 0);
77 }
78
79 public function testCreateNewUserWithoutAuthentication()
80 {
81 // create a new client instead of using $this->client to be sure client isn't authenticated
82 $client = static::createClient();
83 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
84 $client->request('PUT', '/api/user.json', [
85 'username' => 'google',
86 'password' => 'googlegoogle',
87 'email' => 'wallabag@google.com',
88 ]);
89
90 $this->assertEquals(201, $client->getResponse()->getStatusCode());
91
92 $content = json_decode($client->getResponse()->getContent(), true);
93
94 $this->assertArrayHasKey('id', $content);
95 $this->assertArrayHasKey('email', $content);
96 $this->assertArrayHasKey('username', $content);
97 $this->assertArrayHasKey('created_at', $content);
98 $this->assertArrayHasKey('updated_at', $content);
99 $this->assertArrayHasKey('default_client', $content);
100
101 $this->assertEquals('wallabag@google.com', $content['email']);
102 $this->assertEquals('google', $content['username']);
103
104 $this->assertArrayHasKey('client_secret', $content['default_client']);
105 $this->assertArrayHasKey('client_id', $content['default_client']);
106
107 $this->assertEquals('Default client', $content['default_client']['name']);
108
109 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
110
111 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
112 }
113
114 public function testCreateNewUserWithExistingEmail()
115 {
116 $client = static::createClient();
117 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
118 $client->request('PUT', '/api/user.json', [
119 'username' => 'admin',
120 'password' => 'googlegoogle',
121 'email' => 'bigboss@wallabag.org',
122 ]);
123
124 $this->assertEquals(400, $client->getResponse()->getStatusCode());
125
126 $content = json_decode($client->getResponse()->getContent(), true);
127
128 $this->assertArrayHasKey('error', $content);
129 $this->assertArrayHasKey('username', $content['error']);
130 $this->assertArrayHasKey('email', $content['error']);
131
132 // $this->assertEquals('fos_user.username.already_used', $content['error']['username'][0]);
133 // $this->assertEquals('fos_user.email.already_used', $content['error']['email'][0]);
134 // This shouldn't be translated ...
135 $this->assertEquals('This value is already used.', $content['error']['username'][0]);
136 $this->assertEquals('This value is already used.', $content['error']['email'][0]);
137
138 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
139
140 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
141 }
142
143 public function testCreateNewUserWithTooShortPassword()
144 {
145 $client = static::createClient();
146 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
147 $client->request('PUT', '/api/user.json', [
148 'username' => 'facebook',
149 'password' => 'face',
150 'email' => 'facebook@wallabag.org',
151 ]);
152
153 $this->assertEquals(400, $client->getResponse()->getStatusCode());
154
155 $content = json_decode($client->getResponse()->getContent(), true);
156
157 $this->assertArrayHasKey('error', $content);
158 $this->assertArrayHasKey('password', $content['error']);
159
160 $this->assertEquals('validator.password_too_short', $content['error']['password'][0]);
161
162 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
163
164 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
165 }
166
167 public function testCreateNewUserWhenRegistrationIsDisabled()
168 {
169 $client = static::createClient();
170 $client->request('PUT', '/api/user.json', [
171 'username' => 'facebook',
172 'password' => 'face',
173 'email' => 'facebook@wallabag.org',
174 ]);
175
176 $this->assertEquals(403, $client->getResponse()->getStatusCode());
177
178 $content = json_decode($client->getResponse()->getContent(), true);
179
180 $this->assertArrayHasKey('error', $content);
181
182 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
183 }
184 }