3 namespace Friendica\Testsrc\Model\User;
5 use Friendica\Core\Config\Configuration;
6 use Friendica\Model\User\Cookie;
7 use Friendica\Test\DatabaseTest;
8 use Mockery\MockInterface;
10 class CookieTest extends DatabaseTest
12 /** @var MockInterface|Configuration */
15 protected function setUp()
19 $this->config = \Mockery::mock(Configuration::class);
22 public function testInstance()
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();
28 $cookie = new Cookie($this->config, []);
29 $this->assertInstanceOf(Cookie::class, $cookie);
32 public function dataGet()
37 Cookie::NAME => json_encode([
59 Cookie::NAME => 'test',
68 Cookie::NAME => json_encode([
82 * @dataProvider dataGet
84 public function testGet(array $cookieData, bool $hasValues, $uid, $hash, $ip)
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();
90 $cookie = new Cookie($this->config, [], $cookieData);
91 $this->assertInstanceOf(Cookie::class, $cookie);
93 $assertData = $cookie->getData();
96 $this->assertEmpty($assertData);
98 $this->assertNotEmpty($assertData);
100 $this->assertObjectHasAttribute('uid', $assertData);
101 $this->assertEquals($uid, $assertData->uid);
103 $this->assertObjectNotHasAttribute('uid', $assertData);
106 $this->assertObjectHasAttribute('hash', $assertData);
107 $this->assertEquals($hash, $assertData->hash);
109 $this->assertObjectNotHasAttribute('hash', $assertData);
112 $this->assertObjectHasAttribute('ip', $assertData);
113 $this->assertEquals($ip, $assertData->ip);
115 $this->assertObjectNotHasAttribute('ip', $assertData);
120 public function dataCheck()
124 'serverPrivateKey' => 'serverkey',
125 'userPrivateKey' => 'userkey',
126 'password' => 'test',
127 'assertHash' => 'e9b4eb16275a2907b5659d22905b248221d0517dde4a9d5c320b8fe051b1267b',
128 'assertTrue' => true,
131 'serverPrivateKey' => 'serverkey',
132 'userPrivateKey' => '',
135 'assertTrue' => false,
138 'serverPrivateKey' => 'serverkey',
139 'userPrivateKey' => 'bla',
140 'password' => 'nope',
141 'assertHash' => 'real wrong!',
142 'assertTrue' => false,
148 * @dataProvider dataCheck
150 public function testCheck(string $serverPrivateKey, string $userPrivateKey, string $password, string $assertHash, bool $assertTrue)
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();
156 $cookie = new Cookie($this->config, []);
157 $this->assertInstanceOf(Cookie::class, $cookie);
159 $this->assertEquals($assertTrue, $cookie->check($assertHash, $password, $userPrivateKey));
162 public function testSet()
164 $this->markTestIncomplete('Needs mocking of setcookie() first.');
167 public function testClear()
169 $this->markTestIncomplete('Needs mocking of setcookie() first.');