]>
Commit | Line | Data |
---|---|---|
5709ecb3 JB |
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 | ||
426bb453 JB |
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 | ||
5709ecb3 JB |
46 | public function testCreateNewUser() |
47 | { | |
426bb453 | 48 | $this->client->getContainer()->get('craue_config')->set('api_user_registration', 1); |
5709ecb3 JB |
49 | $this->client->request('PUT', '/api/user.json', [ |
50 | 'username' => 'google', | |
51 | 'password' => 'googlegoogle', | |
52 | 'email' => 'wallabag@google.com', | |
53 | ]); | |
54 | ||
a1e61874 | 55 | $this->assertEquals(201, $this->client->getResponse()->getStatusCode()); |
5709ecb3 JB |
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 | ||
65 | $this->assertEquals('wallabag@google.com', $content['email']); | |
66 | $this->assertEquals('google', $content['username']); | |
67 | ||
68 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
fe6461e4 | 69 | |
426bb453 JB |
70 | $this->client->getContainer()->get('craue_config')->set('api_user_registration', 0); |
71 | } | |
72 | ||
73 | public function testCreateNewUserWithoutAuthentication() | |
74 | { | |
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', | |
82 | ]); | |
83 | ||
a1e61874 | 84 | $this->assertEquals(201, $client->getResponse()->getStatusCode()); |
426bb453 JB |
85 | |
86 | $content = json_decode($client->getResponse()->getContent(), true); | |
87 | ||
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); | |
93 | ||
94 | $this->assertEquals('wallabag@google.com', $content['email']); | |
95 | $this->assertEquals('google', $content['username']); | |
fe6461e4 | 96 | |
426bb453 | 97 | $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type')); |
fe6461e4 | 98 | |
426bb453 | 99 | $client->getContainer()->get('craue_config')->set('api_user_registration', 0); |
5709ecb3 JB |
100 | } |
101 | ||
102 | public function testCreateNewUserWithExistingEmail() | |
103 | { | |
426bb453 JB |
104 | $client = static::createClient(); |
105 | $client->getContainer()->get('craue_config')->set('api_user_registration', 1); | |
106 | $client->request('PUT', '/api/user.json', [ | |
fe6461e4 | 107 | 'username' => 'admin', |
5709ecb3 JB |
108 | 'password' => 'googlegoogle', |
109 | 'email' => 'bigboss@wallabag.org', | |
110 | ]); | |
111 | ||
426bb453 | 112 | $this->assertEquals(400, $client->getResponse()->getStatusCode()); |
5709ecb3 | 113 | |
426bb453 | 114 | $content = json_decode($client->getResponse()->getContent(), true); |
5709ecb3 JB |
115 | |
116 | $this->assertArrayHasKey('error', $content); | |
117 | $this->assertArrayHasKey('username', $content['error']); | |
118 | $this->assertArrayHasKey('email', $content['error']); | |
119 | ||
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]); | |
125 | ||
426bb453 JB |
126 | $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type')); |
127 | ||
128 | $client->getContainer()->get('craue_config')->set('api_user_registration', 0); | |
5709ecb3 JB |
129 | } |
130 | ||
131 | public function testCreateNewUserWithTooShortPassword() | |
132 | { | |
426bb453 JB |
133 | $client = static::createClient(); |
134 | $client->getContainer()->get('craue_config')->set('api_user_registration', 1); | |
135 | $client->request('PUT', '/api/user.json', [ | |
5709ecb3 JB |
136 | 'username' => 'facebook', |
137 | 'password' => 'face', | |
138 | 'email' => 'facebook@wallabag.org', | |
139 | ]); | |
140 | ||
426bb453 | 141 | $this->assertEquals(400, $client->getResponse()->getStatusCode()); |
5709ecb3 | 142 | |
426bb453 | 143 | $content = json_decode($client->getResponse()->getContent(), true); |
5709ecb3 JB |
144 | |
145 | $this->assertArrayHasKey('error', $content); | |
146 | $this->assertArrayHasKey('password', $content['error']); | |
147 | ||
148 | $this->assertEquals('validator.password_too_short', $content['error']['password'][0]); | |
149 | ||
426bb453 JB |
150 | $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type')); |
151 | ||
152 | $client->getContainer()->get('craue_config')->set('api_user_registration', 0); | |
153 | } | |
154 | ||
155 | public function testCreateNewUserWhenRegistrationIsDisabled() | |
156 | { | |
157 | $client = static::createClient(); | |
158 | $client->request('PUT', '/api/user.json', [ | |
159 | 'username' => 'facebook', | |
160 | 'password' => 'face', | |
161 | 'email' => 'facebook@wallabag.org', | |
162 | ]); | |
163 | ||
164 | $this->assertEquals(403, $client->getResponse()->getStatusCode()); | |
165 | ||
166 | $content = json_decode($client->getResponse()->getContent(), true); | |
167 | ||
168 | $this->assertArrayHasKey('error', $content); | |
169 | ||
170 | $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type')); | |
5709ecb3 JB |
171 | } |
172 | } |