]>
Commit | Line | Data |
---|---|---|
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->assertSame(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->assertSame('bigboss@wallabag.org', $content['email']); | |
24 | $this->assertSame('Big boss', $content['name']); | |
25 | $this->assertSame('admin', $content['username']); | |
26 | ||
27 | $this->assertSame('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->assertSame(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->assertSame('access_denied', $content['error']); | |
42 | ||
43 | $this->assertSame('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->assertSame(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->assertSame('wallabag@google.com', $content['email']); | |
67 | $this->assertSame('google', $content['username']); | |
68 | ||
69 | $this->assertArrayHasKey('client_secret', $content['default_client']); | |
70 | $this->assertArrayHasKey('client_id', $content['default_client']); | |
71 | ||
72 | $this->assertSame('Default client', $content['default_client']['name']); | |
73 | ||
74 | $this->assertSame('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 | 'client_name' => 'My client name !!', | |
89 | ]); | |
90 | ||
91 | $this->assertSame(201, $client->getResponse()->getStatusCode()); | |
92 | ||
93 | $content = json_decode($client->getResponse()->getContent(), true); | |
94 | ||
95 | $this->assertArrayHasKey('id', $content); | |
96 | $this->assertArrayHasKey('email', $content); | |
97 | $this->assertArrayHasKey('username', $content); | |
98 | $this->assertArrayHasKey('created_at', $content); | |
99 | $this->assertArrayHasKey('updated_at', $content); | |
100 | $this->assertArrayHasKey('default_client', $content); | |
101 | ||
102 | $this->assertSame('wallabag@google.com', $content['email']); | |
103 | $this->assertSame('google', $content['username']); | |
104 | ||
105 | $this->assertArrayHasKey('client_secret', $content['default_client']); | |
106 | $this->assertArrayHasKey('client_id', $content['default_client']); | |
107 | ||
108 | $this->assertSame('My client name !!', $content['default_client']['name']); | |
109 | ||
110 | $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); | |
111 | ||
112 | $client->getContainer()->get('craue_config')->set('api_user_registration', 0); | |
113 | } | |
114 | ||
115 | public function testCreateNewUserWithExistingEmail() | |
116 | { | |
117 | $client = static::createClient(); | |
118 | $client->getContainer()->get('craue_config')->set('api_user_registration', 1); | |
119 | $client->request('PUT', '/api/user.json', [ | |
120 | 'username' => 'admin', | |
121 | 'password' => 'googlegoogle', | |
122 | 'email' => 'bigboss@wallabag.org', | |
123 | ]); | |
124 | ||
125 | $this->assertSame(400, $client->getResponse()->getStatusCode()); | |
126 | ||
127 | $content = json_decode($client->getResponse()->getContent(), true); | |
128 | ||
129 | $this->assertArrayHasKey('error', $content); | |
130 | $this->assertArrayHasKey('username', $content['error']); | |
131 | $this->assertArrayHasKey('email', $content['error']); | |
132 | ||
133 | // $this->assertEquals('fos_user.username.already_used', $content['error']['username'][0]); | |
134 | // $this->assertEquals('fos_user.email.already_used', $content['error']['email'][0]); | |
135 | // This shouldn't be translated ... | |
136 | $this->assertSame('This value is already used.', $content['error']['username'][0]); | |
137 | $this->assertSame('This value is already used.', $content['error']['email'][0]); | |
138 | ||
139 | $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); | |
140 | ||
141 | $client->getContainer()->get('craue_config')->set('api_user_registration', 0); | |
142 | } | |
143 | ||
144 | public function testCreateNewUserWithTooShortPassword() | |
145 | { | |
146 | $client = static::createClient(); | |
147 | $client->getContainer()->get('craue_config')->set('api_user_registration', 1); | |
148 | $client->request('PUT', '/api/user.json', [ | |
149 | 'username' => 'facebook', | |
150 | 'password' => 'face', | |
151 | 'email' => 'facebook@wallabag.org', | |
152 | ]); | |
153 | ||
154 | $this->assertSame(400, $client->getResponse()->getStatusCode()); | |
155 | ||
156 | $content = json_decode($client->getResponse()->getContent(), true); | |
157 | ||
158 | $this->assertArrayHasKey('error', $content); | |
159 | $this->assertArrayHasKey('password', $content['error']); | |
160 | ||
161 | $this->assertSame('validator.password_too_short', $content['error']['password'][0]); | |
162 | ||
163 | $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); | |
164 | ||
165 | $client->getContainer()->get('craue_config')->set('api_user_registration', 0); | |
166 | } | |
167 | ||
168 | public function testCreateNewUserWhenRegistrationIsDisabled() | |
169 | { | |
170 | $client = static::createClient(); | |
171 | $client->request('PUT', '/api/user.json', [ | |
172 | 'username' => 'facebook', | |
173 | 'password' => 'face', | |
174 | 'email' => 'facebook@wallabag.org', | |
175 | ]); | |
176 | ||
177 | $this->assertSame(403, $client->getResponse()->getStatusCode()); | |
178 | ||
179 | $content = json_decode($client->getResponse()->getContent(), true); | |
180 | ||
181 | $this->assertArrayHasKey('error', $content); | |
182 | ||
183 | $this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type')); | |
184 | } | |
185 | } |