diff --git a/.github/workflows/storage.yml b/.github/workflows/storage.yml index 4c94767b5d7..290d54fecf4 100644 --- a/.github/workflows/storage.yml +++ b/.github/workflows/storage.yml @@ -27,6 +27,14 @@ jobs: - name: Install Secret GoogleService-Info.plist run: scripts/decrypt_gha_secret.sh scripts/gha-encrypted/storage-db-plist.gpg \ FirebaseStorage/Tests/Integration/Resources/GoogleService-Info.plist "$plist_secret" + - name: Install Credentials.h + run: scripts/decrypt_gha_secret.sh scripts/gha-encrypted/Storage/Credentials.h.gpg \ + FirebaseStorage/Tests/Integration/Credentials.h "$plist_secret" + - name: Install Credentials.swift + run: | + scripts/decrypt_gha_secret.sh scripts/gha-encrypted/Storage/Credentials.swift.gpg \ + FirebaseStorage/Tests/SwiftIntegration/Credentials.swift "$plist_secret" + cp FirebaseStorage/Tests/SwiftIntegration/Credentials.swift FirebaseStorageSwift/Tests/Integration/ - name: BuildAndTest # can be replaced with pod lib lint with CocoaPods 1.10 run: ([ -z $plist_secret ] || scripts/third_party/travis/retry.sh scripts/build.sh ${{ matrix.pod }} all) @@ -65,7 +73,9 @@ jobs: strategy: matrix: - target: [ios, tvos, macos, watchos] + # watchos is disabled since pod lib lint cannot handle test Auth dep even if the dep is only + # specified for the other three platforms. + target: [ios, tvos, macos] steps: - uses: actions/checkout@v2 - name: Setup Bundler @@ -80,7 +90,7 @@ jobs: if: github.event_name == 'schedule' strategy: matrix: - target: [ios, tvos, macos, watchos] + target: [ios, tvos, macos] flags: [ '--skip-tests --use-modular-headers', '--skip-tests --use-libraries' diff --git a/.gitignore b/.gitignore index 3f54f4916a3..b279c9475b2 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,11 @@ FirebaseMessaging/Tests/IntegrationTests/Resources/GoogleService-Info.plist # FirebaseMessaging test app GoogleService-Info.plist FirebaseMessaging/Apps/Sample/Sample/GoogleService-Info.plist +# Credentials for Firebase Storage Integration Tests +FirebaseStorage/Tests/Integration/Credentials.h +FirebaseStorage/Tests/SwiftIntegration/Credentials.swift +FirebaseStorageSwift/Tests/Integration/Credentials.swift + Secrets.tar # OS X diff --git a/FirebaseStorage.podspec b/FirebaseStorage.podspec index 089989986ae..29c5e3c45db 100644 --- a/FirebaseStorage.podspec +++ b/FirebaseStorage.podspec @@ -52,6 +52,7 @@ Firebase Storage provides robust, secure file uploads and downloads from Firebas int_tests.requires_app_host = true int_tests.resources = 'FirebaseStorage/Tests/Integration/Resources/1mb.dat', 'FirebaseStorage/Tests/Integration/Resources/GoogleService-Info.plist' + int_tests.dependency 'FirebaseAuth', '~> 6.5' end s.test_spec 'swift-integration' do |swift_int_tests| @@ -60,5 +61,6 @@ Firebase Storage provides robust, secure file uploads and downloads from Firebas swift_int_tests.requires_app_host = true swift_int_tests.resources = 'FirebaseStorage/Tests/Integration/Resources/1mb.dat', 'FirebaseStorage/Tests/Integration/Resources/GoogleService-Info.plist' + swift_int_tests.dependency 'FirebaseAuth', '~> 6.5' end end diff --git a/FirebaseStorage/Tests/Integration/Credentials.h b/FirebaseStorage/Tests/Integration/Credentials.h new file mode 100644 index 00000000000..97da9e5256c --- /dev/null +++ b/FirebaseStorage/Tests/Integration/Credentials.h @@ -0,0 +1,30 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + Some of the Credentials needs to be populated for the Integration Tests. + Please follow the following steps to populate the valid Credentials + and copy it to Credentials.swift file: + + You will need to replace the following values: + $KUSER_NAME + The name of the user for Auth SignIn + $KPASSWORD + The password. + */ + +#define KUSER_NAME $KUSER_NAME +#define KPASSWORD $KPASSWORD diff --git a/FirebaseStorage/Tests/Integration/FIRStorageIntegrationTests.m b/FirebaseStorage/Tests/Integration/FIRStorageIntegrationTests.m index 21a23f38585..80daa2f4f28 100644 --- a/FirebaseStorage/Tests/Integration/FIRStorageIntegrationTests.m +++ b/FirebaseStorage/Tests/Integration/FIRStorageIntegrationTests.m @@ -12,43 +12,51 @@ // See the License for the specific language governing permissions and // limitations under the License. +#import #import #import #import #import +#import "Credentials.h" + NSTimeInterval kFIRStorageIntegrationTestTimeout = 60; /** * Firebase Storage Integration tests * - * To run these tests, you need to define the following access rights for your Firebase App: - * - unauthentication read/write access to /ios/public - * - authentication read/write access to /ios/private + * To run these tests, you need to define the following access rights: * - * A sample configuration may look like: + rules_version = '2'; + service firebase.storage { + match /b/{bucket}/o { + match /ios { + match /public/{allPaths=**} { + allow write: if request.auth != null; + allow read: if true; + } + match /private/{allPaths=**} { + allow read, write: if false; + } + } + } + } * - * rules_version = '2'; - * service firebase.storage { - * match /b/{bucket}/o { - * ... - * match /ios { - * match /public/{allPaths=**} { - * allow read, write; - * } - * match /private/{allPaths=**} { - * allow none; - * } - * } - * } - * } + * You also need to enable email/password sign in and add a test user in your + * Firebase Authentication settings. Your account credentials need to match + * the credentials defined in `kTestUser` and `kTestPassword`. * * You can define these access rights in the Firebase Console of your project. */ + +NSString *const kTestUser = KUSER_NAME; +NSString *const kTestPassword = KPASSWORD; + @interface FIRStorageIntegrationTests : XCTestCase @property(strong, nonatomic) FIRApp *app; +@property(strong, nonatomic) FIRAuth *auth; @property(strong, nonatomic) FIRStorage *storage; @end @@ -63,10 +71,20 @@ - (void)setUp { [super setUp]; self.app = [FIRApp defaultApp]; + self.auth = [FIRAuth authWithApp:self.app]; self.storage = [FIRStorage storageForApp:self.app]; static dispatch_once_t once; dispatch_once(&once, ^{ + XCTestExpectation *signInExpectation = [self expectationWithDescription:@"signIn"]; + [self.auth signInWithEmail:kTestUser + password:kTestPassword + completion:^(FIRAuthDataResult *result, NSError *error) { + XCTAssertNil(error); + [signInExpectation fulfill]; + }]; + [self waitForExpectations]; + XCTestExpectation *setUpExpectation = [self expectationWithDescription:@"setUp"]; NSArray *largeFiles = @[ @"ios/public/1mb" ]; @@ -122,9 +140,8 @@ - (void)testName { XCTAssertEqualObjects(ref.description, aGSURI); } -- (void)testUnauthenticatedGetMetadata { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedGetMetadata"]; +- (void)testGetMetadata { + XCTestExpectation *expectation = [self expectationWithDescription:@"testGetMetadata"]; FIRStorageReference *ref = [self.storage.reference child:@"ios/public/1mb"]; [ref metadataWithCompletion:^(FIRStorageMetadata *metadata, NSError *error) { @@ -136,37 +153,8 @@ - (void)testUnauthenticatedGetMetadata { [self waitForExpectations]; } -- (void)testUnauthenticatedUpdateMetadata { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedUpdateMetadata"]; - - FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"]; - - FIRStorageMetadata *meta = [[FIRStorageMetadata alloc] init]; - [meta setContentType:@"lol/custom"]; - [meta setCustomMetadata:@{ - @"lol" : @"custom metadata is neat", - @"ちかてつ" : @"🚇", - @"shinkansen" : @"新幹線" - }]; - - [ref updateMetadata:meta - completion:^(FIRStorageMetadata *metadata, NSError *error) { - XCTAssertEqualObjects(meta.contentType, metadata.contentType); - XCTAssertEqualObjects(meta.customMetadata[@"lol"], metadata.customMetadata[@"lol"]); - XCTAssertEqualObjects(meta.customMetadata[@"ちかてつ"], - metadata.customMetadata[@"ちかてつ"]); - XCTAssertEqualObjects(meta.customMetadata[@"shinkansen"], - metadata.customMetadata[@"shinkansen"]); - XCTAssertNil(error, "Error should be nil"); - [expectation fulfill]; - }]; - - [self waitForExpectations]; -} - -- (void)testUnauthenticatedDelete { - XCTestExpectation *expectation = [self expectationWithDescription:@"testUnauthenticatedDelete"]; +- (void)testDelete { + XCTestExpectation *expectation = [self expectationWithDescription:@"testDelete"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/fileToDelete"]; @@ -205,27 +193,8 @@ - (void)testDeleteWithNilCompletion { [self waitForExpectations]; } -- (void)testUnauthenticatedSimplePutData { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedSimplePutData"]; - FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/testBytesUpload"]; - - NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding]; - - [ref putData:data - metadata:nil - completion:^(FIRStorageMetadata *metadata, NSError *error) { - XCTAssertNotNil(metadata, "Metadata should not be nil"); - XCTAssertNil(error, "Error should be nil"); - [expectation fulfill]; - }]; - - [self waitForExpectations]; -} - -- (void)testUnauthenticatedSimplePutSpecialCharacter { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedSimplePutDataEscapedName"]; +- (void)testPutDataSpecialCharacter { + XCTestExpectation *expectation = [self expectationWithDescription:@"testPutDataSpecialCharacter"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/-._~!$'()*,=:@&+;"]; NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding]; @@ -241,9 +210,9 @@ - (void)testUnauthenticatedSimplePutSpecialCharacter { [self waitForExpectations]; } -- (void)testUnauthenticatedSimplePutDataInBackgroundQueue { +- (void)testPutDataInBackgroundQueue { XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedSimplePutDataInBackgroundQueue"]; + [self expectationWithDescription:@"testPutDataInBackgroundQueue"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/testBytesUpload"]; NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding]; @@ -261,9 +230,8 @@ - (void)testUnauthenticatedSimplePutDataInBackgroundQueue { [self waitForExpectations]; } -- (void)testUnauthenticatedSimplePutEmptyData { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedSimplePutEmptyData"]; +- (void)testPutDataWithEmptyData { + XCTestExpectation *expectation = [self expectationWithDescription:@"testPutDataWithEmptyData"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutEmptyData"]; @@ -281,9 +249,8 @@ - (void)testUnauthenticatedSimplePutEmptyData { [self waitForExpectations]; } -- (void)testUnauthenticatedSimplePutDataUnauthorized { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedSimplePutDataUnauthorized"]; +- (void)testPutData { + XCTestExpectation *expectation = [self expectationWithDescription:@"testPutData"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/private/secretfile.txt"]; NSData *data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding]; @@ -300,9 +267,8 @@ - (void)testUnauthenticatedSimplePutDataUnauthorized { [self waitForExpectations]; } -- (void)testUnauthenticatedSimplePutFile { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedSimplePutFile"]; +- (void)testPutFile { + XCTestExpectation *expectation = [self expectationWithDescription:@"testPutFile"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutFile"]; @@ -372,9 +338,8 @@ - (void)testPutFileWithSpecialCharacters { [self waitForExpectations]; } -- (void)testUnauthenticatedSimplePutDataNoMetadata { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedSimplePutDataNoMetadata"]; +- (void)testPutDataNoMetadata { + XCTestExpectation *expectation = [self expectationWithDescription:@"testPutDataNoMetadata"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutDataNoMetadata"]; @@ -392,9 +357,8 @@ - (void)testUnauthenticatedSimplePutDataNoMetadata { [self waitForExpectations]; } -- (void)testUnauthenticatedSimplePutFileNoMetadata { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedSimplePutFileNoMetadata"]; +- (void)testPutFileNoMetadata { + XCTestExpectation *expectation = [self expectationWithDescription:@"testPutFileNoMetadata"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/testUnauthenticatedSimplePutFileNoMetadata"]; @@ -416,9 +380,8 @@ - (void)testUnauthenticatedSimplePutFileNoMetadata { [self waitForExpectations]; } -- (void)testUnauthenticatedSimpleGetData { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedSimpleGetData"]; +- (void)testGetData { + XCTestExpectation *expectation = [self expectationWithDescription:@"testGetData"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"]; @@ -432,9 +395,9 @@ - (void)testUnauthenticatedSimpleGetData { [self waitForExpectations]; } -- (void)testUnauthenticatedSimpleGetDataInBackgroundQueue { +- (void)testGetDataInBackgroundQueue { XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedSimpleGetDataInBackgroundQueue"]; + [self expectationWithDescription:@"testGetDataInBackgroundQueue"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"]; @@ -450,9 +413,8 @@ - (void)testUnauthenticatedSimpleGetDataInBackgroundQueue { [self waitForExpectations]; } -- (void)testUnauthenticatedSimpleGetDataTooSmall { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedSimpleGetDataTooSmall"]; +- (void)testGetDataTooSmall { + XCTestExpectation *expectation = [self expectationWithDescription:@"testGetDataTooSmall"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"]; @@ -467,9 +429,8 @@ - (void)testUnauthenticatedSimpleGetDataTooSmall { [self waitForExpectations]; } -- (void)testUnauthenticatedSimpleGetDownloadURL { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedSimpleGetDownloadURL"]; +- (void)testGetDownloadURL { + XCTestExpectation *expectation = [self expectationWithDescription:@"testGetDownloadURL"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"]; @@ -494,9 +455,8 @@ - (void)testUnauthenticatedSimpleGetDownloadURL { [self waitForExpectations]; } -- (void)testUnauthenticatedSimpleGetFile { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedSimpleGetFile"]; +- (void)testGetFile { + XCTestExpectation *expectation = [self expectationWithDescription:@"testGetFile"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/helloworld"]; @@ -639,9 +599,8 @@ - (void)testUpdateMetadata { [self waitForExpectations]; } -- (void)testUnauthenticatedResumeGetFile { - XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedResumeGetFile"]; +- (void)testResumeGetFile { + XCTestExpectation *expectation = [self expectationWithDescription:@"testResumeGetFile"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"]; @@ -693,9 +652,9 @@ - (void)testUnauthenticatedResumeGetFile { XCTAssertEqualWithAccuracy(sqrt(INT_MAX - 499), computationResult, 0.1); } -- (void)testUnauthenticatedResumeGetFileInBackgroundQueue { +- (void)testResumeGetFileInBackgroundQueue { XCTestExpectation *expectation = - [self expectationWithDescription:@"testUnauthenticatedResumeGetFileInBackgroundQueue"]; + [self expectationWithDescription:@"testResumeGetFileInBackgroundQueue"]; FIRStorageReference *ref = [self.storage referenceWithPath:@"ios/public/1mb"]; diff --git a/FirebaseStorage/Tests/SwiftIntegration/Credentials.swift b/FirebaseStorage/Tests/SwiftIntegration/Credentials.swift new file mode 100644 index 00000000000..091ae35fe9e --- /dev/null +++ b/FirebaseStorage/Tests/SwiftIntegration/Credentials.swift @@ -0,0 +1,35 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Foundation + +/* + Some of the Credentials needs to be populated for the Swift Tests to work. + Please follow the following steps to populate the valid Credentials + and copy it to Credentials.swift file: + + You will need to replace the following values: + $KUSER_NAME + The name of the user for Auth SignIn + $KPASSWORD + The password. + + */ + +class Credentials { + static let kUserName = KUSER_NAME + static let kPassword = KPASSWORD +} diff --git a/FirebaseStorage/Tests/SwiftIntegration/StorageIntegration.swift b/FirebaseStorage/Tests/SwiftIntegration/StorageIntegration.swift index 8d4ea49fdb9..3f18b90c91a 100644 --- a/FirebaseStorage/Tests/SwiftIntegration/StorageIntegration.swift +++ b/FirebaseStorage/Tests/SwiftIntegration/StorageIntegration.swift @@ -12,14 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. +// See console setup instructions in FIRStorageIntegrationTests.m + +import FirebaseAuth import FirebaseCore import FirebaseStorage import XCTest class StorageIntegration: XCTestCase { var app: FirebaseApp! + var auth: Auth! var storage: Storage! static var once = false + static var signedIn = false override class func setUp() { FirebaseApp.configure() @@ -28,13 +33,17 @@ class StorageIntegration: XCTestCase { override func setUp() { super.setUp() app = FirebaseApp.app() + auth = Auth.auth(app: app) storage = Storage.storage(app: app!) + if !StorageIntegration.signedIn { + signInAndWait() + } + if !StorageIntegration.once { - StorageIntegration.once = true let setupExpectation = expectation(description: "setUp") - let largeFiles = ["ios/public/1mb"] + let largeFiles = ["ios/public/swift-1mb"] let emptyFiles = ["ios/public/empty", "ios/public/list/a", "ios/public/list/b", "ios/public/list/prefix/c"] setupExpectation.expectedFulfillmentCount = largeFiles.count + emptyFiles.count @@ -65,11 +74,13 @@ class StorageIntegration: XCTestCase { XCTFail("Exception thrown setting up files in setUp") } waitForExpectations() + StorageIntegration.once = true } } override func tearDown() { app = nil + auth = nil storage = nil super.tearDown() } @@ -81,7 +92,7 @@ class StorageIntegration: XCTestCase { XCTAssertEqual(ref.description, aGSURI) } - func testUnauthenticatedGetMetadata() { + func testGetMetadata() { let expectation = self.expectation(description: #function) let ref = storage.reference().child("ios/public/1mb") ref.getMetadata(completion: { (metadata, error) -> Void in @@ -92,7 +103,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedGetMetadataUnauthorized() { + func testGetMetadataUnauthorized() { let expectation = self.expectation(description: #function) let ref = storage.reference().child("ios/private/secretfile.txt") ref.getMetadata(completion: { (metadata, error) -> Void in @@ -104,7 +115,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedUpdateMetadata() { + func testUpdateMetadata() { let expectation = self.expectation(description: #function) let meta = StorageMetadata() @@ -126,7 +137,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedDelete() throws { + func testDelete() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/fileToDelete") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") @@ -141,7 +152,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedDeleteNonExistingFile() { + func testDeleteNonExistingFile() { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/fileThatDoesNotExist") ref.delete { error in @@ -152,7 +163,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedDeleteFileUnauthorized() { + func testDeleteFileUnauthorized() { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/private/secretfile.txt") ref.delete { error in @@ -176,7 +187,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutData() throws { + func testSimplePutData() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/testBytesUpload") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") @@ -188,7 +199,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutSpecialCharacter() throws { + func testSimplePutSpecialCharacter() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/-._~!$'()*,=:@&+;") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") @@ -200,7 +211,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutDataInBackgroundQueue() throws { + func testSimplePutDataInBackgroundQueue() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/testBytesUpload") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") @@ -214,9 +225,9 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutEmptyData() { + func testSimplePutEmptyData() { let expectation = self.expectation(description: #function) - let ref = storage.reference(withPath: "ios/public/testUnauthenticatedSimplePutEmptyData") + let ref = storage.reference(withPath: "ios/public/testSimplePutEmptyData") let data = Data() ref.putData(data, metadata: nil, completion: { metadata, error in XCTAssertNotNil(metadata, "Metadata should not be nil") @@ -226,7 +237,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutDataUnauthorized() throws { + func testSimplePutDataUnauthorized() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/private/secretfile.txt") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") @@ -239,9 +250,9 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutFile() throws { + func testSimplePutFile() throws { let expectation = self.expectation(description: #function) - let ref = storage.reference(withPath: "ios/public/testUnauthenticatedSimplePutFile") + let ref = storage.reference(withPath: "ios/public/testSimplePutFile") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory()) let fileURL = tmpDirURL.appendingPathComponent("hello.txt") @@ -294,10 +305,10 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutDataNoMetadata() throws { + func testSimplePutDataNoMetadata() throws { let expectation = self.expectation(description: #function) - let ref = storage.reference(withPath: "ios/public/testUnauthenticatedSimplePutDataNoMetadata") + let ref = storage.reference(withPath: "ios/public/testSimplePutDataNoMetadata") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") ref.putData(data, metadata: nil, completion: { metadata, error in XCTAssertNotNil(metadata, "Metadata should not be nil") @@ -307,7 +318,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutFileNoMetadata() throws { + func testSimplePutFileNoMetadata() throws { let expectation = self.expectation(description: #function) let fileName = "hello&+@_ .txt" @@ -324,7 +335,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutBlankImage() throws { + func testSimplePutBlankImage() throws { let expectation = self.expectation(description: #function) let fileName = "blank.jpg" let ref = storage.reference(withPath: "ios/public/" + fileName) @@ -342,7 +353,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimpleGetData() { + func testSimpleGetData() { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/1mb") @@ -354,7 +365,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimpleGetDataInBackgroundQueue() { + func testSimpleGetDataInBackgroundQueue() { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/1mb") @@ -368,7 +379,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimpleGetDataTooSmall() { + func testSimpleGetDataTooSmall() { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/1mb") @@ -381,7 +392,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimpleGetDownloadURL() { + func testSimpleGetDownloadURL() { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/1mb") @@ -409,7 +420,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimpleGetFileWithCompletion() throws { + func testSimpleGetFileWithCompletion() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/cookie") let cookieString = "Here's a 🍪, yay!" @@ -442,7 +453,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimpleGetFile() throws { + func testSimpleGetFile() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/helloworld") let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory()) @@ -486,10 +497,14 @@ class StorageIntegration: XCTestCase { let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory()) let fileURL = tmpDirURL.appendingPathComponent("hello.dat") let task = ref.write(toFile: fileURL) + var failed = false // Only fail once task.observe(StorageTaskStatus.failure, handler: { snapshot in XCTAssertTrue(snapshot.description.starts(with: " 6.5' end end diff --git a/FirebaseStorageSwift/Tests/Integration/Credentials.swift b/FirebaseStorageSwift/Tests/Integration/Credentials.swift new file mode 100644 index 00000000000..091ae35fe9e --- /dev/null +++ b/FirebaseStorageSwift/Tests/Integration/Credentials.swift @@ -0,0 +1,35 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Foundation + +/* + Some of the Credentials needs to be populated for the Swift Tests to work. + Please follow the following steps to populate the valid Credentials + and copy it to Credentials.swift file: + + You will need to replace the following values: + $KUSER_NAME + The name of the user for Auth SignIn + $KPASSWORD + The password. + + */ + +class Credentials { + static let kUserName = KUSER_NAME + static let kPassword = KPASSWORD +} diff --git a/FirebaseStorageSwift/Tests/Integration/StorageIntegration.swift b/FirebaseStorageSwift/Tests/Integration/StorageIntegration.swift index d6dd8dc96ef..1913c8f8e72 100644 --- a/FirebaseStorageSwift/Tests/Integration/StorageIntegration.swift +++ b/FirebaseStorageSwift/Tests/Integration/StorageIntegration.swift @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +import FirebaseAuth import FirebaseCore import FirebaseStorage import FirebaseStorageSwift @@ -19,8 +20,10 @@ import XCTest class StorageIntegration: XCTestCase { var app: FirebaseApp! + var auth: Auth! var storage: Storage! static var once = false + static var signedIn = false override class func setUp() { FirebaseApp.configure() @@ -29,8 +32,13 @@ class StorageIntegration: XCTestCase { override func setUp() { super.setUp() app = FirebaseApp.app() + auth = Auth.auth(app: app) storage = Storage.storage(app: app!) + if !StorageIntegration.signedIn { + signInAndWait() + } + if !StorageIntegration.once { StorageIntegration.once = true let setupExpectation = expectation(description: "setUp") @@ -74,8 +82,8 @@ class StorageIntegration: XCTestCase { super.tearDown() } - func testUnauthenticatedGetMetadata() { - let expectation = self.expectation(description: "testUnauthenticatedGetMetadata") + func testGetMetadata() { + let expectation = self.expectation(description: "testGetMetadata") let ref = storage.reference().child("ios/public/1mb") ref.getMetadata { result in self.assertResultSuccess(result) @@ -84,7 +92,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedUpdateMetadata() { + func testUpdateMetadata() { let expectation = self.expectation(description: #function) let meta = StorageMetadata() @@ -110,7 +118,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedDelete() throws { + func testDelete() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/fileToDelete") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") @@ -136,7 +144,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutData() throws { + func testSimplePutData() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/testBytesUpload") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") @@ -147,7 +155,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutSpecialCharacter() throws { + func testSimplePutSpecialCharacter() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/-._~!$'()*,=:@&+;") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") @@ -158,7 +166,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutDataInBackgroundQueue() throws { + func testSimplePutDataInBackgroundQueue() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/testBytesUpload") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") @@ -171,9 +179,9 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutEmptyData() { + func testSimplePutEmptyData() { let expectation = self.expectation(description: #function) - let ref = storage.reference(withPath: "ios/public/testUnauthenticatedSimplePutEmptyData") + let ref = storage.reference(withPath: "ios/public/testSimplePutEmptyData") let data = Data() ref.putData(data) { result in self.assertResultSuccess(result) @@ -182,7 +190,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutDataUnauthorized() throws { + func testSimplePutDataUnauthorized() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/private/secretfile.txt") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") @@ -198,7 +206,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutDataUnauthorizedThrow() throws { + func testSimplePutDataUnauthorizedThrow() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/private/secretfile.txt") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") @@ -215,10 +223,10 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutFile() throws { + func testSimplePutFile() throws { let expectation = self.expectation(description: #function) let putFileExpectation = self.expectation(description: "putFile") - let ref = storage.reference(withPath: "ios/public/testUnauthenticatedSimplePutFile") + let ref = storage.reference(withPath: "ios/public/testSimplePutFile") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory()) let fileURL = tmpDirURL.appendingPathComponent("hello.txt") @@ -272,10 +280,10 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutDataNoMetadata() throws { + func testSimplePutDataNoMetadata() throws { let expectation = self.expectation(description: #function) - let ref = storage.reference(withPath: "ios/public/testUnauthenticatedSimplePutDataNoMetadata") + let ref = storage.reference(withPath: "ios/public/testSimplePutDataNoMetadata") let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") ref.putData(data) { result in @@ -285,7 +293,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimplePutFileNoMetadata() throws { + func testSimplePutFileNoMetadata() throws { let expectation = self.expectation(description: #function) let fileName = "hello&+@_ .txt" @@ -301,7 +309,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimpleGetData() { + func testSimpleGetData() { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/1mb") @@ -312,7 +320,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimpleGetDataInBackgroundQueue() { + func testSimpleGetDataInBackgroundQueue() { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/1mb") @@ -325,7 +333,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimpleGetDataTooSmall() { + func testSimpleGetDataTooSmall() { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/1mb") @@ -341,7 +349,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimpleGetDownloadURL() { + func testSimpleGetDownloadURL() { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/1mb") @@ -372,7 +380,7 @@ class StorageIntegration: XCTestCase { waitForExpectations() } - func testUnauthenticatedSimpleGetFile() throws { + func testSimpleGetFile() throws { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/helloworld") let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory()) @@ -438,7 +446,7 @@ class StorageIntegration: XCTestCase { XCTAssertNil(actualMetadata.customMetadata) } - func testUpdateMetadata() { + func testUpdateMetadata2() { let expectation = self.expectation(description: #function) let ref = storage.reference(withPath: "ios/public/1mb") @@ -541,6 +549,18 @@ class StorageIntegration: XCTestCase { waitForExpectations() } + private func signInAndWait() { + let expectation = self.expectation(description: #function) + auth.signIn(withEmail: Credentials.kUserName, + password: Credentials.kPassword) { result, error in + XCTAssertNil(error) + StorageIntegration.signedIn = true + print("Successfully signed in") + expectation.fulfill() + } + waitForExpectations() + } + private func waitForExpectations() { let kFIRStorageIntegrationTestTimeout = 60.0 waitForExpectations(timeout: kFIRStorageIntegrationTestTimeout, diff --git a/scripts/gha-encrypted/Storage/Credentials.h.gpg b/scripts/gha-encrypted/Storage/Credentials.h.gpg new file mode 100644 index 00000000000..c13d59baf3b Binary files /dev/null and b/scripts/gha-encrypted/Storage/Credentials.h.gpg differ diff --git a/scripts/gha-encrypted/Storage/Credentials.swift.gpg b/scripts/gha-encrypted/Storage/Credentials.swift.gpg new file mode 100644 index 00000000000..4d2e6c9fc74 Binary files /dev/null and b/scripts/gha-encrypted/Storage/Credentials.swift.gpg differ diff --git a/scripts/style.sh b/scripts/style.sh index 000f4857a7d..5ff24c2b163 100755 --- a/scripts/style.sh +++ b/scripts/style.sh @@ -138,6 +138,7 @@ s%^./%% \%^build/% d \%^Debug/% d \%^Release/% d +\%^cmake-build-debug/% d # pod gen output \%^gen/% d