session-ios/Signal/test/Models/AccountManagerTest.swift

145 lines
4.6 KiB
Swift
Raw Normal View History

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import XCTest
import PromiseKit
import SignalServiceKit
2017-12-05 19:42:50 +01:00
@testable import Signal
struct VerificationFailedError: Error { }
struct FailedToGetRPRegistrationTokenError: Error { }
enum PushNotificationRequestResult: String {
case FailTSOnly = "FailTSOnly",
FailRPOnly = "FailRPOnly",
FailBoth = "FailBoth",
Succeed = "Succeed"
}
class FailingTSAccountManager: TSAccountManager {
2018-10-15 18:50:07 +02:00
override public init(primaryStorage: OWSPrimaryStorage) {
AssertIsOnMainThread()
2018-05-30 21:28:03 +02:00
2018-10-15 18:50:07 +02:00
super.init(primaryStorage: primaryStorage)
2018-05-30 21:28:03 +02:00
self.phoneNumberAwaitingVerification = "+13235555555"
}
2018-03-12 21:49:57 +01:00
override func verifyAccount(withCode: String,
pin: String?,
success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
failure(VerificationFailedError())
}
override func registerForPushNotifications(pushToken: String, voipToken: String, success successHandler: @escaping () -> Void, failure failureHandler: @escaping (Error) -> Void) {
if pushToken == PushNotificationRequestResult.FailTSOnly.rawValue || pushToken == PushNotificationRequestResult.FailBoth.rawValue {
failureHandler(OWSErrorMakeUnableToProcessServerResponseError())
} else {
successHandler()
}
}
}
class VerifyingTSAccountManager: FailingTSAccountManager {
2018-10-15 20:34:41 +02:00
override func verifyAccount(withCode
: String, pin
: String ?, success : @escaping()->Void, failure
: @escaping(Error)->Void) { success() } s
}
class TokenObtainingTSAccountManager: VerifyingTSAccountManager {
}
2018-09-07 23:19:24 +02:00
class AccountManagerTest: SignalBaseTest {
2018-10-15 18:50:07 +02:00
override func setUp() {
super.setUp()
let tsAccountManager = FailingTSAccountManager(primaryStorage: OWSPrimaryStorage.shared())
let sskEnvironment = SSKEnvironment.shared as! MockSSKEnvironment
sskEnvironment.tsAccountManager = tsAccountManager
}
override func tearDown() {
super.tearDown()
}
func testRegisterWhenEmptyCode() {
2018-10-15 18:50:07 +02:00
let accountManager = AccountManager()
let expectation = self.expectation(description: "should fail")
firstly {
2018-03-12 21:49:57 +01:00
accountManager.register(verificationCode: "", pin: "")
2018-10-13 21:21:46 +02:00
}.done {
XCTFail("Should fail")
}.catch { error in
let nserror = error as NSError
if OWSErrorCode(rawValue: nserror.code) == OWSErrorCode.userError {
expectation.fulfill()
} else {
XCTFail("Unexpected error: \(error)")
}
2018-10-13 21:21:46 +02:00
}.retainUntilComplete()
self.waitForExpectations(timeout: 1.0, handler: nil)
}
func testRegisterWhenVerificationFails() {
2018-10-15 18:50:07 +02:00
let accountManager = AccountManager()
let expectation = self.expectation(description: "should fail")
firstly {
2018-03-12 21:49:57 +01:00
accountManager.register(verificationCode: "123456", pin: "")
2018-10-13 21:21:46 +02:00
}.done {
XCTFail("Should fail")
}.catch { error in
if error is VerificationFailedError {
expectation.fulfill()
} else {
XCTFail("Unexpected error: \(error)")
}
2018-10-13 21:21:46 +02:00
}.retainUntilComplete()
self.waitForExpectations(timeout: 1.0, handler: nil)
}
func testSuccessfulRegistration() {
2018-10-15 18:50:07 +02:00
let tsAccountManager = TokenObtainingTSAccountManager(primaryStorage: OWSPrimaryStorage.shared())
2018-10-15 19:39:45 +02:00
let sskEnvironment = SSKEnvironment.shared as! MockSSKEnvironment
sskEnvironment.tsAccountManager = tsAccountManager
2017-07-11 06:33:12 +02:00
2018-10-15 18:50:07 +02:00
let accountManager = AccountManager()
let expectation = self.expectation(description: "should succeed")
firstly {
2018-03-12 21:49:57 +01:00
accountManager.register(verificationCode: "123456", pin: "")
2018-10-13 21:21:46 +02:00
}.done {
expectation.fulfill()
}.catch { error in
XCTFail("Unexpected error: \(error)")
2018-10-13 21:21:46 +02:00
}.retainUntilComplete()
2017-12-05 19:49:18 +01:00
self.waitForExpectations(timeout: 1.0, handler: nil)
}
func testUpdatePushTokens() {
2018-10-15 18:50:07 +02:00
let accountManager = AccountManager()
let expectation = self.expectation(description: "should fail")
2018-10-13 21:21:46 +02:00
firstly {
accountManager.updatePushTokens(pushToken: PushNotificationRequestResult.FailTSOnly.rawValue, voipToken: "whatever")
}.done {
XCTFail("Expected to fail.")
}.catch { _ in
expectation.fulfill()
2018-10-13 21:21:46 +02:00
}.retainUntilComplete()
self.waitForExpectations(timeout: 1.0, handler: nil)
}
}