yapitest 1.0.3

A YAML-based API testing framework
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use anyhow::{Error, Result, anyhow};
use colored::*;
use serde::Deserialize;
use serde_yaml::{Value, from_value};
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};

use crate::config::{ConfigData, ConfigSpec, TestStepGroupReference};
use crate::test_step::{
    AssertionResult, RunnableTestStep, TestStep, TestStepFailureReason, TestStepResult,
    TestStepSpec,
};

#[derive(Clone)]
pub struct Test {
    pub name: String,
    path: PathBuf,
    pub config: Option<Arc<RwLock<ConfigData>>>,
    pub groups: Option<Vec<String>>,
    setup: Option<String>,
    teardown: Option<String>,
    steps: Vec<Arc<RwLock<dyn RunnableTestStep + Send + Sync>>>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct TestSpec {
    setup: Option<String>,
    teardown: Option<String>,
    steps: Vec<Value>,
    config: Option<ConfigSpec>,
    groups: Option<Vec<String>>,
}

pub fn print_test_results(test_results: &[TestResult], duration_secs: f32, verbosity: u8) {
    if verbosity == 0 {
        return;
    }

    let mut num_passes = 0usize;
    let mut fails: Vec<&TestResult> = vec![];
    for r in test_results {
        if r.passed() { num_passes += 1; } else { fails.push(r); }
    }

    let total = test_results.len();
    let num_failures = fails.len();
    let divider = "".repeat(40);

    println!();
    println!("{}", divider.dimmed());

    if num_failures == 0 {
        println!(
            "{}  ({} total, {:.2}s)",
            format!("Results: {} passed", num_passes).green(),
            total,
            duration_secs
        );
    } else {
        println!(
            "Results: {}  ({} total, {:.2}s)",
            format!("{} passed, {} failed", num_passes, num_failures).red(),
            total,
            duration_secs
        );
    }

    println!("{}", divider.dimmed());

    // Verbosity 2+ shows the FAILURES detail block.
    if verbosity >= 2 && !fails.is_empty() {
        println!();
        println!("{}", "FAILURES".bold());
        for failure in &fails {
            println!();
            println!("  {} {}", "".red(), failure.test_name.bold());
            println!("    File:  {}", failure.test_path.display());
            if let Some(msg) = failure.get_failure_message() {
                println!("    Error: {}", msg);
            }
        }
        println!();
    }
}

pub struct TestResult {
    test_name: String,
    test_path: PathBuf,
    /// All steps that ran (including the failing one if any).
    pub steps: Vec<TestStepResult>,
    success: bool,
    pub duration_ms: u64,
}

impl TestResult {
    pub fn name(&self) -> &str {
        &self.test_name
    }

    pub fn get_failure_message(&self) -> Option<&str> {
        if self.success {
            return None;
        }
        self.steps.last().and_then(|s| s.failure_message.as_deref())
    }

    pub fn assertions(&self) -> impl Iterator<Item = &AssertionResult> {
        self.steps.iter().flat_map(|s| s.assertion_results.iter())
    }

    pub fn passed(&self) -> bool {
        self.success
    }

    pub fn file_path(&self) -> Option<&PathBuf> {
        Some(&self.test_path)
    }

