@@ -45,39 +45,104 @@ final class VaultTimeoutServiceTests: BitwardenTestCase {
45
45
46
46
// MARK: Tests
47
47
48
- /// `.hasPassedSessionTimeout()` returns false if the user should not be timed out.
49
- func test_hasPassedSessionTimeout_false ( ) async throws {
48
+ /// `.hasPassedSessionTimeout()` returns true if the user should be timed out.
49
+ func test_hasPassedSessionTimeout ( ) async throws {
50
50
let account = Account . fixture ( )
51
51
stateService. activeAccount = account
52
- stateService. lastActiveTime [ account. profile. userId] = Date ( )
53
- stateService. vaultTimeout [ account. profile. userId] = . custom( 120 )
52
+ stateService. vaultTimeout [ account. profile. userId] = . fiveMinutes
54
53
55
- let shouldTimeout = try await subject. hasPassedSessionTimeout ( userId: account. profile. userId)
54
+ let currentTime = Date ( year: 2024 , month: 1 , day: 2 , hour: 6 , minute: 0 )
55
+ timeProvider. timeConfig = . mockTime( currentTime)
56
+
57
+ // Last active 4 minutes ago, no timeout.
58
+ stateService. lastActiveTime [ account. profile. userId] = Calendar . current
59
+ . date ( byAdding: . minute, value: - 4 , to: currentTime)
60
+ var shouldTimeout = try await subject. hasPassedSessionTimeout ( userId: account. profile. userId)
56
61
XCTAssertFalse ( shouldTimeout)
62
+
63
+ // Last active 5 minutes ago, timeout.
64
+ stateService. lastActiveTime [ account. profile. userId] = Calendar . current
65
+ . date ( byAdding: . minute, value: - 5 , to: currentTime)
66
+ shouldTimeout = try await subject. hasPassedSessionTimeout ( userId: account. profile. userId)
67
+ XCTAssertTrue ( shouldTimeout)
68
+
69
+ // Last active 6 minutes ago, timeout.
70
+ stateService. lastActiveTime [ account. profile. userId] = Calendar . current
71
+ . date ( byAdding: . minute, value: - 6 , to: currentTime)
72
+ shouldTimeout = try await subject. hasPassedSessionTimeout ( userId: account. profile. userId)
73
+ XCTAssertTrue ( shouldTimeout)
74
+
75
+ // Last active in the distant past, timeout.
76
+ stateService. lastActiveTime [ account. profile. userId] = . distantPast
77
+ shouldTimeout = try await subject. hasPassedSessionTimeout ( userId: account. profile. userId)
78
+ XCTAssertTrue ( shouldTimeout)
57
79
}
58
80
59
- /// `.hasPassedSessionTimeout()` returns false if the user's vault timeout value is negative .
60
- func test_hasPassedSessionTimeout_never ( ) async throws {
81
+ /// `.hasPassedSessionTimeout()` returns false for a timeout value of app restart .
82
+ func test_hasPassedSessionTimeout_appRestart ( ) async throws {
61
83
let account = Account . fixture ( )
62
84
stateService. activeAccount = account
63
- stateService. lastActiveTime [ account. profile. userId] = Date ( )
64
- stateService. vaultTimeout [ account. profile. userId] = . never
85
+ stateService. lastActiveTime [ account. profile. userId] = . distantPast
86
+ stateService. vaultTimeout [ account. profile. userId] = . onAppRestart
65
87
66
88
let shouldTimeout = try await subject. hasPassedSessionTimeout ( userId: account. profile. userId)
67
89
XCTAssertFalse ( shouldTimeout)
68
90
}
69
91
70
- /// `.hasPassedSessionTimeout()` returns true if the user should be timed out.
71
- func test_hasPassedSessionTimeout_true ( ) async throws {
92
+ /// `.hasPassedSessionTimeout()` returns true if the user should be timed out for a custom timeout value .
93
+ func test_hasPassedSessionTimeout_custom ( ) async throws {
72
94
let account = Account . fixture ( )
73
95
stateService. activeAccount = account
96
+ stateService. vaultTimeout [ account. profile. userId] = . custom( 120 )
97
+
98
+ let currentTime = Date ( year: 2024 , month: 1 , day: 2 , hour: 6 , minute: 0 )
99
+ timeProvider. timeConfig = . mockTime( currentTime)
100
+
101
+ // Last active 119 minutes ago, no timeout.
102
+ stateService. lastActiveTime [ account. profile. userId] = Calendar . current
103
+ . date ( byAdding: . minute, value: - 119 , to: currentTime)
104
+ var shouldTimeout = try await subject. hasPassedSessionTimeout ( userId: account. profile. userId)
105
+ XCTAssertFalse ( shouldTimeout)
106
+
107
+ // Last active 120 minutes ago, timeout.
108
+ stateService. lastActiveTime [ account. profile. userId] = Calendar . current
109
+ . date ( byAdding: . minute, value: - 120 , to: currentTime)
110
+ shouldTimeout = try await subject. hasPassedSessionTimeout ( userId: account. profile. userId)
111
+ XCTAssertTrue ( shouldTimeout)
112
+
113
+ // Last active 121 minutes ago, timeout.
114
+ stateService. lastActiveTime [ account. profile. userId] = Calendar . current
115
+ . date ( byAdding: . minute, value: - 121 , to: currentTime)
116
+ shouldTimeout = try await subject. hasPassedSessionTimeout ( userId: account. profile. userId)
117
+ XCTAssertTrue ( shouldTimeout)
118
+
119
+ // Last active in the distant past, timeout.
74
120
stateService. lastActiveTime [ account. profile. userId] = . distantPast
75
- stateService. vaultTimeout [ account. profile. userId] = . oneMinute
121
+ shouldTimeout = try await subject. hasPassedSessionTimeout ( userId: account. profile. userId)
122
+ XCTAssertTrue ( shouldTimeout)
123
+ }
124
+
125
+ /// `.hasPassedSessionTimeout()` returns true if there's no last active time recorded for the user.
126
+ func test_hasPassedSessionTimeout_noLastActiveTime( ) async throws {
127
+ let account = Account . fixture ( )
128
+ stateService. activeAccount = account
129
+ stateService. vaultTimeout [ account. profile. userId] = . fiveMinutes
76
130
77
131
let shouldTimeout = try await subject. hasPassedSessionTimeout ( userId: account. profile. userId)
78
132
XCTAssertTrue ( shouldTimeout)
79
133
}
80
134
135
+ /// `.hasPassedSessionTimeout()` returns false if the user's vault timeout value is negative.
136
+ func test_hasPassedSessionTimeout_never( ) async throws {
137
+ let account = Account . fixture ( )
138
+ stateService. activeAccount = account
139
+ stateService. lastActiveTime [ account. profile. userId] = . distantPast
140
+ stateService. vaultTimeout [ account. profile. userId] = . never
141
+
142
+ let shouldTimeout = try await subject. hasPassedSessionTimeout ( userId: account. profile. userId)
143
+ XCTAssertFalse ( shouldTimeout)
144
+ }
145
+
81
146
/// Tests that locking and unlocking the vault works correctly.
82
147
func test_lock_unlock( ) async throws {
83
148
let account = Account . fixtureAccountLogin ( )
0 commit comments