]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php
test line, forgot to remove it
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Tests / Controller / WallabagRestControllerTest.php
CommitLineData
68c6f1bd
NL
1<?php
2
3namespace Wallabag\CoreBundle\Tests\Controller;
4
5use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
7class 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(
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());
38
39 $client->request('GET', '/api/salts/notfound.json');
40 $this->assertEquals(404, $client->getResponse()->getStatusCode());
41 }
42
19aee7cd 43 public function testGetOneEntry()
f5deb024 44 {
68c6f1bd 45 $client = $this->createClient();
e1dd7f70 46 $client->request('GET', '/api/salts/admin.json');
c9fa9677 47 $salt = json_decode($client->getResponse()->getContent());
e1dd7f70 48
c9fa9677 49 $headers = $this->generateHeaders('admin', 'test', $salt[0]);
e1dd7f70 50
c9fa9677
NL
51 $entry = $client->getContainer()
52 ->get('doctrine.orm.entity_manager')
53 ->getRepository('WallabagCoreBundle:Entry')
54 ->findOneByIsArchived(false);
55
56 if (!$entry) {
57 $this->markTestSkipped('No content found in db.');
58 }
59
60 $client->request('GET', '/api/entries/'.$entry->getId().'.json', array(), array(), $headers);
61 $this->assertContains($entry->getTitle(), $client->getResponse()->getContent());
e1dd7f70 62
19aee7cd
NL
63 $this->assertTrue(
64 $client->getResponse()->headers->contains(
65 'Content-Type',
66 'application/json'
67 )
e1dd7f70 68 );
19aee7cd
NL
69 }
70
71 public function testGetEntries()
72 {
73 $client = $this->createClient();
74 $client->request('GET', '/api/salts/admin.json');
c9fa9677 75 $salt = json_decode($client->getResponse()->getContent());
19aee7cd 76
c9fa9677 77 $headers = $this->generateHeaders('admin', 'test', $salt[0]);
e1dd7f70
NL
78
79 $client->request('GET', '/api/entries', array(), array(), $headers);
80 $this->assertContains('Mailjet', $client->getResponse()->getContent());
68c6f1bd
NL
81
82 $this->assertTrue(
83 $client->getResponse()->headers->contains(
84 'Content-Type',
85 'application/json'
86 )
87 );
68c6f1bd 88 }
c9fa9677
NL
89
90 public function testDeleteEntry()
91 {
92 $client = $this->createClient();
93 $client->request('GET', '/api/salts/admin.json');
94 $salt = json_decode($client->getResponse()->getContent());
95
96 $headers = $this->generateHeaders('admin', 'test', $salt[0]);
97
98 $entry = $client->getContainer()
99 ->get('doctrine.orm.entity_manager')
100 ->getRepository('WallabagCoreBundle:Entry')
101 ->findOneByIsDeleted(false);
102
103 if (!$entry) {
104 $this->markTestSkipped('No content found in db.');
105 }
106
107 $client->request('DELETE', '/api/entries/'.$entry->getId().'.json', array(), array(), $headers);
108
109 $this->assertEquals(200, $client->getResponse()->getStatusCode());
110
111 $res = $client->getContainer()
112 ->get('doctrine.orm.entity_manager')
113 ->getRepository('WallabagCoreBundle:Entry')
114 ->findOneById($entry->getId());
115 $this->assertEquals($res->isDeleted(), true);
116 }
2734044a 117}