CustomAuthTests.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import "FIRAuthApiTestsBase.h"
  18. /** The user name string for Custom Auth testing account. */
  19. static NSString *const kCustomAuthTestingAccountUserID = KCUSTOM_AUTH_USER_ID;
  20. /** The url for obtaining a valid custom token string used to test Custom Auth. */
  21. static NSString *const kCustomTokenUrl = KCUSTOM_AUTH_TOKEN_URL;
  22. /** The url for obtaining an expired but valid custom token string used to test Custom Auth failure.
  23. */
  24. static NSString *const kExpiredCustomTokenUrl = KCUSTOM_AUTH_TOKEN_EXPIRED_URL;
  25. /** The invalid custom token string for testing Custom Auth. */
  26. static NSString *const kInvalidCustomToken = @"invalid token.";
  27. /** Error message for invalid custom token sign in. */
  28. NSString *kInvalidTokenErrorMessage =
  29. @"Invalid assertion format. 3 dot separated segments required.";
  30. @interface CustomAuthTests : FIRAuthApiTestsBase
  31. @end
  32. @implementation CustomAuthTests
  33. - (void)DISABLE_testSignInWithValidCustomAuthToken {
  34. FIRAuth *auth = [FIRAuth auth];
  35. if (!auth) {
  36. XCTFail(@"Could not obtain auth object.");
  37. }
  38. NSError *error;
  39. NSString *customToken = [NSString stringWithContentsOfURL:[NSURL URLWithString:kCustomTokenUrl]
  40. encoding:NSUTF8StringEncoding
  41. error:&error];
  42. if (!customToken) {
  43. XCTFail(@"There was an error retrieving the custom token: %@", error);
  44. }
  45. NSLog(@"The valid token is: %@", customToken);
  46. XCTestExpectation *expectation =
  47. [self expectationWithDescription:@"CustomAuthToken sign-in finished."];
  48. [auth signInWithCustomToken:customToken
  49. completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
  50. if (error) {
  51. NSLog(@"Valid token sign in error: %@", error);
  52. }
  53. [expectation fulfill];
  54. }];
  55. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  56. handler:^(NSError *error) {
  57. if (error != nil) {
  58. XCTFail(@"Failed to wait for expectations "
  59. @"in CustomAuthToken sign in. Error: %@",
  60. error.localizedDescription);
  61. }
  62. }];
  63. XCTAssertEqualObjects(auth.currentUser.uid, kCustomAuthTestingAccountUserID);
  64. }
  65. - (void)DISABLE_testSignInWithValidCustomAuthExpiredToken {
  66. FIRAuth *auth = [FIRAuth auth];
  67. if (!auth) {
  68. XCTFail(@"Could not obtain auth object.");
  69. }
  70. NSError *error;
  71. NSString *customToken =
  72. [NSString stringWithContentsOfURL:[NSURL URLWithString:kExpiredCustomTokenUrl]
  73. encoding:NSUTF8StringEncoding
  74. error:&error];
  75. if (!customToken) {
  76. XCTFail(@"There was an error retrieving the custom token: %@", error);
  77. }
  78. XCTestExpectation *expectation =
  79. [self expectationWithDescription:@"CustomAuthToken sign-in finished."];
  80. __block NSError *apiError;
  81. [auth signInWithCustomToken:customToken
  82. completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
  83. if (error) {
  84. apiError = error;
  85. }
  86. [expectation fulfill];
  87. }];
  88. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  89. handler:^(NSError *error) {
  90. if (error != nil) {
  91. XCTFail(@"Failed to wait for expectations "
  92. @"in CustomAuthToken sign in. Error: %@",
  93. error.localizedDescription);
  94. }
  95. }];
  96. XCTAssertNil(auth.currentUser);
  97. XCTAssertEqual(apiError.code, FIRAuthErrorCodeInvalidCustomToken);
  98. }
  99. - (void)DISABLE_testSignInWithInvalidCustomAuthToken {
  100. FIRAuth *auth = [FIRAuth auth];
  101. if (!auth) {
  102. XCTFail(@"Could not obtain auth object.");
  103. }
  104. XCTestExpectation *expectation =
  105. [self expectationWithDescription:@"Invalid CustomAuthToken sign-in finished."];
  106. [auth signInWithCustomToken:kInvalidCustomToken
  107. completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
  108. XCTAssertEqualObjects(error.localizedDescription, kInvalidTokenErrorMessage);
  109. [expectation fulfill];
  110. }];
  111. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  112. handler:^(NSError *error) {
  113. if (error != nil) {
  114. XCTFail(@"Failed to wait for expectations "
  115. @"in CustomAuthToken sign in. Error: %@",
  116. error.localizedDescription);
  117. }
  118. }];
  119. }
  120. - (void)DISABLE_testInMemoryUserAfterSignOut {
  121. FIRAuth *auth = [FIRAuth auth];
  122. if (!auth) {
  123. XCTFail(@"Could not obtain auth object.");
  124. }
  125. NSError *error;
  126. NSString *customToken = [NSString stringWithContentsOfURL:[NSURL URLWithString:kCustomTokenUrl]
  127. encoding:NSUTF8StringEncoding
  128. error:&error];
  129. if (!customToken) {
  130. XCTFail(@"There was an error retrieving the custom token: %@", error);
  131. }
  132. XCTestExpectation *expectation =
  133. [self expectationWithDescription:@"CustomAuthToken sign-in finished."];
  134. __block NSError *rpcError;
  135. [auth signInWithCustomToken:customToken
  136. completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
  137. if (error) {
  138. rpcError = error;
  139. }
  140. [expectation fulfill];
  141. }];
  142. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  143. handler:^(NSError *error) {
  144. if (error != nil) {
  145. XCTFail(@"Failed to wait for expectations "
  146. @"in CustomAuthToken sign in. Error: %@",
  147. error.localizedDescription);
  148. }
  149. }];
  150. XCTAssertEqualObjects(auth.currentUser.uid, kCustomAuthTestingAccountUserID);
  151. XCTAssertNil(rpcError);
  152. FIRUser *inMemoryUser = auth.currentUser;
  153. XCTestExpectation *expectation1 = [self expectationWithDescription:@"Profile data change."];
  154. [auth signOut:NULL];
  155. rpcError = nil;
  156. NSString *newEmailAddress = [self fakeRandomEmail];
  157. XCTAssertNotEqualObjects(newEmailAddress, inMemoryUser.email);
  158. [inMemoryUser updateEmail:newEmailAddress
  159. completion:^(NSError *_Nullable error) {
  160. rpcError = error;
  161. [expectation1 fulfill];
  162. }];
  163. [self waitForExpectationsWithTimeout:kExpectationsTimeout handler:nil];
  164. XCTAssertEqualObjects(inMemoryUser.email, newEmailAddress);
  165. XCTAssertNil(rpcError);
  166. XCTAssertNil(auth.currentUser);
  167. }
  168. @end