upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/login.rs145
1 files changed, 145 insertions, 0 deletions
diff --git a/tests/login.rs b/tests/login.rs
new file mode 100644
index 0000000..a7e1889
--- /dev/null
+++ b/tests/login.rs
@@ -0,0 +1,145 @@
1use anyhow::Result;
2use serial_test::serial;
3use test_utils::*;
4
5static EXPECTED_NSEC_PROMPT: &str = "login with nsec (or hex private key)";
6
7fn standard_login() -> Result<CliTester> {
8 let mut p = CliTester::new(["login"]);
9
10 p.expect_input_eventually(EXPECTED_NSEC_PROMPT)?
11 .succeeds_with(TEST_KEY_1_NSEC)?;
12
13 p.expect_end_eventually()?;
14 Ok(p)
15}
16
17mod when_first_time_login {
18 use super::*;
19
20 #[test]
21 #[serial]
22 fn prompts_for_nsec() -> Result<()> {
23 with_fresh_config(|| {
24 standard_login()?;
25 Ok(())
26 })
27 }
28
29 #[test]
30 #[serial]
31 fn succeeds_with_text_logged_in_as_npub() -> Result<()> {
32 with_fresh_config(|| {
33 let mut p = CliTester::new(["login"]);
34
35 p.expect_input(EXPECTED_NSEC_PROMPT)?
36 .succeeds_with(TEST_KEY_1_NSEC)?;
37
38 p.expect_end_with(format!("logged in as {}\r\n", TEST_KEY_1_NSEC).as_str())
39 })
40 }
41
42 #[test]
43 #[serial]
44 fn next_time_returns_logged_in_as_npub() -> Result<()> {
45 with_fresh_config(|| {
46 standard_login()?.exit()?;
47
48 CliTester::new(["login"])
49 .expect(format!("logged in as {}\r\n", TEST_KEY_1_NSEC).as_str())?
50 .exit()
51 })
52 }
53}
54
55mod when_called_with_nsec_parameter {
56 use super::*;
57
58 #[test]
59 #[serial]
60 fn valid_nsec_param_succeeds_without_prompts() -> Result<()> {
61 with_fresh_config(|| {
62 CliTester::new(["--nsec", TEST_KEY_2_NSEC, "login"])
63 .expect_end_with(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str())?;
64
65 CliTester::new(["login"])
66 .expect(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str())?
67 .exit()
68 })
69 }
70
71 mod when_logging_in_as_different_nsec {
72 use super::*;
73
74 #[test]
75 #[serial]
76 fn valid_nsec_param_succeeds_without_prompts_and_logs_in() -> Result<()> {
77 with_fresh_config(|| {
78 standard_login()?.exit()?;
79
80 CliTester::new(["--nsec", TEST_KEY_2_NSEC, "login"])
81 .expect(format!("logged in as {}\r\n", TEST_KEY_1_NSEC).as_str())?
82 .expect_end_with(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str())?;
83
84 CliTester::new(["login"])
85 .expect(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str())?
86 .exit()
87 })
88 }
89 }
90}
91
92mod when_logged_in {
93 use super::*;
94
95 #[test]
96 #[serial]
97 fn returns_logged_in_as_npub() -> Result<()> {
98 with_fresh_config(|| {
99 standard_login()?.exit()?;
100
101 CliTester::new(["login"])
102 .expect(format!("logged in as {}\r\n", TEST_KEY_1_NSEC).as_str())?
103 .exit()
104 })
105 }
106
107 #[test]
108 #[serial]
109 fn prompts_to_log_in_with_different_nsec() -> Result<()> {
110 with_fresh_config(|| {
111 standard_login()?.exit()?;
112
113 let mut p = CliTester::new(["login"]);
114 p.expect(format!("logged in as {}\r\n", TEST_KEY_1_NSEC).as_str())?;
115
116 p.expect_input(EXPECTED_NSEC_PROMPT)?
117 .succeeds_with(TEST_KEY_2_NSEC)?;
118
119 p.expect_end_with(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str())
120 })
121 }
122 mod when_logging_in_as_different_nsec {
123 use super::*;
124
125 #[test]
126 #[serial]
127 fn confirmed_as_logged_in_as_additional_user() -> Result<()> {
128 with_fresh_config(|| {
129 standard_login()?.exit()?;
130
131 let mut p = CliTester::new(["login"]);
132 p.expect(format!("logged in as {}\r\n", TEST_KEY_1_NSEC).as_str())?;
133
134 p.expect_input(EXPECTED_NSEC_PROMPT)?
135 .succeeds_with(TEST_KEY_2_NSEC)?;
136
137 p.expect_end_with(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str())?;
138
139 CliTester::new(["login"])
140 .expect(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str())?
141 .exit()
142 })
143 }
144 }
145}