Skip to content

Commit

Permalink
Merge pull request #18 from dovuofficial/feature/add-policy-publish-task
Browse files Browse the repository at this point in the history
Add policy publish to SDK
  • Loading branch information
mattsmithies authored Nov 27, 2024
2 parents 91e0854 + ed2370f commit 9dd1572
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/Domain/CredentialDocumentBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function __construct(object $block_data, EntityStatus $entityStatus = nul
$this->credential_subject = $this->checkForOptionalSubject();
}

private function checkForOptionalSubject() : null|object {
private function checkForOptionalSubject(): null|object
{
return array_key_exists('document', $this->block_data) ?
(object) $this->block_data['document']['credentialSubject']['0'] :
null;
Expand Down
5 changes: 2 additions & 3 deletions src/Domain/GuardianToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ public static function none(): self
{
return new self((object) [
"adminId" => null,
"tokenId" => ""
"tokenId" => "",
]);
}


/**
* When "adminId" from parent obj is falsely then ignore "id()" fn in client.
*
Expand All @@ -26,7 +25,7 @@ public static function none(): self
*/
public function isTokenPublished(): bool
{
return !! $this->token->adminId;
return ! ! $this->token->adminId;
}

public function id(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/TrustchainQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ public function withResult(?CredentialDocumentBlock $result): self

public function hasTrustchainResult(): bool
{
return !! $this->result;
return ! ! $this->result;
}
}
18 changes: 17 additions & 1 deletion src/Service/PolicyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Dovu\GuardianPhpSdk\Service;

use Dovu\GuardianPhpSdk\Domain\GuardianToken;
use Exception;
use Dovu\GuardianPhpSdk\Domain\TaskInstance;

class PolicyService extends AbstractService
{
Expand All @@ -17,6 +17,22 @@ public function get($id): object
return (object) $this->httpClient->get("policies/{$id}")->data();
}

public function publish($id, $version): object
{
return (object) $this->httpClient->put("policies/push/{$id}/publish", [
'policyVersion' => $version,
])->data();
}

public function publishSync(callable $callback, string $id, string $version): TaskInstance
{
$callback($id, $version);

$import = TaskInstance::from($this->publish($id, $version));

return $this->runnerTask($import, $callback);
}

public function assign(string $username, string $policy_id, bool $assign = true): object
{
$payload = [
Expand Down
7 changes: 4 additions & 3 deletions src/Support/PolicyMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Dovu\GuardianPhpSdk\Support;

use Dovu\GuardianPhpSdk\Domain\TaskInstance;

enum PolicyStatus: string
{
case DRAFT = 'DRAFT';
Expand All @@ -23,10 +25,9 @@ public static function context(PolicyContext $context): self
return new self($context);
}

// TODO: Implement later to publish
public function publish()
public function publishVersion(callable $callback, string $version): TaskInstance
{

return $this->context->policies->publishSync($callback, $this->context->policyId, $version);
}

public function dryRun()
Expand Down
48 changes: 34 additions & 14 deletions tests/ResearchElvClientGuardianTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Dovu\GuardianPhpSdk\Constants\GuardianApprovalOption;
use Dovu\GuardianPhpSdk\Constants\GuardianRole;
use Dovu\GuardianPhpSdk\Domain\PolicySchemaDocument;
use Dovu\GuardianPhpSdk\Domain\Trustchain;
use Dovu\GuardianPhpSdk\Domain\TrustchainQuery;
use Dovu\GuardianPhpSdk\DovuGuardianAPI;
use Dovu\GuardianPhpSdk\Support\DryRunScenario;
Expand Down Expand Up @@ -116,7 +115,7 @@
describe('Functional Guardian Test', function () {
beforeEach(function () {
$this->sdk = new DovuGuardianAPI();
// $this->sdk->setGuardianBaseUrl("http://localhost:3000/api/v1/");
// $this->sdk->setGuardianBaseUrl("http://localhost:3000/api/v1/");


$config = EnvConfig::instance();
Expand All @@ -125,11 +124,6 @@

$policy_id = $config->get(Env::POLICY_ID);

// TODO: Remove. mmcm elv
// $policy_id = "66b5e1e76efceb6b593efa73";
// $policy_id = "66c616cc6efceb6b593f08de";
// $policy_id = $config->testLocalPolicy();

$context = PolicyContext::using($this->sdk)->for($policy_id);

$this->helper = new GuardianSDKHelper($this->sdk, $policy_id);
Expand Down Expand Up @@ -214,6 +208,28 @@

})->skip();

it('A policy can be published', function () {

$this->helper->authenticateAsRegistry();

expect($this->policy_mode->hasPolicyStatus(PolicyStatus::DRAFT)
|| $this->policy_mode->hasPolicyStatus(PolicyStatus::DRY_RUN))->toBeTruthy();


$publish_fn = function ($state) {
ray("Publish progress");
ray($state);

expect($state)->toBeTruthy();
};


$publish = $this->policy_mode->publishVersion($publish_fn, '1');

expect($publish->result['isValid'])->toBeTrue();

})->skip("Publish testing for local dev, currently.");

it('A policy can create users and view them in dry run', function () {

$this->helper->authenticateAsRegistry();
Expand Down Expand Up @@ -514,7 +530,7 @@
expect($response->hasTrustchainResult())->toBeTruthy();

// TODO: WORK IN PROGRESS
// ray($trustchain->format());
// ray($trustchain->format());
})->skip();

it('An admin can get the token for a policy', function () {
Expand All @@ -527,6 +543,8 @@

$token = $this->sdk->policies->token($policy_id);

ray($token);

expect($token->hasValidToken())->toBeBool();
expect($token->id())->toBeTruthy();

Expand All @@ -537,7 +555,7 @@
expect($token->hasValidToken())->toBeTruthy();
expect($token->id())->toBeFalsy();

});
})->skip();

it('Fetch a specification of a schema for workflow import and validation', function () {

Expand Down Expand Up @@ -610,9 +628,11 @@
expect($timestamp)->toBeTruthy();

// When something happens, complete
$status_update_callable = function ($state) {
ray("Status update");
ray($state);
$status_update_callable = function ($state) use ($registry_user, $password) {

$this->helper->authenticateAsRegistry($registry_user, $password);

ray("Status Import Update");

if ($state->result) {
ray("done");
Expand Down Expand Up @@ -655,7 +675,7 @@
* 3. Update the context objects with the correct uploaded "registry" user and imported policy id.
*/

// $this->policy_mode->dryRun();
// $this->policy_mode->dryRun();

/**
* Create mediator object.
Expand Down Expand Up @@ -793,7 +813,7 @@
->setApprovalOption(ApprovalOption::APPROVE)
->setFilterValue($claim['uuid'])
->run();

ray('$approve_claim');
ray($result);

Expand Down

0 comments on commit 9dd1572

Please sign in to comment.