FIRStorageTokenAuthorizerTests.m 9.3 KB

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