]> git.mxchange.org Git - friendica.git/blob - tests/src/Model/User/CookieTest.php
Move Cookie to own class (with tests)
[friendica.git] / tests / src / Model / User / CookieTest.php
1 <?php
2
3 namespace Friendica\Testsrc\Model\User;
4
5 use Friendica\Core\Config\Configuration;
6 use Friendica\Model\User\Cookie;
7 use Friendica\Test\DatabaseTest;
8 use Mockery\MockInterface;
9
10 class CookieTest extends DatabaseTest
11 {
12         /** @var MockInterface|Configuration */
13         private $config;
14
15         protected function setUp()
16         {
17                 parent::setUp();;
18
19                 $this->config = \Mockery::mock(Configuration::class);
20         }
21
22         public function testInstance()
23         {
24                 $this->config->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(1)->once();
25                 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn('1235')->once();
26                 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn('7')->once();
27
28                 $cookie = new Cookie($this->config, []);
29                 $this->assertInstanceOf(Cookie::class, $cookie);
30         }
31
32         public function dataGet()
33         {
34                 return [
35                         'default'    => [
36                                 'cookieData' => [
37                                         Cookie::NAME => json_encode([
38                                                 'uid'  => -1,
39                                                 'hash' => 12345,
40                                                 'ip'   => '127.0.0.1',
41                                         ])
42                                 ],
43                                 'hasValues'  => true,
44                                 'uid'        => -1,
45                                 'hash'       => 12345,
46                                 'ip'         => '127.0.0.1',
47                         ],
48                         'missing'    => [
49                                 'cookieData' => [
50
51                                 ],
52                                 'hasValues'  => false,
53                                 'uid'        => null,
54                                 'hash'       => null,
55                                 'ip'         => null,
56                         ],
57                         'invalid'    => [
58                                 'cookieData' => [
59                                         Cookie::NAME => 'test',
60                                 ],
61                                 'hasValues'  => false,
62                                 'uid'        => null,
63                                 'hash'       => null,
64                                 'ip'         => null,
65                         ],
66                         'incomplete' => [
67                                 'cookieData' => [
68                                         Cookie::NAME => json_encode([
69                                                 'uid'  => -1,
70                                                 'hash' => 12345,
71                                         ])
72                                 ],
73                                 'hasValues'  => true,
74                                 'uid'        => -1,
75                                 'hash'       => 12345,
76                                 'ip'         => null,
77                         ],
78                 ];
79         }
80
81         /**
82          * @dataProvider dataGet
83          */
84         public function testGet(array $cookieData, bool $hasValues, $uid, $hash, $ip)
85         {
86                 $this->config->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(1)->once();
87                 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn('1235')->once();
88                 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn('7')->once();
89
90                 $cookie = new Cookie($this->config, [], $cookieData);
91                 $this->assertInstanceOf(Cookie::class, $cookie);
92
93                 $assertData = $cookie->getData();
94
95                 if (!$hasValues) {
96                         $this->assertEmpty($assertData);
97                 } else {
98                         $this->assertNotEmpty($assertData);
99                         if (isset($uid)) {
100                                 $this->assertObjectHasAttribute('uid', $assertData);
101                                 $this->assertEquals($uid, $assertData->uid);
102                         } else {
103                                 $this->assertObjectNotHasAttribute('uid', $assertData);
104                         }
105                         if (isset($hash)) {
106                                 $this->assertObjectHasAttribute('hash', $assertData);
107                                 $this->assertEquals($hash, $assertData->hash);
108                         } else {
109                                 $this->assertObjectNotHasAttribute('hash', $assertData);
110                         }
111                         if (isset($ip)) {
112                                 $this->assertObjectHasAttribute('ip', $assertData);
113                                 $this->assertEquals($ip, $assertData->ip);
114                         } else {
115                                 $this->assertObjectNotHasAttribute('ip', $assertData);
116                         }
117                 }
118         }
119
120         public function dataCheck()
121         {
122                 return [
123                         'default'   => [
124                                 'serverPrivateKey' => 'serverkey',
125                                 'userPrivateKey'   => 'userkey',
126                                 'password'         => 'test',
127                                 'assertHash'       => 'e9b4eb16275a2907b5659d22905b248221d0517dde4a9d5c320b8fe051b1267b',
128                                 'assertTrue'       => true,
129                         ],
130                         'emptyUser' => [
131                                 'serverPrivateKey' => 'serverkey',
132                                 'userPrivateKey'   => '',
133                                 'password'         => '',
134                                 'assertHash'       => '',
135                                 'assertTrue'       => false,
136                         ],
137                         'invalid' => [
138                                 'serverPrivateKey' => 'serverkey',
139                                 'userPrivateKey'   => 'bla',
140                                 'password'         => 'nope',
141                                 'assertHash'       => 'real wrong!',
142                                 'assertTrue'       => false,
143                         ]
144                 ];
145         }
146
147         /**
148          * @dataProvider dataCheck
149          */
150         public function testCheck(string $serverPrivateKey, string $userPrivateKey, string $password, string $assertHash, bool $assertTrue)
151         {
152                 $this->config->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(1)->once();
153                 $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn($serverPrivateKey)->once();
154                 $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn('7')->once();
155
156                 $cookie = new Cookie($this->config, []);
157                 $this->assertInstanceOf(Cookie::class, $cookie);
158
159                 $this->assertEquals($assertTrue, $cookie->check($assertHash, $password, $userPrivateKey));
160         }
161
162         public function testSet()
163         {
164                 $this->markTestIncomplete('Needs mocking of setcookie() first.');
165         }
166
167         public function testClear()
168         {
169                 $this->markTestIncomplete('Needs mocking of setcookie() first.');
170         }
171 }