]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\Wallabag\ApiBundle\Controller; | |
4 | ||
5 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | |
6 | use Wallabag\ApiBundle\Entity\Client; | |
7 | ||
8 | class DeveloperControllerTest extends WallabagCoreTestCase | |
9 | { | |
10 | public function testCreateClient() | |
11 | { | |
12 | $this->logInAs('admin'); | |
13 | $client = $this->getClient(); | |
14 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
15 | $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); | |
16 | ||
17 | $crawler = $client->request('GET', '/developer/client/create'); | |
18 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
19 | ||
20 | $form = $crawler->filter('button[type=submit]')->form(); | |
21 | ||
22 | $data = [ | |
23 | 'client[name]' => 'My app', | |
24 | ]; | |
25 | ||
26 | $crawler = $client->submit($form, $data); | |
27 | ||
28 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
29 | ||
30 | $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); | |
31 | $this->assertGreaterThan(count($nbClients), count($newNbClients)); | |
32 | ||
33 | $this->assertGreaterThan(1, $alert = $crawler->filter('.settings ul li strong')->extract(['_text'])); | |
34 | $this->assertContains('My app', $alert[0]); | |
35 | } | |
36 | ||
37 | public function testCreateToken() | |
38 | { | |
39 | $client = $this->getClient(); | |
40 | $apiClient = $this->createApiClientForUser('admin'); | |
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 | ||
50 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
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 | ||
59 | public function testListingClient() | |
60 | { | |
61 | $this->logInAs('admin'); | |
62 | $client = $this->getClient(); | |
63 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
64 | $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); | |
65 | ||
66 | $crawler = $client->request('GET', '/developer'); | |
67 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
68 | $this->assertEquals(count($nbClients), $crawler->filter('ul[class=collapsible] li')->count()); | |
69 | } | |
70 | ||
71 | public function testDeveloperHowto() | |
72 | { | |
73 | $this->logInAs('admin'); | |
74 | $client = $this->getClient(); | |
75 | ||
76 | $crawler = $client->request('GET', '/developer/howto/first-app'); | |
77 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
78 | } | |
79 | ||
80 | public function testRemoveClient() | |
81 | { | |
82 | $client = $this->getClient(); | |
83 | $adminApiClient = $this->createApiClientForUser('admin'); | |
84 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
85 | ||
86 | // Try to remove an admin's client with a wrong user | |
87 | $this->logInAs('bob'); | |
88 | $client->request('GET', '/developer'); | |
89 | $this->assertContains('no_client', $client->getResponse()->getContent()); | |
90 | ||
91 | $this->logInAs('bob'); | |
92 | $client->request('GET', '/developer/client/delete/'.$adminApiClient->getId()); | |
93 | $this->assertEquals(403, $client->getResponse()->getStatusCode()); | |
94 | ||
95 | // Try to remove the admin's client with the good user | |
96 | $this->logInAs('admin'); | |
97 | $crawler = $client->request('GET', '/developer'); | |
98 | ||
99 | $link = $crawler | |
100 | ->filter('div[class=collapsible-body] p a') | |
101 | ->eq(0) | |
102 | ->link() | |
103 | ; | |
104 | ||
105 | $client->click($link); | |
106 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
107 | ||
108 | $this->assertNull( | |
109 | $em->getRepository('WallabagApiBundle:Client')->find($adminApiClient->getId()), | |
110 | 'The client should have been removed' | |
111 | ); | |
112 | } | |
113 | ||
114 | /** | |
115 | * @param string $username | |
116 | * | |
117 | * @return Client | |
118 | */ | |
119 | private function createApiClientForUser($username) | |
120 | { | |
121 | $client = $this->getClient(); | |
122 | $em = $client->getContainer()->get('doctrine.orm.entity_manager'); | |
123 | $userManager = $client->getContainer()->get('fos_user.user_manager'); | |
124 | $user = $userManager->findUserBy(array('username' => $username)); | |
125 | $apiClient = new Client($user); | |
126 | $apiClient->setName('My app'); | |
127 | $apiClient->setAllowedGrantTypes(['password']); | |
128 | $em->persist($apiClient); | |
129 | $em->flush(); | |
130 | ||
131 | return $apiClient; | |
132 | } | |
133 | } |