Skip to content

Instantly share code, notes, and snippets.

@kazurof
Last active January 10, 2016 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazurof/5d5b520cb4ad3491110e to your computer and use it in GitHub Desktop.
Save kazurof/5d5b520cb4ad3491110e to your computer and use it in GitHub Desktop.
CakePHPでBASIC認証をするComponent ref: http://qiita.com/kazurof/items/94a07b672f062e2db568
<?php
class BasicAuthComponent extends Component {
public function startup(Controller $controller) {
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_USER'] !== BASIC_USERID || $_SERVER['PHP_AUTH_PW'] !== BASIC_PASSWD) {
$controller->autoRender = false;
$controller->response->header('WWW-Authenticate: Basic realm="Please enter your ID and password"');
$controller->response->statusCode(401);
$controller->response->send();
$this->_stop("id / password Required");
}
}
}
<?php
App::uses('ComponentCollection', 'Controller');
App::uses('Component', 'Controller');
App::uses('BasicAuthComponent', 'Controller/Component');
App::uses('Controller', 'Controller');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
class BasicAuthComponentTest extends CakeTestCase {
public $fixtures = [
];
public $response = null;
public $controller = null;
public $component = null;
public function setUp() {
parent::setUp();
$this->response = $this->getMockBuilder('CakeResponse')->setMethods(['header', 'statusCode', 'send'])->getMock();
$this->controller = new Controller(new CakeRequest(), $this->response);
$this->component = $this->getMockBuilder('BasicAuthComponent')->setConstructorArgs([new ComponentCollection()])
->setMethods(['_stop'])
->getMock();
}
public function tearDown() {
parent::tearDown();
}
public function test_ログインが成功する() {
$_SERVER['PHP_AUTH_USER'] = "test";
$_SERVER['PHP_AUTH_PW'] = "pass";
$this->response->expects($this->never())->method('header');
$this->response->expects($this->never())->method('statusCode');
$this->response->expects($this->never())->method('send');
$this->component->expects($this->never())->method('_stop');
$this->component->initialize($this->controller);
$this->component->startup($this->controller);
$this->assertTrue($this->controller->autoRender);
}
public function test_ログインが失敗する_id_passが正しくない() {
$_SERVER['PHP_AUTH_USER'] = "nantoka";
$_SERVER['PHP_AUTH_PW'] = "kantoka";
$this->response->expects($this->once())->method('header')->with('WWW-Authenticate: Basic realm="Please enter your ID and password"');
$this->response->expects($this->once())->method('statusCode')->with(401);
$this->response->expects($this->once())->method('send')->with();
$this->component->expects($this->once())->method('_stop')->with("id / password Required");
$this->component->initialize($this->controller);
$this->component->startup($this->controller);
$this->assertFalse($this->controller->autoRender);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment