2020-07-29 23:50:30 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import CombineExpectations
|
2020-08-31 01:40:58 +00:00
|
|
|
import HTTP
|
2020-08-31 00:18:19 +00:00
|
|
|
import Mastodon
|
2020-09-04 00:54:05 +00:00
|
|
|
import MockKeychain
|
2020-08-31 18:57:02 +00:00
|
|
|
import ServiceLayer
|
2020-09-01 07:33:49 +00:00
|
|
|
import ServiceLayerMocks
|
|
|
|
@testable import ViewModels
|
2020-09-05 02:31:43 +00:00
|
|
|
import XCTest
|
2020-08-31 10:21:01 +00:00
|
|
|
|
2020-07-29 23:50:30 +00:00
|
|
|
class AddIdentityViewModelTests: XCTestCase {
|
|
|
|
func testAddIdentity() throws {
|
2020-09-13 00:50:22 +00:00
|
|
|
let uuid = UUID(uuidString: "E621E1F8-C36C-495A-93FC-0C247A3E6E5F")!
|
|
|
|
let environment = AppEnvironment.mock(uuid: { uuid })
|
|
|
|
let allIdentitiesService = try AllIdentitiesService(environment: environment)
|
2020-09-09 12:05:43 +00:00
|
|
|
let sut = AddIdentityViewModel(
|
2020-09-13 00:50:22 +00:00
|
|
|
allIdentitiesService: allIdentitiesService,
|
2020-09-10 04:51:31 +00:00
|
|
|
instanceURLService: InstanceURLService(environment: environment))
|
2020-10-05 22:50:05 +00:00
|
|
|
let addedIdRecorder = allIdentitiesService.identitiesCreated.record()
|
2020-07-29 23:50:30 +00:00
|
|
|
|
|
|
|
sut.urlFieldText = "https://mastodon.social"
|
2020-08-09 11:27:38 +00:00
|
|
|
sut.logInTapped()
|
2020-07-29 23:50:30 +00:00
|
|
|
|
2020-10-05 22:50:05 +00:00
|
|
|
_ = try wait(for: addedIdRecorder.next(), timeout: 1)
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testAddIdentityWithoutScheme() throws {
|
2020-09-13 00:50:22 +00:00
|
|
|
let uuid = UUID(uuidString: "E621E1F8-C36C-495A-93FC-0C247A3E6E5F")!
|
|
|
|
let environment = AppEnvironment.mock(uuid: { uuid })
|
|
|
|
let allIdentitiesService = try AllIdentitiesService(environment: environment)
|
2020-09-09 12:05:43 +00:00
|
|
|
let sut = AddIdentityViewModel(
|
2020-09-13 00:50:22 +00:00
|
|
|
allIdentitiesService: allIdentitiesService,
|
2020-09-10 04:51:31 +00:00
|
|
|
instanceURLService: InstanceURLService(environment: environment))
|
2020-10-05 22:50:05 +00:00
|
|
|
let addedIdRecorder = allIdentitiesService.identitiesCreated.record()
|
2020-07-29 23:50:30 +00:00
|
|
|
|
|
|
|
sut.urlFieldText = "mastodon.social"
|
2020-08-09 11:27:38 +00:00
|
|
|
sut.logInTapped()
|
2020-07-29 23:50:30 +00:00
|
|
|
|
2020-10-05 22:50:05 +00:00
|
|
|
_ = try wait(for: addedIdRecorder.next(), timeout: 1)
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testInvalidURL() throws {
|
2020-09-09 12:05:43 +00:00
|
|
|
let environment = AppEnvironment.mock()
|
|
|
|
let sut = AddIdentityViewModel(
|
|
|
|
allIdentitiesService: try AllIdentitiesService(environment: environment),
|
2020-09-10 04:51:31 +00:00
|
|
|
instanceURLService: InstanceURLService(environment: environment))
|
2020-08-02 22:23:01 +00:00
|
|
|
let recorder = sut.$alertItem.record()
|
|
|
|
|
|
|
|
XCTAssertNil(try wait(for: recorder.next(), timeout: 1))
|
2020-07-29 23:50:30 +00:00
|
|
|
|
|
|
|
sut.urlFieldText = "🐘.social"
|
2020-08-09 11:27:38 +00:00
|
|
|
sut.logInTapped()
|
2020-07-29 23:50:30 +00:00
|
|
|
|
|
|
|
let alertItem = try wait(for: recorder.next(), timeout: 1)
|
|
|
|
|
2020-09-10 04:51:31 +00:00
|
|
|
XCTAssertEqual((alertItem?.error as? AddIdentityError), AddIdentityError.unableToConnectToInstance)
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
2020-08-02 22:23:01 +00:00
|
|
|
|
|
|
|
func testDoesNotAlertCanceledLogin() throws {
|
2020-09-09 12:05:43 +00:00
|
|
|
let environment = AppEnvironment.mock(webAuthSessionType: CanceledLoginMockWebAuthSession.self)
|
|
|
|
let sut = AddIdentityViewModel(
|
|
|
|
allIdentitiesService: try AllIdentitiesService(environment: environment),
|
2020-09-10 04:51:31 +00:00
|
|
|
instanceURLService: InstanceURLService(environment: environment))
|
2020-08-02 22:23:01 +00:00
|
|
|
let recorder = sut.$alertItem.record()
|
|
|
|
|
|
|
|
XCTAssertNil(try wait(for: recorder.next(), timeout: 1))
|
|
|
|
|
|
|
|
sut.urlFieldText = "https://mastodon.social"
|
2020-08-09 11:27:38 +00:00
|
|
|
sut.logInTapped()
|
2020-08-02 22:23:01 +00:00
|
|
|
|
|
|
|
try wait(for: recorder.next().inverted, timeout: 1)
|
|
|
|
}
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|