upleb.uk

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

summaryrefslogtreecommitdiff
path: root/test_utils/src
diff options
context:
space:
mode:
Diffstat (limited to 'test_utils/src')
-rw-r--r--test_utils/src/lib.rs116
1 files changed, 112 insertions, 4 deletions
diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs
index 495e8d2..1a4231a 100644
--- a/test_utils/src/lib.rs
+++ b/test_utils/src/lib.rs
@@ -3,14 +3,38 @@ use std::ffi::OsStr;
3use anyhow::{ensure, Context, Result}; 3use anyhow::{ensure, Context, Result};
4use dialoguer::theme::{ColorfulTheme, Theme}; 4use dialoguer::theme::{ColorfulTheme, Theme};
5use directories::ProjectDirs; 5use directories::ProjectDirs;
6use nostr::{self, prelude::FromSkStr};
7use once_cell::sync::Lazy;
6use rexpect::session::{Options, PtySession}; 8use rexpect::session::{Options, PtySession};
7use strip_ansi_escapes::strip_str; 9use strip_ansi_escapes::strip_str;
8 10
9pub static TEST_KEY_1_NSEC: &str = 11pub static TEST_KEY_1_NSEC: &str =
10 "nsec1ppsg5sm2aexq06juxmu9evtutr6jkwkhp98exxxvwamhru9lyx9s3rwseq"; 12 "nsec1ppsg5sm2aexq06juxmu9evtutr6jkwkhp98exxxvwamhru9lyx9s3rwseq";
13pub static TEST_KEY_1_SK_HEX: &str =
14 "08608a436aee4c07ea5c36f85cb17c58f52b3ad7094f9318cc777771f0bf218b";
15pub static TEST_KEY_1_NPUB: &str =
16 "npub175lyhnt6nn00qjw0v3navw9pxgv43txnku0tpxprl4h6mvpr6a5qlphudg";
17pub static TEST_KEY_1_DISPLAY_NAME: &str = "bob";
18pub static TEST_KEY_1_ENCRYPTED: &str = "ncryptsec1qyq607h3cykxc3f2a44u89cdk336fptccn3fm5pf3nmf93d3c86qpunc7r6klwcn6lyszjy72wxwqq9aljg4pm6atvjrds9e248yhv76xfnt464265kgnjsvg8rlg06wg4sp9uljzfpu8zuaztcvfn2j8ggdrg8mldh850cy75efsyqqansert9wqmn4e6khpgvfz7h5le9";
19pub static TEST_KEY_1_ENCRYPTED_WEAK: &str = "ncryptsec1qy8ke0tjqnn8wt3w6lnc86c27ry3qrptxctjfcgruryxy0at238kwyjwsswd7z88thysruzw3awlrsxjvw5uptcd7vt70ft9rtkx00m8cgy3khm4hxa5d2gfnc6athnfruy2eyl6pkas8k34jg85z7xjqqadzfzh9rp0fzxqtw0tvxksac3n8yc98uksvuf93e0lcvqy8j6";
20pub static TEST_KEY_1_KEYS: Lazy<nostr::Keys> =
21 Lazy::new(|| nostr::Keys::from_sk_str(TEST_KEY_1_NSEC).unwrap());
11 22
12pub static TEST_KEY_2_NSEC: &str = 23pub static TEST_KEY_2_NSEC: &str =
13 "nsec1ypglg6nj6ep0g2qmyfqcv2al502gje3jvpwye6mthmkvj93tqkesknv6qm"; 24 "nsec1ypglg6nj6ep0g2qmyfqcv2al502gje3jvpwye6mthmkvj93tqkesknv6qm";
25pub static TEST_KEY_2_NPUB: &str =
26 "npub1h2yz2eh0798nh25hvypenrz995nla9dktfuk565ljf3ghnkhdljsul834e";
27
28pub static TEST_KEY_2_DISPLAY_NAME: &str = "carole";
29pub static TEST_KEY_2_ENCRYPTED: &str = "...2";
30pub static TEST_KEY_2_KEYS: Lazy<nostr::Keys> =
31 Lazy::new(|| nostr::Keys::from_sk_str(TEST_KEY_2_NSEC).unwrap());
32
33pub static TEST_INVALID_NSEC: &str = "nsec1ppsg5sm2aex";
34pub static TEST_PASSWORD: &str = "769dfd£pwega8SHGv3!#Bsfd5t";
35pub static TEST_INVALID_PASSWORD: &str = "INVALID769dfd£pwega8SHGv3!";
36pub static TEST_WEAK_PASSWORD: &str = "fhaiuhfwe";
37pub static TEST_RANDOM_TOKEN: &str = "lkjh2398HLKJ43hrweiJ6FaPfdssgtrg";
14 38
15/// wrapper for a cli testing tool - currently wraps rexpect and dialoguer 39/// wrapper for a cli testing tool - currently wraps rexpect and dialoguer
16/// 40///
@@ -41,6 +65,16 @@ impl CliTester {
41 i.prompt(true).context("initial input prompt")?; 65 i.prompt(true).context("initial input prompt")?;
42 Ok(i) 66 Ok(i)
43 } 67 }
68
69 pub fn expect_password(&mut self, prompt: &str) -> Result<CliTesterPasswordPrompt> {
70 let mut i = CliTesterPasswordPrompt {
71 tester: self,
72 prompt: prompt.to_string(),
73 confirmation_prompt: "".to_string(),
74 };
75 i.prompt().context("initial password prompt")?;
76 Ok(i)
77 }
44} 78}
45 79
46pub struct CliTesterInputPrompt<'a> { 80pub struct CliTesterInputPrompt<'a> {
@@ -101,6 +135,70 @@ impl CliTesterInputPrompt<'_> {
101 } 135 }
102} 136}
103 137
138pub struct CliTesterPasswordPrompt<'a> {
139 tester: &'a mut CliTester,
140 prompt: String,
141 confirmation_prompt: String,
142}
143
144impl CliTesterPasswordPrompt<'_> {
145 fn prompt(&mut self) -> Result<&mut Self> {
146 let p = match self.confirmation_prompt.is_empty() {
147 true => self.prompt.as_str(),
148 false => self.confirmation_prompt.as_str(),
149 };
150
151 let mut s = String::new();
152 self.tester
153 .formatter
154 .format_password_prompt(&mut s, p)
155 .expect("diagluer theme formatter should succeed");
156
157 ensure!(s.contains(p), "dialoguer must be broken");
158
159 self.tester
160 .expect(format!("\r{}", sanatize(s)).as_str())
161 .context("expect password input prompt")?;
162 Ok(self)
163 }
164
165 pub fn with_confirmation(&mut self, prompt: &str) -> Result<&mut Self> {
166 self.confirmation_prompt = prompt.to_string();
167 Ok(self)
168 }
169
170 pub fn succeeds_with(&mut self, password: &str) -> Result<&mut Self> {
171 self.tester.send_line(password)?;
172
173 self.tester
174 .expect("\r\n")
175 .context("expect new lines after password input")?;
176
177 if !self.confirmation_prompt.is_empty() {
178 self.prompt()
179 .context("expect password confirmation prompt")?;
180 self.tester.send_line(password)?;
181 self.tester
182 .expect("\r\n\r")
183 .context("expect new lines after password confirmation input")?;
184 }
185
186 let mut s = String::new();
187 self.tester
188 .formatter
189 .format_password_prompt_selection(&mut s, self.prompt.as_str())
190 .expect("diagluer theme formatter should succeed");
191
192 ensure!(s.contains(self.prompt.as_str()), "dialoguer must be broken");
193
194 self.tester
195 .expect(format!("\r{}\r\n", sanatize(s)).as_str())
196 .context("expect password prompt success")?;
197
198 Ok(self)
199 }
200}
201
104impl CliTester { 202impl CliTester {
105 pub fn new<I, S>(args: I) -> Self 203 pub fn new<I, S>(args: I) -> Self
106 where 204 where
@@ -108,7 +206,17 @@ impl CliTester {
108 S: AsRef<OsStr>, 206 S: AsRef<OsStr>,
109 { 207 {
110 Self { 208 Self {
111 rexpect_session: rexpect_with(args).expect("rexpect to spawn new process"), 209 rexpect_session: rexpect_with(args, 2000).expect("rexpect to spawn new process"),
210 formatter: ColorfulTheme::default(),
211 }
212 }
213 pub fn new_with_timeout<I, S>(timeout_ms: u64, args: I) -> Self
214 where
215 I: IntoIterator<Item = S>,
216 S: AsRef<OsStr>,
217 {
218 Self {
219 rexpect_session: rexpect_with(args, timeout_ms).expect("rexpect to spawn new process"),
112 formatter: ColorfulTheme::default(), 220 formatter: ColorfulTheme::default(),
113 } 221 }
114 } 222 }
@@ -122,7 +230,7 @@ impl CliTester {
122 .process 230 .process
123 .exit() 231 .exit()
124 .expect("process to exit"); 232 .expect("process to exit");
125 self.rexpect_session = rexpect_with(args).expect("rexpect to spawn new process"); 233 self.rexpect_session = rexpect_with(args, 2000).expect("rexpect to spawn new process");
126 self 234 self
127 } 235 }
128 236
@@ -213,7 +321,7 @@ fn sanatize(s: String) -> String {
213 .collect::<String>() 321 .collect::<String>()
214} 322}
215 323
216pub fn rexpect_with<I, S>(args: I) -> Result<PtySession, rexpect::error::Error> 324pub fn rexpect_with<I, S>(args: I, timeout_ms: u64) -> Result<PtySession, rexpect::error::Error>
217where 325where
218 I: IntoIterator<Item = S>, 326 I: IntoIterator<Item = S>,
219 S: AsRef<std::ffi::OsStr>, 327 S: AsRef<std::ffi::OsStr>,
@@ -224,7 +332,7 @@ where
224 rexpect::session::spawn_with_options( 332 rexpect::session::spawn_with_options(
225 cmd, 333 cmd,
226 Options { 334 Options {
227 timeout_ms: Some(2000), 335 timeout_ms: Some(timeout_ms),
228 strip_ansi_escape_codes: true, 336 strip_ansi_escape_codes: true,
229 }, 337 },
230 ) 338 )