|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2019 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Google\Cloud\Samples\Storage\Tests; |
| 19 | + |
| 20 | +use Google\Cloud\Storage\StorageClient; |
| 21 | +use Symfony\Component\Console\Tester\CommandTester; |
| 22 | + |
| 23 | +/** |
| 24 | + * Unit Tests for BucketPolicyOnlyCommand. |
| 25 | + */ |
| 26 | +class BucketPolicyOnlyCommandTest extends \PHPUnit_Framework_TestCase |
| 27 | +{ |
| 28 | + protected static $hasCredentials; |
| 29 | + protected $commandTester; |
| 30 | + protected $storage; |
| 31 | + protected $bucket; |
| 32 | + |
| 33 | + public static function setUpBeforeClass() |
| 34 | + { |
| 35 | + $path = getenv('GOOGLE_APPLICATION_CREDENTIALS'); |
| 36 | + self::$hasCredentials = $path && file_exists($path) && |
| 37 | + filesize($path) > 0; |
| 38 | + } |
| 39 | + |
| 40 | + public function setUp() |
| 41 | + { |
| 42 | + // Sleep to avoid the rate limit for creating/deleting. |
| 43 | + sleep(5 + rand(2, 4)); |
| 44 | + $application = require __DIR__ . '/../storage.php'; |
| 45 | + $this->commandTester = new CommandTester($application->get('bucket-policy-only')); |
| 46 | + $this->storage = new StorageClient(); |
| 47 | + if (!self::$hasCredentials) { |
| 48 | + $this->markTestSkipped('No application credentials were found.'); |
| 49 | + } |
| 50 | + |
| 51 | + // Append random because tests for multiple PHP versions were running at the same time. |
| 52 | + $bucketName = 'php-bucket-policy-only-' . time() . '-' . rand(1000, 9999); |
| 53 | + $this->bucket = $this->storage->createBucket($bucketName); |
| 54 | + } |
| 55 | + |
| 56 | + public function tearDown() |
| 57 | + { |
| 58 | + $this->bucket->delete(); |
| 59 | + } |
| 60 | + |
| 61 | + public function testEnableBucketPolicyOnly() |
| 62 | + { |
| 63 | + $this->commandTester->execute( |
| 64 | + [ |
| 65 | + 'bucket' => $this->bucket->name(), |
| 66 | + '--enable' => true, |
| 67 | + ], |
| 68 | + ['interactive' => false] |
| 69 | + ); |
| 70 | + $outputString = <<<EOF |
| 71 | +Bucket Policy Only was enabled for {$this->bucket->name()} |
| 72 | +
|
| 73 | +EOF; |
| 74 | + $this->expectOutputString($outputString); |
| 75 | + $this->bucket->reload(); |
| 76 | + $bucketInformation = $this->bucket->info(); |
| 77 | + $bucketPolicyOnly = $bucketInformation['iamConfiguration']['bucketPolicyOnly']; |
| 78 | + $this->assertTrue($bucketPolicyOnly['enabled']); |
| 79 | + } |
| 80 | + |
| 81 | + /** @depends testEnableBucketPolicyOnly */ |
| 82 | + public function testDisableBucketPolicyOnly() |
| 83 | + { |
| 84 | + $this->commandTester->execute( |
| 85 | + [ |
| 86 | + 'bucket' => $this->bucket->name(), |
| 87 | + '--disable' => true, |
| 88 | + ], |
| 89 | + ['interactive' => false] |
| 90 | + ); |
| 91 | + |
| 92 | + $outputString = <<<EOF |
| 93 | +Bucket Policy Only was disabled for {$this->bucket->name()} |
| 94 | +
|
| 95 | +EOF; |
| 96 | + $this->expectOutputString($outputString); |
| 97 | + $this->bucket->reload(); |
| 98 | + $bucketInformation = $this->bucket->info(); |
| 99 | + $bucketPolicyOnly = $bucketInformation['iamConfiguration']['bucketPolicyOnly']; |
| 100 | + $this->assertFalse($bucketPolicyOnly['enabled']); |
| 101 | + } |
| 102 | + |
| 103 | + /** @depends testDisableBucketPolicyOnly */ |
| 104 | + public function testGetBucketPolicyOnly() |
| 105 | + { |
| 106 | + $this->commandTester->execute( |
| 107 | + [ |
| 108 | + 'bucket' => $this->bucket->name(), |
| 109 | + '--get' => true, |
| 110 | + ], |
| 111 | + ['interactive' => false] |
| 112 | + ); |
| 113 | + |
| 114 | + $outputString = <<<EOF |
| 115 | +Bucket Policy Only is disabled for {$this->bucket->name()} |
| 116 | +
|
| 117 | +EOF; |
| 118 | + $this->expectOutputString($outputString); |
| 119 | + $this->bucket->reload(); |
| 120 | + $bucketInformation = $this->bucket->info(); |
| 121 | + $bucketPolicyOnly = $bucketInformation['iamConfiguration']['bucketPolicyOnly']; |
| 122 | + $this->assertFalse($bucketPolicyOnly['enabled']); |
| 123 | + } |
| 124 | +} |
0 commit comments