Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#675 User requests password reset from Blackberry] #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .metadata/version.ini

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void shouldInvokeProcessSuccessOnSuccess() throws Exception {
Response response = new Response(result, 200);

listener.done(requestContext, response);
verify(callback).onProcessSuccess();
verify(callback).onProcessSuccess("");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void shouldCallProcessSuccessOnSuccess() throws Exception {

listener.done(requestContext, successfulResponse());

verify(requestCallback).onProcessSuccess();
verify(requestCallback).onProcessSuccess("");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.rapidftr.services;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.junit.After;
import org.junit.Assert;
Expand All @@ -10,21 +13,26 @@
import com.rapidftr.Key;
import com.rapidftr.datastore.MockStore;
import com.rapidftr.net.HttpService;
import com.rapidftr.utilities.HttpUtility;
import com.rapidftr.utilities.Settings;
import com.sun.me.web.request.Arg;
import com.sun.me.web.request.PostData;

public class LoginServiceTest {
private LoginSettings settings;
private HttpService service;
private HttpService httpService;
private MockStore store;
LoginService loginService;

@Before
public void setUpStore(){
service = mock(HttpService.class);
httpService = mock(HttpService.class);
store = new MockStore(new Key("mock"));
settings = new LoginSettings(new Settings(store));
settings.setLastUsedUserName("test");
settings.setLastUsedPassword("pass");
settings.setAuthorisationTokenForOfflineLogin("token");
loginService = new LoginService(httpService, settings);
}

@After
Expand All @@ -34,18 +42,29 @@ public void clearData(){

@Test
public void shouldAuthenticateOfflineUserWithLastUsedCredentials() {
LoginService login = new LoginService(service, settings);
Assert.assertTrue(login.offlineLogin("test", "pass"));

Assert.assertTrue(loginService.offlineLogin("test", "pass"));
String user = store.getString("current.user");
Assert.assertNotNull(user);
Assert.assertEquals("test", user);
}

@Test
public void shouldNotAuthenticateOfflineUserCredentialsAreInValid() {
LoginService login = new LoginService(service, settings);
Assert.assertFalse(login.offlineLogin("test2", "pass"));
Assert.assertFalse(loginService.offlineLogin("test2", "pass"));
String user = store.getString("current.user");
Assert.assertNull(user);
}

@Test
public void shouldRequestForPasswordRecovery(){
String username = "username";
String imei = "000000000000000";
Arg[] postArgs = new Arg[]{new Arg("password_recovery_request[user_name]", username),
new Arg("password_recovery_request[imei]", imei)};
loginService.recoverPassword(username, imei);
verify(httpService).post(eq("password_recovery_requests"),
eq(postArgs),eq(new Arg[]{HttpUtility.HEADER_ACCEPT_JSON}),
(com.sun.me.web.request.RequestListener)any(), eq((PostData)null),any());
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package com.rapidftr.controllers;

import net.rim.device.api.system.GPRSInfo;

import com.rapidftr.controllers.internal.Dispatcher;
import com.rapidftr.controllers.internal.RequestAwareController;
import com.rapidftr.net.ConnectionFactory;
import com.rapidftr.process.Process;
import com.rapidftr.screens.LoginScreen;
import com.rapidftr.screens.internal.UiStack;
import com.rapidftr.services.LoginService;
import com.rapidftr.utilities.Constants;

public class LoginController extends RequestAwareController {

Process callingProcess;
private ConnectionFactory connectionFactory;
private final String OFFLINE_LOGIN_ERROR_MESSAGE = "You are working offline. You must authenticate with your credentials from the last successful online log in.";

private boolean isPasswordRecoveryClicked = false;

public LoginController(LoginScreen screen, UiStack uiStack,
LoginService loginService,
ConnectionFactory connectionFactory,
Expand All @@ -38,13 +42,21 @@ public void login(String userName, String password) {
}
}

public void requestPasswordRecovery(String userName){
isPasswordRecoveryClicked = true;
getScreenCallBack().setProgressMessage("Requesting Password recovery ...");
LoginService loginService = (LoginService)service;
loginService.recoverPassword(userName, GPRSInfo.imeiToString(GPRSInfo.getIMEI()));

}
public void onProcessComplete(boolean status) {
if (status) {
if (!isPasswordRecoveryClicked && status) {
popScreen();
if (callingProcess != null) {
callingProcess.startProcess();
}
}
isPasswordRecoveryClicked = false;
}

public void showLoginScreen(Process callingProcess) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import com.rapidftr.utilities.Constants;

import net.rim.device.api.system.CoverageInfo;
import net.rim.device.api.system.WLANInfo;

public class ConnectionFactory {

public HttpConnection openConnection(String url) throws IOException {
if (isNotConnected()) {
throw new IOException("Could not establish connection with host because all connectors are offline");
throw new IOException(Constants.CONNECTIONS_ARE_OFFLINE);
}
if (isWIFIAvailable()) {
url = url + ";interface=wifi";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.rapidftr.net;

import java.io.IOException;
import java.util.Hashtable;

import javax.microedition.io.HttpConnection;

import com.rapidftr.services.ServiceException;
import com.sun.me.web.path.ResultException;
import com.sun.me.web.request.Arg;
import com.sun.me.web.request.PostData;
import com.sun.me.web.request.RequestListener;
Expand All @@ -18,6 +21,7 @@ public class HttpBatchRequestHandler implements RequestListener {
private boolean processCompleted = false;
private HttpService service;
private ResponseErrors errors;
private static final String PASSWORD_RECOVERY = "password_recovery_requests";

public HttpBatchRequestHandler(HttpService httpService) {
service = httpService;
Expand Down Expand Up @@ -87,9 +91,21 @@ public void writeProgress(Object context, int bytes, int total) {
// requestCallBack.writeProgress(context, bytes, total);
}

public void markProcessComplete() {
public void markProcessComplete(Object context, Response response) {
markRequestsAsCompleted();
String responseMessage = null;
try {
if(response != null && context !=null && ((Hashtable) context).get(PASSWORD_RECOVERY) != null)
responseMessage = response.getResult().getAsString("response");
} catch (ResultException e) {
throw new ServiceException("JSON returned from password recovery do not have key 'result'");
}
String message = (response != null && responseMessage!=null)? responseMessage: "";
requestCallBack.onProcessSuccess(message);
}

private void markRequestsAsCompleted() {
unprocessedRequests = totalRequests = 0;
requestCallBack.onProcessSuccess();
}

public void markProcessFailed() {
Expand All @@ -104,7 +120,10 @@ public void markProcessFailed(String failureMessage) {
terminateProcess();
requestCallBack.onProcessFail(failureMessage);
}


public void markProcessSuccess(String successMessage){
requestCallBack.onProcessSuccess(successMessage);
}
public void terminateProcess() {
service.cancelRequest();
totalRequests = 0;
Expand Down Expand Up @@ -135,24 +154,24 @@ public void done(Object context, Response response) {
} else {
handleResponseErrors(context, response);
}
cleanUp();
cleanUp(context, response);

}

private synchronized void cleanUp() {
private synchronized void cleanUp(Object context, Response response) {
if (unprocessedRequests > 0) {
unprocessedRequests--;
}
checkForProcessCompletion();
checkForProcessCompletion(context, response);
}

public synchronized void checkForProcessCompletion() {
public synchronized void checkForProcessCompletion(Object context, Response response) {
if (unprocessedRequests == 0 && ! processCompleted) {
processCompleted = true;
if (failedRequest) {
markProcessFailed();
} else {
markProcessComplete();
markProcessComplete(context, response);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface RequestCallBack {

void onProcessStart();

void onProcessSuccess();
void onProcessSuccess(String successMessage);

void onProcessFail(String failureMessage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void onProcessFail(String failureMessage) {
showInformation();
}

public void onProcessSuccess() {
public void onProcessSuccess(String message) {
showInformation();
}

Expand Down
50 changes: 40 additions & 10 deletions RapidFTR-Blackberry/src/com/rapidftr/screens/LoginScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class LoginScreen extends CustomScreen implements ScreenCallBack {
private Button loginButton;
private Manager buttonManager;
private Button cancelButton;
private Button passwordRecoveryButton;
private boolean isLoginClicked = false;
private boolean isPasswordRecoveryClicked = false;

public LoginScreen(HttpSettings httpSettings) {
super();
Expand Down Expand Up @@ -85,7 +88,13 @@ public void fieldChanged(Field field, int context) {
onLoginButtonClicked();
}
});


passwordRecoveryButton = new Button("Reset Password");
passwordRecoveryButton.setChangeListener(new FieldChangeListener(){
public void fieldChanged(Field field, int context){
onResetPasswordClicked();
}
});
cancelButton = new Button("Cancel");
cancelButton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Expand Down Expand Up @@ -117,6 +126,7 @@ private void clearProgressMessage() {
}

private void onLoginButtonClicked() {
isLoginClicked = true;
httpSettings.setHost(urlField.getText());

usernameField.setFocus();
Expand All @@ -125,12 +135,22 @@ private void onLoginButtonClicked() {
showCancelButton();
}

private LoginController getController() {

private void onResetPasswordClicked() {
isPasswordRecoveryClicked = true;
httpSettings.setHost(urlField.getText());

usernameField.setFocus();
getController().requestPasswordRecovery(usernameField.getText());
showCancelButton();

}
private LoginController getController() {
return ((LoginController) controller);
}

private void onCancelButtonClicked() {
showLoginButton();
showLoginScreenButtons();
}

private void showCancelButton() {
Expand All @@ -139,13 +159,14 @@ private void showCancelButton() {
}

public void setUp() {
showLoginButton();
showLoginScreenButtons();
clearProgressMessage();
}

private void showLoginButton() {
private void showLoginScreenButtons() {
buttonManager.deleteAll();
buttonManager.add(loginButton);
buttonManager.add(passwordRecoveryButton);
}


Expand All @@ -157,7 +178,7 @@ public void onConnectionProblem() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
setProgressMessage("Connection Problem ");
showLoginButton();
showLoginScreenButtons();
}
});
}
Expand All @@ -166,17 +187,26 @@ public void updateProgress(int progress) {

}

public void onProcessSuccess() {
resetCredentials(true);
getController().synchronizeForms();
public void onProcessSuccess(final String message) {
if (isPasswordRecoveryClicked){
isPasswordRecoveryClicked = false;
showLoginScreenWithMessage(message);
}else {
resetCredentials(true);
getController().synchronizeForms();
}
}

public void onProcessFail(final String message) {
showLoginScreenWithMessage(message);
}

private void showLoginScreenWithMessage(final String message) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
if (!(message == null || "".equals(message.trim())))
setProgressMessage(message);
showLoginButton();
showLoginScreenButtons();
resetCredentials(false);
passwordField.setFocus();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void run() {
});
}

public void onProcessSuccess() {
public void onProcessSuccess(String message) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
downloadProgressBar.setLabel("Complete");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private synchronized void checkIfDone() {
if (hasError) {
requestCallBack.onProcessFail("Errors have occurred");
} else {
requestCallBack.onProcessSuccess();
requestCallBack.onProcessSuccess("");
}
}
}
Expand Down
Loading