infinigit-cli 0.1.1

Browser-linked Internet Identity CLI for InfiniGit
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
#![cfg(unix)]

use std::{fs, os::unix::fs::PermissionsExt, path::Path, process::Command};
use tempfile::TempDir;

fn path_with(directory: &Path) -> String {
    format!(
        "{}:{}",
        directory.display(),
        std::env::var("PATH").unwrap_or_default()
    )
}

#[test]
fn login_status_and_logout_use_icp_web_delegation_and_git_identity_config() {
    let temp = TempDir::new().unwrap();
    let icp = temp.path().join("icp");
    let log = temp.path().join("icp.log");
    let git_config = temp.path().join("gitconfig");
    fs::write(
        &icp,
        r#"#!/usr/bin/env bash
set -e
printf '%s\n' "$*" >>"$INFINIGIT_TEST_ICP_LOG"
if [[ "$*" == 'identity list -q' ]]; then exit 0; fi
if [[ "$*" == *'identity principal'* ]]; then printf 'aaaaa-aa\n'; fi
"#,
    )
    .unwrap();
    fs::set_permissions(&icp, fs::Permissions::from_mode(0o755)).unwrap();
    let binary = env!("CARGO_BIN_EXE_infinigit");
    let common = |command: &mut Command| {
        command
            .env("PATH", path_with(temp.path()))
            .env("GIT_CONFIG_GLOBAL", &git_config)
            .env("INFINIGIT_TEST_ICP_LOG", &log);
    };

    let mut login = Command::new(binary);
    common(&mut login);
    let output = login
        .args([
            "auth",
            "login",
            "--name",
            "browser",
            "--auth",
            "https://id.ai",
            "--app",
            "https://code.example",
        ])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(String::from_utf8_lossy(&output.stdout).contains("aaaaa-aa"));
    let calls = fs::read_to_string(&log).unwrap();
    assert!(calls.contains("identity link web browser --auth https://id.ai --app https://code.example --storage keyring"));
    assert!(calls.contains("identity principal --identity browser"));
    let configured = Command::new("git")
        .args(["config", "--global", "--get", "infinigit.identity"])
        .env("GIT_CONFIG_GLOBAL", &git_config)
        .output()
        .unwrap();
    assert_eq!(
        String::from_utf8_lossy(&configured.stdout).trim(),
        "browser"
    );

    let mut status = Command::new(binary);
    common(&mut status);
    assert!(
        status
            .args(["auth", "status", "--name", "browser"])
            .status()
            .unwrap()
            .success()
    );

    let mut logout = Command::new(binary);
    common(&mut logout);
    assert!(
        logout
            .args(["auth", "logout", "--name", "browser"])
            .status()
            .unwrap()
            .success()
    );
    let calls = fs::read_to_string(&log).unwrap();
    assert!(calls.contains("identity delete browser"));
    let configured = Command::new("git")
        .args(["config", "--global", "--get", "infinigit.identity"])
        .env("GIT_CONFIG_GLOBAL", &git_config)
        .status()
        .unwrap();
    assert!(!configured.success());
}

#[test]
fn login_refuses_to_replace_an_existing_linked_identity() {
    let temp = TempDir::new().unwrap();
    let icp = temp.path().join("icp");
    fs::write(&icp, "#!/usr/bin/env bash\nif [[ \"$*\" == 'identity list -q' ]]; then printf 'infinigit\\n'; fi\n").unwrap();
    fs::set_permissions(&icp, fs::Permissions::from_mode(0o755)).unwrap();
    let output = Command::new(env!("CARGO_BIN_EXE_infinigit"))
        .args(["auth", "login"])
        .env("PATH", path_with(temp.path()))
        .env("GIT_CONFIG_GLOBAL", temp.path().join("gitconfig"))
        .output()
        .unwrap();
    assert!(!output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).contains("already exists"));
}

#[test]
fn logout_cannot_delete_an_unrelated_icp_identity() {
    let temp = TempDir::new().unwrap();
    let icp = temp.path().join("icp");
    fs::write(&icp, "#!/usr/bin/env bash\nexit 99\n").unwrap();
    fs::set_permissions(&icp, fs::Permissions::from_mode(0o755)).unwrap();
    let config = temp.path().join("gitconfig");
    assert!(
        Command::new("git")
            .args(["config", "--global", "infinigit.identity", "infinigit"])
            .env("GIT_CONFIG_GLOBAL", &config)
            .status()
            .unwrap()
            .success()
    );
    let output = Command::new(env!("CARGO_BIN_EXE_infinigit"))
        .args(["auth", "logout", "--name", "valuable-wallet"])
        .env("PATH", path_with(temp.path()))
        .env("GIT_CONFIG_GLOBAL", config)
        .output()
        .unwrap();
    assert!(!output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).contains("refusing to delete"));
}

