3 namespace Tests\Wallabag\ApiBundle\Controller
;
5 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase
;
6 use Wallabag\ApiBundle\Entity\Client
;
8 class DeveloperControllerTest
extends WallabagCoreTestCase
10 public function testCreateClient()
12 $this->logInAs('admin');
13 $client = $this->getClient();
14 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
15 $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll();
17 $crawler = $client->request('GET', '/developer/client/create');
18 $this->assertSame(200, $client->getResponse()->getStatusCode());
20 $form = $crawler->filter('button[id=client_save]')->form();
23 'client[name]' => 'My app',
26 $crawler = $client->submit($form, $data);
28 $this->assertSame(200, $client->getResponse()->getStatusCode());
30 $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findAll();
31 $this->assertGreaterThan(\
count($nbClients), \
count($newNbClients));
33 $this->assertGreaterThan(1, $alert = $crawler->filter('.settings ul li strong')->extract(['_text']));
34 $this->assertContains('My app', $alert[0]);
37 public function testCreateToken()
39 $client = $this->getClient();
40 $apiClient = $this->createApiClientForUser('admin');
42 $client->request('POST', '/oauth/v2/token', [
43 'grant_type' => 'password',
44 'client_id' => $apiClient->getPublicId(),
45 'client_secret' => $apiClient->getSecret(),
46 'username' => 'admin',
47 'password' => 'mypassword',
50 $this->assertSame(200, $client->getResponse()->getStatusCode());
52 $data = json_decode($client->getResponse()->getContent(), true);
53 $this->assertArrayHasKey('access_token', $data);
54 $this->assertArrayHasKey('expires_in', $data);
55 $this->assertArrayHasKey('token_type', $data);
56 $this->assertArrayHasKey('refresh_token', $data);
59 public function testCreateTokenWithBadClientId()
61 $client = $this->getClient();
62 $client->request('POST', '/oauth/v2/token', [
63 'grant_type' => 'password',
64 'client_id' => '$WALLABAG_CLIENT_ID',
65 'client_secret
' => 'secret
',
66 'username
' => 'admin
',
67 'password
' => 'mypassword
',
70 $this->assertSame(400, $client->getResponse()->getStatusCode());
73 public function testListingClient()
75 $this->logInAs('admin
');
76 $client = $this->getClient();
77 $em = $client->getContainer()->get('doctrine
.orm
.entity_manager
');
78 $nbClients = $em->getRepository('WallabagApiBundle
:Client
')->findAll();
80 $crawler = $client->request('GET
', '/developer
');
81 $this->assertSame(200, $client->getResponse()->getStatusCode());
82 $this->assertSame(\count($nbClients), $crawler->filter('ul
[class=collapsible
] li
')->count());
85 public function testDeveloperHowto()
87 $this->logInAs('admin
');
88 $client = $this->getClient();
90 $crawler = $client->request('GET
', '/developer
/howto
/first
-app
');
91 $this->assertSame(200, $client->getResponse()->getStatusCode());
94 public function testRemoveClient()
96 $client = $this->getClient();
97 $adminApiClient = $this->createApiClientForUser('admin
');
98 $em = $client->getContainer()->get('doctrine
.orm
.entity_manager
');
100 // Try to remove an admin's client with a wrong user
101 $this->logInAs('bob');
102 $client->request('GET', '/developer');
103 $this->assertContains('no_client', $client->getResponse()->getContent());
105 $this->logInAs('bob');
106 $client->request('GET', '/developer/client/delete/' . $adminApiClient->getId());
107 $this->assertSame(403, $client->getResponse()->getStatusCode());
109 // Try to remove the admin's client with the good user
110 $this->logInAs('admin');
111 $crawler = $client->request('GET', '/developer');
114 ->filter('div[class=collapsible-body] p a')
119 $client->click($link);
120 $this->assertSame(302, $client->getResponse()->getStatusCode());
123 $em->getRepository('WallabagApiBundle:Client')->find($adminApiClient->getId()),
124 'The client should have been removed'
129 * @param string $username
130 * @param array $grantTypes
134 private function createApiClientForUser($username, $grantTypes = ['password'])
136 $client = $this->getClient();
137 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
138 $userManager = $client->getContainer()->get('fos_user.user_manager');
139 $user = $userManager->findUserBy(['username' => $username]);
140 $apiClient = new Client($user);
141 $apiClient->setName('My app');
142 $apiClient->setAllowedGrantTypes($grantTypes);
143 $em->persist($apiClient);