Skip to content

Commit c003952

Browse files
AdrienClairembaulttrasher
authored andcommitted
Mock login in tests
1 parent dc2aeef commit c003952

File tree

4 files changed

+54
-30
lines changed

4 files changed

+54
-30
lines changed

phpunit/DbTestCase.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function tearDown(): void
7272
*
7373
* @return \Auth
7474
*/
75-
protected function login(
75+
protected function realLogin(
7676
string $user_name = TU_USER,
7777
string $user_pass = TU_PASS,
7878
bool $noauto = true,
@@ -87,6 +87,33 @@ protected function login(
8787
return $auth;
8888
}
8989

90+
/**
91+
* "Fake" login process (for performances reason), if you need a real login
92+
* use the realLogin() method instead.
93+
*
94+
* The rule engine will not be executed.
95+
*
96+
* A session will be loaded for the supplied user without checking if his
97+
* account is valid.
98+
*
99+
* The "user_pass" parameter is no longer used but was not removed to
100+
* avoid needing to update all the occurences in existings tests.
101+
*/
102+
protected function login(
103+
string $user_name = TU_USER,
104+
string $user_pass = "",
105+
): \Auth {
106+
\Session::destroy();
107+
\Session::start();
108+
109+
$auth = new Auth();
110+
$auth->user = getItemByTypeName(User::class, $user_name);
111+
$auth->auth_succeded = true;
112+
Session::init($auth);
113+
114+
return $auth;
115+
}
116+
90117
/**
91118
* Log out current user
92119
*

phpunit/LDAP/AuthLdapTest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ public function testSyncUser()
12261226
public function testLdapAuth()
12271227
{
12281228
//try to log in from a user that does not exist yet
1229-
$auth = $this->login('brazil6', 'password', false);
1229+
$auth = $this->realLogin('brazil6', 'password', false);
12301230

12311231
$user = new \User();
12321232
$user->getFromDBbyName('brazil6');
@@ -1266,7 +1266,7 @@ public function testLdapAuth()
12661266
$this->assertSame('brazil7', $user->fields['name']);
12671267
$this->assertSame('uid=brazil7,ou=people,ou=R&D,dc=glpi,dc=org', $user->fields['user_dn']);
12681268

1269-
$auth = $this->login('brazil7', 'password', false, true);
1269+
$auth = $this->realLogin('brazil7', 'password', false, true);
12701270

12711271
$this->assertTrue($auth->user_present);
12721272
$this->assertSame($user->fields['user_dn'], $auth->user_dn);
@@ -1283,8 +1283,8 @@ public function testLdapAuth()
12831283
)
12841284
);
12851285

1286-
$this->login('brazil7', 'password', false, false);
1287-
$auth = $this->login('brazil7test', 'password', false);
1286+
$this->realLogin('brazil7', 'password', false, false);
1287+
$auth = $this->realLogin('brazil7test', 'password', false);
12881288

12891289
//reset entry before any test can fail
12901290
$this->assertTrue(
@@ -1322,7 +1322,7 @@ public function testLdapAuth()
13221322
(int) $user->add($dup)
13231323
);
13241324

1325-
$auth = $this->login('brazil6', 'password', false);
1325+
$auth = $this->realLogin('brazil6', 'password', false);
13261326
$this->assertSame($aid, $auth->user->fields['auths_id']);
13271327
$this->assertSame('brazil6', $auth->user->fields['name']);
13281328
$this->assertSame('uid=brazil6,ou=people,ou=R&D,dc=glpi,dc=org', $auth->user->fields['user_dn']);
@@ -2305,7 +2305,7 @@ public function testRuleRight()
23052305
]);
23062306

23072307
// login the user to force a real synchronisation and get it's glpi id
2308-
$this->login('brazil6', 'password', false);
2308+
$this->realLogin('brazil6', 'password', false);
23092309
$users_id = \User::getIdByName('brazil6');
23102310
$this->assertGreaterThan(0, $users_id);
23112311
// check the user got the entity/profiles assigned
@@ -2389,7 +2389,7 @@ public function testGroupRuleRight()
23892389
])->getID();
23902390

23912391
// login the user to force a real synchronisation and get it's glpi id
2392-
$this->login('brazil6', 'password', false);
2392+
$this->realLogin('brazil6', 'password', false);
23932393
$users_id = \User::getIdByName('brazil6');
23942394
$this->assertGreaterThan(0, $users_id);
23952395

@@ -2408,7 +2408,7 @@ public function testGroupRuleRight()
24082408
]);
24092409

24102410
// Login
2411-
$this->login('brazil6', 'password', false);
2411+
$this->realLogin('brazil6', 'password', false);
24122412
$users_id = \User::getIdByName('brazil6');
24132413
$this->assertGreaterThan(0, $users_id);
24142414

@@ -2452,7 +2452,7 @@ public function testGroupRuleRight()
24522452
]);
24532453

24542454
// Login
2455-
$this->login('brazil6', 'password', false);
2455+
$this->realLogin('brazil6', 'password', false);
24562456
$users_id = \User::getIdByName('brazil6');
24572457
$this->assertGreaterThan(0, $users_id);
24582458

@@ -2493,7 +2493,7 @@ public function testRuleRightGroupCriteria()
24932493
$this->createRule($builder);
24942494

24952495
// login the user to force a real synchronisation (and creation into DB)
2496-
$this->login('brazil7', 'password', false);
2496+
$this->realLogin('brazil7', 'password', false);
24972497
$users_id = \User::getIdByName('brazil7');
24982498

24992499
// Add group to user
@@ -2510,7 +2510,7 @@ public function testRuleRightGroupCriteria()
25102510
$this->assertCount(0, $rights);
25112511

25122512
// Log in again to trigger rule
2513-
$this->login('brazil7', 'password', false);
2513+
$this->realLogin('brazil7', 'password', false);
25142514

25152515
// Check that the correct profile was set
25162516
$rights = (new Profile_User())->find([
@@ -2525,7 +2525,7 @@ public function testRuleRightGroupCriteria()
25252525
public function testLdapUnavailable()
25262526
{
25272527
//Import user that doesn't exist yet
2528-
$auth = $this->login('brazil5', 'password', false);
2528+
$auth = $this->realLogin('brazil5', 'password', false);
25292529

25302530
$user = new \User();
25312531
$user->getFromDBbyName('brazil5');
@@ -2551,7 +2551,7 @@ public function testLdapUnavailable()
25512551
])
25522552
);
25532553

2554-
$this->login('brazil5', 'password', false, false);
2554+
$this->realLogin('brazil5', 'password', false, false);
25552555
$this->hasPhpLogRecordThatContains(
25562556
"Unable to bind to LDAP server `openldap:1234` with RDN `cn=Manager,dc=glpi,dc=org`\nerror: Can't contact LDAP server (-1)",
25572557
LogLevel::WARNING
@@ -2588,7 +2588,7 @@ public function testLdapDeletionOnLogin()
25882588
);
25892589

25902590
//Import user that doesn't exist yet
2591-
$auth = $this->login('logintest', 'password');
2591+
$auth = $this->realLogin('logintest', 'password');
25922592

25932593
$user = new \User();
25942594
$user->getFromDBbyName('logintest');
@@ -2665,7 +2665,7 @@ public function testIgnoreImport()
26652665
'value' => '1', // Reject
26662666
]);
26672667
// login the user to force synchronisation
2668-
$this->login('brazil6', 'password', false, false);
2668+
$this->realLogin('brazil6', 'password', false, false);
26692669

26702670
// Check title not created
26712671
$ut = new UserTitle();

phpunit/functional/CommonDBTMTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ public function testTimezones()
949949
$this->assertGreaterThan(0, count($DB->getTimezones()));
950950

951951
//login with default TZ
952-
$this->login();
952+
$this->realLogin();
953953
//add a Computer with creation and update dates
954954
$comp = new \Computer();
955955
$cid = $comp->add([
@@ -970,7 +970,7 @@ public function testTimezones()
970970
$this->assertTrue($user->getFromDB($user->fields['id']));
971971
$this->assertSame('Europe/Paris', $user->fields['timezone']);
972972

973-
$this->login('glpi', 'glpi');
973+
$this->realLogin('glpi', 'glpi');
974974
$this->assertTrue($comp->getFromDB($cid));
975975
$this->assertMatchesRegularExpression('/2019-03-04 1[12]:00:00/', $comp->fields['date_creation']);
976976
}

phpunit/functional/RuleRightTest.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function testLocalAccount()
111111
]);
112112

113113
// login the user to force a real synchronisation and get it's glpi id
114-
$this->login(TU_USER, TU_PASS, false);
114+
$this->realLogin(TU_USER, TU_PASS, false);
115115
$users_id = \User::getIdByName(TU_USER);
116116
$this->assertGreaterThan(0, $users_id);
117117

@@ -126,7 +126,7 @@ public function testLocalAccount()
126126
'value' => -1, // Full structure
127127
]);
128128

129-
$this->login(TU_USER, TU_PASS, false);
129+
$this->realLogin(TU_USER, TU_PASS, false);
130130
$user->getFromDB($users_id);
131131
$this->assertEquals(null, $user->fields['entities_id']);
132132

@@ -159,7 +159,7 @@ public function testLocalAccount()
159159
\SingletonRuleList::getInstance("RuleRight", 0)->list = [];
160160

161161
// login again
162-
$this->login(TU_USER, TU_PASS, false);
162+
$this->realLogin(TU_USER, TU_PASS, false);
163163

164164
// check the user got the entity/profiles assigned
165165
$pu = \Profile_User::getForUser($users_id, true);
@@ -188,7 +188,7 @@ public function testLocalAccountNoRules()
188188
];
189189

190190
$user = new \User();
191-
$this->login();
191+
$this->realLogin();
192192
$users_id = $user->add($testuser);
193193
$this->assertGreaterThan(0, $users_id);
194194

@@ -202,15 +202,15 @@ public function testLocalAccountNoRules()
202202
$this->assertEquals(1, $right['is_default_profile']);
203203

204204
// Log in to force rules right processing
205-
$this->login($testuser['name'], $testuser['password'], false);
205+
$this->realLogin($testuser['name'], $testuser['password'], false);
206206

207207
// Get rights again, should not have changed
208208
$pu = \Profile_User::getForUser($users_id, true);
209209
$this->assertCount(1, $pu);
210210
$right2 = array_pop($pu);
211211
$this->assertEquals($right, $right2);
212212

213-
$this->login();
213+
$this->realLogin();
214214
// Change the $right profile, since this is the default profile it should
215215
// be fixed on next login
216216
$pu = new \Profile_User();
@@ -223,7 +223,7 @@ public function testLocalAccountNoRules()
223223
$this->assertEquals(1, $pu->fields['is_default_profile']);
224224
$this->assertEquals(1, $pu->fields['is_dynamic']);
225225

226-
$this->login($testuser['name'], $testuser['password'], false);
226+
$this->realLogin($testuser['name'], $testuser['password'], false);
227227
$pu = \Profile_User::getForUser($users_id, true);
228228
$this->assertCount(1, $pu);
229229
$right3 = array_pop($pu);
@@ -232,9 +232,6 @@ public function testLocalAccountNoRules()
232232
unset($right['id']);
233233
unset($right3['id']);
234234
$this->assertEquals($right, $right3);
235-
236-
// Clean session
237-
$this->login();
238235
}
239236

240237
public function testDenyLogin()
@@ -273,7 +270,7 @@ public function testDenyLogin()
273270
])
274271
);
275272

276-
$this->login(TU_USER, TU_PASS, true, false);
273+
$this->realLogin(TU_USER, TU_PASS, true, false);
277274
$events = getAllDataFromTable('glpi_events', [
278275
'service' => 'login',
279276
'type' => 'system',
@@ -321,7 +318,7 @@ public function testLanguage()
321318
$this->assertEquals(null, $user->getField('language'));
322319

323320
// login
324-
$this->login(TU_USER, TU_PASS);
321+
$this->realLogin(TU_USER, TU_PASS);
325322

326323
// language from rule
327324
$user->getFromDBByName(TU_USER);

0 commit comments

Comments
 (0)