testlib/
lib.rs

1//! # Test library functions
2//!
3//! These are common functions used in the integration tests for the script and for the lambda.
4use assert_fs::TempDir;
5use flate2::{read::GzDecoder, write::GzEncoder, Compression};
6use serde_json::Value;
7use std::fs::*;
8use std::path::{Path, PathBuf};
9use tar::Archive;
10use tar::Builder;
11
12/// # Represents the fields to be anonymised
13pub struct MetadataJson {
14    pub contact_email: String,
15    pub contact_name: String,
16    pub checksum: String,
17}
18
19/// # Creates a test tar.gz file
20pub fn create_package(input_dir: &TempDir, json: &str, file_name: Option<String>) -> PathBuf {
21    let package_dir: PathBuf = input_dir.join(PathBuf::from("TDR-2023"));
22    let tar_path: PathBuf = input_dir.join(PathBuf::from(
23        file_name.unwrap_or(String::from("TDR-2023.tar.gz")),
24    ));
25    create_dir_all(&package_dir).unwrap();
26    let metadata_path: PathBuf = package_dir.join(Path::new("TRE-TDR-2023-metadata.json"));
27    let docx_path: PathBuf = package_dir.join(Path::new("test.docx"));
28
29    write(metadata_path, json).unwrap();
30    write(docx_path, "").unwrap();
31    let tar_gz: File = File::create(tar_path.clone()).unwrap();
32    let enc: GzEncoder<File> = GzEncoder::new(tar_gz, Compression::default());
33    let mut tar: Builder<GzEncoder<File>> = Builder::new(enc);
34    tar.append_dir_all("TDR-2023", &package_dir).unwrap();
35    tar_path
36}
37
38/// # Decompresses the test tar.gz file
39pub fn decompress_test_file(path_to_tar: &PathBuf, output_path: &TempDir) {
40    let tar_gz: File = File::open(path_to_tar).unwrap();
41    let tar: GzDecoder<File> = GzDecoder::new(tar_gz);
42    let mut archive: Archive<GzDecoder<File>> = Archive::new(tar);
43    archive.unpack(output_path).unwrap();
44}
45
46/// # Read the metadata.json file and parse the fields to be anonymised
47pub fn get_metadata_json_fields(output_dir: &Path) -> MetadataJson {
48    let metadata: String = read_to_string(
49        output_dir
50            .join("TST-2023")
51            .join("TRE-TST-2023-metadata.json"),
52    )
53    .unwrap();
54    let json_value: Value = serde_json::from_str(&metadata).unwrap();
55    metadata_from_json_value(&json_value)
56}
57
58/// # Parse the fields from the json value
59fn metadata_from_json_value(json_value: &Value) -> MetadataJson {
60    let tdr = json_value["parameters"]["TDR"].clone();
61
62    MetadataJson {
63        contact_email: tdr["Contact-Email"].as_str().unwrap().to_string(),
64        contact_name: tdr["Contact-Name"].as_str().unwrap().to_string(),
65        checksum: tdr["Document-Checksum-sha256"]
66            .as_str()
67            .unwrap()
68            .to_string(),
69    }
70}
71
72/// # An input string with the filename missing
73pub fn json_missing_filename() -> &'static str {
74    r#"
75    {
76      "parameters": {
77        "PARSER": {
78          "name": "test"
79        },
80        "TDR": {
81          "Document-Checksum-sha256": "3c7b9ef49d36659762c34c63bae05b4cf07d6406c2736720385ed0c6f015840a"
82        }
83      }
84    }
85    "#
86}
87
88/// # An valid input string
89pub fn valid_json() -> &'static str {
90    r#"
91    {
92      "parameters": {
93        "PARSER": {
94          "name": "test"
95        },
96        "TDR": {
97          "Document-Checksum-sha256": "3c7b9ef49d36659762c34c63bae05b4cf07d6406c2736720385ed0c6f015840a"
98        },
99        "TRE": {
100          "payload": {
101            "filename": "test.docx"
102          }
103        }
104      }
105    }
106    "#
107}