EmailPasswordAuthTests.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2017 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 testing email address for testCreateAccountWithEmailAndPassword. */
  19. static NSString *const kNewEmailToCreateUser = @"user+email_new_user@example.com";
  20. /** The testing email address for testSignInExistingUserWithEmailAndPassword. */
  21. static NSString *const kExistingEmailToSignIn = @"user+email_existing_user@example.com";
  22. /** The testing password for testSignInExistingUserWithEmailAndPassword. */
  23. static NSString *const kExistingPasswordToSignIn = @"password";
  24. @interface EmailPasswordAuthTests : FIRAuthApiTestsBase
  25. @end
  26. @implementation EmailPasswordAuthTests
  27. - (void)testCreateAccountWithEmailAndPassword {
  28. SKIP_IF_ON_MOBILE_HARNESS
  29. FIRAuth *auth = [FIRAuth auth];
  30. if (!auth) {
  31. XCTFail(@"Could not obtain auth object.");
  32. }
  33. XCTestExpectation *expectation =
  34. [self expectationWithDescription:@"Created account with email and password."];
  35. [auth createUserWithEmail:kNewEmailToCreateUser
  36. password:@"password"
  37. completion:^(FIRAuthDataResult *result, NSError *error) {
  38. if (error) {
  39. NSLog(@"createUserWithEmail has error: %@", error);
  40. }
  41. [expectation fulfill];
  42. }];
  43. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  44. handler:^(NSError *error) {
  45. if (error != nil) {
  46. XCTFail(@"Failed to wait for expectations "
  47. @"in creating account. Error: %@",
  48. error.localizedDescription);
  49. }
  50. }];
  51. XCTAssertEqualObjects(auth.currentUser.email, kNewEmailToCreateUser);
  52. [self deleteCurrentUser];
  53. }
  54. - (void)testSignInExistingUserWithEmailAndPassword {
  55. FIRAuth *auth = [FIRAuth auth];
  56. if (!auth) {
  57. XCTFail(@"Could not obtain auth object.");
  58. }
  59. XCTestExpectation *expectation =
  60. [self expectationWithDescription:@"Signed in existing account with email and password."];
  61. [auth signInWithEmail:kExistingEmailToSignIn
  62. password:kExistingPasswordToSignIn
  63. completion:^(FIRAuthDataResult *user, NSError *error) {
  64. if (error) {
  65. NSLog(@"Signing in existing account has error: %@", error);
  66. }
  67. [expectation fulfill];
  68. }];
  69. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  70. handler:^(NSError *error) {
  71. if (error != nil) {
  72. XCTFail(@"Failed to wait for expectations "
  73. @"in signing in existing account. Error: %@",
  74. error.localizedDescription);
  75. }
  76. }];
  77. XCTAssertEqualObjects(auth.currentUser.email, kExistingEmailToSignIn);
  78. }
  79. @end