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

OPPIA-1053, OPPIA-1484: Add tests for AsyncTasks #1316

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
Empty file.
3 changes: 3 additions & 0 deletions app/src/androidTest/assets/tests/accounts/oppia_accounts.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
username,password,apikey,email,first_name,last_name,
testuser1,password1,testApiKey1,testuser1@test.com,Test,User1,
testuser2,password2,testApiKey2,testuser2@test.com,Test,User2,
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package androidTestFiles.task;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import android.content.Context;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import org.digitalcampus.oppia.application.SessionManager;
import org.digitalcampus.oppia.database.DbHelper;
import org.digitalcampus.oppia.model.User;
import org.digitalcampus.oppia.task.ChangePasswordTask;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;

import androidTestFiles.database.TestDataManager;
import androidTestFiles.utils.MockApiEndpoint;
import androidTestFiles.utils.parent.MockedApiEndpointTaskTest;

@RunWith(AndroidJUnit4.class)
public class ChangePasswordTaskTest extends MockedApiEndpointTaskTest {
public static final String ERROR_CHANGEPASSWORD_DIFFERENT = "responses/response_400_change_password.json";
public static final String ORIGINAL_USER_PASSWORD = "password";

private Context context;
private DbHelper db;

@Before
public void setUp() throws Exception {
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
db = DbHelper.getInstance(context);
TestDataManager testDataManager = new TestDataManager(db);
testDataManager.addUsers();
SessionManager.loginUser(context, db.getUser("user1"));
}

@After
public void tearDown() throws Exception {
if (mockServer!=null)
mockServer.shutdown();
}

private void changePassword(String newPassword, String repeatPassword) {
final CountDownLatch signal = new CountDownLatch(1);

ChangePasswordTask task = new ChangePasswordTask(context, new MockApiEndpoint(mockServer));
task.setResponseListener(new ChangePasswordTask.ResponseListener() {
@Override
public void onSuccess() {
signal.countDown();
}

@Override
public void onError(String error) {
signal.countDown();
}
});

task.execute(newPassword, repeatPassword);

try {
signal.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private String getPasswordEncrypted(String password) {
User user = new User();
user.setPassword(password);
return user.getPasswordEncrypted();
}

@Test
public void changePasswordIfSameRepeatedPassword() throws Exception {
startServer(201, "");

String newPassword = "newPassword";
String repeatPassword = "newPassword";

changePassword(newPassword, repeatPassword);

User u = db.getUser(SessionManager.getUsername(context));
assertEquals(getPasswordEncrypted(newPassword), u.getPasswordEncrypted());
assertNotEquals(getPasswordEncrypted(ORIGINAL_USER_PASSWORD), u.getPasswordEncrypted());
}

@Test
public void dontChangePasswordIfDifferentRepeatedPassword() throws Exception {
startServer(400, ERROR_CHANGEPASSWORD_DIFFERENT);

String newPassword = "newPassword";
String repeatPassword = "differentPassword";

changePassword(newPassword, repeatPassword);

User u = db.getUser("user1");
assertNotEquals(getPasswordEncrypted(newPassword), u.getPasswordEncrypted());
assertEquals(getPasswordEncrypted(ORIGINAL_USER_PASSWORD), u.getPasswordEncrypted());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package androidTestFiles.task;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;

import android.content.Context;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import org.digitalcampus.oppia.application.SessionManager;
import org.digitalcampus.oppia.database.DbHelper;
import org.digitalcampus.oppia.exception.UserNotFoundException;
import org.digitalcampus.oppia.task.DeleteAccountTask;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;

import androidTestFiles.database.TestDataManager;
import androidTestFiles.utils.MockApiEndpoint;
import androidTestFiles.utils.parent.MockedApiEndpointTaskTest;

@RunWith(AndroidJUnit4.class)
public class DeleteAccountTaskTest extends MockedApiEndpointTaskTest {

private Context context;
private DbHelper db;

@Before
public void setUp() throws Exception {
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
db = DbHelper.getInstance(context);
TestDataManager testDataManager = new TestDataManager(db);
testDataManager.addUsers();
SessionManager.loginUser(context, db.getUser("user1"));
}

@After
public void tearDown() throws Exception {
if (mockServer!=null)
mockServer.shutdown();
}

private void deleteAccount(String userPassword) {
final CountDownLatch signal = new CountDownLatch(1);

DeleteAccountTask task = new DeleteAccountTask(context, new MockApiEndpoint(mockServer));
task.setResponseListener(new DeleteAccountTask.ResponseListener() {
@Override
public void onSuccess() {
signal.countDown();
}

@Override
public void onError(String error) {
signal.countDown();
}
});

task.execute(userPassword);

try {
signal.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Test
public void deleteAccount_correctPassword_accountIsDeleted() throws Exception {
startServer(201, "");

assertNotNull(db.getUser(SessionManager.getUsername(context)));

String correctPassword = "password";
deleteAccount(correctPassword);

assertThrows(UserNotFoundException.class, () -> db.getUser(SessionManager.getUsername(context)));
}

@Test
public void deleteAccount_wrongPassword_accountIsNotDeleted() throws Exception {
startServer(400, "");

assertNotNull(db.getUser(SessionManager.getUsername(context)));

String wrongPassword = "wrongPassword";
deleteAccount(wrongPassword);

assertNotNull(db.getUser(SessionManager.getUsername(context)));
}

@Test
public void deleteAccount_serverConnectionError_accountIsNotDeleted() throws Exception {
startServer(500, "");

assertNotNull(db.getUser(SessionManager.getUsername(context)));

String correctPassword = "password";
deleteAccount(correctPassword);

assertNotNull(db.getUser(SessionManager.getUsername(context)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package androidTestFiles.task;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;

import org.digitalcampus.oppia.database.DbHelper;
import org.digitalcampus.oppia.exception.UserNotFoundException;
import org.digitalcampus.oppia.task.PreloadAccountsTask;
import org.digitalcampus.oppia.utils.storage.Storage;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import androidTestFiles.utils.FileUtils;


public class PreloadUserAccountsTaskTest {

private Context context;
private DbHelper db;

@Before
public void setUp() throws IOException {
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
db = DbHelper.getInstance(context);
File location = new File(Storage.getStorageLocationRoot(context));
String filename = "oppia_accounts.csv";
FileUtils.copyFileFromAssets(context, "tests/accounts", filename, location, filename);
}

private void preloadUserAccounts(boolean expectedResult) {
final CountDownLatch signal = new CountDownLatch(1);

PreloadAccountsTask task = new PreloadAccountsTask(context);
task.setPreloadAccountsListener(result -> {
assertEquals(expectedResult, result.isSuccess());
signal.countDown();
});

task.execute();

try {
signal.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Test
public void preloadUserAccountsTest_success() throws UserNotFoundException {
// Users don't exists
assertThrows(UserNotFoundException.class, () -> db.getUser("testuser1"));
assertThrows(UserNotFoundException.class, () -> db.getUser("testuser2"));

// Preload user accounts
preloadUserAccounts(true);

// Users exist
assertNotNull(db.getUser("testuser1"));
assertNotNull(db.getUser("testuser2"));
}

}
Loading