Skip to content

Commit

Permalink
Merge pull request #298 from tmc-cli/ask-server-from-user-mikko
Browse files Browse the repository at this point in the history
Ask server from user if not given #267
  • Loading branch information
Salmela authored Jun 21, 2016
2 parents 3fe2ea8 + f4108de commit dcefec9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,9 @@ Once installation is complete, you can log in using `tmc login`. This saves your
~ $ tmc login
username: my-username
password:
server address:
Login successful.
```
By default, tmc-cli connects to University of Helsinki MOOC server. To log in to another server, specify the server with the `-s` or `--server` switch:
```
~ $ tmc login -s [SERVER_ADDRESS]
```

##Listing courses

Expand Down
35 changes: 9 additions & 26 deletions src/main/java/fi/helsinki/cs/tmc/cli/command/LoginCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,22 @@ public void getOptions(Options options) {
@Override
public void run(CommandLine args, Io io) {
this.io = io;
String username = getUsername(args);
String password = getPassword(args);
String serverAddress = getServerAddress(args);
String username = getLoginInfo(args, "u", "username: ");
String password = getLoginInfo(args, "p", "password: ");
String serverAddress = getLoginInfo(args, "s", "server address: ");

Settings settings = new Settings(serverAddress, username, password);
if (loginPossible(settings) && saveLoginSettings(settings)) {
io.println("Login successful.");
}
}

private String getUsername(CommandLine line) {
String username = line.getOptionValue("u");
if (username == null) {
username = io.readLine("username: ");
}
return username;
}

private String getPassword(CommandLine line) {
String password = line.getOptionValue("p");
if (password == null) {
password = io.readPassword("password: ");
}
return password;
}

private String getServerAddress(CommandLine line) {
String serverAddress = line.getOptionValue("s");
if (serverAddress == null) {
// todo: don't hardcode the default value, get it from somewhere
serverAddress = "https://tmc.mooc.fi/mooc";

private String getLoginInfo(CommandLine line, String option, String prompt) {
String info = line.getOptionValue(option);
if (info == null) {
info = io.readLine(prompt);
}
return serverAddress;
return info;
}

private boolean saveLoginSettings(Settings settings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.rmi.ServerException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -65,6 +66,16 @@ public void logsInWithCorrectServerUserAndPassword() {
app.run(args);
assertThat(io.out(), containsString("Login successful."));
}

@Test
public void userGetsErrorMessageIfLoginFails() {
when(mockCore.listCourses((ProgressObserver) anyObject()))
.thenReturn(successfulCallable());
when(SettingsIo.save(any(Settings.class))).thenReturn(false);
String[] args = {"login", "-s", SERVER, "-u", USERNAME, "-p", "WrongPassword"};
app.run(args);
assertThat(io.out(), containsString("Login failed."));
}

@Test
public void catches401IfCorrectServerAndWrongUsername() {
Expand All @@ -79,6 +90,20 @@ public List<Course> call() throws Exception {
app.run(args);
assertThat(io.out(), containsString("Incorrect username or password."));
}

@Test
public void userGetsErrorMessageIfUnableToConnectToServer() {
Callable<List<Course>> callableEx = new Callable<List<Course>>() {
@Override
public List<Course> call() throws Exception {
throw new ServerException("SomeException");
}
};
when(mockCore.listCourses((ProgressObserver) anyObject())).thenReturn(callableEx);
String[] args = {"login", "-s", SERVER, "-u", "foo", "-p", PASSWORD};
app.run(args);
assertThat(io.out(), containsString("Unable to connect to server"));
}

@Test
public void loginAsksUsernameFromUserIfNotGiven() {
Expand All @@ -95,7 +120,17 @@ public void loginAsksPasswordFromUserIfNotGiven() {
when(mockCore.listCourses((ProgressObserver) anyObject()))
.thenReturn(successfulCallable());
String[] args = {"login", "-s", SERVER, "-u", USERNAME};
io.addPasswordPrompt(PASSWORD);
io.addLinePrompt(PASSWORD);
app.run(args);
assertTrue(io.allPromptsUsed());
}

@Test
public void loginAsksServerFromUserIfNotGiven() {
when(mockCore.listCourses((ProgressObserver) anyObject()))
.thenReturn(successfulCallable());
String[] args = {"login", "-p", PASSWORD, "-u", USERNAME};
io.addLinePrompt(SERVER);
app.run(args);
assertTrue(io.allPromptsUsed());
}
Expand Down

0 comments on commit dcefec9

Please sign in to comment.