Skip to content

Commit

Permalink
Failing tests for revised set_bearer functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Mar 15, 2019
1 parent 56f9c23 commit 23975e1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 22 deletions.
53 changes: 52 additions & 1 deletion tests/testApiChangeEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
class TestApiChangeEmail extends WP_Auth0_Test_Case {

use httpHelpers {
use HttpHelpers {
httpMock as protected httpMockDefault;
}

Expand All @@ -32,6 +32,15 @@ public static function setUpBeforeClass() {
self::$api_client_creds = new WP_Auth0_Api_Client_Credentials( self::$opts );
}

/**
* Runs after each test completes.
*/
public function tearDown() {
parent::tearDown();
delete_transient( 'auth0_api_token' );
delete_transient( 'auth0_api_token_scope' );
}

/**
* Test that empty parameters will return false.
*/
Expand Down Expand Up @@ -129,6 +138,48 @@ public function testThatSuccessfulApiCallReturnsTrue() {
$this->assertEmpty( self::$error_log->get() );
}

/**
* Test that the bearer setting succeeds if there is a token stored with the correct scope.
*/
public function testThatSetBearerSucceedsWithStoredToken() {
$this->startHttpMocking();

set_transient( 'auth0_api_token', uniqid() );
set_transient( 'auth0_api_token_scope', 'update:users' );

$this->http_request_type = 'wp_error';
$api = new WP_Auth0_Api_Change_Email( self::$opts, self::$api_client_creds );
$this->assertTrue( $api->call( uniqid(), uniqid() ) );
}

/**
* Test that set bearer fails if there is no stored token and a CC grant fails.
*/
public function testThatSetBearerFailsWhenCannotGetToken() {
$this->startHttpMocking();

$this->http_request_type = 'wp_error';
$api = new WP_Auth0_Api_Change_Email( self::$opts, self::$api_client_creds );
$this->assertFalse( $api->call( uniqid(), uniqid() ) );

$this->assertFalse( get_transient( 'auth0_api_token' ) );
$this->assertFalse( get_transient( 'auth0_api_token_scope' ) );
}

/**
* Test that set bearer succeeds if a token with the correct scope can be retrieved.
*/
public function testThatSetBearerSucceedsWhenCanGetToken() {
$this->startHttpMocking();

$this->http_request_type = 'success_access_token';
$api = new WP_Auth0_Api_Change_Email( self::$opts, self::$api_client_creds );
$this->assertFalse( $api->call( uniqid(), uniqid() ) );

$this->assertEquals( '__test_access_token__', get_transient( 'auth0_api_token' ) );
$this->assertEquals( 'test:scope', get_transient( 'auth0_api_token_scope' ) );
}

/*
* Test helper functions.
*/
Expand Down
24 changes: 3 additions & 21 deletions tests/testApiClientCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
*/
class TestApiClientCredentials extends WP_Auth0_Test_Case {

use HttpHelpers {
httpMock as protected httpMockDefault;
}
use HttpHelpers;

/**
* Run after each test.
Expand Down Expand Up @@ -114,30 +112,14 @@ public function testThatSuccessfulCallStoresTokenAndScope() {
$this->startHttpMocking();
$api_client_creds = new WP_Auth0_Api_Client_Credentials( self::$opts );

$this->http_request_type = 'access_token';
$this->http_request_type = 'success_access_token';
$timeout = time() + 1000;
$this->assertEquals( '__test_access_token__', $api_client_creds->call() );
$this->assertEquals( '__test_access_token__', get_transient( 'auth0_api_token' ) );
$this->assertEquals( 'test:scope', get_transient( 'auth0_api_token_scope' ) );
$this->assertEquals( 'update:users', get_transient( 'auth0_api_token_scope' ) );
$this->assertLessThan( $timeout, (int) get_transient( '_transient_timeout_auth0_api_token_scope' ) );
$this->assertLessThan( $timeout, (int) get_transient( '_transient_timeout_auth0_api_token' ) );
$log = self::$error_log->get();
$this->assertCount( 0, $log );
}

/**
* Specific mock API responses for this suite.
*
* @return array|null|WP_Error
*/
public function httpMock() {
switch ( $this->getResponseType() ) {
case 'access_token':
return [
'body' => '{"access_token":"__test_access_token__","scope":"test:scope","expires_in":1000}',
'response' => [ 'code' => 200 ],
];
}
return $this->httpMockDefault();
}
}
8 changes: 8 additions & 0 deletions tests/traits/httpHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public function getResponseType() {
* @param string|null $url - Remote URL.
*
* @return array|WP_Error
*
* @throws Exception - If set to halt on response.
*/
public function httpMock( $response_type = null, array $args = null, $url = null ) {
switch ( $response_type ?: $this->getResponseType() ) {
Expand Down Expand Up @@ -149,6 +151,12 @@ public function httpMock( $response_type = null, array $args = null, $url = null
'response' => [ 'code' => 200 ],
];

case 'success_access_token':
return [
'body' => '{"access_token":"__test_access_token__","scope":"update:users","expires_in":1000}',
'response' => [ 'code' => 200 ],
];

default:
return new WP_Error( 2, 'No mock type found.' );
}
Expand Down

0 comments on commit 23975e1

Please sign in to comment.