]>
Commit | Line | Data |
---|---|---|
68c6f1bd NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Tests\Controller; | |
4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | |
6 | ||
7 | class WallabagRestControllerTest extends WebTestCase | |
8 | { | |
19aee7cd NL |
9 | /** |
10 | * Generate HTTP headers for authenticate user on API | |
11 | * | |
12 | * @param $username | |
13 | * @param $password | |
14 | * @param $salt | |
15 | * | |
16 | * @return array | |
17 | */ | |
18 | private function generateHeaders($username, $password, $salt) | |
19 | { | |
20 | $encryptedPassword = sha1($password.$username.$salt); | |
21 | $nonce = substr(md5(uniqid('nonce_', true)), 0, 16); | |
22 | ||
23 | $now = new \DateTime('now', new \DateTimeZone('UTC')); | |
24 | $created = (string) $now->format('Y-m-d\TH:i:s\Z'); | |
25 | $digest = base64_encode(sha1(base64_decode($nonce).$created.$encryptedPassword, true)); | |
26 | ||
27 | return array( | |
28 | 'PHP_AUTH_USER' => 'username', | |
29 | 'HTTP_AUTHORIZATION' => 'Authorization profile="UsernameToken"', | |
30 | 'HTTP_x-wsse' => 'X-WSSE: UsernameToken Username="'.$username.'", PasswordDigest="'.$digest.'", Nonce="'.$nonce.'", Created="'.$created.'"', | |
31 | ); | |
32 | } | |
33 | ||
f5deb024 NL |
34 | public function testGetSalt() |
35 | { | |
36 | $client = $this->createClient(); | |
37 | $client->request('GET', '/api/salts/admin.json'); | |
38 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
39 | ||
40 | $client->request('GET', '/api/salts/notfound.json'); | |
41 | $this->assertEquals(404, $client->getResponse()->getStatusCode()); | |
42 | } | |
43 | ||
19aee7cd | 44 | public function testGetOneEntry() |
f5deb024 | 45 | { |
68c6f1bd | 46 | $client = $this->createClient(); |
e1dd7f70 | 47 | $client->request('GET', '/api/salts/admin.json'); |
c9fa9677 | 48 | $salt = json_decode($client->getResponse()->getContent()); |
e1dd7f70 | 49 | |
c9fa9677 | 50 | $headers = $this->generateHeaders('admin', 'test', $salt[0]); |
e1dd7f70 | 51 | |
c9fa9677 NL |
52 | $entry = $client->getContainer() |
53 | ->get('doctrine.orm.entity_manager') | |
54 | ->getRepository('WallabagCoreBundle:Entry') | |
55 | ->findOneByIsArchived(false); | |
56 | ||
57 | if (!$entry) { | |
58 | $this->markTestSkipped('No content found in db.'); | |
59 | } | |
60 | ||
61 | $client->request('GET', '/api/entries/'.$entry->getId().'.json', array(), array(), $headers); | |
62 | $this->assertContains($entry->getTitle(), $client->getResponse()->getContent()); | |
e1dd7f70 | 63 | |
19aee7cd NL |
64 | $this->assertTrue( |
65 | $client->getResponse()->headers->contains( | |
66 | 'Content-Type', | |
67 | 'application/json' | |
68 | ) | |
e1dd7f70 | 69 | ); |
19aee7cd NL |
70 | } |
71 | ||
72 | public function testGetEntries() | |
73 | { | |
74 | $client = $this->createClient(); | |
75 | $client->request('GET', '/api/salts/admin.json'); | |
c9fa9677 | 76 | $salt = json_decode($client->getResponse()->getContent()); |
19aee7cd | 77 | |
c9fa9677 | 78 | $headers = $this->generateHeaders('admin', 'test', $salt[0]); |
e1dd7f70 NL |
79 | |
80 | $client->request('GET', '/api/entries', array(), array(), $headers); | |
81 | $this->assertContains('Mailjet', $client->getResponse()->getContent()); | |
68c6f1bd NL |
82 | |
83 | $this->assertTrue( | |
84 | $client->getResponse()->headers->contains( | |
85 | 'Content-Type', | |
86 | 'application/json' | |
87 | ) | |
88 | ); | |
68c6f1bd | 89 | } |
c9fa9677 NL |
90 | |
91 | public function testDeleteEntry() | |
92 | { | |
93 | $client = $this->createClient(); | |
94 | $client->request('GET', '/api/salts/admin.json'); | |
95 | $salt = json_decode($client->getResponse()->getContent()); | |
96 | ||
97 | $headers = $this->generateHeaders('admin', 'test', $salt[0]); | |
98 | ||
99 | $entry = $client->getContainer() | |
100 | ->get('doctrine.orm.entity_manager') | |
101 | ->getRepository('WallabagCoreBundle:Entry') | |
102 | ->findOneByIsDeleted(false); | |
103 | ||
104 | if (!$entry) { | |
105 | $this->markTestSkipped('No content found in db.'); | |
106 | } | |
107 | ||
108 | $client->request('DELETE', '/api/entries/'.$entry->getId().'.json', array(), array(), $headers); | |
109 | ||
110 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
111 | ||
112 | $res = $client->getContainer() | |
113 | ->get('doctrine.orm.entity_manager') | |
114 | ->getRepository('WallabagCoreBundle:Entry') | |
115 | ->findOneById($entry->getId()); | |
116 | $this->assertEquals($res->isDeleted(), true); | |
117 | } | |
2734044a | 118 | } |