-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLoginFormTests.swift
116 lines (101 loc) · 4.01 KB
/
LoginFormTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
@testable import Coder_Desktop
@testable import CoderSDK
import Mocker
import SwiftUI
import Testing
import ViewInspector
@MainActor
@Suite(.timeLimit(.minutes(1)))
struct LoginTests {
let state: AppState
let sut: LoginForm
let view: any View
init() {
state = AppState(persistent: false)
sut = LoginForm()
let store = UserDefaults(suiteName: #file)!
store.removePersistentDomain(forName: #file)
view = sut.environmentObject(state)
}
@Test
func testInitialView() async throws {
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
#expect(throws: Never.self) { try view.find(text: "Coder Desktop") }
#expect(throws: Never.self) { try view.find(text: "Server URL") }
#expect(throws: Never.self) { try view.find(button: "Next") }
}
}
}
@Test
func testInvalidServerURL() async throws {
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
try view.find(ViewType.TextField.self).setInput("http://")
try view.find(button: "Next").tap()
#expect(throws: Never.self) { try view.find(ViewType.Alert.self) }
}
}
}
@Test
func testValidServerURL() async throws {
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
try view.find(ViewType.TextField.self).setInput("https://coder.example.com")
try view.find(button: "Next").tap()
#expect(throws: Never.self) { try view.find(text: "Session Token") }
#expect(throws: Never.self) { try view.find(ViewType.SecureField.self) }
#expect(throws: Never.self) { try view.find(button: "Sign In") }
}
}
}
@Test
func testBackButton() async throws {
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
try view.find(ViewType.TextField.self).setInput("https://coder.example.com")
try view.find(button: "Next").tap()
try view.find(button: "Back").tap()
#expect(throws: Never.self) { try view.find(text: "Coder Desktop") }
#expect(throws: Never.self) { try view.find(button: "Next") }
}
}
}
@Test
func testFailedAuthentication() async throws {
let url = URL(string: "https://testFailedAuthentication.com")!
Mock(url: url.appendingPathComponent("/api/v2/users/me"), statusCode: 401, data: [.get: Data()]).register()
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
try view.find(ViewType.TextField.self).setInput(url.absoluteString)
try view.find(button: "Next").tap()
#expect(throws: Never.self) { try view.find(text: "Session Token") }
try view.find(ViewType.SecureField.self).setInput("invalid-token")
try await view.actualView().submit()
#expect(throws: Never.self) { try view.find(ViewType.Alert.self) }
}
}
}
@Test
func testSuccessfulLogin() async throws {
let url = URL(string: "https://testSuccessfulLogin.com")!
let user = User(
id: UUID(),
username: "admin"
)
try Mock(
url: url.appendingPathComponent("/api/v2/users/me"),
statusCode: 200,
data: [.get: Client.encoder.encode(user)]
).register()
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
try view.find(ViewType.TextField.self).setInput(url.absoluteString)
try view.find(button: "Next").tap()
try view.find(ViewType.SecureField.self).setInput("valid-token")
try await view.actualView().submit()
#expect(state.hasSession)
}
}
}
}