-
Notifications
You must be signed in to change notification settings - Fork 34
Implemented canLogIn check for authenticate method. FIXES #52
#53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ | |
| class RESTfulAPI_TokenAuthenticator_Test extends RESTfulAPI_Tester | ||
| { | ||
| protected $requiredExtensions = array( | ||
| 'Member' => array('RESTfulAPI_TokenAuthExtension') | ||
| 'Member' => array('RESTfulAPI_TokenAuthExtension', 'TestCanLoginExtension') | ||
| ); | ||
|
|
||
| protected function getAuthenticator() | ||
|
|
@@ -35,6 +35,11 @@ public function setUpOnce() | |
| 'Email' => '[email protected]', | ||
| 'Password' => 'test' | ||
| ))->write(); | ||
|
|
||
| Member::create(array( | ||
| 'Email' => '[email protected]', | ||
| 'Password' => 'flame.sword' | ||
| )); | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -209,4 +214,84 @@ public function testAuthenticate() | |
| "TokenAuth authentication failure should return a RESTfulAPI_Error" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public function testCanLogin(){ | ||
| $auth = $this->getAuthenticator(); | ||
|
|
||
| // check login when canLogIn check fails | ||
| $request = new SS_HTTPRequest( | ||
| 'GET', | ||
| 'api/auth/login', | ||
| array( | ||
| 'email' => '[email protected]', | ||
| 'pwd' => 'flame.sword' | ||
| ) | ||
| ); | ||
|
|
||
| $result = $auth->login($request); | ||
|
|
||
| $this->assertEquals( | ||
| $result['code'], | ||
| RESTfulAPI_TokenAuthenticator::AUTH_CODE_LOGIN_FAIL, | ||
| "Logging in a member whose `canLogIn` check fails should not be allowed." | ||
| ); | ||
|
|
||
| // check authenticate when canLogIn check fails | ||
|
|
||
| $request = new SS_HTTPRequest( | ||
| 'GET', | ||
| 'api/auth/login', | ||
| array( | ||
| 'email' => '[email protected]', | ||
| 'pwd' => 'test' | ||
| ) | ||
| ); | ||
|
|
||
| $result = $auth->login($request); | ||
|
|
||
| $this->assertEquals( | ||
| $result['code'], | ||
| RESTfulAPI_TokenAuthenticator::AUTH_CODE_LOGGED_IN, | ||
| "Logging in a member whose `canLogIn` check doesn't fail should be allowed." | ||
| ); | ||
|
|
||
| $request = new SS_HTTPRequest( | ||
| 'GET', | ||
| 'api/ApiTest_Book/1' | ||
| ); | ||
| $request->addHeader('X-Silverstripe-Apitoken', $result['token']); | ||
|
|
||
| // deny all users for the authenticate request | ||
| TestCanLoginExtension::$denyAll = true; | ||
|
|
||
| $result = $auth->authenticate($request); | ||
|
|
||
| TestCanLoginExtension::$denyAll = false; | ||
|
|
||
| $this->assertContainsOnlyInstancesOf( | ||
| 'RESTfulAPI_Error', | ||
| array($result), | ||
| "TokenAuth authentication should fail when 'canLogIn' fails" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extension to test "canLogin" | ||
| */ | ||
| class TestCanLoginExtension extends DataExtension | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be cleaner to have this as a different file and rename the class to follow the same pattern as the other ones like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's only being used for this test-case.. so moving it to an external file seems counter-intuitive as it makes understanding of the test more difficult. As for naming, feel free to rename it. |
||
| { | ||
| public static $denyAll = false; | ||
|
|
||
| public function canLogIn(ValidationResult &$result) | ||
| { | ||
| if(self::$denyAll){ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need all this login? We could just have one line like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to test |
||
| $result->error('All access denied'); | ||
| } | ||
|
|
||
| if($this->owner->Email === '[email protected]'){ | ||
| // deny access to the balrog! | ||
| $result->error('You shall not pass!'); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need to do another test here? Since all that changes is the
iflogic in thecanLogInExtesnion?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to test
loginandauthenticate. The first test is for a user that can't login, the second test is for a user that was logged in (eg. has a token) but then some condition changes, so his access via token (authenticate) should fail.