Skip to content

Commit

Permalink
Improve tests (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
brecht-vermeersch authored Nov 17, 2024
1 parent a82f656 commit 004bc11
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Blob/BlobContainerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public function generateSasUri(BlobSasBuilder $blobSasBuilder): UriInterface
}

/**
* @codeCoverageIgnore
* @return \Generator<TaggedBlob>
*/
public function findBlobsByTag(string $tagFilterSqlExpression): \Generator
Expand Down
1 change: 1 addition & 0 deletions src/Blob/BlobServiceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function getBlobContainers(?string $prefix = null): \Generator


/**
* @codeCoverageIgnore
* @return \Generator<TaggedBlob>
*/
public function findBlobsByTag(string $tagFilterSqlExpression): \Generator
Expand Down
4 changes: 1 addition & 3 deletions src/Common/Middleware/AddXMsClientRequestIdMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ final class AddXMsClientRequestIdMiddleware
public function __invoke(callable $handler): \Closure
{
return function (RequestInterface $request, array $options) use ($handler) {
if ($request->hasHeader('x-ms-client-request-id')) {
$request = $request->withHeader('x-ms-version', \bin2hex(\random_bytes(16)));
}
$request = $request->withHeader('x-ms-client-request-id', \bin2hex(\random_bytes(16)));

return $handler($request, $options);
};
Expand Down
18 changes: 18 additions & 0 deletions tests/Blob/Feature/BlobContainerClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
use AzureOss\Storage\Blob\BlobServiceClient;
use AzureOss\Storage\Blob\Exceptions\ContainerAlreadyExistsException;
use AzureOss\Storage\Blob\Exceptions\ContainerNotFoundException;
use AzureOss\Storage\Blob\Exceptions\UnableToGenerateSasException;
use AzureOss\Storage\Blob\Models\Blob;
use AzureOss\Storage\Blob\Models\BlobPrefix;
use AzureOss\Storage\Blob\Models\GetBlobsOptions;
use AzureOss\Storage\Blob\Sas\BlobContainerSasPermissions;
use AzureOss\Storage\Blob\Sas\BlobSasBuilder;
use AzureOss\Storage\Common\ApiVersion;
use AzureOss\Storage\Common\Auth\StorageSharedKeyCredential;
use AzureOss\Storage\Common\Sas\SasIpRange;
use AzureOss\Storage\Tests\Blob\BlobFeatureTestCase;
use GuzzleHttp\Psr7\Uri;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -302,6 +305,9 @@ public function generate_sas_uri_works(): void
$sas = $this->containerClient->generateSasUri(
BlobSasBuilder::new()
->setPermissions(new BlobContainerSasPermissions(list: true))
->setVersion(ApiVersion::LATEST->value)
->setIPRange(new SasIpRange("0.0.0.0", "255.255.255.255"))
->setStartsOn(new \DateTime())
->setExpiresOn((new \DateTime())->modify("+ 1min")),
);

Expand All @@ -310,6 +316,18 @@ public function generate_sas_uri_works(): void
iterator_to_array($sasServiceClient->getBlobs());
}

#[Test]
public function generate_sas_uri_throws_when_there_are_no_shared_key_credentials(): void
{
$this->expectException(UnableToGenerateSasException::class);

$containerClientWithoutCredentials = new BlobContainerClient(new Uri("example.com"));

$containerClientWithoutCredentials->generateSasUri(
BlobSasBuilder::new(),
);
}

#[Test]
public function get_properties_works(): void
{
Expand Down
19 changes: 19 additions & 0 deletions tests/Blob/Feature/BlobServiceClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

use AzureOss\Storage\Blob\BlobServiceClient;
use AzureOss\Storage\Blob\Exceptions\InvalidConnectionStringException;
use AzureOss\Storage\Blob\Exceptions\UnableToGenerateSasException;
use AzureOss\Storage\Common\ApiVersion;
use AzureOss\Storage\Common\Sas\AccountSasBuilder;
use AzureOss\Storage\Common\Sas\AccountSasPermissions;
use AzureOss\Storage\Common\Sas\AccountSasResourceTypes;
use AzureOss\Storage\Common\Sas\SasIpRange;
use AzureOss\Storage\Tests\Blob\BlobFeatureTestCase;
use GuzzleHttp\Psr7\Uri;
use PHPUnit\Framework\Attributes\Test;

final class BlobServiceClientTest extends BlobFeatureTestCase
Expand Down Expand Up @@ -165,11 +169,26 @@ public function generate_account_sas_uri_works(): void
AccountSasBuilder::new()
->setPermissions(new AccountSasPermissions(list: true))
->setResourceTypes(new AccountSasResourceTypes(service: true))
->setIpRange(new SasIpRange("0.0.0.0", "255.255.255.255"))
->setVersion(ApiVersion::LATEST->value)
->setStartsOn((new \DateTime()))
->setExpiresOn((new \DateTime())->modify("+ 1min")),
);

$sasServiceClient = new BlobServiceClient($sas);

iterator_to_array($sasServiceClient->getBlobContainers());
}

#[Test]
public function generate_account_sas_throws_when_there_are_no_shared_key_credentials(): void
{
$this->expectException(UnableToGenerateSasException::class);

$serviceClientWithoutCredentials = new BlobServiceClient(new Uri("example.com"));

$serviceClientWithoutCredentials->generateAccountSasUri(
AccountSasBuilder::new(),
);
}
}
42 changes: 42 additions & 0 deletions tests/Blob/Unit/BlobContainerSasPermissionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace AzureOss\Storage\Tests\Blob\Unit;

