From 8019f4280aff7983e682ea0030377036a555fe80 Mon Sep 17 00:00:00 2001 From: Isaac Askew Date: Tue, 11 Apr 2023 17:26:59 -0600 Subject: [PATCH 1/2] Adding loginAs method with tests --- src/Parse/ParseUser.php | 27 +++++++++++++++++++++++++++ tests/Parse/ParseUserTest.php | 21 +++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/Parse/ParseUser.php b/src/Parse/ParseUser.php index 12283991..cf84a359 100644 --- a/src/Parse/ParseUser.php +++ b/src/Parse/ParseUser.php @@ -157,6 +157,33 @@ public static function logIn($username, $password) return $user; } + /** + * Uses the master key to log in and return a valid ParseUser, or throws if invalid. + * + * @param $userId + * + * @throws ParseException + * + * @return static + */ + public static function logInAs($userId) + { + if (!$userId) { + throw new ParseException( + 'Cannot log in as user with an empty user id', + 200 + ); + } + $data = ['userId' => $userId]; + $result = ParseClient::_request('POST', 'loginAs', '', json_encode($data), true); + $user = new static(); + $user->_mergeAfterFetch($result); + $user->handleSaveResult(true); + ParseClient::getStorage()->set('user', $user); + + return $user; + } + /** * Logs in with Facebook details, or throws if invalid. * diff --git a/tests/Parse/ParseUserTest.php b/tests/Parse/ParseUserTest.php index 5e600115..078d53a4 100644 --- a/tests/Parse/ParseUserTest.php +++ b/tests/Parse/ParseUserTest.php @@ -83,6 +83,27 @@ public function testLoginWrongPassword() ParseUser::logIn('asdf', 'bogus'); } + public function testLoginAsSuccess() + { + $user = new ParseUser(); + $user->setUsername('plainusername'); + $user->setPassword('plainpassword'); + $user->signUp(); + + $id = $user->getObjectId(); + $loggedInUser = ParseUser::logInAs($id); + $this->assertTrue($loggedInUser->isAuthenticated()); + $this->assertEquals('plainusername', $loggedInUser->get('username')); + + ParseUser::logOut(); + } + + public function testLoginAsEmptyUsername() + { + $this->expectException('Parse\ParseException', 'Cannot log in as user with an empty user id.'); + ParseUser::logInAs(''); + } + public function testLoginWithFacebook() { $this->expectException( From bc3c306a3501361a48667845270e8304b225ee03 Mon Sep 17 00:00:00 2001 From: Isaac Askew Date: Fri, 21 Apr 2023 16:59:53 -0600 Subject: [PATCH 2/2] Adding test to handle non-existence user and improving documentation for logInAs --- src/Parse/ParseUser.php | 2 +- tests/Parse/ParseUserTest.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Parse/ParseUser.php b/src/Parse/ParseUser.php index cf84a359..b8bd277e 100644 --- a/src/Parse/ParseUser.php +++ b/src/Parse/ParseUser.php @@ -164,7 +164,7 @@ public static function logIn($username, $password) * * @throws ParseException * - * @return static + * @return ParseUser */ public static function logInAs($userId) { diff --git a/tests/Parse/ParseUserTest.php b/tests/Parse/ParseUserTest.php index 078d53a4..1c57c909 100644 --- a/tests/Parse/ParseUserTest.php +++ b/tests/Parse/ParseUserTest.php @@ -104,6 +104,12 @@ public function testLoginAsEmptyUsername() ParseUser::logInAs(''); } + public function testLoginAsNonexistentUser() + { + $this->expectException('Parse\ParseException', 'user not found.'); + ParseUser::logInAs('a1b2c3d4e5'); + } + public function testLoginWithFacebook() { $this->expectException(