-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathRootTests.swift
292 lines (252 loc) · 10.7 KB
/
RootTests.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//
// RootTests.swift
// Authenticator
//
// Copyright (c) 2017-2023 Authenticator authors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import XCTest
@testable import Authenticator
class RootTests: XCTestCase {
private struct FakeError: Error {}
private let defaultDigitGroupSize = 2
let displayTime = DisplayTime(date: Date())
func testShowBackupInfo() {
var root = Root(deviceCanScan: false)
// Ensure there is no modal visible.
let (firstViewModel, _) = root.viewModel(with: [], at: displayTime, digitGroupSize: defaultDigitGroupSize)
guard case .none = firstViewModel.modal else {
XCTFail("Expected .none, got \(firstViewModel.modal)")
return
}
// Show the backup info.
let showAction: Root.Action = .tokenListAction(.showBackupInfo)
let showEffect: Root.Effect?
do {
showEffect = try root.update(with: showAction)
} catch {
XCTFail("Unexpected error: \(error)")
return
}
XCTAssertNil(showEffect)
// Ensure the backup info modal is visible.
let (secondViewModel, _) = root.viewModel(with: [], at: displayTime, digitGroupSize: defaultDigitGroupSize)
switch secondViewModel.modal {
case .menu(let menu):
switch menu.child {
case .info(let infoViewModel):
XCTAssert(infoViewModel.title == "Backups")
default:
XCTFail("Expected Backups .info, got \(menu.child)")
}
default:
XCTFail("Expected .menu, got \(secondViewModel.modal)")
}
// Hide the backup info.
let hideAction: Root.Action = .menuAction(.infoEffect(.done))
let hideEffect: Root.Effect?
do {
hideEffect = try root.update(with: hideAction)
} catch {
XCTFail("Unexpected error: \(error)")
return
}
XCTAssertNil(hideEffect)
// Ensure the backup info modal no longer visible.
let (thirdViewModel, _) = root.viewModel(with: [], at: displayTime, digitGroupSize: defaultDigitGroupSize)
guard case .none = thirdViewModel.modal else {
XCTFail("Expected .none, got \(thirdViewModel.modal)")
return
}
}
func testShowLicenseInfo() {
var root = Root(deviceCanScan: false)
// Ensure there is no modal visible.
let (firstViewModel, _) = root.viewModel(with: [], at: displayTime, digitGroupSize: defaultDigitGroupSize)
guard case .none = firstViewModel.modal else {
XCTFail("Expected .none, got \(firstViewModel.modal)")
return
}
// Show the info list.
let showInfoAction: Root.Action = .tokenListAction(.showInfo)
let showInfoEffect: Root.Effect?
do {
showInfoEffect = try root.update(with: showInfoAction)
} catch {
XCTFail("Unexpected error: \(error)")
return
}
XCTAssertNil(showInfoEffect)
// Ensure the info list modal is visible.
let (nextViewModel, _) = root.viewModel(with: [], at: displayTime, digitGroupSize: defaultDigitGroupSize)
guard case .menu(let menu) = nextViewModel.modal, case .none = menu.child else {
XCTFail("Expected .info list, got \(nextViewModel.modal)")
return
}
// Show the license info.
let showAction: Root.Action = .menuAction(.infoListEffect(.showLicenseInfo))
let showEffect: Root.Effect?
do {
showEffect = try root.update(with: showAction)
} catch {
XCTFail("Unexpected error: \(error)")
return
}
XCTAssertNil(showEffect)
// Ensure the license info modal is visible.
let (secondViewModel, _) = root.viewModel(with: [], at: displayTime, digitGroupSize: defaultDigitGroupSize)
switch secondViewModel.modal {
case .menu(let menu):
switch menu.child {
case .info(let infoViewModel):
XCTAssert(infoViewModel.title == "Acknowledgements")
default:
XCTFail("Expected Acknowledgements .info, got \(menu.child)")
}
default:
XCTFail("Expected .menu, got \(secondViewModel.modal)")
}
// Hide the license info.
let hideAction: Root.Action = .menuAction(.infoEffect(.done))
let hideEffect: Root.Effect?
do {
hideEffect = try root.update(with: hideAction)
} catch {
XCTFail("Unexpected error: \(error)")
return
}
XCTAssertNil(hideEffect)
// Ensure the license info modal no longer visible.
let (thirdViewModel, _) = root.viewModel(with: [], at: displayTime, digitGroupSize: defaultDigitGroupSize)
guard case .none = thirdViewModel.modal else {
XCTFail("Expected .none, got \(thirdViewModel.modal)")
return
}
}
func testOpenURL() {
var root = Root(deviceCanScan: false)
guard let url = URL(string: "https://example.com") else {
XCTFail("Failed to initialize URL.")
return
}
let action: Root.Action = .menuAction(.infoEffect(.openURL(url)))
let effect: Root.Effect?
do {
XCTAssertNil(try root.update(with: .tokenListAction(.showBackupInfo)))
effect = try root.update(with: action)
} catch {
XCTFail("Unexpected error: \(error)")
return
}
guard case .some(.openURL(url)) = effect else {
XCTFail("Expected .openURL(\(url)), got \(String(describing: effect))")
return
}
}
func testEventAddTokenFailed() {
var root = Root(deviceCanScan: false)
let event: Root.Event = .addTokenFailed(FakeError())
let effect = root.update(with: event)
// TODO: check that the component state hasn't changed
guard case .some(.showErrorMessage("Failed to add token.")) = effect else {
XCTFail("Expected .showErrorMessage(\"Failed to add token.\"), got \(String(describing: effect))")
return
}
}
// MARK: Events
func testEventAddTokenFromURLSucceeded() {
var root = Root(deviceCanScan: false)
let event: Root.Event = .addTokenFromURLSucceeded
let effect = root.update(with: event)
// TODO: check that the component state hasn't changed
XCTAssertNil(effect)
}
func testEventTokenFormSucceeded() {
var root = Root(deviceCanScan: false)
func modalViewModel(from root: Root) -> RootViewModel.ModalViewModel {
return root.viewModel(with: [], at: displayTime, digitGroupSize: defaultDigitGroupSize).viewModel.modal
}
// Ensure the initial view model has no modal.
guard case .none = modalViewModel(from: root) else {
XCTFail("The initial view model should have no modal.")
return
}
// Show the token entry form.
do {
let effect = try root.update(with: .tokenListAction(.beginAddToken))
XCTAssertNil(effect)
} catch {
XCTFail("Caught unexpected error: \(error)")
}
// Ensure the view model now has a modal entry form.
guard case .entryForm = modalViewModel(from: root) else {
XCTFail("The view model should have a modal entry form.")
return
}
// Signal token entry success.
let effect = root.update(with: .tokenFormSucceeded)
XCTAssertNil(effect)
// Ensure the token entry form hides on success.
guard case .none = modalViewModel(from: root) else {
XCTFail("The final view model should have no modal.")
return
}
}
func testEventSaveTokenFailed() {
var root = Root(deviceCanScan: false)
let event: Root.Event = .saveTokenFailed(FakeError())
let effect = root.update(with: event)
// TODO: check that the component state hasn't changed
guard case .some(.showErrorMessage("Failed to save token.")) = effect else {
XCTFail("Expected .showErrorMessage(\"Failed to save token.\"), got \(String(describing: effect))")
return
}
}
func testEventUpdateTokenFailed() {
var root = Root(deviceCanScan: false)
let event: Root.Event = .updateTokenFailed(FakeError())
let effect = root.update(with: event)
// TODO: check that the component state hasn't changed
guard case .some(.showErrorMessage("Failed to update token.")) = effect else {
XCTFail("Expected .showErrorMessage(\"Failed to update token.\"), got \(String(describing: effect))")
return
}
}
func testEventMoveTokenFailed() {
var root = Root(deviceCanScan: false)
let event: Root.Event = .moveTokenFailed(FakeError())
let effect = root.update(with: event)
// TODO: check that the component state hasn't changed
guard case .some(.showErrorMessage("Failed to move token.")) = effect else {
XCTFail("Expected .showErrorMessage(\"Failed to move token.\"), got \(String(describing: effect))")
return
}
}
func testEventDeleteTokenFailed() {
var root = Root(deviceCanScan: false)
let event: Root.Event = .deleteTokenFailed(FakeError())
let effect = root.update(with: event)
// TODO: check that the component state hasn't changed
guard case .some(.showErrorMessage("Failed to delete token.")) = effect else {
XCTFail("Expected .showErrorMessage(\"Failed to delete token.\"), got \(String(describing: effect))")
return
}
}
}