Skip to content

Commit 4d038e8

Browse files
committed
test(storage): address reviewer feedback for ip filter
1 parent ae44c36 commit 4d038e8

3 files changed

Lines changed: 115 additions & 22 deletions

File tree

Storage/src/Connection/ServiceDefinition/storage-v1.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,11 @@
330330
"properties": {
331331
"mode": {
332332
"type": "string",
333-
"description": "The bucket's IP filter mode. E.g., 'Enabled', 'Disabled'."
333+
"description": "The bucket's IP filter mode. E.g., 'Enabled', 'Disabled'.",
334+
"enum": [
335+
"Enabled",
336+
"Disabled"
337+
]
334338
},
335339
"publicNetworkSource": {
336340
"type": "object",

Storage/tests/System/ManageBucketsTest.php

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,7 @@ public function encryptionEnforcementConfigs()
584584
*/
585585
public function testCreateBucketWithIpFilterDisabled()
586586
{
587-
$keyfile = json_decode(file_get_contents(getenv('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH')), true);
588-
$projectId = $keyfile['project_id'];
587+
$projectId = self::getProjectId(getenv('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH'));
589588

590589
$ipFilterConfig = [
591590
'mode' => 'Disabled',
@@ -700,32 +699,80 @@ public function testUpdateBucketWithInvalidIpFilterFails()
700699
/**
701700
* @group storage-ipfilter
702701
*/
703-
public function testBucketAccessWithoutIpFilter()
702+
public function testGetBucketWithIpFilter()
704703
{
705704
$bucketName = uniqid(self::TESTING_PREFIX);
706-
$bucket = self::createBucket(self::$client, $bucketName);
705+
$ipFilterConfig = [
706+
'mode' => 'Disabled',
707+
'publicNetworkSource' => [
708+
'allowedIpCidrRanges' => ['1.2.3.0/24']
709+
]
710+
];
707711

708-
$this->assertArrayNotHasKey('ipFilter', $bucket->info());
712+
self::createBucket(
713+
self::$client,
714+
$bucketName,
715+
['ipFilter' => $ipFilterConfig]
716+
);
709717

710-
$objectName = 'test_no_ip_filter.txt';
711-
$objectContent = 'hello world';
718+
$bucket = self::$client->bucket($bucketName);
719+
$info = $bucket->reload();
712720

713-
$object = $bucket->upload($objectContent, ['name' => $objectName]);
714-
$this->assertEquals($objectContent, $object->downloadAsString());
721+
$this->assertArrayHasKey('ipFilter', $info);
722+
$this->assertEquals('Disabled', $info['ipFilter']['mode']);
723+
}
715724

716-
$unauthBucket = self::$unauthenticatedClient->bucket($bucketName);
717-
$unauthObject = $unauthBucket->object($objectName);
725+
/**
726+
* @group storage-ipfilter
727+
*/
728+
public function testDisableBucketIpFilter()
729+
{
730+
$bucketName = uniqid(self::TESTING_PREFIX);
731+
$ipFilterConfig = [
732+
'mode' => 'Enabled',
733+
'publicNetworkSource' => [
734+
'allowedIpCidrRanges' => ['1.2.3.0/24']
735+
]
736+
];
718737

719-
try {
720-
$unauthObject->downloadAsString();
721-
$this->fail('Expected anonymous download to fail.');
722-
} catch (ServiceException $e) {
723-
$this->assertEquals(401, $e->getCode());
724-
$this->assertStringContainsString('Anonymous caller', $e->getMessage());
725-
}
738+
$bucket = self::createBucket(
739+
self::$client,
740+
$bucketName,
741+
['ipFilter' => $ipFilterConfig]
742+
);
726743

727-
$object->delete();
744+
$bucket->update([
745+
'ipFilter' => [
746+
'mode' => 'Disabled'
747+
]
748+
]);
749+
750+
$info = $bucket->reload();
751+
$this->assertEquals('Disabled', $info['ipFilter']['mode']);
752+
}
753+
754+
/**
755+
* @group storage-ipfilter
756+
*/
757+
public function testDeleteBucketWithIpFilter()
758+
{
759+
$bucketName = uniqid(self::TESTING_PREFIX);
760+
$ipFilterConfig = [
761+
'mode' => 'Disabled',
762+
'publicNetworkSource' => [
763+
'allowedIpCidrRanges' => ['1.2.3.0/24']
764+
]
765+
];
766+
767+
$bucket = self::createBucket(
768+
self::$client,
769+
$bucketName,
770+
['ipFilter' => $ipFilterConfig]
771+
);
772+
773+
$this->assertTrue($bucket->exists());
728774
$bucket->delete();
775+
$this->assertFalse($bucket->exists());
729776
}
730777

731778
/**

Storage/tests/Unit/BucketTest.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,19 +707,61 @@ public function testBucketAccessDeniedByIpFilter()
707707
$bucket->info();
708708
}
709709

710-
public function testBucketAccessWithoutIpFilter()
710+
public function testGetBucketWithIpFilter()
711711
{
712+
$ipFilterConfig = [
713+
'mode' => 'Disabled',
714+
'publicNetworkSource' => [
715+
'allowedIpCidrRanges' => ['1.2.3.0/24']
716+
]
717+
];
718+
712719
$bucketInfo = [
713720
'name' => self::BUCKET_NAME,
721+
'ipFilter' => $ipFilterConfig
714722
];
723+
715724
$this->connection->getBucket(Argument::any())
716725
->shouldBeCalled()
717726
->willReturn($bucketInfo);
718727

719728
$bucket = $this->getBucket();
720729
$info = $bucket->info();
721730

722-
$this->assertArrayNotHasKey('ipFilter', $info);
731+
$this->assertArrayHasKey('ipFilter', $info);
732+
$this->assertEquals('Disabled', $info['ipFilter']['mode']);
733+
}
734+
735+
public function testDisableBucketIpFilter()
736+
{
737+
$ipFilterConfig = [
738+
'mode' => 'Disabled'
739+
];
740+
741+
$bucketInfo = [
742+
'name' => self::BUCKET_NAME,
743+
'ipFilter' => $ipFilterConfig
744+
];
745+
746+
$this->connection->patchBucket(Argument::withEntry('ipFilter', $ipFilterConfig))
747+
->shouldBeCalled()
748+
->willReturn($bucketInfo);
749+
750+
$bucket = $this->getBucket();
751+
$info = $bucket->update(['ipFilter' => $ipFilterConfig]);
752+
753+
$this->assertArrayHasKey('ipFilter', $info);
754+
$this->assertEquals('Disabled', $info['ipFilter']['mode']);
755+
}
756+
757+
public function testDeleteBucketWithIpFilter()
758+
{
759+
$this->connection->deleteBucket(Argument::any())
760+
->shouldBeCalled()
761+
->willReturn([]);
762+
763+
$bucket = $this->getBucket();
764+
$this->assertNull($bucket->delete());
723765
}
724766

725767
public function testGetsInfo()

0 commit comments

Comments
 (0)