Skip to content

Commit

Permalink
MDL-78337 tool_brickfield: Attempt immediate validation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlkin committed Aug 14, 2024
1 parent fcf23db commit 5f69794
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
13 changes: 12 additions & 1 deletion admin/tool/brickfield/classes/brickfieldconnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require_once("{$CFG->libdir}/filelib.php");

use curl;
use ErrorException;
use moodle_url;

/**
Expand Down Expand Up @@ -122,7 +123,12 @@ protected function get_url_for_command(array $command): string {
protected function call(array $command, $params = ''): string {
$url = $this->get_url_for_command($command);
if ($command['method'] === 'get') {
return $this->get($url, $params);
try {
return $this->get($url, $params);
} catch (ErrorException $e) {
// URL blocked or otherwise inaccessible.
return '';
}
}

if ($command['method'] === 'post') {
Expand Down Expand Up @@ -210,6 +216,11 @@ protected function get_registration_id_for_credentials(string $apikey = null, st
$this->set_headers($headers);
$response = $this->call(self::ACTION_CHECK_REGISTRATION);

if (empty($this->info['http_code'])) {
// The response was blocked.
return '';
}

if ((int)$this->info['http_code'] !== 200) {
// The response was unsuccessful.
return '';
Expand Down
7 changes: 7 additions & 0 deletions admin/tool/brickfield/classes/registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ public function set_keys_for_registration(string $apikey, string $secretkey): bo
$this->set_api_key($apikey);
$this->set_secret_key($secretkey);
$this->set_not_validated();
// Attempt to validate the registration.
$this->validate();
if (!$this->status_is_validated()) {
// If the validation failed here, start the grace period.
$this->set_not_validated();
$this->set_summary_time();
}
if ($this->summarytime <= 0) {
$this->set_summary_time();
}
Expand Down
3 changes: 2 additions & 1 deletion admin/tool/brickfield/tests/registration_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public function test_set_keys_for_registration() {
// State when valid format keys are sent.
$this->assertTrue($regobj->set_keys_for_registration(mock_brickfieldconnect::VALIDAPIKEY,
mock_brickfieldconnect::VALIDSECRETKEY));
$this->assertTrue($regobj->validation_pending());
$this->assertTrue($regobj->toolkit_is_active());
$this->assertFalse($regobj->validation_pending());
$this->assertEquals($regobj->get_api_key(), mock_brickfieldconnect::VALIDAPIKEY);
$this->assertEquals($regobj->get_secret_key(), mock_brickfieldconnect::VALIDSECRETKEY);
}
Expand Down

0 comments on commit 5f69794

Please sign in to comment.