]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php
refactor and test one entry
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Tests / Controller / WallabagRestControllerTest.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Tests\Controller;
4
5 use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
7 class WallabagRestControllerTest extends WebTestCase
8 {
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
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
44 public function testGetOneEntry()
45 {
46 $client = $this->createClient();
47 $client->request('GET', '/api/salts/admin.json');
48 $content = json_decode($client->getResponse()->getContent());
49
50 $headers = $this->generateHeaders('admin', 'test', $content[0]);
51
52 $client->request('GET', '/api/entries/1.json', array(), array(), $headers);
53 $this->assertContains('This is my content', $client->getResponse()->getContent());
54
55 $this->assertTrue(
56 $client->getResponse()->headers->contains(
57 'Content-Type',
58 'application/json'
59 )
60 );
61 }
62
63 public function testGetEntries()
64 {
65 $client = $this->createClient();
66 $client->request('GET', '/api/salts/admin.json');
67 $content = json_decode($client->getResponse()->getContent());
68
69 $headers = $this->generateHeaders('admin', 'test', $content[0]);
70
71 $client->request('GET', '/api/entries', array(), array(), $headers);
72 $this->assertContains('Mailjet', $client->getResponse()->getContent());
73
74 $this->assertTrue(
75 $client->getResponse()->headers->contains(
76 'Content-Type',
77 'application/json'
78 )
79 );
80 }
81 }