Skip to content

Commit

Permalink
[ASV-149] Extracted verification method from CredentialsValidator val…
Browse files Browse the repository at this point in the history
…idate() method;

Created a test for the same class;
  • Loading branch information
franciscoCalado committed Mar 5, 2018
1 parent 1d2aef8 commit e0c801b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cm.aptoide.accountmanager;

import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import rx.Completable;

public class CredentialsValidator {
Expand All @@ -12,25 +14,28 @@ public class CredentialsValidator {
*/
public Completable validate(AptoideCredentials credentials, boolean validatePassword) {
return Completable.defer(() -> {
if (isEmpty(credentials.getEmail()) && isEmpty(credentials.getPassword())) {
return Completable.error(
new AccountValidationException(AccountValidationException.EMPTY_EMAIL_AND_PASSWORD));
} else if (isEmpty(credentials.getPassword())) {
return Completable.error(
new AccountValidationException(AccountValidationException.EMPTY_PASSWORD));
} else if (isEmpty(credentials.getEmail())) {
return Completable.error(
new AccountValidationException(AccountValidationException.EMPTY_EMAIL));
} else if (validatePassword && (credentials.getPassword()
.length() < 8 || !has1number1letter(credentials.getPassword()))) {
return Completable.error(
new AccountValidationException(AccountValidationException.INVALID_PASSWORD));
}
int x = validateFields(credentials, validatePassword);
if (x != -1) return Completable.error(new AccountValidationException(x));
return Completable.complete();
});
}

private boolean has1number1letter(String password) {
@Nullable @VisibleForTesting
protected int validateFields(AptoideCredentials credentials, boolean validatePassword) {
if (isEmpty(credentials.getEmail()) && isEmpty(credentials.getPassword())) {
return AccountValidationException.EMPTY_EMAIL_AND_PASSWORD;
} else if (isEmpty(credentials.getPassword())) {
return AccountValidationException.EMPTY_PASSWORD;
} else if (isEmpty(credentials.getEmail())) {
return AccountValidationException.EMPTY_EMAIL;
} else if (validatePassword && (credentials.getPassword()
.length() < 8 || !has1number1letter(credentials.getPassword()))) {
return AccountValidationException.INVALID_PASSWORD;
}
return -1;
}

@VisibleForTesting protected boolean has1number1letter(String password) {
boolean hasLetter = false;
boolean hasNumber = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cm.aptoide.accountmanager;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.validateMockitoUsage;

/**
* Created by franciscocalado on 02/03/18.
*/

public class CredentialsValidatorTest {

private CredentialsValidator credentialsValidator;
private AptoideCredentials validCredentials;
private AptoideCredentials invalidPasswordCredentials;
private AptoideCredentials emptyEmailCredentials;
private AptoideCredentials emptyPasswordCredentials;
private AptoideCredentials emptyCredentials;
private int result;

@Before public void setupCredentialsValidator() {
MockitoAnnotations.initMocks(this);

credentialsValidator = new CredentialsValidator();
result = -2;
}

@Test public void validateSuccessTest() {
validCredentials = new AptoideCredentials("test@test.com", "mypasstest1");

result = credentialsValidator.validateFields(validCredentials, true);
assertEquals(-1, result);
}

@Test public void validateEmptyCredentialsErrorTest() {
emptyCredentials = new AptoideCredentials("", "");

result = credentialsValidator.validateFields(emptyCredentials, true);
assertEquals(AccountValidationException.EMPTY_EMAIL_AND_PASSWORD, result);
}

@Test public void validateEmptyEmailCredentialsErrorTest() {
emptyEmailCredentials = new AptoideCredentials("", "test1");

result = credentialsValidator.validateFields(emptyEmailCredentials, true);
assertEquals(AccountValidationException.EMPTY_EMAIL, result);
}

@Test public void validateEmptyPasswordCredentialsErrorTest() {
emptyPasswordCredentials = new AptoideCredentials("test@test.com", "");

result = credentialsValidator.validateFields(emptyPasswordCredentials, true);
assertEquals(AccountValidationException.EMPTY_PASSWORD, result);
}

@Test public void validateInvalidPasswordErrorTest() {
invalidPasswordCredentials = new AptoideCredentials("test", "error");

result = credentialsValidator.validateFields(invalidPasswordCredentials, true);
assertEquals(AccountValidationException.INVALID_PASSWORD, result);
}

@After public void teardownCredentialsValidator() {
credentialsValidator = null;
result = -2;
}

@After public void validate() {
validateMockitoUsage();
}
}

0 comments on commit e0c801b

Please sign in to comment.