]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php
add test for empty salt
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Tests / Controller / WallabagRestControllerTest.php
CommitLineData
68c6f1bd
NL
1<?php
2
3namespace Wallabag\CoreBundle\Tests\Controller;
4
874e3e10 5use Wallabag\CoreBundle\Tests\WallabagTestCase;
68c6f1bd 6
874e3e10 7class WallabagRestControllerTest extends WallabagTestCase
68c6f1bd 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(
19aee7cd
NL
28 'HTTP_AUTHORIZATION' => 'Authorization profile="UsernameToken"',
29 'HTTP_x-wsse' => 'X-WSSE: UsernameToken Username="'.$username.'", PasswordDigest="'.$digest.'", Nonce="'.$nonce.'", Created="'.$created.'"',
30 );
31 }
32
f5deb024
NL
33 public function testGetSalt()
34 {
35 $client = $this->createClient();
36 $client->request('GET', '/api/salts/admin.json');
37 $this->assertEquals(200, $client->getResponse()->getStatusCode());
f170f315 38 $this->assertNotEmpty(json_decode($client->getResponse()->getContent()));
f5deb024
NL
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}