Skip to content

Commit

Permalink
Add tests to the Connection package to increase coverage. (#39963)
Browse files Browse the repository at this point in the history
* Migrated PHPUNIT XML file to new schema.

* Migrated schema using phpunit.

* Added test coverage for the Manager signature verification method.

* Minor lint edit.

* Changelog.

* Added an is_null check to make Phan happy.

* Second time's the charm - checking the error object correctly.

---------

Co-authored-by: tbradsha <32492176+tbradsha@users.noreply.github.com>

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/12550233129

Upstream-Ref: Automattic/jetpack@59b0e8d
  • Loading branch information
tbradsha authored and matticbot committed Dec 30, 2024
1 parent 60de7bd commit 9ff3f77
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 15 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [6.2.2-alpha] - unreleased

This is an alpha version! The changes listed here are not final.

### Added
- Added tests to increase code coverage.

## [6.2.1] - 2024-12-16
### Changed
- Updated package dependencies. [#40564]
Expand Down Expand Up @@ -1271,6 +1278,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Separate the connection library into its own package.

[6.2.2-alpha]: https://github.com/Automattic/jetpack-connection/compare/v6.2.1...v6.2.2-alpha
[6.2.1]: https://github.com/Automattic/jetpack-connection/compare/v6.2.0...v6.2.1
[6.2.0]: https://github.com/Automattic/jetpack-connection/compare/v6.1.1...v6.2.0
[6.1.1]: https://github.com/Automattic/jetpack-connection/compare/v6.1.0...v6.1.1
Expand Down
25 changes: 13 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<phpunit bootstrap="tests/php/bootstrap.php" backupGlobals="false" colors="true" convertDeprecationsToExceptions="true">
<testsuites>
<testsuite name="general">
<directory prefix="test" suffix=".php">tests/php</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">src</directory>
<directory suffix=".php">legacy</directory>
</whitelist>
</filter>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/php/bootstrap.php" backupGlobals="false" colors="true" convertDeprecationsToExceptions="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
<directory suffix=".php">legacy</directory>
</include>
</coverage>
<testsuites>
<testsuite name="general">
<directory prefix="test" suffix=".php">tests/php</directory>
</testsuite>
</testsuites>
</phpunit>
5 changes: 3 additions & 2 deletions src/class-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,9 @@ private function internal_verify_xml_rpc_signature() {

if (
empty( $token_key )
||
empty( $version ) || (string) $jetpack_api_version !== $version ) {
|| empty( $version )
|| (string) $jetpack_api_version !== $version
) {
return new \WP_Error( 'malformed_token', 'Malformed token in request', compact( 'signature_details', 'error_type' ) );
}

Expand Down
2 changes: 1 addition & 1 deletion src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Package_Version {

const PACKAGE_VERSION = '6.2.1';
const PACKAGE_VERSION = '6.2.2-alpha';

const PACKAGE_SLUG = 'connection';

Expand Down
74 changes: 74 additions & 0 deletions tests/php/test_Manager_unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,4 +716,78 @@ public function test_is_ready_for_cleanup_yes() {

$this->assertTrue( $is_ready );
}

/**
* Test the case when no token nor signature are set in GET parameters.
*
* @return void
*/
public function test_verify_xml_rpc_signature_returns_false_no_signature() {
unset( $_GET['token'] );
unset( $_GET['signature'] );
$this->assertFalse( $this->manager->verify_xml_rpc_signature() );
}

/**
* Test the case when a token lookup results in an error.
*
* @return void
*/
public function test_verify_xml_rpc_signature_token_lookup_error() {
$_GET['token'] = 'abcde:1:0';
$_GET['signature'] = 'bogus signature';
Constants::set_constant( 'JETPACK__API_VERSION', 1 );

$access_token = new WP_Error( 'test_error' );
$this->tokens->expects( $this->once() )
->method( 'get_access_token' )
->willReturn( $access_token );

$error = null;
add_action(
'jetpack_verify_signature_error',
function ( $e ) use ( &$error ) {
$error = $e;
}
);

$this->assertFalse( $this->manager->verify_xml_rpc_signature() );
$this->assertSame( $access_token, $error );
}

public function signature_data_provider() {
return array(
array( 'abcde:1:aaa', 'bogus signature', 'malformed_user_id' ),
array( 'bogus token', 'bogus signature', 'malformed_token' ),
array( 'abcde:1:987', 'bogus signature', 'unknown_user' ),
array( 'abcde:1:0', 'bogus signature', 'unknown_token' ),
);
}
/**
* Test the case where internal verification function encounters malformed data.
*
* @dataProvider signature_data_provider
* @param String $token auth token.
* @param String $signature auth signature.
* @param String $error_code the returned error code.
* @return void
*/
public function test_verify_xml_rpc_signature_malformed_user_id( $token, $signature, $error_code ) {
Constants::set_constant( 'JETPACK__API_VERSION', 1 );

$_GET['token'] = $token;
$_GET['signature'] = $signature;

$error = null;
add_action(
'jetpack_verify_signature_error',
function ( $e ) use ( &$error ) {
$error = $e;
}
);

$this->assertFalse( $this->manager->verify_xml_rpc_signature() );
$this->assertNotNull( $error );
$this->assertEquals( $error_code, $error->get_error_code() );
}
}

0 comments on commit 9ff3f77

Please sign in to comment.