From 1374097b9a90c1d699dc0dd28dd5747b80fbb609 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Wed, 18 Dec 2019 21:08:32 -0800 Subject: [PATCH] Move WP_Auth0_InitialSetup::init() actions to functions; clean up notices --- WP_Auth0.php | 52 ++++++++- composer.json | 3 +- lib/initial-setup/WP_Auth0_InitialSetup.php | 82 ++++--------- .../WP_Auth0_InitialSetup_Consent.php | 4 +- .../initial-setup/connection_profile.php | 2 +- tests/testErrorLog.php | 3 + tests/testInitialSetup.php | 108 ++++++++++++++++++ tests/testInitialSetupConsent.php | 4 +- 8 files changed, 189 insertions(+), 69 deletions(-) create mode 100644 tests/testInitialSetup.php diff --git a/WP_Auth0.php b/WP_Auth0.php index 2db491fd..23bb25d1 100644 --- a/WP_Auth0.php +++ b/WP_Auth0.php @@ -123,9 +123,6 @@ public function init() { add_filter( 'plugin_action_links_' . $this->basename, [ $this, 'wp_add_plugin_settings_link' ] ); - $initial_setup = new WP_Auth0_InitialSetup( $this->a0_options ); - $initial_setup->init(); - $this->router = new WP_Auth0_Routes( $this->a0_options ); } @@ -477,6 +474,55 @@ function wp_auth0_db_check_update() { * Core WP hooks */ +function wp_auth0_setup_error_admin_notices() { + if ( empty( $_REQUEST['error'] ) ) { + return false; + } + + $initial_setup = new WP_Auth0_InitialSetup( WP_Auth0_Options::Instance() ); + + switch ( $_REQUEST['error'] ) { + + case 'cant_create_client': + $initial_setup->cant_create_client_message(); + break; + + case 'cant_create_client_grant': + $initial_setup->cant_create_client_grant_message(); + break; + + case 'cant_exchange_token': + $initial_setup->cant_exchange_token_message(); + break; + + case 'rejected': + $initial_setup->rejected_message(); + break; + + case 'access_denied': + $initial_setup->access_denied_message(); + break; + + default: + $initial_setup->notify_error(); + } + + return true; +} +add_action( 'admin_notices', 'wp_auth0_setup_error_admin_notices' ); + +function wp_auth0_setup_callback_step1() { + $setup_conn = new WP_Auth0_InitialSetup_ConnectionProfile( WP_Auth0_Options::Instance() ); + $setup_conn->callback(); +} +add_action( 'admin_action_wpauth0_callback_step1', 'wp_auth0_setup_callback_step1' ); + +function wp_auth0_setup_callback_step3_social() { + $setup_admin = new WP_Auth0_InitialSetup_AdminUser( WP_Auth0_Options::Instance() ); + $setup_admin->callback(); +} +add_action( 'admin_action_wpauth0_callback_step3_social', 'wp_auth0_setup_callback_step3_social' ); + /** * Function to call the method that clears out the error log. * diff --git a/composer.json b/composer.json index 375381bf..8e7311c0 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,8 @@ "phpcbf": "\"vendor/bin/phpcbf\"", "phpcbf-tests": "\"vendor/bin/phpcbf\" --standard=phpcs-test-ruleset.xml -s ./tests/", "sniffs": "\"vendor/bin/phpcs\" -e", - "test": "\"vendor/bin/phpunit\" --coverage-text", + "test": "\"vendor/bin/phpunit\"", + "test-cov": "\"vendor/bin/phpunit\" --coverage-text", "test-group": "\"vendor/bin/phpunit\" --coverage-text --group", "test-ci": "\"vendor/bin/phpunit\" --coverage-clover=coverage.xml", "pre-commit-no-tests": [ "@phpcbf", "@phpcbf-tests", "@phpcs-tests", "@compat", "@phpcs-i18n" ], diff --git a/lib/initial-setup/WP_Auth0_InitialSetup.php b/lib/initial-setup/WP_Auth0_InitialSetup.php index 4ba6e0b7..35703368 100644 --- a/lib/initial-setup/WP_Auth0_InitialSetup.php +++ b/lib/initial-setup/WP_Auth0_InitialSetup.php @@ -19,45 +19,8 @@ public function __construct( WP_Auth0_Options $a0_options ) { $this->end_step = new WP_Auth0_InitialSetup_End( $this->a0_options ); } - /** - * @deprecated - 3.10.0, will move add_action calls out of this class in the next major. - * - * @codeCoverageIgnore - Deprecated. - */ - public function init() { - - add_action( 'admin_action_wpauth0_callback_step1', [ $this->connection_profile, 'callback' ] ); - add_action( 'admin_action_wpauth0_callback_step3_social', [ $this->adminuser_step, 'callback' ] ); - - if ( isset( $_REQUEST['page'] ) && 'wpa0-setup' === $_REQUEST['page'] ) { - if ( isset( $_REQUEST['error'] ) ) { - add_action( 'admin_notices', [ $this, 'notify_error' ] ); - } - } - - if ( isset( $_REQUEST['error'] ) && 'cant_create_client' == $_REQUEST['error'] ) { - add_action( 'admin_notices', [ $this, 'cant_create_client_message' ] ); - } - - if ( isset( $_REQUEST['error'] ) && 'cant_create_client_grant' == $_REQUEST['error'] ) { - add_action( 'admin_notices', [ $this, 'cant_create_client_grant_message' ] ); - } - - if ( isset( $_REQUEST['error'] ) && 'cant_exchange_token' == $_REQUEST['error'] ) { - add_action( 'admin_notices', [ $this, 'cant_exchange_token_message' ] ); - } - - if ( isset( $_REQUEST['error'] ) && 'rejected' == $_REQUEST['error'] ) { - add_action( 'admin_notices', [ $this, 'rejected_message' ] ); - } - - if ( isset( $_REQUEST['error'] ) && 'access_denied' == $_REQUEST['error'] ) { - add_action( 'admin_notices', [ $this, 'access_denied' ] ); - } - } - public function notify_error() { - printf( '
%s
', strip_tags( $_REQUEST['error'] ) ); + printf( '

%s

', strip_tags( $_REQUEST['error'] ) ); } public function render_setup_page() { @@ -103,14 +66,13 @@ public function render_setup_page() { public function cant_create_client_message() { ?> -
+

- - - - - + + + + .

@@ -119,21 +81,21 @@ public function cant_create_client_message() { public function cant_create_client_grant_message() { ?> -
+

- + APIs > Auth0 Management API > Machine to Machine Applications tab and authorize this Application. ', 'wp-auth0' ); ?> - + , ', WP_Auth0_Api_Client::get_required_scopes() ); ?> - - - + + +

@@ -142,13 +104,13 @@ public function cant_create_client_grant_message() { public function cant_exchange_token_message() { ?> -
+

- - - - + + + + https://a0_options->get( 'domain' ); ?>

@@ -158,22 +120,22 @@ public function cant_exchange_token_message() { public function rejected_message() { ?> -
+

- +

- + https://manage.auth0.com

diff --git a/lib/initial-setup/WP_Auth0_InitialSetup_Consent.php b/lib/initial-setup/WP_Auth0_InitialSetup_Consent.php index f73145f9..363fc804 100644 --- a/lib/initial-setup/WP_Auth0_InitialSetup_Consent.php +++ b/lib/initial-setup/WP_Auth0_InitialSetup_Consent.php @@ -112,7 +112,7 @@ public function consent_callback( $name ) { $client_response = WP_Auth0_Api_Client::create_client( $domain, $this->access_token, $name ); if ( $client_response === false ) { - wp_redirect( admin_url( 'admin.php?page=wpa0&error=cant_create_client' ) ); + wp_redirect( admin_url( 'admin.php?page=wpa0-setup&error=cant_create_client' ) ); exit; } @@ -192,7 +192,7 @@ public function consent_callback( $name ) { $grant_response = WP_Auth0_Api_Client::create_client_grant( $this->access_token, $client_id ); if ( false === $grant_response ) { - wp_redirect( admin_url( 'admin.php?page=wpa0&error=cant_create_client_grant' ) ); + wp_redirect( admin_url( 'admin.php?page=wpa0-setup&error=cant_create_client_grant' ) ); exit; } diff --git a/templates/initial-setup/connection_profile.php b/templates/initial-setup/connection_profile.php index cf17c707..5cc757af 100644 --- a/templates/initial-setup/connection_profile.php +++ b/templates/initial-setup/connection_profile.php @@ -142,7 +142,7 @@

+ target="_blank">


diff --git a/tests/testErrorLog.php b/tests/testErrorLog.php index 4536c902..cf1b9a0f 100644 --- a/tests/testErrorLog.php +++ b/tests/testErrorLog.php @@ -17,6 +17,8 @@ class TestErrorLog extends WP_Auth0_Test_Case { use RedirectHelpers; + use UsersHelper; + use WpDieHelper; /** @@ -268,6 +270,7 @@ public function testThatNonAdminStopsProcess() { public function testThatErrorLogCanBeCleared() { $this->startRedirectHalting(); + $this->setGlobalUser(); $_POST['nonce'] = wp_create_nonce( 'clear_error_log' ); $error_log = new WP_Auth0_ErrorLog(); $error_log::insert_error( uniqid(), uniqid() ); diff --git a/tests/testInitialSetup.php b/tests/testInitialSetup.php new file mode 100644 index 00000000..cc8836d8 --- /dev/null +++ b/tests/testInitialSetup.php @@ -0,0 +1,108 @@ + [ + 'priority' => 10, + 'accepted_args' => 1, + ], + ]; + $this->assertHookedFunction( 'admin_action_wpauth0_callback_step3_social', $expect_hooked ); + + $expect_hooked = [ + 'wp_auth0_setup_callback_step1' => [ + 'priority' => 10, + 'accepted_args' => 1, + ], + ]; + $this->assertHookedFunction( 'admin_action_wpauth0_callback_step1', $expect_hooked ); + + $expect_hooked = [ + 'wp_auth0_setup_error_admin_notices' => [ + 'priority' => 10, + 'accepted_args' => 1, + ], + ]; + $this->assertHookedFunction( 'admin_notices', $expect_hooked ); + } + + public function testThatNoErrorReturnsFalseWithNoOutput() { + ob_start(); + $this->assertFalse( wp_auth0_setup_error_admin_notices() ); + $this->assertEmpty( ob_get_clean() ); + } + + public function testThatCantCreateClientHasCorrectNotice() { + $_REQUEST['error'] = 'cant_create_client'; + ob_start(); + $this->assertTrue( wp_auth0_setup_error_admin_notices() ); + $notice_html = ob_get_clean(); + + $this->assertContains( '
', $notice_html ); + $this->assertContains( 'There was an error creating the Auth0 App', $notice_html ); + } + + public function testThatCantCreateGrantHasCorrectNotice() { + $_REQUEST['error'] = 'cant_create_client_grant'; + ob_start(); + $this->assertTrue( wp_auth0_setup_error_admin_notices() ); + $notice_html = ob_get_clean(); + + $this->assertContains( '
', $notice_html ); + $this->assertContains( 'There was an error creating the necessary client grants', $notice_html ); + } + + public function testThatCantExchangeTokenHasCorrectNotice() { + $_REQUEST['error'] = 'cant_exchange_token'; + ob_start(); + $this->assertTrue( wp_auth0_setup_error_admin_notices() ); + $notice_html = ob_get_clean(); + + $this->assertContains( '
', $notice_html ); + $this->assertContains( 'There was an error retrieving your Auth0 credentials', $notice_html ); + } + + public function testThatRejectedHasCorrectNotice() { + $_REQUEST['error'] = 'rejected'; + ob_start(); + $this->assertTrue( wp_auth0_setup_error_admin_notices() ); + $notice_html = ob_get_clean(); + + $this->assertContains( '
', $notice_html ); + $this->assertContains( 'The required scoped were rejected', $notice_html ); + } + + public function testThatAccessDeniedHasCorrectNotice() { + $_REQUEST['error'] = 'access_denied'; + ob_start(); + $this->assertTrue( wp_auth0_setup_error_admin_notices() ); + $notice_html = ob_get_clean(); + + $this->assertContains( '
', $notice_html ); + $this->assertContains( 'Please create your Auth0 account first', $notice_html ); + } + + public function testThatUnknownErrorHasCorrectNotice() { + $_REQUEST['error'] = '__test_unknown_error__'; + ob_start(); + $this->assertTrue( wp_auth0_setup_error_admin_notices() ); + $notice_html = ob_get_clean(); + + $this->assertContains( '
', $notice_html ); + $this->assertContains( '__test_unknown_error__', $notice_html ); + } +} diff --git a/tests/testInitialSetupConsent.php b/tests/testInitialSetupConsent.php index 36a3cceb..ec911793 100644 --- a/tests/testInitialSetupConsent.php +++ b/tests/testInitialSetupConsent.php @@ -100,7 +100,7 @@ public function testThatClientCreationFailureIsRedirected() { $redirect_url = parse_url( $caught_redirect['location'] ); $this->assertEquals( '/wp-admin/admin.php', $redirect_url['path'] ); - $this->assertContains( 'page=wpa0', $redirect_url['query'] ); + $this->assertContains( 'page=wpa0-setup', $redirect_url['query'] ); $this->assertContains( 'error=cant_create_client', $redirect_url['query'] ); $this->assertCount( 1, self::$error_log->get() ); @@ -192,7 +192,7 @@ public function testThatNewConnectionIsCreatedAndFailedClientGrantRedirects() { $redirect_url = parse_url( $caught_redirect['location'] ); $this->assertEquals( '/wp-admin/admin.php', $redirect_url['path'] ); - $this->assertContains( 'page=wpa0', $redirect_url['query'] ); + $this->assertContains( 'page=wpa0-setup', $redirect_url['query'] ); $this->assertContains( 'error=cant_create_client_grant', $redirect_url['query'] ); $this->assertEquals( 'TEST_CLIENT_ID', self::$opts->get( 'client_id' ) );