#[test]
fn independent_device_login_creates_a_scoped_pairing_request() {
    let temp = TempDir::new().unwrap();
    let icp = temp.path().join("icp");
    let log = temp.path().join("icp.log");
    fs::write(&icp, r#"#!/usr/bin/env bash
set -e
printf '%s\n' "$*" >>"$INFINIGIT_TEST_ICP_LOG"
if [[ "$*" == 'identity list -q' ]]; then exit 0; fi
if [[ "$*" == *'request_device_link'* ]]; then printf 'variant { ok = record { id = 42 : nat } }\n'; fi
"#).unwrap();
    fs::set_permissions(&icp, fs::Permissions::from_mode(0o755)).unwrap();
    let config = temp.path().join("gitconfig");
    for (key, value) in [
        ("infinigit.directory-canister", "aaaaa-aa"),
        ("infinigit.network", "http://127.0.0.1:4943"),
        ("infinigit.root-key", "fetch"),
        ("infinigit.app-origin", "http://frontend.localhost:4943"),
    ] {
        assert!(
            Command::new("git")
                .args(["config", "--global", key, value])
                .env("GIT_CONFIG_GLOBAL", &config)
                .status()
                .unwrap()
                .success()
        );
    }
    let output = Command::new(env!("CARGO_BIN_EXE_infinigit"))
        .args([
            "auth",
            "link-device",
            "--name",
            "laptop",
            "--label",
            "Work laptop",
            "--read-only",
            "--storage",
            "plaintext",
            "--directory",
            "aaaaa-aa",
            "--network",
            "http://127.0.0.1:4943",
            "--root-key",
            "fetch",
        ])
        .env("PATH", path_with(temp.path()))
        .env("GIT_CONFIG_GLOBAL", &config)
        .env("INFINIGIT_TEST_ICP_LOG", &log)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("42:"));
    assert!(stdout.contains("/#/settings/devices"));
    let calls = fs::read_to_string(&log).unwrap();
    assert!(calls.contains("identity new laptop --storage plaintext"));
    assert!(calls.contains("request_device_link"));
    assert!(calls.contains("true, false, null"));
    assert!(calls.contains("--identity laptop --network http://127.0.0.1:4943 --root-key fetch"));
}

