Debian Codegraph @

tests/classes/AuthTest.php

raw symbols SCIP
1 <?php
2
3 namespace Liuch\DmarcSrg;
4
5 use Liuch\DmarcSrg\Users\User;
6 use Liuch\DmarcSrg\Users\AdminUser;
7 use Liuch\DmarcSrg\Exception\AuthException;
8 use Liuch\DmarcSrg\Database\DatabaseController;
9 use Liuch\DmarcSrg\Database\UserMapperInterface;
10
11 class AuthTest extends \PHPUnit\Framework\TestCase
12 {
13 public function testIsEnabledWithNoPassword(): void
14 {
15 $core = $this->coreWithConfigValue('admin/password', null);
16 $this->assertFalse((new Auth($core))->isEnabled());
17 }
18
19 public function testIsEnabledWithPassword(): void
20 {
21 $core = $this->coreWithConfigValue('admin/password', 'some');
22 $this->assertTrue((new Auth($core))->isEnabled());
23 }
24
25 public function testCorrectLogin(): void
26 {
27 $core = $this->getMockBuilder(Core::class)
28 ->disableOriginalConstructor()
29 ->onlyMethods([ 'config', 'setCurrentUser' ])
30 ->getMock();
31 $core->expects($this->exactly(3))
32 ->method('config')
33 ->with($this->logicalOr(
34 $this->equalTo('admin/password'),
35 $this->equalTo('users/user_management')
36 ))
37 ->willReturnCallback(function ($param) {
38 return $param === 'admin/password' ? 'some' : false;
39 });
40 $core->expects($this->once())
41 ->method('setCurrentUser')
42 ->with($this->callback(function ($param) {
43 return $param instanceof User;
44 }));
45 $result = (new Auth($core))->login('', 'some');
46 $this->assertIsArray($result);
47 $this->assertSame(0, $result['error_code']);
48 $this->assertSame('Authentication succeeded', $result['message']);
49 }
50
51 public function testLoginWithIncorrectUsername(): void
52 {
53 $mp = $this->getMockBuilder(UserMapperInterface::class)
54 ->disableOriginalConstructor()
55 ->getMock();
56 $mp->expects($this->once())
57 ->method('exists')
58 ->willReturn(false);
59
60 $db = $this->getMockBuilder(DatabaseController::class)
61 ->disableOriginalConstructor()
62 ->onlyMethods([ 'getMapper' ])
63 ->getMock();
64 $db->expects($this->once())
65 ->method('getMapper')
66 ->with('user')
67 ->willReturn($mp);
68
69 $core = $this->getMockBuilder(Core::class)
70 ->disableOriginalConstructor()
71 ->onlyMethods([ 'database', 'config' ])
72 ->getMock();
73 $core->expects($this->once())
74 ->method('database')
75 ->willReturn($db);
76 $core->expects($this->exactly(2))
77 ->method('config')
78 ->with($this->logicalOr(
79 $this->equalTo('admin/password'),
80 $this->equalTo('users/user_management')
81 ))
82 ->willReturnCallback(function ($param) {
83 return $param === 'admin/password' ? 'some' : true;
84 });
85
86 $this->expectException(AuthException::class);
87 (new Auth($core))->login('fake_user', 'password');
88 }
89
90 public function testLogout(): void
91 {
92 $core = $this->getMockBuilder(Core::class)
93 ->onlyMethods([ 'setCurrentUser' ])
94 ->disableOriginalConstructor()
95 ->getMock();
96 $core->expects($this->once())->method('setCurrentUser')->with(null);
97
98 $result = (new Auth($core))->logout();
99 $this->assertArrayHasKey('error_code', $result);
100 $this->assertArrayHasKey('message', $result);
101 $this->assertEquals(0, $result['error_code']);
102 $this->assertEquals('Logged out successfully', $result['message']);
103 }
104
105 public function testIsAllowedWhenAuthDisabled(): void
106 {
107 $core = $this->getMockBuilder(Core::class)
108 ->disableOriginalConstructor()
109 ->onlyMethods([ 'config', 'getCurrentUser' ])
110 ->getMock();
111 $core->expects($this->once())
112 ->method('config')
113 ->with($this->equalTo('admin/password'))
114 ->willReturn(null);
115 $core->expects($this->never())
116 ->method('getCurrentUser');
117 $this->assertNull((new Auth($core))->isAllowed(User::LEVEL_ADMIN));
118 }
119
120 public function testIsAllowedWithActiveSession(): void
121 {
122 $core = $this->getMockBuilder(Core::class)
123 ->disableOriginalConstructor()
124 ->onlyMethods([ 'config', 'getCurrentUser' ])
125 ->getMock();
126 $core->expects($this->once())
127 ->method('config')
128 ->with($this->equalTo('admin/password'))
129 ->willReturn('some');
130 $core->expects($this->once())
131 ->method('getCurrentUser')
132 ->willReturn(new AdminUser($core));
133 $this->assertNull((new Auth($core))->isAllowed(User::LEVEL_ADMIN));
134 }
135
136 public function testIsAllowedWithoutSession(): void
137 {
138 $core = $this->getMockBuilder(Core::class)
139 ->disableOriginalConstructor()
140 ->onlyMethods([ 'config', 'getCurrentUser' ])
141 ->getMock();
142 $core->expects($this->exactly(3))
143 ->method('config')
144 ->with($this->logicalOr(
145 $this->equalTo('admin/password'),
146 $this->equalTo('users/user_management')
147 ))
148 ->willReturnCallback(function ($param) {
149 return $param === 'admin/password' ? 'some' : false;
150 });
151 $core->expects($this->once())
152 ->method('getCurrentUser')
153 ->willReturn(null);
154 $this->expectException(AuthException::class);
155 $this->expectExceptionCode(-2);
156 $this->expectExceptionMessage('Authentication needed');
157 (new Auth($core))->isAllowed(User::LEVEL_ADMIN);
158 }
159
160 private function coreWithConfigValue(string $key, $value)
161 {
162 $core = $this->getMockBuilder(Core::class)->disableOriginalConstructor()->onlyMethods([ 'config' ])->getMock();
163 $core->expects($this->once())->method('config')->with($this->equalTo($key))->willReturn($value);
164 return $core;
165 }
166 }