From 38ddf8f829aa45e4926c1757c951c78cfe0ab8b8 Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Tue, 7 Nov 2017 11:18:07 -0800 Subject: [PATCH 1/2] Adds ParseSession::upgradeToRevocableSession --- src/Parse/ParseSession.php | 18 ++++++++++++++++++ tests/Parse/ParseSessionTest.php | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/Parse/ParseSession.php b/src/Parse/ParseSession.php index 92d8edf5..f42c4e50 100644 --- a/src/Parse/ParseSession.php +++ b/src/Parse/ParseSession.php @@ -86,6 +86,24 @@ public static function _isRevocable($token) return strpos($token, 'r:') === 0; } + public static function upgradeToRevocableSession() + { + $token = ParseUser::getCurrentUser()->getSessionToken(); + $response = ParseClient::_request( + 'POST', + 'upgradeToRevocableSession', + $token, + null, + false + ); + $session = new self(); + $session->_mergeAfterFetch($response); + $session->handleSaveResult(); + ParseUser::become($session->getSessionToken()); + + //echo "\n\nGOT RESPONSE: ".json_encode($response) . "\n\n"; + } + /** * After a save, perform Session object specific logic. */ diff --git a/tests/Parse/ParseSessionTest.php b/tests/Parse/ParseSessionTest.php index 1d48a79a..70cac4b1 100644 --- a/tests/Parse/ParseSessionTest.php +++ b/tests/Parse/ParseSessionTest.php @@ -56,4 +56,27 @@ public function testRevocableSession() $this->setExpectedException('Parse\ParseException', 'invalid session token'); ParseUser::become($sessionToken); } + + /** + * @group upgrade-to-revocable-session + */ + public function testUpgradeToRevocableSession() + { + $user = new ParseUser(); + $user->setUsername('revocable_username'); + $user->setPassword('revocable_password'); + $user->signUp(); + + $session = ParseSession::getCurrentSession(); + $this->assertEquals($user->getSessionToken(), $session->getSessionToken()); + + // upgrade the current session (changes our session as well) + ParseSession::upgradeToRevocableSession(); + + // verify that our session has changed, and our updated current user matches it + $session = ParseSession::getCurrentSession(); + $user = ParseUser::getCurrentUser(); + $this->assertEquals($user->getSessionToken(), $session->getSessionToken()); + $this->assertTrue($session->isCurrentSessionRevocable()); + } } From 0e3687839be1287e5046022cb9e988f6697eccdc Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Tue, 7 Nov 2017 11:43:27 -0800 Subject: [PATCH 2/2] added exception & exception test --- src/Parse/ParseSession.php | 36 +++++++++++++++++++------------- tests/Parse/ParseSessionTest.php | 10 +++++++++ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/Parse/ParseSession.php b/src/Parse/ParseSession.php index f42c4e50..c36a3299 100644 --- a/src/Parse/ParseSession.php +++ b/src/Parse/ParseSession.php @@ -86,22 +86,30 @@ public static function _isRevocable($token) return strpos($token, 'r:') === 0; } + /** + * Upgrades the current session to a revocable one + * + * @throws ParseException + */ public static function upgradeToRevocableSession() { - $token = ParseUser::getCurrentUser()->getSessionToken(); - $response = ParseClient::_request( - 'POST', - 'upgradeToRevocableSession', - $token, - null, - false - ); - $session = new self(); - $session->_mergeAfterFetch($response); - $session->handleSaveResult(); - ParseUser::become($session->getSessionToken()); - - //echo "\n\nGOT RESPONSE: ".json_encode($response) . "\n\n"; + $user = ParseUser::getCurrentUser(); + if ($user) { + $token = $user->getSessionToken(); + $response = ParseClient::_request( + 'POST', + 'upgradeToRevocableSession', + $token, + null, + false + ); + $session = new self(); + $session->_mergeAfterFetch($response); + $session->handleSaveResult(); + ParseUser::become($session->getSessionToken()); + } else { + throw new ParseException('No session to upgrade.'); + } } /** diff --git a/tests/Parse/ParseSessionTest.php b/tests/Parse/ParseSessionTest.php index 70cac4b1..9ca8f152 100644 --- a/tests/Parse/ParseSessionTest.php +++ b/tests/Parse/ParseSessionTest.php @@ -79,4 +79,14 @@ public function testUpgradeToRevocableSession() $this->assertEquals($user->getSessionToken(), $session->getSessionToken()); $this->assertTrue($session->isCurrentSessionRevocable()); } + + /** + * @group upgrade-to-revocable-session + */ + public function testBadUpgradeToRevocableSession() + { + // upgrade the current session (changes our session as well) + $this->setExpectedException('Parse\ParseException', 'No session to upgrade.'); + ParseSession::upgradeToRevocableSession(); + } }