Skip to content

Commit

Permalink
Merge pull request 1EdTech#65 from packbackbooks/podb-612-replace-reg…
Browse files Browse the repository at this point in the history
…-error

PODB-612: Replace registration error with dynamic function.
  • Loading branch information
lin-brian-l authored Jul 28, 2022
2 parents 18c31d6 + c37baab commit 5ea5cd7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/LtiOidcLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class LtiOidcLogin
public const ERROR_MSG_LAUNCH_URL = 'No launch URL configured';
public const ERROR_MSG_ISSUER = 'Could not find issuer';
public const ERROR_MSG_LOGIN_HINT = 'Could not find login hint';
public const ERROR_MSG_REGISTRATION = 'Could not find registration details';

private $db;
private $cache;
Expand Down Expand Up @@ -113,11 +112,14 @@ public function validateOidcLogin($request)
}

// Fetch Registration Details.
$registration = $this->db->findRegistrationByIssuer($request['iss'], $request['client_id'] ?? null);
$clientId = $request['client_id'] ?? null;
$registration = $this->db->findRegistrationByIssuer($request['iss'], $clientId);

// Check we got something.
if (empty($registration)) {
throw new OidcException(static::ERROR_MSG_REGISTRATION, 1);
$errorMsg = LtiMessageLaunch::getMissingRegistrationErrorMsg($request['iss'], $clientId);

throw new OidcException($errorMsg, 1);
}

// Return Registration.
Expand Down
13 changes: 12 additions & 1 deletion tests/LtiOidcLoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Packback\Lti1p3\Interfaces\ICache;
use Packback\Lti1p3\Interfaces\ICookie;
use Packback\Lti1p3\Interfaces\IDatabase;
use Packback\Lti1p3\LtiMessageLaunch;
use Packback\Lti1p3\LtiOidcLogin;
use Packback\Lti1p3\OidcException;

Expand Down Expand Up @@ -84,6 +85,10 @@ public function testValidatesFailsIfLoginHintIsNotSet()
$this->oidcLogin->validateOidcLogin($request);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testValidatesFailsIfRegistrationNotFound()
{
$request = [
Expand All @@ -93,8 +98,14 @@ public function testValidatesFailsIfRegistrationNotFound()
$this->database->shouldReceive('findRegistrationByIssuer')
->once()->andReturn(null);

// Use an alias to mock LtiMessageLaunch::getMissingRegistrationErrorMsg()
$expectedError = 'Registration not found!';
Mockery::mock('alias:'.LtiMessageLaunch::class)
->shouldReceive('getMissingRegistrationErrorMsg')
->andReturn($expectedError);

$this->expectException(OidcException::class);
$this->expectExceptionMessage(LtiOidcLogin::ERROR_MSG_REGISTRATION);
$this->expectExceptionMessage($expectedError);

$this->oidcLogin->validateOidcLogin($request);
}
Expand Down

0 comments on commit 5ea5cd7

Please sign in to comment.