3 namespace Wallabag\CoreBundle\Tests\Controller
;
5 use Wallabag\CoreBundle\Tests\WallabagTestCase
;
7 class WallabagRestControllerTest
extends WallabagTestCase
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 'HTTP_AUTHORIZATION' => 'Authorization profile="UsernameToken"',
29 'HTTP_x-wsse' => 'X-WSSE: UsernameToken Username="'.$username.'", PasswordDigest="'.$digest.'", Nonce="'.$nonce.'", Created="'.$created.'"',
33 public function testGetSalt()
35 $client = $this->createClient();
36 $client->request('GET', '/api/salts/admin.json');
37 $this->assertEquals(200, $client->getResponse()->getStatusCode());
38 $this->assertNotEmpty(json_decode($client->getResponse()->getContent()));
40 $client->request('GET', '/api/salts/notfound.json');
41 $this->assertEquals(404, $client->getResponse()->getStatusCode());
44 public function testWithBadHeaders()
46 $client = $this->createClient();
48 $entry = $client->getContainer()
49 ->get('doctrine.orm.entity_manager')
50 ->getRepository('WallabagCoreBundle:Entry')
51 ->findOneByIsArchived(false);
54 $this->markTestSkipped('No content found in db.');
58 'HTTP_AUTHORIZATION' => 'Authorization profile="UsernameToken"',
59 'HTTP_x-wsse' => 'X-WSSE: UsernameToken Username="admin", PasswordDigest="Wr0ngDig3st", Nonce="n0Nc3", Created="2015-01-01T13:37:00Z"',
62 $client->request('GET', '/api/entries/'.$entry->getId().'.json', array(), array(), $badHeaders);
63 $this->assertEquals(403, $client->getResponse()->getStatusCode());
66 public function testGetOneEntry()
68 $client = $this->createClient();
69 $client->request('GET', '/api/salts/admin.json');
70 $salt = json_decode($client->getResponse()->getContent());
72 $headers = $this->generateHeaders('admin', 'mypassword', $salt[0]);
74 $entry = $client->getContainer()
75 ->get('doctrine.orm.entity_manager')
76 ->getRepository('WallabagCoreBundle:Entry')
77 ->findOneByIsArchived(false);
80 $this->markTestSkipped('No content found in db.');
83 $client->request('GET', '/api/entries/'.$entry->getId().'.json', array(), array(), $headers);
84 $this->assertContains($entry->getTitle(), $client->getResponse()->getContent());
87 $client->getResponse()->headers
->contains(
94 public function testGetEntries()
96 $client = $this->createClient();
97 $client->request('GET', '/api/salts/admin.json');
98 $salt = json_decode($client->getResponse()->getContent());
100 $headers = $this->generateHeaders('admin', 'mypassword', $salt[0]);
102 $client->request('GET', '/api/entries', array(), array(), $headers);
104 $this->assertEquals(200, $client->getResponse()->getStatusCode());
106 $this->assertGreaterThanOrEqual(1, count(json_decode($client->getResponse()->getContent())));
108 $this->assertContains('Mailjet', $client->getResponse()->getContent());
111 $client->getResponse()->headers
->contains(
118 public function testDeleteEntry()
120 $client = $this->createClient();
121 $client->request('GET', '/api/salts/admin.json');
122 $salt = json_decode($client->getResponse()->getContent());
124 $headers = $this->generateHeaders('admin', 'mypassword', $salt[0]);
126 $entry = $client->getContainer()
127 ->get('doctrine.orm.entity_manager')
128 ->getRepository('WallabagCoreBundle:Entry')
132 $this->markTestSkipped('No content found in db.');
135 $client->request('DELETE', '/api/entries/'.$entry->getId().'.json', array(), array(), $headers);
137 $this->assertEquals(200, $client->getResponse()->getStatusCode());
139 // We'll try to delete this entry again
140 $client->request('GET', '/api/salts/admin.json');
141 $salt = json_decode($client->getResponse()->getContent());
143 $headers = $this->generateHeaders('admin', 'mypassword', $salt[0]);
145 $client->request('DELETE', '/api/entries/'.$entry->getId().'.json', array(), array(), $headers);
147 $this->assertEquals(404, $client->getResponse()->getStatusCode());
150 public function testGetTagsEntry()
152 $client = $this->createClient();
153 $client->request('GET', '/api/salts/admin.json');
154 $salt = json_decode($client->getResponse()->getContent());
155 $headers = $this->generateHeaders('admin', 'mypassword', $salt[0]);
157 $entry = $client->getContainer()
158 ->get('doctrine.orm.entity_manager')
159 ->getRepository('WallabagCoreBundle:Entry')
160 ->findOneWithTags(1);
165 $this->markTestSkipped('No content found in db.');
169 foreach ($entry->getTags() as $tag) {
170 $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel());
173 $client->request('GET', '/api/entries/'.$entry->getId().'/tags', array(), array(), $headers);
175 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT
), $client->getResponse()->getContent());
178 public function testPostTagsOnEntry()
180 $client = $this->createClient();
181 $client->request('GET', '/api/salts/admin.json');
182 $salt = json_decode($client->getResponse()->getContent());
183 $headers = $this->generateHeaders('admin', 'mypassword', $salt[0]);
185 $entry = $client->getContainer()
186 ->get('doctrine.orm.entity_manager')
187 ->getRepository('WallabagCoreBundle:Entry')
191 $this->markTestSkipped('No content found in db.');
194 $newTags = 'tag1,tag2,tag3';
196 $client->request('POST', '/api/entries/'.$entry->getId().'/tags', array('tags' => $newTags), array(), $headers);
198 $this->assertEquals(200, $client->getResponse()->getStatusCode());
200 $entryDB = $client->getContainer()
201 ->get('doctrine.orm.entity_manager')
202 ->getRepository('WallabagCoreBundle:Entry')
203 ->find($entry->getId());
206 foreach ($entryDB->getTags()->toArray() as $tag) {
207 $tagsInDB[$tag->getId()] = $tag->getLabel();
210 foreach (explode(',', $newTags) as $tag) {
211 $this->assertContains($tag, $tagsInDB);