Skip to content

Commit 43ed0b5

Browse files
committed
Refactor AuthMonitor tests
1 parent 6f6404d commit 43ed0b5

File tree

1 file changed

+47
-62
lines changed

1 file changed

+47
-62
lines changed

src/auth_monitor_tests.rs

Lines changed: 47 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,35 @@ use crate::auth_monitor_options::AuthMonitorOptions;
77
use crate::auth_monitor_params::AuthMonitorParams;
88
use crate::test_utils::test_file::TestFile;
99

10-
struct AuthMonitorTest {
11-
auth_monitor: AuthMonitor,
12-
}
13-
14-
impl AuthMonitorTest {
15-
pub fn new(path: &str, options: AuthMonitorOptions) -> AuthMonitorTest {
16-
println!("Creating AuthMonitor with options: {}", options);
17-
let auth_monitor = AuthMonitor::new(AuthMonitorParams {
18-
filepath: String::from(path),
19-
options,
20-
})
21-
.expect("Error creating AuthMonitor");
22-
return AuthMonitorTest { auth_monitor };
23-
}
24-
25-
pub fn expect_auth_failure_limit_not_reached(&mut self) {
26-
assert_eq!(self.auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
27-
}
10+
const MAX_FAILED_ATTEMPTS_TEST_RANGE: Range<i32> = 2..15;
2811

29-
pub fn expect_auth_failure_limit_reached(&mut self) {
30-
assert_eq!(self.auth_monitor.update(), AuthState::AuthFailureLimitReached);
31-
}
12+
fn create_auth_monitor(path: &str, options: AuthMonitorOptions) -> AuthMonitor {
13+
println!("Creating AuthMonitor with options: {}", options);
14+
return AuthMonitor::new(AuthMonitorParams {
15+
filepath: String::from(path),
16+
options,
17+
})
18+
.expect("Error creating AuthMonitor");
3219
}
3320

34-
const MAX_FAILED_ATTEMPTS_TEST_RANGE: Range<i32> = 2..15;
35-
3621
#[test]
37-
fn when_file_does_not_exist_then_changes_are_monitored_after_it_is_created() {
22+
pub fn when_file_does_not_exist_then_changes_are_monitored_after_it_is_created() {
3823
let mut file = TestFile::empty();
3924
file.remove();
4025

4126
let options = AuthMonitorOptions::default();
42-
let mut test = AuthMonitorTest::new(file.path(), options);
43-
test.expect_auth_failure_limit_not_reached();
27+
let mut auth_monitor = create_auth_monitor(file.path(), options);
28+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
4429

4530
file.create();
4631

4732
for i in 0usize..(options.max_failed_attempts - 1) as usize {
4833
file.write_auth_failed_message(i);
49-
test.expect_auth_failure_limit_not_reached();
34+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
5035
}
5136

5237
file.write_auth_failed_message(0);
53-
test.expect_auth_failure_limit_reached();
38+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitReached);
5439
}
5540

5641
#[test]
@@ -61,21 +46,21 @@ pub fn when_auth_failure_limit_is_reached_then_update_callback_is_invoked() {
6146
max_failed_attempts,
6247
..AuthMonitorOptions::default()
6348
};
64-
let mut test = AuthMonitorTest::new(file.path(), options);
65-
test.expect_auth_failure_limit_not_reached();
49+
let mut auth_monitor = create_auth_monitor(file.path(), options);
50+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
6651

6752
for i in 0usize..(max_failed_attempts - 1) as usize {
6853
file.write_auth_failed_message(i);
69-
test.expect_auth_failure_limit_not_reached();
54+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
7055
}
7156

7257
for i in 0usize..(max_failed_attempts * 2) as usize {
7358
file.write_other_message(i);
74-
test.expect_auth_failure_limit_not_reached();
59+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
7560
}
7661

7762
file.write_auth_failed_message(0);
78-
test.expect_auth_failure_limit_reached();
63+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitReached);
7964
}
8065
}
8166

@@ -87,13 +72,13 @@ pub fn when_auth_failure_limit_is_reached_between_updates_then_next_update_invok
8772
max_failed_attempts,
8873
..AuthMonitorOptions::default()
8974
};
90-
let mut test = AuthMonitorTest::new(file.path(), options);
91-
test.expect_auth_failure_limit_not_reached();
75+
let mut auth_monitor = create_auth_monitor(file.path(), options);
76+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
9277

9378
file.write_auth_failed_messages(max_failed_attempts as usize);
9479
file.write_other_messages(max_failed_attempts as usize);
9580

96-
test.expect_auth_failure_limit_reached();
81+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitReached);
9782
}
9883
}
9984

@@ -104,14 +89,14 @@ pub fn when_reset_time_has_passed_then_reset_failed_attempt_counter() {
10489
reset_after_seconds: 5,
10590
..AuthMonitorOptions::default()
10691
};
107-
let mut test = AuthMonitorTest::new(file.path(), options);
108-
test.expect_auth_failure_limit_not_reached();
92+
let mut auth_monitor = create_auth_monitor(file.path(), options);
93+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
10994

11095
let failed_attempts_safe_limit = (options.max_failed_attempts - 1) as usize;
11196