    fn make_failure(
        test_name: &String,
        test_path: &PathBuf,
        steps: Vec<TestStepResult>,
    ) -> TestResult {
        TestResult {
            test_name: test_name.to_string(),
            test_path: test_path.to_path_buf(),
            steps,
            success: false,
            duration_ms: 0,
        }
    }
}

fn is_test_name(key: &str) -> bool {
    let lower_name = key.to_lowercase();
    lower_name.starts_with("test") || lower_name.ends_with("test")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_step::{AssertionResult, TestStepFailureReason, TestStepResult};

    fn make_step_failure(msg: &str) -> TestStepResult {
        TestStepResult::make_failure(
            Some("step"),
            TestStepFailureReason::StatusCodeError,
            msg.to_owned(),
        )
    }

    // ── is_test_name ─────────────────────────────────────────────────────────

    #[test]
    fn test_is_test_name_prefix() {
        assert!(is_test_name("test_login"));
        assert!(is_test_name("Test Login"));
        assert!(is_test_name("TEST_CREATE_USER"));
    }

    #[test]
    fn test_is_test_name_suffix() {
        assert!(is_test_name("login_test"));
        assert!(is_test_name("create user test"));
    }

    #[test]
    fn test_is_test_name_neither() {
        assert!(!is_test_name("config"));
        assert!(!is_test_name("setup"));
        assert!(!is_test_name("login_flow"));
    }

    #[test]
    fn test_is_test_name_case_insensitive() {
        assert!(is_test_name("TEST"));
        assert!(is_test_name("MYTEST"));
    }

    // ── TestResult ───────────────────────────────────────────────────────────

    #[test]
    fn test_result_passed_is_true_on_success() {
        let result = TestResult {
            test_name: "my test".to_owned(),
            test_path: PathBuf::from("/test.yaml"),
            steps: vec![],
            success: true,
            duration_ms: 0,
        };
        assert!(result.passed());
    }

    #[test]
    fn test_result_get_failure_message_returns_none_on_success() {
        let result = TestResult {
            test_name: "my test".to_owned(),
            test_path: PathBuf::from("/test.yaml"),
            steps: vec![],
            success: true,
            duration_ms: 0,
        };
        assert!(result.get_failure_message().is_none());
    }

    #[test]
    fn test_result_get_failure_message_returns_last_step_message() {
        let step = make_step_failure("expected 200, got 404");
        let result = TestResult {
            test_name: "my test".to_owned(),
            test_path: PathBuf::from("/test.yaml"),
            steps: vec![step],
            success: false,
            duration_ms: 0,
        };
        assert_eq!(result.get_failure_message(), Some("expected 200, got 404"));
    }

    #[test]
    fn test_result_assertions_iterates_all_steps() {
        let mut step1 = make_step_failure("bad status");
        step1.assertion_results = vec![
            AssertionResult { name: "status 200".to_owned(), passed: false, message: Some("bad status".to_owned()) },
        ];
        let mut step2 = make_step_failure("bad body");
        step2.assertion_results = vec![
            AssertionResult { name: "name".to_owned(), passed: true, message: None },
            AssertionResult { name: "age".to_owned(), passed: false, message: Some("bad body".to_owned()) },
        ];
        let result = TestResult {
            test_name: "t".to_owned(),
            test_path: PathBuf::from("/t.yaml"),
            steps: vec![step1, step2],
            success: false,
            duration_ms: 0,
        };
        assert_eq!(result.assertions().count(), 3);
    }
}

impl Test {
    pub fn path(&self) -> &PathBuf {
        &self.path
    }

    pub fn add_config(&mut self, config: Arc<RwLock<ConfigData>>) {
        match &self.config {
            Some(cfg) => {
                enum Relation {
                    NewIsParent,
                    CurrentIsParent,
                    Unrelated(String, String),
                    NoParents,
                }

                let relation = {
                    let new_guard = config.read().unwrap();
                    let cfg_guard = cfg.read().unwrap();
                    match (new_guard.path.parent(), cfg_guard.path.parent()) {
                        (Some(new_dir), Some(current_dir)) => {
                            if current_dir.starts_with(new_dir) {
                                Relation::NewIsParent
                            } else if new_dir.starts_with(current_dir) {
                                Relation::CurrentIsParent
                            } else {
                                Relation::Unrelated(
                                    new_dir.display().to_string(),
                                    current_dir.display().to_string(),
                                )
                            }
                        }
                        _ => Relation::NoParents,
                    }
                };

                match relation {
                    Relation::NewIsParent => cfg.write().unwrap().set_parent(config),
                    Relation::CurrentIsParent => {
                        config.write().unwrap().set_parent(Arc::clone(cfg));
                    }
                    Relation::Unrelated(a, b) => panic!(
                        "ERROR: Cannot set parentage with unrelated configs {} {}",
                        a, b
                    ),
                    Relation::NoParents => {}
                }
            }
            None => {
                self.config = Some(Arc::clone(&config));
            }
        }
    }

    pub fn from_spec(path: PathBuf, name: String, spec: TestSpec) -> Result<Test> {
        let mut config: Option<Arc<RwLock<ConfigData>>> = None;
        if let Some(config_spec) = spec.config {
            let loaded_config = ConfigData::from_spec(&path, config_spec)?;
            config = Some(Arc::new(RwLock::new(loaded_config)));
        }

        let mut test_steps: Vec<Arc<RwLock<dyn RunnableTestStep + Send + Sync>>> = vec![];

        for step in spec.steps {
            // Check for a plain string reference first so we can move the value
            // into from_value without cloning when it's a structured step spec.
            if let Some(step_name) = step.as_str() {
                test_steps.push(Arc::new(RwLock::new(
                    TestStepGroupReference::from_id(step_name.to_owned()),
                )));
            } else {
                match from_value::<TestStepSpec>(step) {
                    Ok(test_step_spec) => {
                        test_steps.push(Arc::new(RwLock::new(TestStep::from_spec(test_step_spec))));
                    }
                    Err(_) => return Err(anyhow!("Error Decoding Step in test {}", name)),
                }
            }
        }

        Ok(Test {
            name,
            path,
            setup: spec.setup,
            teardown: spec.teardown,
            steps: test_steps,
            config,
            groups: spec.groups,
        })
    }

