]>
Commit | Line | Data |
---|---|---|
68c6f1bd NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Tests\Controller; | |
4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | |
6 | ||
7 | class 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( | |
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 | ||
f5deb024 NL |
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 | ||
19aee7cd | 44 | public function testGetOneEntry() |
f5deb024 | 45 | { |
68c6f1bd | 46 | $client = $this->createClient(); |
e1dd7f70 NL |
47 | $client->request('GET', '/api/salts/admin.json'); |
48 | $content = json_decode($client->getResponse()->getContent()); | |
e1dd7f70 | 49 | |
19aee7cd | 50 | $headers = $this->generateHeaders('admin', 'test', $content[0]); |
e1dd7f70 | 51 | |
19aee7cd NL |
52 | $client->request('GET', '/api/entries/1.json', array(), array(), $headers); |
53 | $this->assertContains('This is my content', $client->getResponse()->getContent()); | |
e1dd7f70 | 54 | |
19aee7cd NL |
55 | $this->assertTrue( |
56 | $client->getResponse()->headers->contains( | |
57 | 'Content-Type', | |
58 | 'application/json' | |
59 | ) | |
e1dd7f70 | 60 | ); |
19aee7cd NL |
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]); | |
e1dd7f70 NL |
70 | |
71 | $client->request('GET', '/api/entries', array(), array(), $headers); | |
72 | $this->assertContains('Mailjet', $client->getResponse()->getContent()); | |
68c6f1bd NL |
73 | |
74 | $this->assertTrue( | |
75 | $client->getResponse()->headers->contains( | |
76 | 'Content-Type', | |
77 | 'application/json' | |
78 | ) | |
79 | ); | |
68c6f1bd | 80 | } |
2734044a | 81 | } |