FIRStorageTokenAuthorizerTests.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FIRStorageTestHelpers.h"
  15. #import <FirebaseCore/FIRAppInternal.h>
  16. #import "FIRAuthInteropFake.h"
  17. @interface FIRStorageTokenAuthorizerTests : XCTestCase
  18. @property(strong, nonatomic) GTMSessionFetcher *fetcher;
  19. @end
  20. @implementation FIRStorageTokenAuthorizerTests
  21. - (void)setUp {
  22. [super setUp];
  23. NSURLRequest *fetchRequest = [NSURLRequest requestWithURL:[FIRStorageTestHelpers objectURL]];
  24. self.fetcher = [GTMSessionFetcher fetcherWithRequest:fetchRequest];
  25. GTMSessionFetcherService *fetcherService = [[GTMSessionFetcherService alloc] init];
  26. FIRAuthInteropFake *auth =
  27. [[FIRAuthInteropFake alloc] initWithToken:kFIRStorageTestAuthToken userID:nil error:nil];
  28. self.fetcher.authorizer = [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  29. fetcherService:fetcherService
  30. authProvider:auth];
  31. }
  32. - (void)tearDown {
  33. self.fetcher = nil;
  34. [super tearDown];
  35. }
  36. - (void)testSuccessfulAuth {
  37. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulAuth"];
  38. self.fetcher.testBlock = ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  39. #pragma clang diagnostic push
  40. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  41. XCTAssertTrue([self.fetcher.authorizer isAuthorizedRequest:fetcher.request]);
  42. #pragma clang diagnostic pop
  43. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  44. statusCode:200
  45. HTTPVersion:kHTTPVersion
  46. headerFields:nil];
  47. response(httpResponse, nil, nil);
  48. };
  49. [self.fetcher
  50. beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  51. NSDictionary<NSString *, NSString *> *headers = self.fetcher.request.allHTTPHeaderFields;
  52. NSString *authHeader = [headers objectForKey:@"Authorization"];
  53. NSString *firebaseToken =
  54. [NSString stringWithFormat:kFIRStorageAuthTokenFormat, kFIRStorageTestAuthToken];
  55. XCTAssertEqualObjects(authHeader, firebaseToken);
  56. [expectation fulfill];
  57. }];
  58. [FIRStorageTestHelpers waitForExpectation:self];
  59. }
  60. - (void)testUnsuccessfulAuth {
  61. XCTestExpectation *expectation = [self expectationWithDescription:@"testUnsuccessfulAuth"];
  62. NSError *authError = [NSError errorWithDomain:FIRStorageErrorDomain
  63. code:FIRStorageErrorCodeUnauthenticated
  64. userInfo:nil];
  65. FIRAuthInteropFake *failedAuth =
  66. [[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:authError];
  67. GTMSessionFetcherService *fetcherService = [[GTMSessionFetcherService alloc] init];
  68. self.fetcher.authorizer = [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  69. fetcherService:fetcherService
  70. authProvider:failedAuth];
  71. self.fetcher.testBlock = ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  72. #pragma clang diagnostic push
  73. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  74. XCTAssertEqual([self.fetcher.authorizer isAuthorizedRequest:fetcher.request], NO);
  75. #pragma cland diagnostic pop
  76. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  77. statusCode:401
  78. HTTPVersion:kHTTPVersion
  79. headerFields:nil];
  80. response(httpResponse, nil, authError);
  81. };
  82. [self.fetcher
  83. beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  84. NSDictionary<NSString *, NSString *> *headers = self.fetcher.request.allHTTPHeaderFields;
  85. NSString *authHeader = [headers objectForKey:@"Authorization"];
  86. XCTAssertNil(authHeader);
  87. XCTAssertEqualObjects(error.domain, FIRStorageErrorDomain);
  88. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthenticated);
  89. [expectation fulfill];
  90. }];
  91. [FIRStorageTestHelpers waitForExpectation:self];
  92. }
  93. - (void)testSuccessfulUnauthenticatedAuth {
  94. XCTestExpectation *expectation =
  95. [self expectationWithDescription:@"testSuccessfulUnauthenticatedAuth"];
  96. // Simulate Auth not being included at all.
  97. GTMSessionFetcherService *fetcherService = [[GTMSessionFetcherService alloc] init];
  98. self.fetcher.authorizer = [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  99. fetcherService:fetcherService
  100. authProvider:nil];
  101. self.fetcher.testBlock = ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  102. #pragma clang diagnostic push
  103. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  104. XCTAssertFalse([self.fetcher.authorizer isAuthorizedRequest:fetcher.request]);
  105. #pragma cland diagnostic pop
  106. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  107. statusCode:200
  108. HTTPVersion:kHTTPVersion
  109. headerFields:nil];
  110. response(httpResponse, nil, nil);
  111. };
  112. [self.fetcher
  113. beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  114. NSDictionary<NSString *, NSString *> *headers = self.fetcher.request.allHTTPHeaderFields;
  115. NSString *authHeader = [headers objectForKey:@"Authorization"];
  116. XCTAssertNil(authHeader);
  117. XCTAssertNil(error);
  118. [expectation fulfill];
  119. }];
  120. [FIRStorageTestHelpers waitForExpectation:self];
  121. }
  122. - (void)testIsAuthorizing {
  123. XCTestExpectation *expectation = [self expectationWithDescription:@"testIsAuthorizing"];
  124. self.fetcher.testBlock = ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  125. XCTAssertFalse([fetcher.authorizer isAuthorizingRequest:fetcher.request]);
  126. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  127. statusCode:200
  128. HTTPVersion:kHTTPVersion
  129. headerFields:nil];
  130. response(httpResponse, nil, nil);
  131. };
  132. [self.fetcher
  133. beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  134. [expectation fulfill];
  135. }];
  136. [FIRStorageTestHelpers waitForExpectation:self];
  137. }
  138. - (void)testStopAuthorizingNoop {
  139. XCTestExpectation *expectation = [self expectationWithDescription:@"testStopAuthorizingNoop"];
  140. self.fetcher.testBlock = ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  141. // Since both of these are noops, we expect that invoking them
  142. // will still result in successful authentication
  143. [fetcher.authorizer stopAuthorization];
  144. [fetcher.authorizer stopAuthorizationForRequest:fetcher.request];
  145. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  146. statusCode:200
  147. HTTPVersion:kHTTPVersion
  148. headerFields:nil];
  149. response(httpResponse, nil, nil);
  150. };
  151. [self.fetcher
  152. beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  153. NSDictionary<NSString *, NSString *> *headers = self.fetcher.request.allHTTPHeaderFields;
  154. NSString *authHeader = [headers objectForKey:@"Authorization"];
  155. NSString *firebaseToken =
  156. [NSString stringWithFormat:kFIRStorageAuthTokenFormat, kFIRStorageTestAuthToken];
  157. XCTAssertEqualObjects(authHeader, firebaseToken);
  158. [expectation fulfill];
  159. }];
  160. [FIRStorageTestHelpers waitForExpectation:self];
  161. }
  162. - (void)testEmail {
  163. XCTestExpectation *expectation = [self expectationWithDescription:@"testEmail"];
  164. self.fetcher.testBlock = ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  165. XCTAssertNil([fetcher.authorizer userEmail]);
  166. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  167. statusCode:200
  168. HTTPVersion:kHTTPVersion
  169. headerFields:nil];
  170. response(httpResponse, nil, nil);
  171. };
  172. [self.fetcher
  173. beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  174. [expectation fulfill];
  175. }];
  176. [FIRStorageTestHelpers waitForExpectation:self];
  177. }
  178. @end