]>
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 | 19 | |
5614df19 | 20 | $form = $crawler->filter('button[id=client_save]')->form(); |
8a4690b6 | 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(); | |
2a1ceb67 | 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 | ||
2e5b2fa8 | 37 | public function testCreateToken() |
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 | ||
3a2d4cf9 JB |
59 | public function testCreateTokenWithBadClientId() |
60 | { | |
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', | |
68 | ]); | |
69 | ||
70 | $this->assertSame(400, $client->getResponse()->getStatusCode()); | |
71 | } | |
72 | ||
9bf15f02 JB |
73 | public function testListingClient() |
74 | { | |
75 | $this->logInAs('admin'); | |
76 | $client = $this->getClient(); | |
77 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
78 | $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); | |
79 | ||
80 | $crawler = $client->request('GET', '/developer'); | |
f808b016 | 81 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
2a1ceb67 | 82 | $this->assertSame(\count($nbClients), $crawler->filter('ul[class=collapsible] li')->count()); |
9bf15f02 JB |
83 | } |
84 | ||
85 | public function testDeveloperHowto() | |
86 | { | |
87 | $this->logInAs('admin'); | |
88 | $client = $this->getClient(); | |
89 | ||
90 | $crawler = $client->request('GET', '/developer/howto/first-app'); | |
f808b016 | 91 | $this->assertSame(200, $client->getResponse()->getStatusCode()); |
9bf15f02 JB |
92 | } |
93 | ||
94 | public function testRemoveClient() | |
95 | { | |
9bf15f02 | 96 | $client = $this->getClient(); |
7ab5eb95 | 97 | $adminApiClient = $this->createApiClientForUser('admin'); |
9bf15f02 | 98 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); |
9bf15f02 | 99 | |
f24ea59e NL |
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()); | |
104 | ||
f24ea59e | 105 | $this->logInAs('bob'); |
f808b016 JB |
106 | $client->request('GET', '/developer/client/delete/' . $adminApiClient->getId()); |
107 | $this->assertSame(403, $client->getResponse()->getStatusCode()); | |
f24ea59e NL |
108 | |
109 | // Try to remove the admin's client with the good user | |
110 | $this->logInAs('admin'); | |
9bf15f02 JB |
111 | $crawler = $client->request('GET', '/developer'); |
112 | ||
113 | $link = $crawler | |
114 | ->filter('div[class=collapsible-body] p a') | |
115 | ->eq(0) | |
116 | ->link() | |
117 | ; | |
118 | ||
119 | $client->click($link); | |
f808b016 | 120 | $this->assertSame(302, $client->getResponse()->getStatusCode()); |
9bf15f02 | 121 | |
7ab5eb95 | 122 | $this->assertNull( |
123 | $em->getRepository('WallabagApiBundle:Client')->find($adminApiClient->getId()), | |
124 | 'The client should have been removed' | |
125 | ); | |
126 | } | |
127 | ||
128 | /** | |
129 | * @param string $username | |
f808b016 | 130 | * @param array $grantTypes |
7ab5eb95 | 131 | * |
132 | * @return Client | |
133 | */ | |
caa0b176 | 134 | private function createApiClientForUser($username, $grantTypes = ['password']) |
7ab5eb95 | 135 | { |
136 | $client = $this->getClient(); | |
137 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
115de64e | 138 | $userManager = $client->getContainer()->get('fos_user.user_manager.test'); |
f808b016 | 139 | $user = $userManager->findUserBy(['username' => $username]); |
7ab5eb95 | 140 | $apiClient = new Client($user); |
141 | $apiClient->setName('My app'); | |
caa0b176 | 142 | $apiClient->setAllowedGrantTypes($grantTypes); |
7ab5eb95 | 143 | $em->persist($apiClient); |
144 | $em->flush(); | |
145 | ||
146 | return $apiClient; | |
8a4690b6 NL |
147 | } |
148 | } |