Skip to content

Commit

Permalink
Improve unit testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
terrylinooo committed Jan 18, 2020
1 parent 12e0804 commit 780537f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Shieldon/Shieldon.php
Expand Up @@ -1487,10 +1487,14 @@ private function _run(): int
);
}

// @codeCoverageIgnoreStart

} catch (RuntimeException $e) {
// echo $e->getMessage();
// Do not throw error, becasue the third-party services might be unavailable.
}

// @codeCoverageIgnoreEnd
}

// For an incoming request already in the rule list, return the rule type immediately.
Expand Down
32 changes: 31 additions & 1 deletion tests/Shieldon/Component/TrustedBotTest.php
Expand Up @@ -43,7 +43,10 @@ public function testGetDeniedList()

public function testIsDenied()
{
$this->assertFalse(false);
$trustedBotComponent = new TrustedBot();
$result = $trustedBotComponent->isDenied();

$this->assertFalse($result);
}

public function testIsAllowed()
Expand Down Expand Up @@ -228,6 +231,33 @@ public function testFakeGoogleBot_2()
$this->assertFalse($isFakeGooglebot);
}

/**
* Situation 3: Fake user-agent.
*/
public function testFakeGoogleBot_3()
{
$_SERVER['HTTP_USER_AGENT'] = 'Googlebot/2.1 (+http://www.google.com/bot.html)';

$trustedBotComponent = new TrustedBot();
$trustedBotComponent->setIp('127.0.0.1', false);
$trustedBotComponent->setRdns('localhost');

$reflection = new \ReflectionObject($trustedBotComponent);
$t = $reflection->getProperty('checkFakeRdns');
$t->setAccessible(true);

// Disable checking fake RDNS.
$t->setValue($trustedBotComponent, false);

$result = $trustedBotComponent->isAllowed();

$this->assertFalse($result);

$isFakeGooglebot = $trustedBotComponent->isFakeRobot();

$this->assertTrue($isFakeGooglebot);
}

public function testGetDenyStatusCode()
{
$trustedBotComponent = new TrustedBot();
Expand Down
32 changes: 31 additions & 1 deletion tests/Shieldon/ShieldonTest.php
Expand Up @@ -263,6 +263,17 @@ public function testAction($driver = 'sqlite')
$this->assertSame($shieldon::RESPONSE_ALLOW, $result);
}

public function testNoComponentAndFilters()
{
$shieldon = get_testing_shieldon_instance();
$shieldon->setChannel('test_shieldon_detect');
$shieldon->setIp('39.27.1.1');
$shieldon->disableFiltering();
$result = $shieldon->run();

$this->assertSame($shieldon::RESPONSE_ALLOW, $result);
}

public function testGetComponent()
{
$shieldon = new \Shieldon\Shieldon();
Expand Down Expand Up @@ -398,6 +409,7 @@ public function testSetProperty()
$shieldon->setProperty('cookie_domain', 'localhost');
$shieldon->setProperty('display_online_info', true);
$shieldon->setProperty('display_lineup_info', true);
$shieldon->setProperty('display_user_info', true);

$reflection = new \ReflectionObject($shieldon);
$t = $reflection->getProperty('properties');
Expand Down Expand Up @@ -585,7 +597,9 @@ public function testOutput($driver = 'sqlite')
$_SERVER['REQUEST_URI'] = '/';

$shieldon = get_testing_shieldon_instance($driver);
$shieldon->setProperty('display_lineup_info', false);
$shieldon->setProperty('display_lineup_info', true);
$shieldon->setProperty('display_user_info', true);
$shieldon->setProperty('display_online_info', true);
$shieldon->driver->rebuild();

// Limit
Expand Down Expand Up @@ -628,6 +642,10 @@ public function testOutput($driver = 'sqlite')

$shieldon->setIp('141.112.175.1');

$shieldon->setProperty('display_lineup_info', false);
$shieldon->setProperty('display_user_info', false);
$shieldon->setProperty('display_online_info', false);

$shieldon->setProperty('time_unit_quota', [
's' => 2,
'm' => 20,
Expand Down Expand Up @@ -1128,4 +1146,16 @@ public function testDenyAttempts()
$this->assertEquals($result, $shieldon::RESPONSE_DENY);
}
}

public function testFakeTrustedBot()
{
$_SERVER['HTTP_USER_AGENT'] = 'google';

$shieldon = get_testing_shieldon_instance();
$shieldon->setComponent(new \Shieldon\Component\TrustedBot());
$shieldon->disableFiltering();
$result = $shieldon->run();

$this->assertSame($shieldon::RESPONSE_DENY, $result);
}
}

0 comments on commit 780537f

Please sign in to comment.