FIRAuthApiTestsBase.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/LICENSE2.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 "FIRAuthApiTestsBase.h"
  17. @implementation FIRAuthApiTestsBase
  18. - (void)setUp {
  19. [super setUp];
  20. [self signOut];
  21. }
  22. - (void)tearDown {
  23. [super tearDown];
  24. }
  25. - (void)signInAnonymously {
  26. FIRAuth *auth = [FIRAuth auth];
  27. if (!auth) {
  28. XCTFail(@"Could not obtain auth object.");
  29. }
  30. XCTestExpectation *expectation = [self expectationWithDescription:@"Anonymous sign-in finished."];
  31. [auth signInAnonymouslyWithCompletion:^(FIRAuthDataResult *result, NSError *error) {
  32. if (error) {
  33. NSLog(@"Anonymous sign in error: %@", error);
  34. }
  35. [expectation fulfill];
  36. }];
  37. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  38. handler:^(NSError *error) {
  39. if (error != nil) {
  40. XCTFail(@"Failed to wait for expectations "
  41. @"in anonymousy sign in. Error: %@",
  42. error.localizedDescription);
  43. }
  44. }];
  45. }
  46. - (void)signOut {
  47. NSError *signOutError;
  48. BOOL status = [[FIRAuth auth] signOut:&signOutError];
  49. // Just log the error because we don't want to fail the test if signing out
  50. // fails.
  51. if (!status) {
  52. NSLog(@"Error signing out: %@", signOutError);
  53. }
  54. }
  55. - (void)deleteCurrentUser {
  56. FIRAuth *auth = [FIRAuth auth];
  57. if (!auth) {
  58. NSLog(@"Could not obtain auth object.");
  59. }
  60. XCTestExpectation *expectation =
  61. [self expectationWithDescription:@"Delete current user finished."];
  62. [auth.currentUser deleteWithCompletion:^(NSError *_Nullable error) {
  63. if (error) {
  64. XCTFail(@"Failed to delete user. Error: %@.", error);
  65. }
  66. [expectation fulfill];
  67. }];
  68. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  69. handler:^(NSError *error) {
  70. if (error != nil) {
  71. XCTFail(@"Failed to wait for expectations "
  72. @"in deleting user. Error: %@",
  73. error.localizedDescription);
  74. }
  75. }];
  76. }
  77. - (NSString *)fakeRandomEmail {
  78. NSMutableString *fakeEmail = [[NSMutableString alloc] init];
  79. for (int i = 0; i < 10; i++) {
  80. [fakeEmail
  81. appendString:[NSString stringWithFormat:@"%c", 'a' + arc4random_uniform('z' - 'a' + 1)]];
  82. }
  83. [fakeEmail appendString:@"@gmail.com"];
  84. return fakeEmail;
  85. }
  86. @end