CustomAuthTests.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 FirebaseAuth;
  18. #import "FIRAuthApiTestsBase.h"
  19. /** The user name string for Custom Auth testing account. */
  20. static NSString *const kCustomAuthTestingAccountUserID = KCUSTOM_AUTH_USER_ID;
  21. /** The url for obtaining a valid custom token string used to test Custom Auth. */
  22. static NSString *const kCustomTokenUrl = KCUSTOM_AUTH_TOKEN_URL;
  23. /** The url for obtaining an expired but valid custom token string used to test Custom Auth failure.
  24. */
  25. static NSString *const kExpiredCustomTokenUrl = KCUSTOM_AUTH_TOKEN_EXPIRED_URL;
  26. /** The invalid custom token string for testing Custom Auth. */
  27. static NSString *const kInvalidCustomToken = @"invalid token.";
  28. /** Error message for invalid custom token sign in. */
  29. NSString *kInvalidTokenErrorMessage =
  30. @"Invalid assertion format. 3 dot separated segments required.";
  31. @interface CustomAuthTests : FIRAuthApiTestsBase
  32. @end
  33. @implementation CustomAuthTests
  34. - (void)DISABLE_testSignInWithValidCustomAuthToken {
  35. FIRAuth *auth = [FIRAuth auth];
  36. if (!auth) {
  37. XCTFail(@"Could not obtain auth object.");
  38. }
  39. NSError *error;
  40. NSString *customToken = [NSString stringWithContentsOfURL:[NSURL URLWithString:kCustomTokenUrl]
  41. encoding:NSUTF8StringEncoding
  42. error:&error];
  43. if (!customToken) {
  44. XCTFail(@"There was an error retrieving the custom token: %@", error);
  45. }
  46. NSLog(@"The valid token is: %@", customToken);
  47. XCTestExpectation *expectation =
  48. [self expectationWithDescription:@"CustomAuthToken sign-in finished."];
  49. [auth signInWithCustomToken:customToken
  50. completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
  51. if (error) {
  52. NSLog(@"Valid token sign in error: %@", error);
  53. }
  54. [expectation fulfill];
  55. }];
  56. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  57. handler:^(NSError *error) {
  58. if (error != nil) {
  59. XCTFail(@"Failed to wait for expectations "
  60. @"in CustomAuthToken sign in. Error: %@",
  61. error.localizedDescription);
  62. }
  63. }];
  64. XCTAssertEqualObjects(auth.currentUser.uid, kCustomAuthTestingAccountUserID);
  65. }
  66. - (void)DISABLE_testSignInWithValidCustomAuthExpiredToken {
  67. FIRAuth *auth = [FIRAuth auth];
  68. if (!auth) {
  69. XCTFail(@"Could not obtain auth object.");
  70. }
  71. NSError *error;
  72. NSString *customToken =
  73. [NSString stringWithContentsOfURL:[NSURL URLWithString:kExpiredCustomTokenUrl]
  74. encoding:NSUTF8StringEncoding
  75. error:&error];
  76. if (!customToken) {
  77. XCTFail(@"There was an error retrieving the custom token: %@", error);
  78. }
  79. XCTestExpectation *expectation =
  80. [self expectationWithDescription:@"CustomAuthToken sign-in finished."];
  81. __block NSError *apiError;
  82. [auth signInWithCustomToken:customToken
  83. completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
  84. if (error) {
  85. apiError = error;
  86. }
  87. [expectation fulfill];
  88. }];
  89. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  90. handler:^(NSError *error) {
  91. if (error != nil) {
  92. XCTFail(@"Failed to wait for expectations "
  93. @"in CustomAuthToken sign in. Error: %@",
  94. error.localizedDescription);
  95. }
  96. }];
  97. XCTAssertNil(auth.currentUser);
  98. XCTAssertEqual(apiError.code, FIRAuthErrorCodeInvalidCustomToken);
  99. }
  100. - (void)DISABLE_testSignInWithInvalidCustomAuthToken {
  101. FIRAuth *auth = [FIRAuth auth];
  102. if (!auth) {
  103. XCTFail(@"Could not obtain auth object.");
  104. }
  105. XCTestExpectation *expectation =
  106. [self expectationWithDescription:@"Invalid CustomAuthToken sign-in finished."];
  107. [auth signInWithCustomToken:kInvalidCustomToken
  108. completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
  109. XCTAssertEqualObjects(error.localizedDescription, kInvalidTokenErrorMessage);
  110. [expectation fulfill];
  111. }];
  112. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  113. handler:^(NSError *error) {
  114. if (error != nil) {
  115. XCTFail(@"Failed to wait for expectations "
  116. @"in CustomAuthToken sign in. Error: %@",
  117. error.localizedDescription);
  118. }
  119. }];
  120. }
  121. - (void)DISABLE_testInMemoryUserAfterSignOut {
  122. FIRAuth *auth = [FIRAuth auth];
  123. if (!auth) {
  124. XCTFail(@"Could not obtain auth object.");
  125. }
  126. NSError *error;
  127. NSString *customToken = [NSString stringWithContentsOfURL:[NSURL URLWithString:kCustomTokenUrl]
  128. encoding:NSUTF8StringEncoding
  129. error:&error];
  130. if (!customToken) {
  131. XCTFail(@"There was an error retrieving the custom token: %@", error);
  132. }
  133. XCTestExpectation *expectation =
  134. [self expectationWithDescription:@"CustomAuthToken sign-in finished."];
  135. __block NSError *rpcError;
  136. [auth signInWithCustomToken:customToken
  137. completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
  138. if (error) {
  139. rpcError = error;
  140. }
  141. [expectation fulfill];
  142. }];
  143. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  144. handler:^(NSError *error) {
  145. if (error != nil) {
  146. XCTFail(@"Failed to wait for expectations "
  147. @"in CustomAuthToken sign in. Error: %@",
  148. error.localizedDescription);
  149. }
  150. }];
  151. XCTAssertEqualObjects(auth.currentUser.uid, kCustomAuthTestingAccountUserID);
  152. XCTAssertNil(rpcError);
  153. FIRUser *inMemoryUser = auth.currentUser;
  154. XCTestExpectation *expectation1 = [self expectationWithDescription:@"Profile data change."];
  155. [auth signOut:NULL];
  156. rpcError = nil;
  157. NSString *newEmailAddress = [self fakeRandomEmail];
  158. XCTAssertNotEqualObjects(newEmailAddress, inMemoryUser.email);
  159. [inMemoryUser updateEmail:newEmailAddress
  160. completion:^(NSError *_Nullable error) {
  161. rpcError = error;
  162. [expectation1 fulfill];
  163. }];
  164. [self waitForExpectationsWithTimeout:kExpectationsTimeout handler:nil];
  165. XCTAssertEqualObjects(inMemoryUser.email, newEmailAddress);
  166. XCTAssertNil(rpcError);
  167. XCTAssertNil(auth.currentUser);
  168. }
  169. @end