-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OPPIA-1053, OPPIA-1484: Add tests for AsyncTasks
- ChangePasswordTask - DeleteAccountTask - PreloadUserAccountsTask - SubmitQuizAttemptsTask
- Loading branch information
1 parent
aeea234
commit e81858e
Showing
9 changed files
with
479 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
108 changes: 108 additions & 0 deletions
108
app/src/androidTest/java/androidTestFiles/task/ChangePasswordTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
app/src/androidTest/java/androidTestFiles/task/DeleteAccountTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
app/src/androidTest/java/androidTestFiles/task/PreloadUserAccountsTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
|
||
} |
Oops, something went wrong.