3 namespace Wallabag\CoreBundle\Tests\Controller
;
5 use Symfony\Bundle\FrameworkBundle\Test\WebTestCase
;
7 class WallabagRestControllerTest
extends WebTestCase
10 * Generate HTTP headers for authenticate user on API
18 private function generateHeaders($username, $password, $salt)
20 $encryptedPassword = sha1($password.$username.$salt);
21 $nonce = substr(md5(uniqid('nonce_', true)), 0, 16);
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));
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.'"',
34 public function testGetSalt()
36 $client = $this->createClient();
37 $client->request('GET', '/api/salts/admin.json');
38 $this->assertEquals(200, $client->getResponse()->getStatusCode());
40 $client->request('GET', '/api/salts/notfound.json');
41 $this->assertEquals(404, $client->getResponse()->getStatusCode());
44 public function testGetOneEntry()
46 $client = $this->createClient();
47 $client->request('GET', '/api/salts/admin.json');
48 $salt = json_decode($client->getResponse()->getContent());
50 $headers = $this->generateHeaders('admin', 'test', $salt[0]);
52 $entry = $client->getContainer()
53 ->get('doctrine.orm.entity_manager')
54 ->getRepository('WallabagCoreBundle:Entry')
55 ->findOneByIsArchived(false);
58 $this->markTestSkipped('No content found in db.');
61 $client->request('GET', '/api/entries/'.$entry->getId().'.json', array(), array(), $headers);
62 $this->assertContains($entry->getTitle(), $client->getResponse()->getContent());
65 $client->getResponse()->headers
->contains(
72 public function testGetEntries()
74 $client = $this->createClient();
75 $client->request('GET', '/api/salts/admin.json');
76 $salt = json_decode($client->getResponse()->getContent());
78 $headers = $this->generateHeaders('admin', 'test', $salt[0]);
80 $client->request('GET', '/api/entries', array(), array(), $headers);
81 $this->assertContains('Mailjet', $client->getResponse()->getContent());
84 $client->getResponse()->headers
->contains(
91 public function testDeleteEntry()
93 $client = $this->createClient();
94 $client->request('GET', '/api/salts/admin.json');
95 $salt = json_decode($client->getResponse()->getContent());
97 $headers = $this->generateHeaders('admin', 'test', $salt[0]);
99 $entry = $client->getContainer()
100 ->get('doctrine.orm.entity_manager')
101 ->getRepository('WallabagCoreBundle:Entry')
102 ->findOneByIsDeleted(false);
105 $this->markTestSkipped('No content found in db.');
108 $client->request('DELETE', '/api/entries/'.$entry->getId().'.json', array(), array(), $headers);
110 $this->assertEquals(200, $client->getResponse()->getStatusCode());
112 $res = $client->getContainer()
113 ->get('doctrine.orm.entity_manager')
114 ->getRepository('WallabagCoreBundle:Entry')
115 ->findOneById($entry->getId());
116 $this->assertEquals($res->isDeleted(), true);