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
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::{bail, Context, Result};
use move_command_line_common::movey_constants::{MOVEY_CREDENTIAL_PATH, MOVEY_URL};
use std::fs;
use toml_edit::easy::Value;

pub fn get_registry_api_token(move_home: &str) -> Result<String> {
    if let Ok(content) = get_api_token(&move_home) {
        Ok(content)
    } else {
        bail!(
            "There seems to be an error with your Movey API token. \
            Please run `move movey-login` and follow the instructions."
        )
    }
}

pub fn get_api_token(move_home: &str) -> Result<String> {
    let credential_path = format!("{}{}", move_home, MOVEY_CREDENTIAL_PATH);
    let mut toml: Value = read_credential_file(&credential_path)?;
    let token = get_registry_field(&mut toml, "token")?;
    Ok(token.to_string().replace("\"", ""))
}

pub fn get_movey_url(move_home: &str) -> Result<String> {
    let credential_path = format!("{}{}", move_home, MOVEY_CREDENTIAL_PATH);
    let contents = fs::read_to_string(&credential_path)?;
    let mut toml: Value = contents.parse()?;

    let movey_url = get_registry_field(&mut toml, "url");
    if let Ok(url) = movey_url {
        Ok(url.to_string().replace("\"", ""))
    } else {
        Ok(MOVEY_URL.to_string())
    }
}

fn get_registry_field<'a>(toml: &'a mut Value, field: &'a str) -> Result<&'a mut Value> {
    let registry = toml
        .as_table_mut()
        .context(format!("Error parsing {}", MOVEY_CREDENTIAL_PATH))?
        .get_mut("registry")
        .context(format!("Error parsing {}", MOVEY_CREDENTIAL_PATH))?;
    let value = registry
        .as_table_mut()
        .context("Error parsing registry table")?
        .get_mut(field)
        .context("Error parsing token")?;
    Ok(value)
}

pub fn read_credential_file(credential_path: &str) -> Result<Value> {
    let content = match fs::read_to_string(&credential_path) {
        Ok(content) => content,
        Err(error) => bail!("Error reading input: {}", error),
    };
    content.parse().map_err(|e| {
        anyhow::Error::from(e).context(format!(
            "could not parse input at {} as TOML",
            &credential_path
        ))
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{env, fs::File};

    fn setup_move_home(test_path: &str) -> (String, String) {
        let cwd = env::current_dir().unwrap();
        let mut move_home: String = String::from(cwd.to_string_lossy());
        move_home.push_str(&test_path);
        let credential_path = move_home.clone() + MOVEY_CREDENTIAL_PATH;

        (move_home, credential_path)
    }

    fn clean_up(move_home: &str) {
        let _ = fs::remove_dir_all(move_home);
    }

    #[test]
    fn get_api_token_works() {
        let test_path = String::from("/get_api_token_works");
        let (move_home, credential_path) = setup_move_home(&test_path);
        let _ = fs::create_dir_all(&move_home);
        File::create(&credential_path).unwrap();

        let content = r#"
            [registry]
            token = "test-token"
            "#;
        fs::write(&credential_path, content).unwrap();

        let token = get_registry_api_token(&move_home).unwrap();
        assert!(token.contains("test-token"));

        clean_up(&move_home)
    }

    #[test]
    fn get_api_token_fails_if_there_is_no_move_home_directory() {
        let test_path = String::from("/get_api_token_fails_if_there_is_no_move_home_directory");
        let (move_home, _) = setup_move_home(&test_path);
        let _ = fs::remove_dir_all(&move_home);

        let token = get_registry_api_token(&move_home);
        assert!(token.is_err());

        clean_up(&move_home)
    }

    #[test]
    fn get_api_token_fails_if_there_is_no_credential_file() {
        let test_path = String::from("/get_api_token_fails_if_there_is_no_credential_file");
        let (move_home, _) = setup_move_home(&test_path);
        let _ = fs::remove_dir_all(&move_home);
        fs::create_dir_all(&move_home).unwrap();

        let token = get_registry_api_token(&move_home);
        assert!(token.is_err());

        clean_up(&move_home)
    }

    #[test]
    fn get_api_token_fails_if_credential_file_is_in_wrong_format() {
        let test_path = String::from("/get_api_token_fails_if_credential_file_is_in_wrong_format");
        let (move_home, credential_path) = setup_move_home(&test_path);
        let _ = fs::remove_dir_all(&move_home);
        fs::create_dir_all(&move_home).unwrap();
        File::create(&credential_path).unwrap();

        let missing_double_quote = r#"
            [registry]
            token = test-token
            "#;
        fs::write(&credential_path, missing_double_quote).unwrap();
        let token = get_registry_api_token(&move_home);
        assert!(token.is_err());

        let wrong_token_field = r#"
            [registry]
            tokens = "test-token"
            "#;
        fs::write(&credential_path, wrong_token_field).unwrap();
        let token = get_registry_api_token(&move_home);
        assert!(token.is_err());

        clean_up(&move_home)
    }

    #[test]
    fn get_movey_url_works() {
        let test_path = String::from("/get_movey_url_works");
        let (move_home, credential_path) = setup_move_home(&test_path);
        let _ = fs::create_dir_all(&move_home);
        File::create(&credential_path).unwrap();
        let content = r#"
            [registry]
            token = "test-token"
            url = "test-url"
            "#;
        fs::write(&credential_path, content).unwrap();

        let url = get_movey_url(&move_home).unwrap();
        assert_eq!(url, "test-url");

        clean_up(&move_home)
    }

    #[test]
    fn get_movey_url_returns_default_url_if_url_field_not_existed() {
        let test_path = String::from("/get_movey_url_returns_default_url_if_url_field_not_existed");
        let (move_home, credential_path) = setup_move_home(&test_path);
        let _ = fs::create_dir_all(&move_home);
        File::create(&credential_path).unwrap();
        let content = r#"
            [registry]
            token = "test-token"
            "#;
        fs::write(&credential_path, content).unwrap();

        let url = get_movey_url(&move_home).unwrap();
        assert_eq!(url, MOVEY_URL);

        clean_up(&move_home)
    }
}