#[test]
fn device_link_defaults_to_passwordless_local_storage() {
    let temp = TempDir::new().unwrap();
    let icp = temp.path().join("icp");
    let log = temp.path().join("icp.log");
    fs::write(&icp, r#"#!/usr/bin/env bash
set -e
printf '%s\n' "$*" >>"$INFINIGIT_TEST_ICP_LOG"
if [[ "$*" == 'identity list -q' ]]; then exit 0; fi
if [[ "$*" == *'request_device_link'* ]]; then printf 'variant { ok = record { id = 7 : nat } }\n'; fi
"#).unwrap();
    fs::set_permissions(&icp, fs::Permissions::from_mode(0o755)).unwrap();
    let config = temp.path().join("gitconfig");
    for (key, value) in [
        ("infinigit.directory-canister", "aaaaa-aa"),
        ("infinigit.network", "http://127.0.0.1:4943"),
        ("infinigit.root-key", "fetch"),
    ] {
        assert!(
            Command::new("git")
                .args(["config", "--global", key, value])
                .env("GIT_CONFIG_GLOBAL", &config)
                .status()
                .unwrap()
                .success()
        );
    }
    let output = Command::new(env!("CARGO_BIN_EXE_infinigit"))
        .args([
            "auth",
            "link-device",
            "--name",
            "headless",
            "--label",
            "Headless host",
            "--directory",
            "aaaaa-aa",
        ])
        .env("PATH", path_with(temp.path()))
        .env("GIT_CONFIG_GLOBAL", &config)
        .env("INFINIGIT_TEST_ICP_LOG", &log)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let calls = fs::read_to_string(log).unwrap();
    assert!(calls.contains("identity new headless --storage plaintext"));
    assert!(calls.contains("--network ic --root-key mainnet"));
}

#[test]
fn local_development_mode_uses_launcher_git_configuration() {
    let temp = TempDir::new().unwrap();
    let icp = temp.path().join("icp");
    let log = temp.path().join("icp.log");
    fs::write(&icp, r#"#!/usr/bin/env bash
set -e
printf '%s\n' "$*" >>"$INFINIGIT_TEST_ICP_LOG"
if [[ "$*" == 'identity list -q' ]]; then exit 0; fi
if [[ "$*" == *'request_device_link'* ]]; then printf 'variant { ok = record { id = 8 : nat } }\n'; fi
"#).unwrap();
    fs::set_permissions(&icp, fs::Permissions::from_mode(0o755)).unwrap();
    let config = temp.path().join("gitconfig");
    for (key, value) in [
        ("infinigit.directory-canister", "local-directory"),
        ("infinigit.network", "http://127.0.0.1:4943"),
        ("infinigit.root-key", "fetch"),
    ] {
        assert!(
            Command::new("git")
                .args(["config", "--global", key, value])
                .env("GIT_CONFIG_GLOBAL", &config)
                .status()
                .unwrap()
                .success()
        );
    }
    let output = Command::new(env!("CARGO_BIN_EXE_infinigit"))
        .args([
            "auth",
            "link-device",
            "--name",
            "easy-local",
            "--label",
            "Easy local",
        ])
        .env("INFINIGIT_LOCAL_DEV", "1")
        .env("PATH", path_with(temp.path()))
        .env("GIT_CONFIG_GLOBAL", &config)
        .env("INFINIGIT_TEST_ICP_LOG", &log)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let calls = fs::read_to_string(&log).unwrap();
    assert!(calls.contains("canister call local-directory request_device_link"));
    assert!(calls.contains("--network http://127.0.0.1:4943 --root-key fetch"));

    let output = Command::new(env!("CARGO_BIN_EXE_infinigit"))
        .args([
            "auth",
            "link-device",
            "--name",
            "explicit-local",
            "--directory",
            "explicit-directory",
            "--network",
            "http://localhost:8000",
            "--root-key",
            "mainnet",
        ])
        .env("INFINIGIT_LOCAL_DEV", "true")
        .env("PATH", path_with(temp.path()))
        .env("GIT_CONFIG_GLOBAL", &config)
        .env("INFINIGIT_TEST_ICP_LOG", &log)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let calls = fs::read_to_string(log).unwrap();
    assert!(calls.contains("canister call explicit-directory request_device_link"));
    assert!(calls.contains("--network http://localhost:8000 --root-key mainnet"));
}

#[test]
fn device_link_can_resume_with_an_identity_created_by_a_failed_attempt() {
    let temp = TempDir::new().unwrap();
    let icp = temp.path().join("icp");
    let log = temp.path().join("icp.log");
    fs::write(&icp, r#"#!/usr/bin/env bash
set -e
printf '%s\n' "$*" >>"$INFINIGIT_TEST_ICP_LOG"
if [[ "$*" == 'identity list -q' ]]; then printf 'my-laptop\n'; fi
if [[ "$*" == *'request_device_link'* ]]; then printf 'variant { ok = record { id = 9 : nat } }\n'; fi
"#).unwrap();
    fs::set_permissions(&icp, fs::Permissions::from_mode(0o755)).unwrap();
    let config = temp.path().join("gitconfig");
    for (key, value) in [
        ("infinigit.directory-canister", "aaaaa-aa"),
        ("infinigit.network", "http://127.0.0.1:4943"),
        ("infinigit.root-key", "fetch"),
    ] {
        assert!(
            Command::new("git")
                .args(["config", "--global", key, value])
                .env("GIT_CONFIG_GLOBAL", &config)
                .status()
                .unwrap()
                .success()
        );
    }
    let output = Command::new(env!("CARGO_BIN_EXE_infinigit"))
        .args([
            "auth",
            "link-device",
            "--name",
            "my-laptop",
            "--label",
            "My laptop",
            "--reuse-existing",
            "--directory",
            "aaaaa-aa",
            "--network",
            "http://127.0.0.1:4943",
            "--root-key",
            "fetch",
        ])
        .env("PATH", path_with(temp.path()))
        .env("GIT_CONFIG_GLOBAL", &config)
        .env("INFINIGIT_TEST_ICP_LOG", &log)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let calls = fs::read_to_string(log).unwrap();
    assert!(!calls.contains("identity new my-laptop"));
    assert!(calls.contains("--network http://127.0.0.1:4943 --root-key fetch"));
}

#[test]
fn device_link_requires_a_directory_until_mainnet_is_deployed() {
    let temp = TempDir::new().unwrap();
    let output = Command::new(env!("CARGO_BIN_EXE_infinigit"))
        .args(["auth", "link-device", "--name", "mainnet-device"])
        .env_remove("INFINIGIT_DIRECTORY_CANISTER_ID")
        .env("PATH", path_with(temp.path()))
        .env("GIT_CONFIG_GLOBAL", temp.path().join("gitconfig"))
        .output()
        .unwrap();
    assert!(!output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).contains("pass --directory"));

    let output = Command::new(env!("CARGO_BIN_EXE_infinigit"))
        .args(["auth", "link-device", "--name", "invalid-local-mode"])
        .env("INFINIGIT_LOCAL_DEV", "yes")
        .env("PATH", path_with(temp.path()))
        .output()
        .unwrap();
    assert!(!output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).contains("must be 1, true, 0, or false"));
}