]>
Commit | Line | Data |
---|---|---|
8a4690b6 | 1 | <?php |
5bf8f3f1 | 2 | |
ee32248f | 3 | namespace Tests\Wallabag\ApiBundle\Controller; |
8a4690b6 | 4 | |
23634d5d | 5 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; |
7ab5eb95 | 6 | use Wallabag\ApiBundle\Entity\Client; |
8a4690b6 NL |
7 | |
8 | class DeveloperControllerTest extends WallabagCoreTestCase | |
9 | { | |
9bf15f02 | 10 | public function testCreateClient() |
8a4690b6 NL |
11 | { |
12 | $this->logInAs('admin'); | |
13 | $client = $this->getClient(); | |
9bf15f02 JB |
14 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); |
15 | $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); | |
8a4690b6 NL |
16 | |
17 | $crawler = $client->request('GET', '/developer/client/create'); | |
f808b016 | 18 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
8a4690b6 NL |
19 | |
20 | $form = $crawler->filter('button[type=submit]')->form(); | |
21 | ||
9c545fe0 TC |
22 | $data = [ |
23 | 'client[name]' => 'My app', | |
24 | ]; | |
25 | ||
26 | $crawler = $client->submit($form, $data); | |
8a4690b6 | 27 | |
f808b016 | 28 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
9bf15f02 JB |
29 | |
30 | $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); | |
31 | $this->assertGreaterThan(count($nbClients), count($newNbClients)); | |
9c545fe0 TC |
32 | |
33 | $this->assertGreaterThan(1, $alert = $crawler->filter('.settings ul li strong')->extract(['_text'])); | |
34 | $this->assertContains('My app', $alert[0]); | |
9bf15f02 JB |
35 | } |
36 | ||
caa0b176 | 37 | public function testCreateTokenFromPasswords() |
ee32248f JB |
38 | { |
39 | $client = $this->getClient(); | |
7ab5eb95 | 40 | $apiClient = $this->createApiClientForUser('admin'); |
ee32248f JB |
41 | |
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', | |
48 | ]); | |
49 | ||
f808b016 | 50 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
ee32248f JB |
51 | |
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); | |
57 | } | |
58 | ||
caa0b176 TC |
59 | public function testCreateTokenFromClientCredentialsOnly() |
60 | { | |
61 | $client = $this->getClient(); | |
62 | $apiClient = $this->createApiClientForUser('admin', ['client_credentials']); | |
63 | ||
64 | $client->request('POST', '/oauth/v2/token', [ | |
65 | 'grant_type' => 'client_credentials', | |
66 | 'client_id' => $apiClient->getPublicId(), | |
67 | 'client_secret' => $apiClient->getSecret(), | |
68 | ]); | |
69 | ||
f808b016 | 70 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
caa0b176 TC |
71 | |
72 | $data = json_decode($client->getResponse()->getContent(), true); | |
73 | $this->assertArrayHasKey('access_token', $data); | |
74 | $this->assertArrayHasKey('expires_in', $data); | |
75 | $this->assertArrayHasKey('token_type', $data); | |
76 | // Client Credentials created-clients have no refresh tokens | |
77 | } | |
78 | ||
9bf15f02 JB |
79 | public function testListingClient() |
80 | { | |
81 | $this->logInAs('admin'); | |
82 | $client = $this->getClient(); | |
83 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
84 | $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); | |
85 | ||
86 | $crawler = $client->request('GET', '/developer'); | |
f808b016 JB |
87 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
88 | $this->assertSame(count($nbClients), $crawler->filter('ul[class=collapsible] li')->count()); | |
9bf15f02 JB |
89 | } |
90 | ||
91 | public function testDeveloperHowto() | |
92 | { | |
93 | $this->logInAs('admin'); | |
94 | $client = $this->getClient(); | |
95 | ||
96 | $crawler = $client->request('GET', '/developer/howto/first-app'); | |
f808b016 | 97 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
9bf15f02 JB |
98 | } |
99 | ||
100 | public function testRemoveClient() | |
101 | { | |
9bf15f02 | 102 | $client = $this->getClient(); |
7ab5eb95 | 103 | $adminApiClient = $this->createApiClientForUser('admin'); |
9bf15f02 | 104 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); |
9bf15f02 | 105 | |
f24ea59e NL |
106 | // Try to remove an admin's client with a wrong user |
107 | $this->logInAs('bob'); | |
108 | $client->request('GET', '/developer'); | |
109 | $this->assertContains('no_client', $client->getResponse()->getContent()); | |
110 | ||
f24ea59e | 111 | $this->logInAs('bob'); |
f808b016 JB |
112 | $client->request('GET', '/developer/client/delete/' . $adminApiClient->getId()); |
113 | $this->assertSame(403, $client->getResponse()->getStatusCode()); | |
f24ea59e NL |
114 | |
115 | // Try to remove the admin's client with the good user | |
116 | $this->logInAs('admin'); | |
9bf15f02 JB |
117 | $crawler = $client->request('GET', '/developer'); |
118 | ||
119 | $link = $crawler | |
120 | ->filter('div[class=collapsible-body] p a') | |
121 | ->eq(0) | |
122 | ->link() | |
123 | ; | |
124 | ||
125 | $client->click($link); | |
f808b016 | 126 | $this->assertSame(302, $client->getResponse()->getStatusCode()); |
9bf15f02 | 127 | |
7ab5eb95 | 128 | $this->assertNull( |
129 | $em->getRepository('WallabagApiBundle:Client')->find($adminApiClient->getId()), | |
130 | 'The client should have been removed' | |
131 | ); | |
132 | } | |
133 | ||
134 | /** | |
135 | * @param string $username | |
f808b016 | 136 | * @param array $grantTypes |
7ab5eb95 | 137 | * |
138 | * @return Client | |
139 | */ | |
caa0b176 | 140 | private function createApiClientForUser($username, $grantTypes = ['password']) |
7ab5eb95 | 141 | { |
142 | $client = $this->getClient(); | |
143 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
144 | $userManager = $client->getContainer()->get('fos_user.user_manager'); | |
f808b016 | 145 | $user = $userManager->findUserBy(['username' => $username]); |
7ab5eb95 | 146 | $apiClient = new Client($user); |
147 | $apiClient->setName('My app'); | |
caa0b176 | 148 | $apiClient->setAllowedGrantTypes($grantTypes); |
7ab5eb95 | 149 | $em->persist($apiClient); |
150 | $em->flush(); | |
151 | ||
152 | return $apiClient; | |
8a4690b6 NL |
153 | } |
154 | } |