FIRStorageTokenAuthorizerTests.m 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 = [[FIRAuthInteropFake alloc] initWithToken:kFIRStorageTestAuthToken
  27. userID:nil
  28. error:nil];
  29. self.fetcher.authorizer = [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  30. fetcherService:fetcherService
  31. authProvider:auth];
  32. }
  33. - (void)tearDown {
  34. self.fetcher = nil;
  35. [super tearDown];
  36. }
  37. - (void)testSuccessfulAuth {
  38. XCTestExpectation *expectation = [self expectationWithDescription:@"testSuccessfulAuth"];
  39. self.fetcher.testBlock = ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  40. #pragma clang diagnostic push
  41. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  42. XCTAssertTrue([self.fetcher.authorizer isAuthorizedRequest:fetcher.request]);
  43. #pragma clang diagnostic pop
  44. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  45. statusCode:200
  46. HTTPVersion:kHTTPVersion
  47. headerFields:nil];
  48. response(httpResponse, nil, nil);
  49. };
  50. [self.fetcher
  51. beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  52. NSDictionary<NSString *, NSString *> *headers = self.fetcher.request.allHTTPHeaderFields;
  53. NSString *authHeader = [headers objectForKey:@"Authorization"];
  54. NSString *firebaseToken =
  55. [NSString stringWithFormat:kFIRStorageAuthTokenFormat, kFIRStorageTestAuthToken];
  56. XCTAssertEqualObjects(authHeader, firebaseToken);
  57. [expectation fulfill];
  58. }];
  59. [FIRStorageTestHelpers waitForExpectation:self];
  60. }
  61. - (void)testUnsuccessfulAuth {
  62. XCTestExpectation *expectation = [self expectationWithDescription:@"testUnsuccessfulAuth"];
  63. NSError *authError = [NSError errorWithDomain:FIRStorageErrorDomain
  64. code:FIRStorageErrorCodeUnauthenticated
  65. userInfo:nil];
  66. FIRAuthInteropFake *failedAuth = [[FIRAuthInteropFake alloc] initWithToken:nil
  67. userID:nil
  68. error:authError];
  69. GTMSessionFetcherService *fetcherService = [[GTMSessionFetcherService alloc] init];
  70. self.fetcher.authorizer = [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  71. fetcherService:fetcherService
  72. authProvider:failedAuth];
  73. self.fetcher.testBlock = ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  74. #pragma clang diagnostic push
  75. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  76. XCTAssertEqual([self.fetcher.authorizer isAuthorizedRequest:fetcher.request], NO);
  77. #pragma cland diagnostic pop
  78. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  79. statusCode:401
  80. HTTPVersion:kHTTPVersion
  81. headerFields:nil];
  82. response(httpResponse, nil, authError);
  83. };
  84. [self.fetcher
  85. beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  86. NSDictionary<NSString *, NSString *> *headers = self.fetcher.request.allHTTPHeaderFields;
  87. NSString *authHeader = [headers objectForKey:@"Authorization"];
  88. XCTAssertNil(authHeader);
  89. XCTAssertEqualObjects(error.domain, FIRStorageErrorDomain);
  90. XCTAssertEqual(error.code, FIRStorageErrorCodeUnauthenticated);
  91. [expectation fulfill];
  92. }];
  93. [FIRStorageTestHelpers waitForExpectation:self];
  94. }
  95. - (void)testSuccessfulUnauthenticatedAuth {
  96. XCTestExpectation *expectation =
  97. [self expectationWithDescription:@"testSuccessfulUnauthenticatedAuth"];
  98. // Simulate Auth not being included at all.
  99. GTMSessionFetcherService *fetcherService = [[GTMSessionFetcherService alloc] init];
  100. self.fetcher.authorizer = [[FIRStorageTokenAuthorizer alloc] initWithGoogleAppID:@"dummyAppID"
  101. fetcherService:fetcherService
  102. authProvider:nil];
  103. self.fetcher.testBlock = ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  104. #pragma clang diagnostic push
  105. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  106. XCTAssertFalse([self.fetcher.authorizer isAuthorizedRequest:fetcher.request]);
  107. #pragma cland diagnostic pop
  108. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  109. statusCode:200
  110. HTTPVersion:kHTTPVersion
  111. headerFields:nil];
  112. response(httpResponse, nil, nil);
  113. };
  114. [self.fetcher
  115. beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  116. NSDictionary<NSString *, NSString *> *headers = self.fetcher.request.allHTTPHeaderFields;
  117. NSString *authHeader = [headers objectForKey:@"Authorization"];
  118. XCTAssertNil(authHeader);
  119. XCTAssertNil(error);
  120. [expectation fulfill];
  121. }];
  122. [FIRStorageTestHelpers waitForExpectation:self];
  123. }
  124. - (void)testIsAuthorizing {
  125. XCTestExpectation *expectation = [self expectationWithDescription:@"testIsAuthorizing"];
  126. self.fetcher.testBlock = ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  127. XCTAssertFalse([fetcher.authorizer isAuthorizingRequest:fetcher.request]);
  128. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  129. statusCode:200
  130. HTTPVersion:kHTTPVersion
  131. headerFields:nil];
  132. response(httpResponse, nil, nil);
  133. };
  134. [self.fetcher
  135. beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  136. [expectation fulfill];
  137. }];
  138. [FIRStorageTestHelpers waitForExpectation:self];
  139. }
  140. - (void)testStopAuthorizingNoop {
  141. XCTestExpectation *expectation = [self expectationWithDescription:@"testStopAuthorizingNoop"];
  142. self.fetcher.testBlock = ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  143. // Since both of these are noops, we expect that invoking them
  144. // will still result in successful authentication
  145. [fetcher.authorizer stopAuthorization];
  146. [fetcher.authorizer stopAuthorizationForRequest:fetcher.request];
  147. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  148. statusCode:200
  149. HTTPVersion:kHTTPVersion
  150. headerFields:nil];
  151. response(httpResponse, nil, nil);
  152. };
  153. [self.fetcher
  154. beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  155. NSDictionary<NSString *, NSString *> *headers = self.fetcher.request.allHTTPHeaderFields;
  156. NSString *authHeader = [headers objectForKey:@"Authorization"];
  157. NSString *firebaseToken =
  158. [NSString stringWithFormat:kFIRStorageAuthTokenFormat, kFIRStorageTestAuthToken];
  159. XCTAssertEqualObjects(authHeader, firebaseToken);
  160. [expectation fulfill];
  161. }];
  162. [FIRStorageTestHelpers waitForExpectation:self];
  163. }
  164. - (void)testEmail {
  165. XCTestExpectation *expectation = [self expectationWithDescription:@"testEmail"];
  166. self.fetcher.testBlock = ^(GTMSessionFetcher *fetcher, GTMSessionFetcherTestResponse response) {
  167. XCTAssertNil([fetcher.authorizer userEmail]);
  168. NSHTTPURLResponse *httpResponse = [[NSHTTPURLResponse alloc] initWithURL:fetcher.request.URL
  169. statusCode:200
  170. HTTPVersion:kHTTPVersion
  171. headerFields:nil];
  172. response(httpResponse, nil, nil);
  173. };
  174. [self.fetcher
  175. beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  176. [expectation fulfill];
  177. }];
  178. [FIRStorageTestHelpers waitForExpectation:self];
  179. }
  180. @end