From 81bdb1f2af442e3a304c3c34e73de642bf3e4cd7 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Thu, 12 Oct 2017 01:50:09 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=91=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=20=D1=80=D0=B5=D0=B0=D0=BA=D1=86=D0=B8=D0=B8=20=D1=81=D0=B5?= =?UTF-8?q?=D1=80=D0=B2=D0=B5=D1=80=D0=B0=20=D0=BD=D0=B0=20dummy=20=D0=B4?= =?UTF-8?q?=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/components/TestData.php | 164 ++++++++++++++++++++++++++++++++++++ api/config/config.php | 1 + 2 files changed, 165 insertions(+) create mode 100644 api/components/TestData.php diff --git a/api/components/TestData.php b/api/components/TestData.php new file mode 100644 index 0000000..37fcc4b --- /dev/null +++ b/api/components/TestData.php @@ -0,0 +1,164 @@ + 'beforeSignup', + 'signup/repeat-message' => 'beforeRepeatMessage', + 'signup/confirm' => 'beforeSignupConfirm', + 'authentication/forgot-password' => 'beforeForgotPassword', + 'authentication/recover-password' => 'beforeRecoverPassword', + 'default/get' => 'beforeAccountGet', + 'default/email-verification' => 'beforeAccountEmailVerification', + 'default/new-email-verification' => 'beforeAccountNewEmailVerification', + 'default/email' => 'beforeAccountChangeEmail', + ]; + + public static function getInstance(): callable { + return Closure::fromCallable([new static(), 'beforeAction']); + } + + public function beforeAction(ActionEvent $event): void { + $id = $event->action->controller->id . '/' . $event->action->id; + if (!isset(self::MAP[$id])) { + return; + } + + $handler = self::MAP[$id]; + $request = Yii::$app->request; + $response = Yii::$app->response; + $result = $this->$handler($request, $response); + if ($result === null) { + return; + } + + $response->content = Json::encode($result); + + // Prevent request execution + $event->isValid = false; + $event->handled = true; + } + + public function beforeSignup(Request $request): ?array { + $email = $request->post('email'); + if ($email === 'let-me-register@ely.by') { + return ['success' => true]; + } + + return null; + } + + public function beforeRepeatMessage(Request $request): ?array { + $email = $request->post('email'); + if ($email === 'let-me-register@ely.by' || $email === 'let-me-repeat@ely.by') { + return ['success' => true]; + } + + return null; + } + + public function beforeSignupConfirm(Request $request): ?array { + $email = $request->post('key'); + if ($email === 'LETMEIN') { + return [ + 'success' => true, + 'access_token' => 'dummy_token', + 'expires_in' => time() + 60, + ]; + } + + return null; + } + + public function beforeForgotPassword(Request $request): ?array { + $login = $request->post('login'); + if ($login === 'let-me-recover@ely.by') { + return [ + 'success' => true, + 'data' => [ + 'canRepeatIn' => time() + 60, + 'repeatFrequency' => 60, + ], + ]; + } + + return null; + } + + public function beforeRecoverPassword(Request $request): ?array { + $key = $request->post('key'); + if ($key === 'LETMEIN') { + return [ + 'success' => true, + 'access_token' => 'dummy_token', + 'expires_in' => time() + 60, + ]; + } + + return null; + } + + public function beforeAccountGet(Request $request): ?array { + $httpAuth = $request->getHeaders()->get('authorization'); + if ($httpAuth === 'Bearer dummy_token') { + return [ + 'id' => 1, + 'uuid' => 'f63cd5e1-680f-4c2d-baa2-cc7bb174b71a', + 'username' => 'dummy', + 'isOtpEnabled' => false, + 'registeredAt' => time(), + 'lang' => 'en', + 'elyProfileLink' => 'http://ely.by/u1', + 'email' => 'let-me-register@ely.by', + 'isActive' => true, + 'passwordChangedAt' => time(), + 'hasMojangUsernameCollision' => false, + 'shouldAcceptRules' => false, + ]; + } + + return null; + } + + public function beforeAccountEmailVerification(Request $request): ?array { + $httpAuth = $request->getHeaders()->get('authorization'); + if ($httpAuth === 'Bearer dummy_token') { + return [ + 'success' => true, + ]; + } + + return null; + } + + public function beforeAccountNewEmailVerification(Request $request): ?array { + $key = $request->post('key'); + if ($key === 'LETMEIN') { + return [ + 'success' => true, + ]; + } + + return null; + } + + public function beforeAccountChangeEmail(Request $request): ?array { + $key = $request->post('key'); + if ($key === 'LETMEIN') { + return [ + 'success' => true, + 'email' => 'brand-new-email@ely.by', + ]; + } + + return null; + } + +} diff --git a/api/config/config.php b/api/config/config.php index 8969ef5..c38ea0c 100644 --- a/api/config/config.php +++ b/api/config/config.php @@ -87,4 +87,5 @@ return [ 'internal' => api\modules\internal\Module::class, 'accounts' => api\modules\accounts\Module::class, ], + 'on beforeAction' => api\components\TestData::getInstance(), ]; From c855cdb3940f71dfbd23e27226317117dfee0537 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Fri, 13 Oct 2017 18:43:24 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=20=D0=BF=D0=B0?= =?UTF-8?q?=D1=80=D0=BE=D0=BB=D1=8F=20=D0=B4=D0=BB=D1=8F=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D1=81=D0=B0=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8?= =?UTF-8?q?=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20=D1=81=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20email?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/components/TestData.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/api/components/TestData.php b/api/components/TestData.php index 37fcc4b..ac4b417 100644 --- a/api/components/TestData.php +++ b/api/components/TestData.php @@ -130,6 +130,16 @@ class TestData { public function beforeAccountEmailVerification(Request $request): ?array { $httpAuth = $request->getHeaders()->get('authorization'); if ($httpAuth === 'Bearer dummy_token') { + $password = $request->post('password'); + if (empty($password)) { + return [ + 'success' => false, + 'errors' => [ + 'password' => 'error.password_required', + ], + ]; + } + return [ 'success' => true, ];