Odyssey
UserLoginTest.php
1 <?php
2 
3 use PHPUnit\Framework\MockObject\MockObject;
4 use PHPUnit\Framework\TestCase;
5 require_once('UserLogin.php');
6 require_once('CuAdmin.php');
7 require_once('User.php');
8 
9 require_once('cu_credentials.i');
10 require_once('hcuEnv.i');
11 
12 /**
13  * Class UserLoginTest.
14  *
15  * @covers \UserLogin
16  */
17 class UserLoginTest extends TestCase {
18  /**
19  * @var UserLogin $userLogin An instance of "UserLogin" to test.
20  */
21 
22  private $creditUnion = 'CRUISECU';
23  private $userName = 'Methuselah';
24  private $password = 'password';
25 
26  private $userData;
27  private $userLoad;
28  private $cuAdminData;
29  private $cuAdminLoad;
30  private $cuAdminRepo;
31  private $userRepo;
32 
33  /**
34  * {@inheritdoc}
35  */
36  protected function setUp() {
37  parent::setUp();
38 
39  $this->cuAdminData = [
40  'cu' => $this->creditUnion,
41  'fhdays' => 0,
42  'flagset' => 1536,
43  'flagset2' => 11,
44  'flagset3' => 16,
45  'grace' => 4,
46  'lastupdate' => 'Fri May 23 04:32 PM 2008',
47  'livewait' => 0,
48  'min_chlng_qst' => 3,
49  'pname' => 'We are Testy',
50  'trmemomaxlen' => 20,
51  ];
52 
53  $this->userData = [
54  "confidence" => 'HIGH',
55  'cuuser_group_id' => 31,
56  'email' => 'homer@homecu.net',
57  'failedremain' => 10,
58  'flog' => '2013/09/16 12:34:56',
59  'fchange' => 'N',
60  'fremain' => 3,
61  'llog' => '2016/07/08 11:29:56',
62  'mfaquest' => '{"answers":{"54":"Boise","64":"Boise","65":"Boise"},"challenge":0}',
63  'msg_tx' => 2,
64  'passwd' => '$2y$10$QfUKW3O8wmyK9E.2A5DhveiSDasbBUOY6rhM53H2Fe0bkWRY83En.', // password
65  'pchange' => '2019-06-11T12:34:56.123456Z',
66  'primary_account' => 'admin',
67  'savecqid' => 0,
68  'user_id' => 19,
69  'user_name' => 'Methuselah',
70  'userflags' => 15,
71  'freset' => 0
72  ];
73 
74  // Setup reflection to reload the CuAdmin data
75  $classCuAdmin = new ReflectionClass(CuAdmin::class);
76  $this->cuAdminLoad = $classCuAdmin->getMethod('Load');
77  $this->cuAdminLoad->setAccessible(true);
78  // Setup reflection to reload the User data
79  $classUser = new ReflectionClass(User::class);
80  $this->userLoad = $classUser->getMethod('Load');
81  $this->userLoad->setAccessible(true);
82 
83  }
84 
85  /**
86  * @covers \UserLogin::__construct
87  */
88  public function testConstruct() {
89  $this->cuAdminRepo = $this
90  ->getMockBuilder(CuAdminRepo::class)
91  ->setMethods(['Read'])
92  ->getMock();
93  $this->cuAdminRepo
94  ->expects(static::any())
95  ->method('Read')
96  ->with($this->creditUnion)
97  ->willReturn($this->cuAdminData);
98  $this->userRepo = $this
99  ->getMockBuilder(UserRepo::class)
100  ->setMethods(['Read'])
101  ->getMock();
102  $this->userRepo
103  ->expects(static::any())
104  ->method('Read')
105  ->with($this->userName)
106  ->willReturn($this->userData);
107 
108  $user = new User($this->userName, $this->creditUnion, $this->userRepo);
109  $cuAdmin = new CuAdmin($this->creditUnion, $this->cuAdminRepo);
110 
111  $login = new UserLogin($user, $cuAdmin);
112  static::assertInstanceOf('UserLogin', $login);
113 
114  }
115 
116  /**
117  * @covers \UserLogin::IsAccountLocked
118  */
119  public function testIsAccountLocked() {
120 
121  $this->cuAdminRepo = $this
122  ->getMockBuilder(CuAdminRepo::class)
123  ->setMethods(['Read'])
124  ->getMock();
125  $this->userRepo = $this
126  ->getMockBuilder(UserRepo::class)
127  ->setMethods(['Read'])
128  ->getMock();
129  $this->cuAdminRepo
130  ->expects(static::any())
131  ->method('Read')
132  ->with($this->creditUnion)
133  ->will(static::returnCallback(function(){return $this->cuAdminData;}));
134  $this->userRepo
135  ->expects(static::any())
136  ->method('Read')
137  ->with($this->userName)
138  ->will(static::returnCallback(function(){ return $this->userData;}));
139 
140  $cuAdmin = new CuAdmin($this->creditUnion, $this->cuAdminRepo);
141  $user = new User($this->userName, $this->creditUnion, $this->userRepo);
142 
143  $login = new UserLogin($user, $cuAdmin);
144  $login->ValidateUser($this->password);
145 
146  // Everything is fine, not locked
147  static::assertFalse($login->IsAccountLocked());
148 
149  // No more retries failures
150  $this->userData['failedremain'] = 0;
151  $this->userLoad->invoke($user);
152  static::assertTrue($login->IsAccountLocked());
153 
154  // Password exired
155  $this->userData['fchange'] = 'Y';
156  $this->userData['fremain'] = 0;
157  $this->userLoad->invoke($user);
158  static::assertTrue($login->IsAccountLocked());
159 
160  // Challenge questions with force reset
161  $this->cuAdminData['flagset3'] = CuAdmin::CU3_MFA_AUTHCODE;
162  $this->data['userflags'] = User::MEM_FORCE_RESET;
163  $this->data['fremain'] = 0;
164  $this->cuAdminLoad->invoke($cuAdmin);
165  $this->userLoad->invoke($user);
166  static::assertTrue($login->IsAccountLocked());
167 
168  // Challenge questions with changed min_chlg_qst
169  $this->cuAdminData['flagset3'] = CuAdmin::CU3_MFA_AUTHCODE;
170  $this->cuAdminData['min_chlng_qst'] = 10;
171  $this->cuAdminLoad->invoke($cuAdmin);
172  $this->userData['fremain'] = 0;
173  $this->userLoad->invoke($user);
174  static::assertTrue($login->IsAccountLocked());
175 
176  // Alias needs changing
177  $this->cuAdminData['flagset2'] = CuAdmin::CU2_ALIAS_REQ;
178  $this->cuAdminLoad->invoke($cuAdmin);
179  $this->userData['user_name'] = '12345';
180  $this->userData['fremain'] = 0;
181  $this->userLoad->invoke($user);
182  static::assertTrue($login->IsAccountLocked());
183 
184  }
185 
186  /**
187  * @covers \UserLogin::IsValidDeviceCookie
188  */
189  public function testIsValidDeviceCookie() {
190 
191  $this->cuAdminRepo = $this
192  ->getMockBuilder(CuAdminRepo::class)
193  ->setMethods(['Read'])
194  ->getMock();
195  $this->userRepo = $this
196  ->getMockBuilder(UserRepo::class)
197  ->setMethods(['Read'])
198  ->getMock();
199  $this->cuAdminRepo
200  ->expects(static::any())
201  ->method('Read')
202  ->with($this->creditUnion)
203  ->will(static::returnCallback(function(){return $this->cuAdminData;}));
204  $this->userRepo
205  ->expects(static::any())
206  ->method('Read')
207  ->with($this->userName)
208  ->will(static::returnCallback(function(){ return $this->userData;}));
209 
210  $user = new User($this->userName, $this->creditUnion, $this->userRepo);
211  $cuAdmin = new CuAdmin($this->creditUnion, $this->cuAdminRepo);
212  $login = new UserLogin($user, $cuAdmin);
213  static::assertFalse($login->IsValidDeviceCookie($this->creditUnion));
214  }
215 
216  /**
217  * @covers \UserLogin::ValidateUser
218  */
219  public function testValidateUser() {
220 
221  $this->cuAdminRepo = $this
222  ->getMockBuilder(CuAdminRepo::class)
223  ->setMethods(['Read'])
224  ->getMock();
225  $this->userRepo = $this
226  ->getMockBuilder(UserRepo::class)
227  ->setMethods(['Read'])
228  ->getMock();
229  $this->userRepo
230  ->expects(static::any())
231  ->method('Read')
232  ->with($this->userName)
233  ->willReturn($this->userData);
234  $this->cuAdminRepo
235  ->expects(static::any())
236  ->method('Read')
237  ->with($this->creditUnion)
238  ->willReturn($this->cuAdminData);
239 
240  $user = new User($this->userName, $this->creditUnion, $this->userRepo);
241  $cuAdmin = new CuAdmin($this->creditUnion, $this->cuAdminRepo);
242  $login = new UserLogin($user, $cuAdmin);
243  static::assertTrue($login->ValidateUser($this->password));
244  static::assertFalse($login->ValidateUser('notpassword'));
245 
246  }
247 
248  /**
249  * @covers \UserLogin::IsMFAMode
250  */
251  public function testIsMFAMode() {
252 
253  $this->cuAdminRepo = $this
254  ->getMockBuilder(CuAdminRepo::class)
255  ->setMethods(['Read'])
256  ->getMock();
257  $this->userRepo = $this
258  ->getMockBuilder(UserRepo::class)
259  ->setMethods(['Read'])
260  ->getMock();
261  $this->cuAdminRepo
262  ->expects(static::any())
263  ->method('Read')
264  ->with($this->creditUnion)
265  ->will(static::returnCallback(function(){return $this->cuAdminData;}));
266  $this->userRepo
267  ->expects(static::any())
268  ->method('Read')
269  ->with($this->userName)
270  ->will(static::returnCallback(function(){ return $this->userData;}));
271 
272  $user = new User($this->userName, $this->creditUnion, $this->userRepo);
273  $cuAdmin = new CuAdmin($this->creditUnion, $this->cuAdminRepo);
274  $login = new UserLogin($user, $cuAdmin);
275  static::assertTrue($login->IsMFAMode());
276 
277  //change setting to not use MFA auth code
278  $this->cuAdminData['flagset3'] = 131079;
279  $this->cuAdminLoad->invoke($cuAdmin);
280  $this->userLoad->invoke($user);
281  static::assertFalse($login->IsMFAMode());
282  }
283 }
Definition: User.php:7