FIRStorageTokenAuthorizerTests.m 9.7 KB

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