Skip to content

Commit

Permalink
test: simplify allowRedirectAfterSsoLogin method calls in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbrink committed Jan 17, 2025
1 parent b58d0cd commit ac3eaf5
Showing 1 changed file with 47 additions and 22 deletions.
69 changes: 47 additions & 22 deletions library/Integrations/MiniOrange/AllowRedirectAfterSsoLogin.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public function testAllowRedirectAfterSsoLoginDoesNothingIfNotDoingMiniOrangeLog
$allowRedirectAfterSsoLogin = new AllowRedirectAfterSsoLogin($wpService);

$_POST = [];
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin('', 0, 0, 1);
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin(1);

$_POST = ['SAMLResponse' => 'foo'];
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin('', 0, 0, 1);
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin(1);

$_POST = ['RelayState' => 'bar'];
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin('', 0, 0, 1);
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin(1);

$this->assertArrayNotHasKey('applyFilters', $wpService->methodCalls);
}
Expand All @@ -63,51 +63,76 @@ public function testAllowRedirectAfterSsoLoginAppliesFilterToRelayStateIfDoingMi
$allowRedirectAfterSsoLogin = new AllowRedirectAfterSsoLogin($wpService);

$_POST = ['SAMLResponse' => 'foo', 'RelayState' => 'bar'];
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin('', 0, 0, 1);
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin(1);

$this->assertEquals($allowRedirectAfterSsoLogin::REDIRECT_URL_FILTER_HOOK, $wpService->methodCalls['applyFilters'][0][0]);
$this->assertEquals('', $wpService->methodCalls['applyFilters'][0][1]);
}

/**
* @testdox allowRedirectAfterSsoLogin() redirects if filter returns a none empty string
* @testdox allowRedirectAfterSsoLogin() does not attempt to redirect if redirection flag is already set
*/
public function testAllowRedirectAfterSsoLoginRedirectsIfFilterReturnsAValidUrl()
public function testAllowRedirectAfterSsoLoginDoesNotAttemptToRedirectIfRedirectionFlagIsAlreadySet()
{
$wpService = new FakeWpService(['addAction' => true, 'applyFilters' => 'http://example.com', 'wpSafeRedirect' => true]);
$wpService = new FakeWpService([
'addAction' => true,
'applyFilters' => 'http://example.com',
'addFilter' => true,
'wpSafeRedirect' => true]);

$allowRedirectAfterSsoLogin = new AllowRedirectAfterSsoLogin($wpService);

$_POST = ['SAMLResponse' => 'foo', 'RelayState' => 'bar'];
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin('', 0, 0, 1);
$_POST = ['SAMLResponse' => 'foo', 'RelayState' => 'bar', 'customMiniOrgangeLoginRedirectApplied' => true];
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin(1);

$this->assertEquals('http://example.com', $wpService->methodCalls['wpSafeRedirect'][0][0]);
$this->assertArrayNotHasKey('wpSafeRedirect', $wpService->methodCalls);
}

/**
* @testdox allowRedirectAfterSsoLogin() sets a flag to indicate that the redirect has been applied
* @testdox allowRedirectAfterSsoLogin() redirect handler is applied to wp_redirect filter
*/
public function testAllowRedirectAfterSsoLoginSetsAFlagToIndicateThatTheRedirectHasBeenApplied()
public function testAllowRedirectAfterSsoLoginRedirectsToUrlIfFilterIsSet()
{
$wpService = new FakeWpService(['addAction' => true, 'applyFilters' => 'http://example.com', 'wpSafeRedirect' => true]);
$redirectTo = 'http://example.com';
$relayStateUrl = 'http://relayStateUrl.com';

$wpService = new FakeWpService([
'addAction' => true,
'applyFilters' => $redirectTo,
'addFilter' => true,
'wpSafeRedirect' => true]);

$allowRedirectAfterSsoLogin = new AllowRedirectAfterSsoLogin($wpService);

$_POST = ['SAMLResponse' => 'foo', 'RelayState' => 'bar'];
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin('', 0, 0, 1);
$_POST = ['SAMLResponse' => 'foo', 'RelayState' => $relayStateUrl];
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin(1);
$redirectHandlerResult = $wpService->methodCalls['addFilter'][0][1]($relayStateUrl);

$this->assertTrue($_POST[$allowRedirectAfterSsoLogin::APPLIED_FLAG]);
$this->assertEquals('wp_redirect', $wpService->methodCalls['addFilter'][0][0]);
$this->assertEquals($redirectTo, $redirectHandlerResult);
}

/**
* @testdox allowRedirectAfterSsoLogin() does not attempt to redirect if redirection flag is already set
* @testdox allowRedirectAfterSsoLogin() does not redirect if RelayState does not match ongoing redirect
*/
public function testAllowRedirectAfterSsoLoginDoesNotAttemptToRedirectIfRedirectionFlagIsAlreadySet()
public function testAllowRedirectAfterSsoLoginDoesNotRedirectIfRelayStateDoesNotMatchOngoingRedirect()
{
$wpService = new FakeWpService(['addAction' => true, 'applyFilters' => 'http://example.com', 'wpSafeRedirect' => true]);
$redirectTo = 'http://example.com';
$relayStateUrl = 'http://relayStateUrl.com';

$wpService = new FakeWpService([
'addAction' => true,
'applyFilters' => $redirectTo,
'addFilter' => true,
'wpSafeRedirect' => true]);

$allowRedirectAfterSsoLogin = new AllowRedirectAfterSsoLogin($wpService);

$_POST = ['SAMLResponse' => 'foo', 'RelayState' => 'bar', 'customMiniOrgangeLoginRedirectApplied' => true];
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin('', 0, 0, 1);
$_POST = ['SAMLResponse' => 'foo', 'RelayState' => $relayStateUrl];
$allowRedirectAfterSsoLogin->allowRedirectAfterSsoLogin(1);
$redirectHandlerResult = $wpService->methodCalls['addFilter'][0][1]("http://urlNotMatchingRelayStateUrl.com");

$this->assertArrayNotHasKey('wpSafeRedirect', $wpService->methodCalls);
$this->assertEquals('wp_redirect', $wpService->methodCalls['addFilter'][0][0]);
$this->assertEquals('http://urlNotMatchingRelayStateUrl.com', $redirectHandlerResult);
}
}

0 comments on commit ac3eaf5

Please sign in to comment.