2010-10-19 00:29:35 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2010-10-19 00:29:35 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
class BasicAuthTest extends FunctionalTest {
|
2010-10-19 03:26:49 +02:00
|
|
|
|
|
|
|
static $original_unique_identifier_field;
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'BasicAuthTest.yml';
|
2010-10-19 03:26:49 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2010-10-19 03:26:49 +02:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// Fixtures assume Email is the field used to identify the log in identity
|
2013-03-21 19:48:54 +01:00
|
|
|
self::$original_unique_identifier_field = Member::config()->unique_identifier_field;
|
|
|
|
Member::config()->unique_identifier_field = 'Email';
|
2013-06-21 00:32:08 +02:00
|
|
|
Security::$force_database_is_ready = true; // Prevents Member test subclasses breaking ready test
|
2010-10-19 03:26:49 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function tearDown() {
|
2010-10-19 00:29:35 +02:00
|
|
|
parent::tearDown();
|
|
|
|
|
|
|
|
BasicAuth::protect_entire_site(false);
|
2013-03-21 19:48:54 +01:00
|
|
|
Member::config()->unique_identifier_field = self::$original_unique_identifier_field;
|
2013-06-21 00:32:08 +02:00
|
|
|
Security::$force_database_is_ready = null;
|
2010-10-19 00:29:35 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testBasicAuthEnabledWithoutLogin() {
|
2014-03-04 23:47:02 +01:00
|
|
|
$origUser = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : null;
|
|
|
|
$origPw = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : null;
|
2010-10-19 00:29:35 +02:00
|
|
|
|
|
|
|
unset($_SERVER['PHP_AUTH_USER']);
|
|
|
|
unset($_SERVER['PHP_AUTH_PW']);
|
|
|
|
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission');
|
|
|
|
$this->assertEquals(401, $response->getStatusCode());
|
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = $origUser;
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = $origPw;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testBasicAuthDoesntCallActionOrFurtherInitOnAuthFailure() {
|
2014-03-04 23:47:02 +01:00
|
|
|
$origUser = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : null;
|
|
|
|
$origPw = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : null;
|
2010-10-19 00:29:35 +02:00
|
|
|
|
|
|
|
unset($_SERVER['PHP_AUTH_USER']);
|
|
|
|
unset($_SERVER['PHP_AUTH_PW']);
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission');
|
|
|
|
$this->assertFalse(BasicAuthTest_ControllerSecuredWithPermission::$index_called);
|
|
|
|
$this->assertFalse(BasicAuthTest_ControllerSecuredWithPermission::$post_init_called);
|
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'user-in-mygroup@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'test';
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission');
|
|
|
|
$this->assertTrue(BasicAuthTest_ControllerSecuredWithPermission::$index_called);
|
|
|
|
$this->assertTrue(BasicAuthTest_ControllerSecuredWithPermission::$post_init_called);
|
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = $origUser;
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = $origPw;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testBasicAuthEnabledWithPermission() {
|
2014-03-04 23:47:02 +01:00
|
|
|
$origUser = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : null;
|
|
|
|
$origPw = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : null;
|
2010-10-19 00:29:35 +02:00
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'user-in-mygroup@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'wrongpassword';
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission');
|
|
|
|
$this->assertEquals(401, $response->getStatusCode(), 'Invalid users dont have access');
|
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'user-without-groups@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'test';
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission');
|
|
|
|
$this->assertEquals(401, $response->getStatusCode(), 'Valid user without required permission has no access');
|
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'user-in-mygroup@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'test';
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission');
|
|
|
|
$this->assertEquals(200, $response->getStatusCode(), 'Valid user with required permission has access');
|
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = $origUser;
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = $origPw;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testBasicAuthEnabledWithoutPermission() {
|
2014-03-04 23:47:02 +01:00
|
|
|
$origUser = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : null;
|
|
|
|
$origPw = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : null;
|
2010-10-19 00:29:35 +02:00
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'user-without-groups@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'wrongpassword';
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithoutPermission');
|
|
|
|
$this->assertEquals(401, $response->getStatusCode(), 'Invalid users dont have access');
|
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'user-without-groups@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'test';
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithoutPermission');
|
|
|
|
$this->assertEquals(200, $response->getStatusCode(), 'All valid users have access');
|
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = 'user-in-mygroup@test.com';
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = 'test';
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithoutPermission');
|
|
|
|
$this->assertEquals(200, $response->getStatusCode(), 'All valid users have access');
|
|
|
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = $origUser;
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = $origPw;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-18 03:01:09 +01:00
|
|
|
class BasicAuthTest_ControllerSecuredWithPermission extends Controller implements TestOnly {
|
2010-10-19 00:29:35 +02:00
|
|
|
|
|
|
|
static $post_init_called = false;
|
|
|
|
|
|
|
|
static $index_called = false;
|
2011-03-18 03:01:09 +01:00
|
|
|
|
2011-03-30 08:49:11 +02:00
|
|
|
protected $template = 'BlankPage';
|
2010-10-19 00:29:35 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function init() {
|
2010-10-19 00:29:35 +02:00
|
|
|
self::$post_init_called = false;
|
|
|
|
self::$index_called = false;
|
|
|
|
|
|
|
|
BasicAuth::protect_entire_site(true, 'MYCODE');
|
|
|
|
parent::init();
|
2011-03-30 08:49:11 +02:00
|
|
|
|
2010-10-19 00:29:35 +02:00
|
|
|
self::$post_init_called = true;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function index() {
|
2010-10-19 00:29:35 +02:00
|
|
|
self::$index_called = true;
|
|
|
|
}
|
2011-03-18 03:01:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-19 00:29:35 +02:00
|
|
|
}
|
|
|
|
|
2011-03-18 03:01:09 +01:00
|
|
|
class BasicAuthTest_ControllerSecuredWithoutPermission extends Controller implements TestOnly {
|
|
|
|
|
2011-03-30 08:49:11 +02:00
|
|
|
protected $template = 'BlankPage';
|
2011-03-18 03:01:09 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function init() {
|
2010-10-19 00:29:35 +02:00
|
|
|
BasicAuth::protect_entire_site(true, null);
|
|
|
|
parent::init();
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|