| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- // Copyright 2020 Google LLC
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- import Foundation
- import Combine
- import XCTest
- import FirebaseAuth
- class PasswordResetTests: XCTestCase {
- override class func setUp() {
- FirebaseApp.configureForTests()
- }
- override class func tearDown() {
- FirebaseApp.app()?.delete { success in
- if success {
- print("Shut down app successfully.")
- } else {
- print("💥 There was a problem when shutting down the app..")
- }
- }
- }
- override func setUp() {
- do {
- try Auth.auth().signOut()
- } catch {}
- }
- static let apiKey = Credentials.apiKey
- static let fakeEmail = "fakeEmail"
- static let fakeNewEmail = "fakeNewEmail"
- static let fakeCode = "fakeCode"
- static let fakeNewPassword = "fakeNewPassword"
- static let passwordResetRequestType = "PASSWORD_RESET"
- static let verifyEmailRequestType = "VERIFY_EMAIL"
- func testResetPassword() {
- // given
- class MockResetPasswordResponse: FIRResetPasswordResponse {
- override var email: String { return PasswordResetTests.fakeEmail }
- }
- class MockAuthBackend: AuthBackendImplementationMock {
- override func resetPassword(_ request: FIRResetPasswordRequest,
- callback: @escaping FIRResetPasswordCallback) {
- XCTAssertEqual(request.apiKey, PasswordResetTests.apiKey)
- XCTAssertEqual(request.oobCode, PasswordResetTests.fakeCode)
- XCTAssertEqual(request.updatedPassword, PasswordResetTests.fakeNewPassword)
- callback(MockResetPasswordResponse(), nil)
- }
- }
- FIRAuthBackend.setBackendImplementation(MockAuthBackend())
- var cancellables = Set<AnyCancellable>()
- let confirmPasswordResetExpectation = expectation(description: "Password reset confirmed")
- // when
- Auth.auth()
- .confirmPasswordReset(
- withCode: PasswordResetTests.fakeCode,
- newPassword: PasswordResetTests.fakeNewPassword
- )
- .sink(receiveCompletion: { completion in
- switch completion {
- case .finished:
- print("Finished")
- case let .failure(error):
- XCTFail("💥 Something went wrong: \(error)")
- }
- }, receiveValue: {
- confirmPasswordResetExpectation.fulfill()
- })
- .store(in: &cancellables)
- // then
- wait(for: [confirmPasswordResetExpectation], timeout: expectationTimeout)
- }
- func testVerifyPasswordResetCode() {
- // given
- class MockResetPasswordResponse: FIRResetPasswordResponse {
- override var email: String { return PasswordResetTests.fakeEmail }
- override var requestType: String { return PasswordResetTests.passwordResetRequestType }
- }
- class MockAuthBackend: AuthBackendImplementationMock {
- override func resetPassword(_ request: FIRResetPasswordRequest,
- callback: @escaping FIRResetPasswordCallback) {
- XCTAssertEqual(request.apiKey, PasswordResetTests.apiKey)
- XCTAssertEqual(request.oobCode, PasswordResetTests.fakeCode)
- callback(MockResetPasswordResponse(), nil)
- }
- }
- FIRAuthBackend.setBackendImplementation(MockAuthBackend())
- var cancellables = Set<AnyCancellable>()
- let verifyPasswordResetCodeExpectation =
- expectation(description: "Password reset code verified")
- // when
- Auth.auth()
- .verifyPasswordResetCode(PasswordResetTests.fakeCode)
- .sink { completion in
- switch completion {
- case .finished:
- print("Finished")
- case let .failure(error):
- XCTFail("💥 Something went wrong: \(error)")
- }
- } receiveValue: { email in
- verifyPasswordResetCodeExpectation.fulfill()
- }
- .store(in: &cancellables)
- // then
- wait(for: [verifyPasswordResetCodeExpectation], timeout: expectationTimeout)
- }
- func testCheckActionCode() {
- // given
- class MockResetPasswordResponse: FIRResetPasswordResponse {
- override var email: String { return PasswordResetTests.fakeEmail }
- override var verifiedEmail: String { return PasswordResetTests.fakeNewEmail }
- override var requestType: String { return PasswordResetTests.verifyEmailRequestType }
- }
- class MockAuthBackend: AuthBackendImplementationMock {
- override func resetPassword(_ request: FIRResetPasswordRequest,
- callback: @escaping FIRResetPasswordCallback) {
- XCTAssertEqual(request.apiKey, PasswordResetTests.apiKey)
- XCTAssertEqual(request.oobCode, PasswordResetTests.fakeCode)
- callback(MockResetPasswordResponse(), nil)
- }
- }
- FIRAuthBackend.setBackendImplementation(MockAuthBackend())
- var cancellables = Set<AnyCancellable>()
- let checkActionCodeExpectation = expectation(description: "Action code checked")
- // when
- Auth.auth()
- .checkActionCode(code: PasswordResetTests.fakeCode)
- .sink { completion in
- switch completion {
- case .finished:
- print("Finished")
- case let .failure(error):
- XCTFail("💥 Something went wrong: \(error)")
- }
- } receiveValue: { actionCodeInfo in
- XCTAssertEqual(actionCodeInfo.operation, ActionCodeOperation.verifyEmail)
- XCTAssertEqual(actionCodeInfo.email, PasswordResetTests.fakeNewEmail)
- checkActionCodeExpectation.fulfill()
- }
- .store(in: &cancellables)
- // then
- wait(for: [checkActionCodeExpectation], timeout: expectationTimeout)
- }
- public func testApplyActionCode() {
- // given
- class MockSetAccountInfoResponse: FIRSetAccountInfoResponse {}
- class MockAuthBackend: AuthBackendImplementationMock {
- override func setAccountInfo(_ request: FIRSetAccountInfoRequest,
- callback: @escaping FIRSetAccountInfoResponseCallback) {
- callback(MockSetAccountInfoResponse(), nil)
- }
- }
- FIRAuthBackend.setBackendImplementation(MockAuthBackend())
- var cancellables = Set<AnyCancellable>()
- let applyActionCodeExpectation = expectation(description: "Action code applied")
- // when
- Auth.auth()
- .applyActionCode(code: PasswordResetTests.fakeCode)
- .sink { completion in
- switch completion {
- case .finished:
- print("Finished")
- case let .failure(error):
- XCTFail("💥 Something went wrong: \(error)")
- }
- } receiveValue: {
- applyActionCodeExpectation.fulfill()
- }
- .store(in: &cancellables)
- // then
- wait(for: [applyActionCodeExpectation], timeout: expectationTimeout)
- }
- func testSendPasswordResetEmail() {
- // given
- class MockAuthBackend: AuthBackendImplementationMock {
- override func getOOBConfirmationCode(_ request: FIRGetOOBConfirmationCodeRequest,
- callback: @escaping FIRGetOOBConfirmationCodeResponseCallback) {
- XCTAssertEqual(request.apiKey, PasswordResetTests.apiKey)
- XCTAssertEqual(request.email, PasswordResetTests.fakeEmail)
- callback(FIRGetOOBConfirmationCodeResponse(), nil)
- }
- }
- FIRAuthBackend.setBackendImplementation(MockAuthBackend())
- var cancellables = Set<AnyCancellable>()
- let sendPasswordResetExpectation = expectation(description: "Password reset sent")
- // when
- Auth.auth()
- .sendPasswordReset(withEmail: PasswordResetTests.fakeEmail)
- .sink { completion in
- switch completion {
- case .finished:
- print("Finished")
- case let .failure(error):
- XCTFail("💥 Something went wrong: \(error)")
- }
- } receiveValue: {
- sendPasswordResetExpectation.fulfill()
- }
- .store(in: &cancellables)
- // then
- wait(for: [sendPasswordResetExpectation], timeout: expectationTimeout)
- }
- }
|