Skip to content

Commit

Permalink
Merge pull request #456 from catalystfd/port_email_tests
Browse files Browse the repository at this point in the history
factor_email Port improvements to tests from moodle branch
  • Loading branch information
danmarsden authored Feb 29, 2024
2 parents a5877d5 + d919e1b commit 24e017b
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions factor/email/tests/factor_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,23 @@ public function generate_email_ip_address_location_provider(): array {
return [
'real ip v4' => [
// Note - this is the same IP address used by core_iplookup_geoplugin_testcase.
'ip' => '50.0.184.0',
'isunknown' => false
'ip' => '50.0.184.0'
],
'real ip v6' => [
// Ipv6 is not supported by geoplugin, so it should be treated as unknown.
'ip' => '2a01:8900:2:3:8c6c:c0db:3d33:9ce6',
'isunknown' => true
'ip' => '2a01:8900:2:3:8c6c:c0db:3d33:9ce6'
],
'empty ip' => [
'ip' => '',
'isunknown' => true
'ip' => ''
],
'malformed ip' => [
'ip' => '1.1.1.1.1.1.1.1.1.1',
'isunknown' => true
'ip' => '1.1.1.1.1.1.1.1.1.1'
],
'localhost' => [
'ip' => '0.0.0.0',
'isunknown' => true
'ip' => '0.0.0.0'
],
'malformed ip 2' => [
'ip' => 'aaaaaa',
'isunknown' => true
'ip' => 'aaaaaa'
]
];
}
Expand All @@ -66,11 +60,10 @@ public function generate_email_ip_address_location_provider(): array {
* Tests the rendererer generate_email function with regard to its
*
* @param string $ip IP address to test
* @param bool $expectedunknown if the IP given should return an email with the unknown message in it.
*
* @dataProvider generate_email_ip_address_location_provider
*/
public function test_generate_email_ip_address_location(string $ip, bool $expectedunknown) {
public function test_generate_email_ip_address_location(string $ip) {
global $DB, $PAGE;
$this->resetAfterTest(true);

Expand All @@ -97,15 +90,10 @@ public function test_generate_email_ip_address_location(string $ip, bool $expect
// We ignore this in this unit test.
$this->resetDebugging();

$unknownstr = get_string('email:geoinfo:unknown', 'factor_email');

if ($expectedunknown) {
$this->assertContains($unknownstr, $email);
} else {
// Ideally we would test the email contains the correct geo info,
// but since the geo location of a real IP address can change
// we instead test that the unknown message is not there.
$this->assertNotContains($unknownstr, $email);
}
// Note it's difficult to know beforehand where a IP address will resolve to.
// So instead, we just check that it contains EITHER a location or an unknown message.
$containslocation = strpos($email, 'This request appears to have originated from approximately') != false;
$containsunknown = strpos($email, get_string('email:geoinfo:unknown', 'factor_email')) != false;
$this->assertTrue($containslocation || $containsunknown);
}
}

0 comments on commit 24e017b

Please sign in to comment.