diff options
Diffstat (limited to 'tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php')
-rw-r--r-- | tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php new file mode 100644 index 00000000..5735bc58 --- /dev/null +++ b/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php | |||
@@ -0,0 +1,172 @@ | |||
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 | |||
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')); | ||
69 | |||
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 | |||
84 | $this->assertEquals(201, $client->getResponse()->getStatusCode()); | ||
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']); | ||
96 | |||
97 | $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type')); | ||
98 | |||
99 | $client->getContainer()->get('craue_config')->set('api_user_registration', 0); | ||
100 | } | ||
101 | |||
102 | public function testCreateNewUserWithExistingEmail() | ||
103 | { | ||
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', | ||
110 | ]); | ||
111 | |||
112 | $this->assertEquals(400, $client->getResponse()->getStatusCode()); | ||
113 | |||
114 | $content = json_decode($client->getResponse()->getContent(), true); | ||
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 | |||
126 | $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type')); | ||
127 | |||
128 | $client->getContainer()->get('craue_config')->set('api_user_registration', 0); | ||
129 | } | ||
130 | |||
131 | public function testCreateNewUserWithTooShortPassword() | ||
132 | { | ||
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', | ||
139 | ]); | ||
140 | |||
141 | $this->assertEquals(400, $client->getResponse()->getStatusCode()); | ||
142 | |||
143 | $content = json_decode($client->getResponse()->getContent(), true); | ||
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 | |||
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')); | ||
171 | } | ||
172 | } | ||