aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/ApiBundle
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Wallabag/ApiBundle')
-rw-r--r--tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php513
-rw-r--r--tests/Wallabag/ApiBundle/WallabagApiTestCase.php51
2 files changed, 564 insertions, 0 deletions
diff --git a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
new file mode 100644
index 00000000..c39cc357
--- /dev/null
+++ b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
@@ -0,0 +1,513 @@
1<?php
2
3namespace Tests\Wallabag\ApiBundle\Controller;
4
5use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
6
7class WallabagRestControllerTest extends WallabagApiTestCase
8{
9 protected static $salt;
10
11 public function testGetOneEntry()
12 {
13 $entry = $this->client->getContainer()
14 ->get('doctrine.orm.entity_manager')
15 ->getRepository('WallabagCoreBundle:Entry')
16 ->findOneBy(['user' => 1, 'isArchived' => false]);
17
18 if (!$entry) {
19 $this->markTestSkipped('No content found in db.');
20 }
21
22 $this->client->request('GET', '/api/entries/'.$entry->getId().'.json');
23 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
24
25 $content = json_decode($this->client->getResponse()->getContent(), true);
26
27 $this->assertEquals($entry->getTitle(), $content['title']);
28 $this->assertEquals($entry->getUrl(), $content['url']);
29 $this->assertCount(count($entry->getTags()), $content['tags']);
30 $this->assertEquals($entry->getUserName(), $content['user_name']);
31 $this->assertEquals($entry->getUserEmail(), $content['user_email']);
32 $this->assertEquals($entry->getUserId(), $content['user_id']);
33
34 $this->assertTrue(
35 $this->client->getResponse()->headers->contains(
36 'Content-Type',
37 'application/json'
38 )
39 );
40 }
41
42 public function testGetOneEntryWrongUser()
43 {
44 $entry = $this->client->getContainer()
45 ->get('doctrine.orm.entity_manager')
46 ->getRepository('WallabagCoreBundle:Entry')
47 ->findOneBy(['user' => 2, 'isArchived' => false]);
48
49 if (!$entry) {
50 $this->markTestSkipped('No content found in db.');
51 }
52
53 $this->client->request('GET', '/api/entries/'.$entry->getId().'.json');
54
55 $this->assertEquals(403, $this->client->getResponse()->getStatusCode());
56 }
57
58 public function testGetEntries()
59 {
60 $this->client->request('GET', '/api/entries');
61
62 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
63
64 $content = json_decode($this->client->getResponse()->getContent(), true);
65
66 $this->assertGreaterThanOrEqual(1, count($content));
67 $this->assertNotEmpty($content['_embedded']['items']);
68 $this->assertGreaterThanOrEqual(1, $content['total']);
69 $this->assertEquals(1, $content['page']);
70 $this->assertGreaterThanOrEqual(1, $content['pages']);
71
72 $this->assertTrue(
73 $this->client->getResponse()->headers->contains(
74 'Content-Type',
75 'application/json'
76 )
77 );
78 }
79
80 public function testGetStarredEntries()
81 {
82 $this->client->request('GET', '/api/entries', ['star' => 1, 'sort' => 'updated']);
83
84 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
85
86 $content = json_decode($this->client->getResponse()->getContent(), true);
87
88 $this->assertGreaterThanOrEqual(1, count($content));
89 $this->assertNotEmpty($content['_embedded']['items']);
90 $this->assertGreaterThanOrEqual(1, $content['total']);
91 $this->assertEquals(1, $content['page']);
92 $this->assertGreaterThanOrEqual(1, $content['pages']);
93
94 $this->assertTrue(
95 $this->client->getResponse()->headers->contains(
96 'Content-Type',
97 'application/json'
98 )
99 );
100 }
101
102 public function testGetArchiveEntries()
103 {
104 $this->client->request('GET', '/api/entries', ['archive' => 1]);
105
106 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
107
108 $content = json_decode($this->client->getResponse()->getContent(), true);
109
110 $this->assertGreaterThanOrEqual(1, count($content));
111 $this->assertNotEmpty($content['_embedded']['items']);
112 $this->assertGreaterThanOrEqual(1, $content['total']);
113 $this->assertEquals(1, $content['page']);
114 $this->assertGreaterThanOrEqual(1, $content['pages']);
115
116 $this->assertTrue(
117 $this->client->getResponse()->headers->contains(
118 'Content-Type',
119 'application/json'
120 )
121 );
122 }
123
124 public function testDeleteEntry()
125 {
126 $entry = $this->client->getContainer()
127 ->get('doctrine.orm.entity_manager')
128 ->getRepository('WallabagCoreBundle:Entry')
129 ->findOneByUser(1);
130
131 if (!$entry) {
132 $this->markTestSkipped('No content found in db.');
133 }
134
135 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
136
137 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
138
139 $content = json_decode($this->client->getResponse()->getContent(), true);
140
141 $this->assertEquals($entry->getTitle(), $content['title']);
142 $this->assertEquals($entry->getUrl(), $content['url']);
143
144 // We'll try to delete this entry again
145 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
146
147 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
148 }
149
150 public function testPostEntry()
151 {
152 $this->client->request('POST', '/api/entries.json', [
153 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html',
154 'tags' => 'google',
155 'title' => 'New title for my article',
156 ]);
157
158 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
159
160 $content = json_decode($this->client->getResponse()->getContent(), true);
161
162 $this->assertGreaterThan(0, $content['id']);
163 $this->assertEquals('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']);
164 $this->assertEquals(false, $content['is_archived']);
165 $this->assertEquals(false, $content['is_starred']);
166 $this->assertEquals('New title for my article', $content['title']);
167 $this->assertEquals(1, $content['user_id']);
168 $this->assertCount(1, $content['tags']);
169 }
170
171 public function testPostSameEntry()
172 {
173 $this->client->request('POST', '/api/entries.json', [
174 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html',
175 'archive' => '1',
176 'tags' => 'google, apple',
177 ]);
178
179 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
180
181 $content = json_decode($this->client->getResponse()->getContent(), true);
182
183 $this->assertGreaterThan(0, $content['id']);
184 $this->assertEquals('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']);
185 $this->assertEquals(true, $content['is_archived']);
186 $this->assertEquals(false, $content['is_starred']);
187 $this->assertCount(2, $content['tags']);
188 }
189
190 public function testPostArchivedAndStarredEntry()
191 {
192 $this->client->request('POST', '/api/entries.json', [
193 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
194 'archive' => '1',
195 'starred' => '1',
196 ]);
197
198 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
199
200 $content = json_decode($this->client->getResponse()->getContent(), true);
201
202 $this->assertGreaterThan(0, $content['id']);
203 $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
204 $this->assertEquals(true, $content['is_archived']);
205 $this->assertEquals(true, $content['is_starred']);
206 $this->assertEquals(1, $content['user_id']);
207 }
208
209 public function testPostArchivedAndStarredEntryWithoutQuotes()
210 {
211 $this->client->request('POST', '/api/entries.json', [
212 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
213 'archive' => 0,
214 'starred' => 1,
215 ]);
216
217 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
218
219 $content = json_decode($this->client->getResponse()->getContent(), true);
220
221 $this->assertGreaterThan(0, $content['id']);
222 $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
223 $this->assertEquals(false, $content['is_archived']);
224 $this->assertEquals(true, $content['is_starred']);
225 }
226
227 public function testPatchEntry()
228 {
229 $entry = $this->client->getContainer()
230 ->get('doctrine.orm.entity_manager')
231 ->getRepository('WallabagCoreBundle:Entry')
232 ->findOneByUser(1);
233
234 if (!$entry) {
235 $this->markTestSkipped('No content found in db.');
236 }
237
238 // hydrate the tags relations
239 $nbTags = count($entry->getTags());
240
241 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
242 'title' => 'New awesome title',
243 'tags' => 'new tag '.uniqid(),
244 'starred' => '1',
245 'archive' => '0',
246 ]);
247
248 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
249
250 $content = json_decode($this->client->getResponse()->getContent(), true);
251
252 $this->assertEquals($entry->getId(), $content['id']);
253 $this->assertEquals($entry->getUrl(), $content['url']);
254 $this->assertEquals('New awesome title', $content['title']);
255 $this->assertGreaterThan($nbTags, count($content['tags']));
256 $this->assertEquals(1, $content['user_id']);
257 }
258
259 public function testPatchEntryWithoutQuotes()
260 {
261 $entry = $this->client->getContainer()
262 ->get('doctrine.orm.entity_manager')
263 ->getRepository('WallabagCoreBundle:Entry')
264 ->findOneByUser(1);
265
266 if (!$entry) {
267 $this->markTestSkipped('No content found in db.');
268 }
269
270 // hydrate the tags relations
271 $nbTags = count($entry->getTags());
272
273 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
274 'title' => 'New awesome title',
275 'tags' => 'new tag '.uniqid(),
276 'starred' => 1,
277 'archive' => 0,
278 ]);
279
280 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
281
282 $content = json_decode($this->client->getResponse()->getContent(), true);
283
284 $this->assertEquals($entry->getId(), $content['id']);
285 $this->assertEquals($entry->getUrl(), $content['url']);
286 $this->assertEquals('New awesome title', $content['title']);
287 $this->assertGreaterThan($nbTags, count($content['tags']));
288 }
289
290 public function testGetTagsEntry()
291 {
292 $entry = $this->client->getContainer()
293 ->get('doctrine.orm.entity_manager')
294 ->getRepository('WallabagCoreBundle:Entry')
295 ->findOneWithTags(1);
296
297 $entry = $entry[0];
298
299 if (!$entry) {
300 $this->markTestSkipped('No content found in db.');
301 }
302
303 $tags = [];
304 foreach ($entry->getTags() as $tag) {
305 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
306 }
307
308 $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags');
309
310 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
311 }
312
313 public function testPostTagsOnEntry()
314 {
315 $entry = $this->client->getContainer()
316 ->get('doctrine.orm.entity_manager')
317 ->getRepository('WallabagCoreBundle:Entry')
318 ->findOneByUser(1);
319
320 if (!$entry) {
321 $this->markTestSkipped('No content found in db.');
322 }
323
324 $nbTags = count($entry->getTags());
325
326 $newTags = 'tag1,tag2,tag3';
327
328 $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
329
330 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
331
332 $content = json_decode($this->client->getResponse()->getContent(), true);
333
334 $this->assertArrayHasKey('tags', $content);
335 $this->assertEquals($nbTags + 3, count($content['tags']));
336
337 $entryDB = $this->client->getContainer()
338 ->get('doctrine.orm.entity_manager')
339 ->getRepository('WallabagCoreBundle:Entry')
340 ->find($entry->getId());
341
342 $tagsInDB = [];
343 foreach ($entryDB->getTags()->toArray() as $tag) {
344 $tagsInDB[$tag->getId()] = $tag->getLabel();
345 }
346
347 foreach (explode(',', $newTags) as $tag) {
348 $this->assertContains($tag, $tagsInDB);
349 }
350 }
351
352 public function testDeleteOneTagEntry()
353 {
354 $entry = $this->client->getContainer()
355 ->get('doctrine.orm.entity_manager')
356 ->getRepository('WallabagCoreBundle:Entry')
357 ->findOneWithTags(1);
358 $entry = $entry[0];
359
360 if (!$entry) {
361 $this->markTestSkipped('No content found in db.');
362 }
363
364 // hydrate the tags relations
365 $nbTags = count($entry->getTags());
366 $tag = $entry->getTags()[0];
367
368 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
369
370 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
371
372 $content = json_decode($this->client->getResponse()->getContent(), true);
373
374 $this->assertArrayHasKey('tags', $content);
375 $this->assertEquals($nbTags - 1, count($content['tags']));
376 }
377
378 public function testGetUserTags()
379 {
380 $this->client->request('GET', '/api/tags.json');
381
382 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
383
384 $content = json_decode($this->client->getResponse()->getContent(), true);
385
386 $this->assertGreaterThan(0, $content);
387 $this->assertArrayHasKey('id', $content[0]);
388 $this->assertArrayHasKey('label', $content[0]);
389
390 return end($content);
391 }
392
393 /**
394 * @depends testGetUserTags
395 */
396 public function testDeleteUserTag($tag)
397 {
398 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json');
399
400 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
401
402 $content = json_decode($this->client->getResponse()->getContent(), true);
403
404 $this->assertArrayHasKey('label', $content);
405 $this->assertEquals($tag['label'], $content['label']);
406 $this->assertEquals($tag['slug'], $content['slug']);
407
408 $entries = $entry = $this->client->getContainer()
409 ->get('doctrine.orm.entity_manager')
410 ->getRepository('WallabagCoreBundle:Entry')
411 ->findAllByTagId($this->user->getId(), $tag['id']);
412
413 $this->assertCount(0, $entries);
414 }
415
416 public function testGetVersion()
417 {
418 $this->client->request('GET', '/api/version');
419
420 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
421
422 $content = json_decode($this->client->getResponse()->getContent(), true);
423
424 $this->assertEquals($this->client->getContainer()->getParameter('wallabag_core.version'), $content);
425 }
426
427 public function testSaveIsArchivedAfterPost()
428 {
429 $entry = $this->client->getContainer()
430 ->get('doctrine.orm.entity_manager')
431 ->getRepository('WallabagCoreBundle:Entry')
432 ->findOneBy(['user' => 1, 'isArchived' => true]);
433
434 if (!$entry) {
435 $this->markTestSkipped('No content found in db.');
436 }
437
438 $this->client->request('POST', '/api/entries.json', [
439 'url' => $entry->getUrl(),
440 ]);
441
442 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
443
444 $content = json_decode($this->client->getResponse()->getContent(), true);
445
446 $this->assertEquals(true, $content['is_archived']);
447 }
448
449 public function testSaveIsStarredAfterPost()
450 {
451 $entry = $this->client->getContainer()
452 ->get('doctrine.orm.entity_manager')
453 ->getRepository('WallabagCoreBundle:Entry')
454 ->findOneBy(['user' => 1, 'isStarred' => true]);
455
456 if (!$entry) {
457 $this->markTestSkipped('No content found in db.');
458 }
459
460 $this->client->request('POST', '/api/entries.json', [
461 'url' => $entry->getUrl(),
462 ]);
463
464 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
465
466 $content = json_decode($this->client->getResponse()->getContent(), true);
467
468 $this->assertEquals(true, $content['is_starred']);
469 }
470
471 public function testSaveIsArchivedAfterPatch()
472 {
473 $entry = $this->client->getContainer()
474 ->get('doctrine.orm.entity_manager')
475 ->getRepository('WallabagCoreBundle:Entry')
476 ->findOneBy(['user' => 1, 'isArchived' => true]);
477
478 if (!$entry) {
479 $this->markTestSkipped('No content found in db.');
480 }
481
482 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
483 'title' => $entry->getTitle().'++',
484 ]);
485
486 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
487
488 $content = json_decode($this->client->getResponse()->getContent(), true);
489
490 $this->assertEquals(true, $content['is_archived']);
491 }
492
493 public function testSaveIsStarredAfterPatch()
494 {
495 $entry = $this->client->getContainer()
496 ->get('doctrine.orm.entity_manager')
497 ->getRepository('WallabagCoreBundle:Entry')
498 ->findOneBy(['user' => 1, 'isStarred' => true]);
499
500 if (!$entry) {
501 $this->markTestSkipped('No content found in db.');
502 }
503 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
504 'title' => $entry->getTitle().'++',
505 ]);
506
507 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
508
509 $content = json_decode($this->client->getResponse()->getContent(), true);
510
511 $this->assertEquals(true, $content['is_starred']);
512 }
513}
diff --git a/tests/Wallabag/ApiBundle/WallabagApiTestCase.php b/tests/Wallabag/ApiBundle/WallabagApiTestCase.php
new file mode 100644
index 00000000..cf9b3347
--- /dev/null
+++ b/tests/Wallabag/ApiBundle/WallabagApiTestCase.php
@@ -0,0 +1,51 @@
1<?php
2
3namespace Tests\Wallabag\ApiBundle;
4
5use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6use Symfony\Component\BrowserKit\Cookie;
7
8abstract class WallabagApiTestCase extends WebTestCase
9{
10 /**
11 * @var Client
12 */
13 protected $client = null;
14
15 /**
16 * @var \FOS\UserBundle\Model\UserInterface
17 */
18 protected $user;
19
20 public function setUp()
21 {
22 $this->client = $this->createAuthorizedClient();
23 }
24
25 /**
26 * @return Client
27 */
28 protected function createAuthorizedClient()
29 {
30 $client = static::createClient();
31 $container = $client->getContainer();
32
33 /** @var $userManager \FOS\UserBundle\Doctrine\UserManager */
34 $userManager = $container->get('fos_user.user_manager');
35 /** @var $loginManager \FOS\UserBundle\Security\LoginManager */
36 $loginManager = $container->get('fos_user.security.login_manager');
37 $firewallName = $container->getParameter('fos_user.firewall_name');
38
39 $this->user = $userManager->findUserBy(['username' => 'admin']);
40 $loginManager->loginUser($firewallName, $this->user);
41
42 // save the login token into the session and put it in a cookie
43 $container->get('session')->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken()));
44 $container->get('session')->save();
45
46 $session = $container->get('session');
47 $client->getCookieJar()->set(new Cookie($session->getName(), $session->getId()));
48
49 return $client;
50 }
51}