metatext-app-ios-iphone-ipad/Tests/View Models/AddIdentityViewModelTests.s...

76 lines
2.9 KiB
Swift
Raw Normal View History

// 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()
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-08-02 09:02:03 +02:00
XCTAssertEqual(addedIdentity.id, addedIdentityID)
XCTAssertEqual(addedIdentity.url, URL(string: "https://mastodon.social")!)
2020-08-09 04:52:41 +02:00
let secretsService = SecretsService(identityID: addedIdentity.id, keychainService: environment.keychainService)
XCTAssertEqual(
2020-08-09 04:52:41 +02:00
try secretsService.item(.clientID) as String?, "AUTHORIZATION_CLIENT_ID_STUB_VALUE")
XCTAssertEqual(
2020-08-09 04:52:41 +02:00
try secretsService.item(.clientSecret) as String?, "AUTHORIZATION_CLIENT_SECRET_STUB_VALUE")
XCTAssertEqual(
2020-08-09 04:52:41 +02:00
try secretsService.item(.accessToken) as String?, "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()
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)
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))
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 {
2020-08-09 03:37:46 +02:00
let environment = AppEnvironment.fresh(webAuthSessionType: CanceledLoginMockWebAuthSession.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)
}
}