summaryrefslogtreecommitdiffstats
path: root/vendor/gix-credentials/tests/protocol/context.rs
blob: 3cfd850a363890dbd00eb4f74728c1818fff1607 (plain)
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
mod destructure_url_in_place {
    use gix_credentials::protocol::Context;

    fn url_ctx(url: &str) -> Context {
        Context {
            url: Some(url.into()),
            ..Default::default()
        }
    }

    fn assert_eq_parts(
        url: &str,
        proto: &str,
        user: impl Into<Option<&'static str>>,
        host: &str,
        path: impl Into<Option<&'static str>>,
        use_http_path: bool,
    ) {
        let mut ctx = url_ctx(url);
        ctx.destructure_url_in_place(use_http_path).expect("splitting works");
        assert_eq!(ctx.protocol.expect("set proto"), proto);
        match user.into() {
            Some(expected) => assert_eq!(ctx.username.expect("set user"), expected),
            None => assert!(ctx.username.is_none()),
        }
        assert_eq!(ctx.host.expect("set host"), host);
        match path.into() {
            Some(expected) => assert_eq!(ctx.path.expect("set path"), expected),
            None => assert!(ctx.path.is_none()),
        }
    }

    #[test]
    fn parts_are_verbatim_with_non_http_url() {
        // path is always used for non-http
        assert_eq_parts("ssh://user@host:21/path", "ssh", "user", "host:21", "path", false);
        assert_eq_parts("ssh://host.org/path", "ssh", None, "host.org", "path", true);
    }
    #[test]
    fn http_and_https_ignore_the_path_by_default() {
        assert_eq_parts(
            "http://user@example.com/path",
            "http",
            Some("user"),
            "example.com",
            None,
            false,
        );
        assert_eq_parts(
            "https://github.com/byron/gitoxide",
            "https",
            None,
            "github.com",
            None,
            false,
        );
        assert_eq_parts(
            "https://github.com/byron/gitoxide/",
            "https",
            None,
            "github.com",
            "byron/gitoxide",
            true,
        );
    }
}

mod to_prompt {
    use gix_credentials::protocol::Context;

    #[test]
    fn no_scheme_means_no_url() {
        assert_eq!(Context::default().to_prompt("Username"), "Username: ");
    }

    #[test]
    fn any_scheme_means_url_is_included() {
        assert_eq!(
            Context {
                protocol: Some("https".into()),
                host: Some("host".into()),
                ..Default::default()
            }
            .to_prompt("Password"),
            "Password for https://host: "
        );
    }
}

mod to_url {
    use gix_credentials::protocol::Context;

    #[test]
    fn no_protocol_is_nothing() {
        assert_eq!(Context::default().to_url(), None);
    }
    #[test]
    fn protocol_alone_is_enough() {
        assert_eq!(
            Context {
                protocol: Some("https".into()),
                ..Default::default()
            }
            .to_url()
            .unwrap(),
            "https://"
        );
    }
    #[test]
    fn username_is_appended() {
        assert_eq!(
            Context {
                protocol: Some("https".into()),
                username: Some("user".into()),
                ..Default::default()
            }
            .to_url()
            .unwrap(),
            "https://user@"
        );
    }
    #[test]
    fn host_is_appended() {
        assert_eq!(
            Context {
                protocol: Some("https".into()),
                host: Some("host".into()),
                ..Default::default()
            }
            .to_url()
            .unwrap(),
            "https://host"
        );
    }
    #[test]
    fn path_is_appended_with_leading_slash_placed_as_needed() {
        assert_eq!(
            Context {
                protocol: Some("file".into()),
                path: Some("dir/git".into()),
                ..Default::default()
            }
            .to_url()
            .unwrap(),
            "file:///dir/git"
        );
        assert_eq!(
            Context {
                protocol: Some("file".into()),
                path: Some("/dir/git".into()),
                ..Default::default()
            }
            .to_url()
            .unwrap(),
            "file:///dir/git"
        );
    }

    #[test]
    fn all_fields_with_port_but_password_is_never_shown() {
        assert_eq!(
            Context {
                protocol: Some("https".into()),
                username: Some("user".into()),
                password: Some("secret".into()),
                host: Some("example.com:8080".into()),
                path: Some("Byron/gitoxide".into()),
                ..Default::default()
            }
            .to_url()
            .unwrap(),
            "https://user@example.com:8080/Byron/gitoxide"
        );
    }
}