Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit 723f22b

Browse files
authored
Merge pull request #67 from benbc/insist-username-not-empty
Insist that username is not empty
2 parents 5c6f451 + 11bfd87 commit 723f22b

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

cypher-shell/src/main/java/org/neo4j/shell/Main.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,36 @@ void connectInteractively(@Nonnull CypherShell shell, @Nonnull ConnectionConfig
127127
}
128128
// else need to prompt for username and password
129129
if (connectionConfig.username().isEmpty()) {
130-
connectionConfig.setUsername(promptForText("username: ", null));
130+
connectionConfig.setUsername(promptForNonEmptyText("username", null));
131131
}
132132
if (connectionConfig.password().isEmpty()) {
133-
connectionConfig.setPassword(promptForText("password: ", '*'));
133+
connectionConfig.setPassword(promptForText("password", '*'));
134134
}
135135
// try again
136136
shell.connect(connectionConfig);
137137
}
138138
}
139139

140+
/**
141+
* @param prompt
142+
* to display to the user
143+
* @param mask
144+
* single character to display instead of what the user is typing, use null if text is not secret
145+
* @return the text which was entered
146+
* @throws Exception
147+
* in case of errors
148+
*/
149+
@Nonnull
150+
private String promptForNonEmptyText(@Nonnull String prompt, @Nullable Character mask) throws Exception {
151+
String text = promptForText(prompt, mask);
152+
if (!text.isEmpty()) {
153+
return text;
154+
}
155+
out.println(prompt + " cannot be empty");
156+
out.println();
157+
return promptForNonEmptyText(prompt, mask);
158+
}
159+
140160
/**
141161
* @param prompt
142162
* to display to the user
@@ -150,7 +170,7 @@ void connectInteractively(@Nonnull CypherShell shell, @Nonnull ConnectionConfig
150170
private String promptForText(@Nonnull String prompt, @Nullable Character mask) throws Exception {
151171
String line;
152172
ConsoleReader consoleReader = new ConsoleReader(in, out);
153-
line = consoleReader.readLine(prompt, mask);
173+
line = consoleReader.readLine(prompt + ": ", mask);
154174
consoleReader.close();
155175

156176
if (line == null) {

cypher-shell/src/test/java/org/neo4j/shell/MainTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,28 @@ public void connectInteractivelyTriesOnlyOnceIfUserPassExists() throws Exception
170170
}
171171
}
172172

173+
@Test
174+
public void connectInteractivelyRepromptsIfUserIsNotProvided() throws Exception {
175+
doThrow(authException).doNothing().when(shell).connect(connectionConfig);
176+
177+
String inputString = "\nbob\nsecret\n";
178+
InputStream inputStream = new ByteArrayInputStream(inputString.getBytes());
179+
180+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
181+
PrintStream ps = new PrintStream(baos);
182+
183+
Main main = new Main(inputStream, ps);
184+
main.connectInteractively(shell, connectionConfig);
185+
186+
String out = new String(baos.toByteArray(), StandardCharsets.UTF_8);
187+
188+
assertEquals(out, "username: \r\nusername cannot be empty" + System.lineSeparator() + System.lineSeparator() +
189+
"username: bob\r\npassword: ******\r\n");
190+
verify(connectionConfig).setUsername("bob");
191+
verify(connectionConfig).setPassword("secret");
192+
verify(shell, times(2)).connect(connectionConfig);
193+
}
194+
173195
@Test
174196
public void printsVersionAndExits() throws Exception {
175197
CliArgs args = new CliArgs();

0 commit comments

Comments
 (0)