]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php
Merge pull request #2788 from Zettt/master
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / DeveloperControllerTest.php
CommitLineData
8a4690b6 1<?php
5bf8f3f1 2
ee32248f 3namespace Tests\Wallabag\ApiBundle\Controller;
8a4690b6 4
23634d5d 5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
8a4690b6
NL
6
7class DeveloperControllerTest extends WallabagCoreTestCase
8{
9bf15f02 9 public function testCreateClient()
8a4690b6
NL
10 {
11 $this->logInAs('admin');
12 $client = $this->getClient();
9bf15f02
JB
13 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
14 $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll();
8a4690b6
NL
15
16 $crawler = $client->request('GET', '/developer/client/create');
17 $this->assertEquals(200, $client->getResponse()->getStatusCode());
18
19 $form = $crawler->filter('button[type=submit]')->form();
20
9c545fe0
TC
21 $data = [
22 'client[name]' => 'My app',
23 ];
24
25 $crawler = $client->submit($form, $data);
8a4690b6
NL
26
27 $this->assertEquals(200, $client->getResponse()->getStatusCode());
9bf15f02
JB
28
29 $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findAll();
30 $this->assertGreaterThan(count($nbClients), count($newNbClients));
9c545fe0
TC
31
32 $this->assertGreaterThan(1, $alert = $crawler->filter('.settings ul li strong')->extract(['_text']));
33 $this->assertContains('My app', $alert[0]);
9bf15f02
JB
34 }
35
ee32248f
JB
36 /**
37 * @depends testCreateClient
38 */
39 public function testCreateToken()
40 {
41 $client = $this->getClient();
42 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
43 $apiClient = $em->getRepository('WallabagApiBundle:Client')->findOneByName('My app');
44
45 $client->request('POST', '/oauth/v2/token', [
46 'grant_type' => 'password',
47 'client_id' => $apiClient->getPublicId(),
48 'client_secret' => $apiClient->getSecret(),
49 'username' => 'admin',
50 'password' => 'mypassword',
51 ]);
52
53 $this->assertEquals(200, $client->getResponse()->getStatusCode());
54
55 $data = json_decode($client->getResponse()->getContent(), true);
56 $this->assertArrayHasKey('access_token', $data);
57 $this->assertArrayHasKey('expires_in', $data);
58 $this->assertArrayHasKey('token_type', $data);
59 $this->assertArrayHasKey('refresh_token', $data);
60 }
61
9bf15f02
JB
62 public function testListingClient()
63 {
64 $this->logInAs('admin');
65 $client = $this->getClient();
66 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
67 $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll();
68
69 $crawler = $client->request('GET', '/developer');
70 $this->assertEquals(200, $client->getResponse()->getStatusCode());
71 $this->assertEquals(count($nbClients), $crawler->filter('ul[class=collapsible] li')->count());
72 }
73
74 public function testDeveloperHowto()
75 {
76 $this->logInAs('admin');
77 $client = $this->getClient();
78
79 $crawler = $client->request('GET', '/developer/howto/first-app');
80 $this->assertEquals(200, $client->getResponse()->getStatusCode());
81 }
82
83 public function testRemoveClient()
84 {
9bf15f02
JB
85 $client = $this->getClient();
86 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
9bf15f02 87
f24ea59e
NL
88 // Try to remove an admin's client with a wrong user
89 $this->logInAs('bob');
90 $client->request('GET', '/developer');
91 $this->assertContains('no_client', $client->getResponse()->getContent());
92
93 // get an ID of a admin's client
94 $this->logInAs('admin');
95 $nbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId());
96
97 $this->logInAs('bob');
98 $client->request('GET', '/developer/client/delete/'.$nbClients[0]->getId());
99 $this->assertEquals(403, $client->getResponse()->getStatusCode());
100
101 // Try to remove the admin's client with the good user
102 $this->logInAs('admin');
9bf15f02
JB
103 $crawler = $client->request('GET', '/developer');
104
105 $link = $crawler
106 ->filter('div[class=collapsible-body] p a')
107 ->eq(0)
108 ->link()
109 ;
110
111 $client->click($link);
112 $this->assertEquals(302, $client->getResponse()->getStatusCode());
113
f24ea59e 114 $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId());
9bf15f02 115 $this->assertGreaterThan(count($newNbClients), count($nbClients));
8a4690b6
NL
116 }
117}