use AzureOss\Storage\Blob\Sas\BlobContainerSasPermissions;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class BlobContainerSasPermissionsTest extends TestCase
{
#[Test]
public function to_string_works(): void
{
$permissions = new BlobContainerSasPermissions();

self::assertEquals("", (string) $permissions);

$permissions = new BlobContainerSasPermissions(read: true, delete: true);

self::assertEquals("rd", (string) $permissions);

$permissions = new BlobContainerSasPermissions(
read: true,
add: true,
create: true,
write: true,
delete: true,
deleteVersion: true,
list: true,
find: true,
move: true,
execute: true,
ownership: true,
permissions: true,
setImmutabilityPolicy: true,
);

self::assertEquals("racwdxlfmeopi", (string) $permissions);
}
}
43 changes: 43 additions & 0 deletions tests/Blob/Unit/BlobSasPermissionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace AzureOss\Storage\Tests\Blob\Unit;

use AzureOss\Storage\Blob\Sas\BlobSasPermissions;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class BlobSasPermissionsTest extends TestCase
{
#[Test]
public function to_string_works(): void
{
$permissions = new BlobSasPermissions();

self::assertEquals("", (string) $permissions);

$permissions = new BlobSasPermissions(read: true, delete: true);

self::assertEquals("rd", (string) $permissions);

$permissions = new BlobSasPermissions(
read: true,
add: true,
create: true,
write: true,
delete: true,
deleteVersion: true,
permanentDelete: true,
tags: true,
list: true,
move: true,
execute: true,
ownership: true,
permissions: true,
setImmutabilityPolicy: true,
);

self::assertEquals("racwdxyltmeopi", (string) $permissions);
}
}
41 changes: 41 additions & 0 deletions tests/Common/Unit/AccountSasPermissionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace AzureOss\Storage\Tests\Common\Unit;

use AzureOss\Storage\Common\Sas\AccountSasPermissions;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\Test;

class AccountSasPermissionsTest extends TestCase
{
#[Test]
public function to_string_works(): void
{
$permissions = new AccountSasPermissions();

self::assertEquals("", (string) $permissions);

$permissions = new AccountSasPermissions(read: true, delete: true, add: true);

self::assertEquals("rda", (string) $permissions);

$permissions = new AccountSasPermissions(
read: true,
write: true,
delete: true,
permanentDelete: true,
list: true,
add: true,
create: true,
update: true,
process: true,
tags: true,
filter: true,
setImmutabilityPolicy: true,
);

self::assertEquals("rwdylacuptfi", (string) $permissions);
}
}
28 changes: 28 additions & 0 deletions tests/Common/Unit/AccountSasResourceTypesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace AzureOss\Storage\Tests\Common\Unit;

use AzureOss\Storage\Common\Sas\AccountSasResourceTypes;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class AccountSasResourceTypesTest extends TestCase
{
#[Test]
public function to_string_works(): void
{
$resource = new AccountSasResourceTypes();

self::assertEquals("", (string) $resource);

$resource = new AccountSasResourceTypes(container: true);

self::assertEquals("c", (string) $resource);

$resource = new AccountSasResourceTypes(service: true, container: true, object: true);

self::assertEquals("sco", (string) $resource);
}
}
28 changes: 28 additions & 0 deletions tests/Common/Unit/AccountSasServicesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace AzureOss\Storage\Tests\Common\Unit;

use AzureOss\Storage\Common\Sas\AccountSasServices;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class AccountSasServicesTest extends TestCase
{
#[Test]
public function to_string_works(): void
{
$services = new AccountSasServices();

self::assertEquals("", (string) $services);

$services = new AccountSasServices(queue: true);

self::assertEquals("q", (string) $services);

$services = new AccountSasServices(blob: true, queue: true, table: true, file: true);

self::assertEquals("bqtf", (string) $services);
}
}
28 changes: 28 additions & 0 deletions tests/Common/Unit/SasIpRangeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace AzureOss\Storage\Tests\Common\Unit;

use AzureOss\Storage\Common\Sas\SasIpRange;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\Test;

class SasIpRangeTest extends TestCase
{
#[Test]
public function to_string_works_with_start_and_end(): void
{
$ipRange = new SasIpRange("0.0.0.0", "255.255.255.255");

self::assertEquals("0.0.0.0-255.255.255.255", (string) $ipRange);
}

#[Test]
public function to_string_works_with_only_start(): void
{
$ipRange = new SasIpRange("192.168.0.1");

self::assertEquals("192.168.0.1", (string) $ipRange);
}
}
37 changes: 37 additions & 0 deletions tests/Common/Unit/StorageSharedKeyCredentialTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace AzureOss\Storage\Tests\Common\Unit;

use AzureOss\Storage\Common\Auth\StorageSharedKeyCredential;
use AzureOss\Storage\Common\Exceptions\InvalidAccountKeyException;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class StorageSharedKeyCredentialTest extends TestCase
{
#[Test]
public function compute_hmacs_sha256_works(): void
{
$credential = new StorageSharedKeyCredential(
"devstoreaccount1",
"Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==",
);

self::assertEquals("Vir+MCz8c2dq+mnnEDMDF3s6vDBzCyY6WRzlblvWsgw=", $credential->computeHMACSHA256("Hello, world!"));
}

#[Test]
public function compute_hmacs_sha256_throws_when_account_key_is_invalid(): void
{
$this->expectException(InvalidAccountKeyException::class);

$credential = new StorageSharedKeyCredential(
"devstoreaccount1",
"This_is_not_base64!",
);

$credential->computeHMACSHA256("Hello, world!");
}
}

0 comments on commit 004bc11

Please sign in to comment.