11297
for i in 0usize..failed_attempts_safe_limit {
11398
file.write_auth_failed_message(i);
114-
test.expect_auth_failure_limit_not_reached();
99+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
115100
}
116101

117102
let sleep_duration = Duration::from_secs((options.reset_after_seconds + 1) as u64);
@@ -120,11 +105,11 @@ pub fn when_reset_time_has_passed_then_reset_failed_attempt_counter() {
120105

121106
for i in 0usize..failed_attempts_safe_limit {
122107
file.write_auth_failed_message(i);
123-
test.expect_auth_failure_limit_not_reached();
108+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
124109
}
125110

126111
file.write_auth_failed_message(0);
127-
test.expect_auth_failure_limit_reached();
112+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitReached);
128113
}
129114

130115
#[test]
@@ -134,17 +119,17 @@ pub fn when_ignore_subsequent_fails_duration_has_not_elapsed_then_update_callbac
134119
ignore_subsequent_fails_ms: 10,
135120
..AuthMonitorOptions::default()
136121
};
137-
let mut test = AuthMonitorTest::new(file.path(), options);
138-
test.expect_auth_failure_limit_not_reached();
122+
let mut auth_monitor = create_auth_monitor(file.path(), options);
123+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
139124

140125
let max_failed_attempts = options.max_failed_attempts as usize;
141126

142127
for i in 0usize..max_failed_attempts {
143128
file.write_auth_failed_message(i);
144-
test.expect_auth_failure_limit_not_reached();
129+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
145130
}
146131

147-
test.expect_auth_failure_limit_not_reached();
132+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
148133
}
149134

150135
#[test]
@@ -154,8 +139,8 @@ pub fn when_ignore_subsequent_fails_duration_has_elapsed_then_update_callback_is
154139
ignore_subsequent_fails_ms: 10,
155140
..AuthMonitorOptions::default()
156141
};
157-
let mut test = AuthMonitorTest::new(file.path(), options);
158-
test.expect_auth_failure_limit_not_reached();
142+
let mut auth_monitor = create_auth_monitor(file.path(), options);
143+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
159144

160145
let max_failed_attempts = options.max_failed_attempts as usize;
161146
let sleep_duration = Duration::from_millis((options.ignore_subsequent_fails_ms + 1) as u64);
@@ -166,52 +151,52 @@ pub fn when_ignore_subsequent_fails_duration_has_elapsed_then_update_callback_is
166151
sleep(sleep_duration);
167152
}
168153

169-
test.expect_auth_failure_limit_reached();
154+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitReached);
170155
}
171156

172157
#[test]
173-
fn when_file_is_deleted_and_new_one_is_created_then_changes_are_still_monitored() {
158+
pub fn when_file_is_deleted_and_new_one_is_created_then_changes_are_still_monitored() {
174159
let mut file = TestFile::not_empty();
175160
let options = AuthMonitorOptions::default();
176-
let mut test = AuthMonitorTest::new(file.path(), options);
161+
let mut auth_monitor = create_auth_monitor(file.path(), options);
177162
file.remove();
178-
test.expect_auth_failure_limit_not_reached();
163+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
179164

180165
file.create();
181-
test.expect_auth_failure_limit_not_reached();
166+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
182167

183168
file.write_auth_failed_messages(options.max_failed_attempts as usize);
184-
test.expect_auth_failure_limit_reached();
169+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitReached);
185170
}
186171

187172
#[test]
188-
fn when_file_is_renamed_and_new_one_is_created_then_changes_are_still_monitored() {
173+
pub fn when_file_is_renamed_and_new_one_is_created_then_changes_are_still_monitored() {
189174
let mut file = TestFile::not_empty();
190175
let options = AuthMonitorOptions::default();
191-
let mut test = AuthMonitorTest::new(file.path(), options);
176+
let mut auth_monitor = create_auth_monitor(file.path(), options);
192177

193178
let filepath = String::from(file.path());
194179
let new_filepath = format!("{}.bak", file.path());
195180
file.rename(&new_filepath);
196-
test.expect_auth_failure_limit_not_reached();
181+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
197182

198183
let mut new_file = TestFile::new(&filepath);
199-
test.expect_auth_failure_limit_not_reached();
184+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
200185
new_file.write_auth_failed_messages(options.max_failed_attempts as usize);
201-
test.expect_auth_failure_limit_reached();
186+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitReached);
202187
}
203188

204189
#[test]
205-
fn when_file_is_truncated_then_changes_are_still_monitored() {
190+
pub fn when_file_is_truncated_then_changes_are_still_monitored() {
206191
let mut file = TestFile::not_empty();
207192
file.write_other_messages(5);
208193

209194
let options = AuthMonitorOptions::default();
210-
let mut test = AuthMonitorTest::new(file.path(), options);
195+
let mut auth_monitor = create_auth_monitor(file.path(), options);
211196

212197
file.truncate();
213-
test.expect_auth_failure_limit_not_reached();
198+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitNotReached);
214199

215200
file.write_auth_failed_messages(options.max_failed_attempts as usize);
216-
test.expect_auth_failure_limit_reached();
201+
assert_eq!(auth_monitor.update(), AuthState::AuthFailureLimitReached);
217202
}

0 commit comments

Comments
 (0)