    pub fn load_from_file(path: &PathBuf) -> Result<(Option<ConfigData>, Vec<Test>), Error> {
        let mut config: Option<ConfigData> = None;
        let mut tests: Vec<Test> = vec![];

        if let Ok(file) = File::open(path) {
            let reader = BufReader::new(file);
            let test_file_result = serde_yaml::from_reader::<_, Value>(reader);
            match test_file_result {
                Ok(mut test_file) => {
                    // Extract the config entry by value (removing it from the mapping)
                    // so we can pass it to from_val without cloning.
                    if let Value::Mapping(ref mut mapping) = test_file {
                        if let Some(config_value) = mapping.remove("config") {
                            config = Some(ConfigData::from_val(config_value, path)?);
                        }
                    }

                    // Consume the mapping so each test's Value can be moved
                    // into from_value without cloning.
                    if let Value::Mapping(mapping) = test_file {
                        for (key_val, value) in mapping {
                            if let Some(key) = key_val.as_str() {
                                if is_test_name(key) {
                                    match from_value::<TestSpec>(value) {
                                        Ok(test_spec) => {
                                            let test = Test::from_spec(
                                                path.clone(),
                                                key.to_owned(),
                                                test_spec,
                                            )?;
                                            tests.push(test);
                                        }
                                        Err(e) => {
                                            panic!(
                                                "Failed to parse test: {} at {}\n{}",
                                                key,
                                                path.display(),
                                                e
                                            );
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                Err(e) => {
                    return Err(Error::from(e));
                }
            }
        }
        Ok((config, tests))
    }

    pub async fn run(&self) -> TestResult {
        let mut prior_steps: HashMap<String, TestStepResult> = HashMap::new();
        let mut completed_steps: Vec<TestStepResult> = Vec::new();

        macro_rules! fail {
            ($step_result:expr) => {{
                completed_steps.push($step_result);
                return TestResult::make_failure(&self.name, &self.path, completed_steps);
            }};
        }

        // Setup
        if let (Some(setup_id), Some(cfg)) = (self.setup.as_deref(), &self.config) {
            match cfg.read().unwrap().get_step_group(setup_id) {
                Ok(setup) => match setup.run(&self.config, &prior_steps).await {
                    Ok(result) => {
                        prior_steps.insert("setup".to_owned(), result.clone());
                        completed_steps.push(result);
                    }
                    Err(e) => fail!(TestStepResult::make_failure(
                        Some("setup"),
                        TestStepFailureReason::Miscellaneous,
                        format!("setup failed: {}", e),
                    )),
                },
                Err(e) => fail!(TestStepResult::make_failure(
                    Some("setup"),
                    TestStepFailureReason::SharedStepNotFoundError,
                    format!("setup step-set not found: {}", e),
                )),
            }
        }

        // Steps
        for step in self.steps.iter() {
            let real_step = step.read().unwrap();
            match real_step.run(&self.config, &prior_steps).await {
                Ok(result) => {
                    if result.status != TestStepFailureReason::NoFailure {
                        fail!(result);
                    }
                    if let Some(id) = real_step.get_id() {
                        prior_steps.insert(id.clone(), result.clone());
                    }
                    completed_steps.push(result);
                }
                Err(e) => {
                    let step_id = real_step.get_id().map(String::as_str);
                    fail!(TestStepResult::make_failure(
                        step_id,
                        TestStepFailureReason::Miscellaneous,
                        format!("step failed: {}", e),
                    ));
                }
            }
        }

        // Teardown
        if let (Some(teardown_id), Some(cfg)) = (self.teardown.as_deref(), &self.config) {
            match cfg.read().unwrap().get_step_group(teardown_id) {
                Ok(teardown) => match teardown.run(&self.config, &prior_steps).await {
                    Ok(result) => {
                        prior_steps.insert("teardown".to_owned(), result.clone());
                        completed_steps.push(result);
                    }
                    Err(e) => fail!(TestStepResult::make_failure(
                        Some("teardown"),
                        TestStepFailureReason::Miscellaneous,
                        format!("teardown failed: {}", e),
                    )),
                },
                Err(e) => fail!(TestStepResult::make_failure(
                    Some("teardown"),
                    TestStepFailureReason::SharedStepNotFoundError,
                    format!("teardown step-set not found: {}", e),
                )),
            }
        }

        TestResult {
            test_name: self.name.clone(),
            test_path: self.path.clone(),
            steps: completed_steps,
            success: true,
            duration_ms: 0,
        }
    }
}