Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import android.app.Activity;
import android.app.Instrumentation;
Expand Down Expand Up @@ -79,8 +80,9 @@ public void testGetStartedButtonHasClickListener() {
@Test
public void testOnCreateDisablesBackButton() {
try (ActivityScenario<StartActivity> scenario = createScenario()) {
scenario.onActivity(activity ->
assertNotNull("Activity should be created successfully", activity));
scenario.onActivity(activity -> assertTrue(
"Back button should be disabled",
activity.getOnBackPressedDispatcher().hasEnabledCallbacks()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,24 +506,39 @@ public void testBackspaceRemovesLastCharacterAcrossCapsAndSymbolInput() {
}

@Test
public void testAuthenticationModeDisablesBackNavigation() {
public void testAuthenticationModeBackNavigationFinishesAffinity() {
Intent authIntent = new Intent(context, AuthActivity.class)
.putExtra(AuthActivity.EXTRA_MODE, AuthActivity.Mode.AUTHENTICATION.getModeName());

try (ActivityScenario<AuthActivity> scenario = ActivityScenario.launch(authIntent)) {
scenario.onActivity(activity ->
assertTrue("Authentication mode should install a back callback",
activity.getOnBackPressedDispatcher().hasEnabledCallbacks()));
scenario.onActivity(activity -> {
assertTrue("Authentication mode should install a back callback",
activity.getOnBackPressedDispatcher().hasEnabledCallbacks());

activity.getOnBackPressedDispatcher().onBackPressed();

assertTrue("Authentication mode should finish the activity"
+ " when back is pressed",
activity.isFinishing());
});
}
}

Intent createIntent = new Intent(context, AuthActivity.class)
.putExtra(AuthActivity.EXTRA_MODE, AuthActivity.Mode.CREATE_PASSWORD.getModeName());
@Test
public void testNonAuthenticationModesAllowStandardBackNavigation() {
for (AuthActivity.Mode mode : new AuthActivity.Mode[] {
AuthActivity.Mode.CREATE_PASSWORD,
AuthActivity.Mode.CHANGE_PASSWORD,
AuthActivity.Mode.KEY_RECOVERY}) {
Intent intent = new Intent(context, AuthActivity.class)
.putExtra(AuthActivity.EXTRA_MODE, mode.getModeName());

try (ActivityScenario<AuthActivity> createScenario =
ActivityScenario.launch(createIntent)) {
createScenario.onActivity(activity ->
assertFalse("Create-password mode should not disable back navigation",
activity.getOnBackPressedDispatcher().hasEnabledCallbacks()));
try (ActivityScenario<AuthActivity> scenario = ActivityScenario.launch(intent)) {
scenario.onActivity(activity ->
assertFalse("Non-authentication modes should keep"
+ " the default back behavior",
activity.getOnBackPressedDispatcher().hasEnabledCallbacks()));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import android.app.Activity;
import android.app.Instrumentation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.test.core.app.ActivityScenario;
Expand All @@ -40,6 +42,7 @@
import app.notesr.core.util.KeyUtils;
import app.notesr.service.security.AppSecurityException;
import app.notesr.service.security.AppSecurityService;
import app.notesr.util.ActivityUtils;
import io.bloco.faker.Faker;

@RunWith(AndroidJUnit4.class)
Expand Down Expand Up @@ -180,6 +183,9 @@ public void testWrongRecoveryKeyDoesNotFinishActivity() {
doReturn(false).when(spyAppSecurityService)
.isKeyMatchingWithStored(any(byte[].class));

ActivityUtils activityUtils = spy(activity.getActivityUtils());
activity.setActivityUtils(activityUtils);

EditText recoveryKeyField = activity.findViewById(R.id.importRecoveryKeyField);
Button applyButton = activity.findViewById(R.id.applyRecoveryKeyButton);

Expand All @@ -197,6 +203,10 @@ public void testWrongRecoveryKeyDoesNotFinishActivity() {
recoveryKeyField.getText().toString());
assertFalse("Wrong recovery keys should not finish the current activity",
activity.isFinishing());

verify(activityUtils).showToastMessage(
"Wrong key!",
Toast.LENGTH_SHORT);
});
}
}
Expand All @@ -210,6 +220,9 @@ public void testInvalidRecoveryKeyDoesNotFinishActivity() {
activity.setAppSecurityService(spyAppSecurityService);
spyAppSecurityService.setSecrets(getTestCryptoSecrets());

ActivityUtils activityUtils = spy(activity.getActivityUtils());
activity.setActivityUtils(activityUtils);

EditText recoveryKeyField = activity.findViewById(R.id.importRecoveryKeyField);
Button applyButton = activity.findViewById(R.id.applyRecoveryKeyButton);
String invalidKey = "inv4lidkey";
Expand All @@ -226,6 +239,10 @@ public void testInvalidRecoveryKeyDoesNotFinishActivity() {
recoveryKeyField.getText().toString());
assertFalse("Invalid recovery key input should not finish the activity",
activity.isFinishing());

verify(activityUtils).showToastMessage(
"Invalid key!",
Toast.LENGTH_SHORT);
});
}
}
Expand Down
28 changes: 12 additions & 16 deletions app/src/main/java/app/notesr/activity/security/AuthHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public void authenticate() {
var password = passwordBuilder.toCharArray();

if (password.length == 0) {
showToastMessage(R.string.enter_the_code);
activityUtils.showToastMessage(activity.getString(R.string.enter_the_code),
Toast.LENGTH_SHORT);
return;
}

Expand Down Expand Up @@ -137,7 +138,8 @@ public void changePassword() {
try {
secretsRotationService.updatePassword(password);

showToastMessage(R.string.updated);
activityUtils.showToastMessage(activity.getString(R.string.updated),
Toast.LENGTH_SHORT);
activity.startActivity(getNewIntent(NotesListActivity.class));
activity.finish();
} catch (Exception e) {
Expand All @@ -154,16 +156,17 @@ protected char[] proceedPasswordSetting() {
createdPassword = password;
topLabel.setText(activity.getString(R.string.repeat_access_code));
} else {
showToastMessage(String.format(
activityUtils.showToastMessage(String.format(
activity.getString(R.string.minimum_password_length_is_n),
CryptoSecrets.PASSWORD_MIN_LENGTH));
CryptoSecrets.PASSWORD_MIN_LENGTH), Toast.LENGTH_SHORT);
}
} else {
if (Arrays.equals(password, createdPassword)) {
resetPassword();
return password;
} else {
showToastMessage(R.string.code_not_match);
activityUtils.showToastMessage(activity.getString(R.string.code_not_match),
Toast.LENGTH_SHORT);
}
}

Expand Down Expand Up @@ -196,7 +199,8 @@ protected void onAuthenticationFailed() {
throw new RuntimeException(e);
}

showToastMessage(R.string.blocked);
activityUtils.showToastMessage(activity.getString(R.string.blocked),
Toast.LENGTH_SHORT);
activity.startActivity(getNewIntent(KeyRecoveryActivity.class));
activity.finish();
} else {
Expand All @@ -206,9 +210,9 @@ protected void onAuthenticationFailed() {
throw new RuntimeException(e);
}

showToastMessage(String.format(
activityUtils.showToastMessage(String.format(
activity.getString(R.string.wrong_code_you_have_n_attempts),
authAttempts));
authAttempts), Toast.LENGTH_SHORT);
}

resetPassword();
Expand Down Expand Up @@ -243,12 +247,4 @@ protected void resetPassword() {
protected void sleepBeforeRetry() throws InterruptedException {
Thread.sleep(DELAY_AFTER_AUTH_FAILED);
}

protected void showToastMessage(int stringResId) {
showToastMessage(activity.getString(stringResId));
}

protected void showToastMessage(String text) {
activityUtils.showToastMessage(text, Toast.LENGTH_SHORT);
}
}
Loading