2020-07-30 01:50:30 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
import Combine
|
|
|
|
import CombineExpectations
|
|
|
|
@testable import Metatext
|
|
|
|
|
|
|
|
class AddIdentityViewModelTests: XCTestCase {
|
|
|
|
func testAddIdentity() throws {
|
2020-08-03 00:23:01 +02:00
|
|
|
let environment = AppEnvironment.fresh()
|
2020-08-03 17:20:51 +02:00
|
|
|
let sut = AddIdentityViewModel(environment: environment)
|
|
|
|
let addedIDRecorder = sut.addedIdentityID.record()
|
2020-07-30 01:50:30 +02:00
|
|
|
|
|
|
|
sut.urlFieldText = "https://mastodon.social"
|
|
|
|
sut.goTapped()
|
|
|
|
|
2020-08-03 17:20:51 +02:00
|
|
|
let addedIdentityID = try wait(for: addedIDRecorder.next(), timeout: 1)
|
2020-08-03 00:23:01 +02:00
|
|
|
let identityRecorder = environment.identityDatabase.identityObservation(id: addedIdentityID).record()
|
2020-08-03 17:20:51 +02:00
|
|
|
let addedIdentity = try wait(for: identityRecorder.next(), timeout: 1)
|
2020-07-30 01:50:30 +02:00
|
|
|
|
2020-08-02 09:02:03 +02:00
|
|
|
XCTAssertEqual(addedIdentity.id, addedIdentityID)
|
2020-07-30 01:50:30 +02:00
|
|
|
XCTAssertEqual(addedIdentity.url, URL(string: "https://mastodon.social")!)
|
|
|
|
XCTAssertEqual(
|
2020-08-03 00:23:01 +02:00
|
|
|
try environment.secrets.item(.clientID, forIdentityID: addedIdentityID) as String?,
|
2020-07-30 01:50:30 +02:00
|
|
|
"AUTHORIZATION_CLIENT_ID_STUB_VALUE")
|
|
|
|
XCTAssertEqual(
|
2020-08-03 00:23:01 +02:00
|
|
|
try environment.secrets.item(.clientSecret, forIdentityID: addedIdentityID) as String?,
|
2020-07-30 01:50:30 +02:00
|
|
|
"AUTHORIZATION_CLIENT_SECRET_STUB_VALUE")
|
|
|
|
XCTAssertEqual(
|
2020-08-03 00:23:01 +02:00
|
|
|
try environment.secrets.item(.accessToken, forIdentityID: addedIdentityID) as String?,
|
2020-07-30 01:50:30 +02:00
|
|
|
"ACCESS_TOKEN_STUB_VALUE")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAddIdentityWithoutScheme() throws {
|
2020-08-03 00:23:01 +02:00
|
|
|
let environment = AppEnvironment.fresh()
|
2020-08-03 17:20:51 +02:00
|
|
|
let sut = AddIdentityViewModel(environment: environment)
|
|
|
|
let addedIDRecorder = sut.addedIdentityID.record()
|
2020-07-30 01:50:30 +02:00
|
|
|
|
|
|
|
sut.urlFieldText = "mastodon.social"
|
|
|
|
sut.goTapped()
|
|
|
|
|
2020-08-03 17:20:51 +02:00
|
|
|
let addedIdentityID = try wait(for: addedIDRecorder.next(), timeout: 1)
|
2020-08-03 00:23:01 +02:00
|
|
|
let identityRecorder = environment.identityDatabase.identityObservation(id: addedIdentityID).record()
|
2020-08-03 17:20:51 +02:00
|
|
|
let addedIdentity = try wait(for: identityRecorder.next(), timeout: 1)
|
2020-07-30 01:50:30 +02:00
|
|
|
|
|
|
|
XCTAssertEqual(addedIdentity.url, URL(string: "https://mastodon.social")!)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testInvalidURL() throws {
|
2020-08-03 17:20:51 +02:00
|
|
|
let sut = AddIdentityViewModel(environment: .fresh())
|
2020-08-03 00:23:01 +02:00
|
|
|
let recorder = sut.$alertItem.record()
|
|
|
|
|
|
|
|
XCTAssertNil(try wait(for: recorder.next(), timeout: 1))
|
2020-07-30 01:50:30 +02:00
|
|
|
|
|
|
|
sut.urlFieldText = "🐘.social"
|
|
|
|
sut.goTapped()
|
|
|
|
|
|
|
|
let alertItem = try wait(for: recorder.next(), timeout: 1)
|
|
|
|
|
|
|
|
XCTAssertEqual((alertItem?.error as? URLError)?.code, URLError.badURL)
|
|
|
|
}
|
2020-08-03 00:23:01 +02:00
|
|
|
|
|
|
|
func testDoesNotAlertCanceledLogin() throws {
|
|
|
|
let environment = AppEnvironment.fresh(webAuthSessionType: CanceledLoginStubbingWebAuthSession.self)
|
2020-08-03 17:20:51 +02:00
|
|
|
let sut = AddIdentityViewModel(environment: environment)
|
2020-08-03 00:23:01 +02:00
|
|
|
let recorder = sut.$alertItem.record()
|
|
|
|
|
|
|
|
XCTAssertNil(try wait(for: recorder.next(), timeout: 1))
|
|
|
|
|
|
|
|
sut.urlFieldText = "https://mastodon.social"
|
|
|
|
sut.goTapped()
|
|
|
|
|
|
|
|
try wait(for: recorder.next().inverted, timeout: 1)
|
|
|
|
}
|
2020-07-30 01:50:30 +02:00
|
|
|
}
|