element-ios/RiotSwiftUI/Modules/UserSessions/UserSessionDetails/Test/Unit/UserSessionDetailsViewModel...

168 lines
7.5 KiB
Swift

//
// Copyright 2021-2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
//
import XCTest
@testable import RiotSwiftUI
class UserSessionDetailsViewModelTests: XCTestCase {
private static var lastSeenDateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EE, d MMM · HH:mm"
return dateFormatter
}()
func test_whenSessionNameAndLastSeenIPNil_viewStateCorrect() {
let userSessionInfo = createUserSessionInfo(id: "session",
name: nil,
lastSeenIP: nil)
let sessionItems = [
sessionIdItem(sessionId: "session")
]
let sections = [
UserSessionDetailsSectionViewData(header: VectorL10n.userSessionDetailsSessionSectionHeader.uppercased(),
footer: VectorL10n.userSessionDetailsSessionSectionFooter,
items: sessionItems)
]
let expectedModel = UserSessionDetailsViewState(sections: sections)
let sut = UserSessionDetailsViewModel(sessionInfo: userSessionInfo)
XCTAssertEqual(sut.state, expectedModel)
}
func test_whenSessionNameNotNilLastSeenIPNil_viewStateCorrect() {
let userSessionInfo = createUserSessionInfo(id: "session",
name: "session name",
lastSeenIP: nil)
let sessionItems = [
sessionNameItem(sessionName: "session name"),
sessionIdItem(sessionId: "session")
]
let sections = [
UserSessionDetailsSectionViewData(header: VectorL10n.userSessionDetailsSessionSectionHeader.uppercased(),
footer: VectorL10n.userSessionDetailsSessionSectionFooter,
items: sessionItems)
]
let expectedModel = UserSessionDetailsViewState(sections: sections)
let sut = UserSessionDetailsViewModel(sessionInfo: userSessionInfo)
XCTAssertEqual(sut.state, expectedModel)
}
func test_whenUserSessionInfoContainsAllValues_viewStateCorrect() {
let lastSeenTimestamp = Date().timeIntervalSince1970 - 1_000_000
let userSessionInfo = createUserSessionInfo(id: "session",
name: "session name",
lastSeenIP: "0.0.0.0",
lastSeenTimestamp: lastSeenTimestamp,
applicationName: "Element iOS",
applicationVersion: "1.0.0")
let sessionItems = [
sessionNameItem(sessionName: "session name"),
sessionIdItem(sessionId: "session"),
sessionLastActivity(lastSeen: lastSeenTimestamp)
]
let appItems = [
appNameItem(appName: "Element iOS"),
appVersionItem(appVersion: "1.0.0")
]
let deviceItems = [
ipAddressItem(ipAddress: "0.0.0.0")
]
let sections = [
UserSessionDetailsSectionViewData(header: VectorL10n.userSessionDetailsSessionSectionHeader.uppercased(),
footer: VectorL10n.userSessionDetailsSessionSectionFooter,
items: sessionItems),
UserSessionDetailsSectionViewData(header: VectorL10n.userSessionDetailsApplicationSectionHeader.uppercased(),
footer: nil,
items: appItems),
UserSessionDetailsSectionViewData(header: VectorL10n.userSessionDetailsDeviceSectionHeader.uppercased(),
footer: nil,
items: deviceItems)
]
let expectedModel = UserSessionDetailsViewState(sections: sections)
let sut = UserSessionDetailsViewModel(sessionInfo: userSessionInfo)
XCTAssertEqual(sut.state, expectedModel)
}
// MARK: - Private
private func createUserSessionInfo(id: String,
name: String?,
deviceType: DeviceType = .mobile,
isVerified: Bool = false,
lastSeenIP: String?,
lastSeenTimestamp: TimeInterval? = nil,
applicationName: String? = nil,
applicationVersion: String? = nil,
applicationURL: String? = nil,
deviceModel: String? = nil,
deviceOS: String? = nil,
lastSeenIPLocation: String? = nil,
clientName: String? = nil,
clientVersion: String? = nil,
isActive: Bool = true,
isCurrent: Bool = true) -> UserSessionInfo {
UserSessionInfo(id: id,
name: name,
deviceType: deviceType,
verificationState: isVerified ? .verified : .unverified,
lastSeenIP: lastSeenIP,
lastSeenTimestamp: lastSeenTimestamp,
applicationName: applicationName,
applicationVersion: applicationVersion,
applicationURL: applicationURL,
deviceModel: deviceModel,
deviceOS: deviceOS,
lastSeenIPLocation: lastSeenIPLocation,
clientName: clientName,
clientVersion: clientVersion,
isActive: isActive,
isCurrent: isCurrent)
}
private func sessionNameItem(sessionName: String) -> UserSessionDetailsSectionItemViewData {
.init(title: VectorL10n.userSessionDetailsSessionName,
value: sessionName)
}
private func sessionIdItem(sessionId: String) -> UserSessionDetailsSectionItemViewData {
.init(title: VectorL10n.keyVerificationManuallyVerifyDeviceIdTitle,
value: sessionId)
}
private func sessionLastActivity(lastSeen: TimeInterval) -> UserSessionDetailsSectionItemViewData {
.init(title: VectorL10n.userSessionDetailsLastActivity,
value: Self.lastSeenDateFormatter.string(from: Date(timeIntervalSince1970: lastSeen)))
}
private func appNameItem(appName: String) -> UserSessionDetailsSectionItemViewData {
.init(title: VectorL10n.userSessionDetailsApplicationName,
value: appName)
}
private func appVersionItem(appVersion: String) -> UserSessionDetailsSectionItemViewData {
.init(title: VectorL10n.userSessionDetailsApplicationVersion,
value: appVersion)
}
private func ipAddressItem(ipAddress: String) -> UserSessionDetailsSectionItemViewData {
.init(title: VectorL10n.userSessionDetailsDeviceIpAddress,
value: ipAddress)
}
}