Skip to content

Commit

Permalink
Tests: Delete redundant test, add missing one.
Browse files Browse the repository at this point in the history
New tests were added for `rest_delete_totp` in #504 which cover the same functionality.

`generate_qr_code_url` was also refactored there, but a test wasn't included.
  • Loading branch information
iandunn committed Feb 8, 2023
1 parent be5c0a9 commit 695a61a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
/npm-debug.log
/vendor/
/dist/
/tests/.phpunit.result.cache
/tests/logs/
/wordpress/
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
cacheResultFile="./tests/.phpunit.result.cache"
>
<php>
<env name="WP_PHPUNIT__TESTS_CONFIG" value="tests/wp-config.php" />
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/class-two-factor-backup-codes-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function test_cannot_generate_code_for_different_user() {
/**
* Verify that an admin can create Backup codes for another user.
*
* @covers Two_Factor_Backup_Codes::ajax_generate_json
* @covers Two_Factor_Backup_Codes::rest_generate_codes
*/
public function test_generate_codes_for_other_users() {
wp_set_current_user( self::$admin_id );
Expand Down
41 changes: 13 additions & 28 deletions tests/providers/class-two-factor-totp.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ public function test_user_two_factor_options_generates_key() {
$this->assertStringContainsString( __( 'Authentication Code:', 'two-factor' ), $content );
}

/**
* Verify QR code URL generation.
*
* @covers Two_Factor_Totp::generate_qr_code_url
*/
public function test_generate_qr_code_url() {
$user = new WP_User( self::factory()->user->create() );
$expected = 'otpauth://totp/Test%20Blog%3A'. rawurlencode( $user->user_login ) .'?secret=my%20secret%20key&#038;issuer=Test%20Blog';
$actual = $this->provider->generate_qr_code_url( $user, 'my secret key' );

$this->assertSame( $expected, $actual );
}

/**
* Verify base32 encoding.
*
Expand Down Expand Up @@ -195,32 +208,4 @@ public function test_is_valid_key() {
$this->assertFalse( $this->provider->is_valid_key( 'abc233' ), 'Lowercase chars are invalid' );
$this->assertFalse( $this->provider->is_valid_key( 'has a space' ), 'Spaces not allowed' );
}

/**
* Verify secret deletion.
*
* @covers Two_Factor_Totp::user_two_factor_options_update
*/
public function test_user_can_delete_secret() {
$user = new WP_User( self::factory()->user->create() );
$key = $this->provider->generate_key();

// Configure secret for the user.
$this->provider->set_user_totp_key( $user->ID, $key );

$this->assertEquals(
$key,
$this->provider->get_user_totp_key( $user->ID ),
'Secret was stored and can be fetched'
);

$this->provider->delete_user_totp_key( $user->ID );

$this->assertEquals(
'',
$this->provider->get_user_totp_key( $user->ID ),
'Secret has been deleted'
);
}

}

0 comments on commit 695a61a

Please sign in to comment.