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 @@ -11,7 +11,7 @@
* Parses a JSON response from the confirm backup code API call
* and populates a PersonalIdSessionData instance.
*/
public class ConfirmBackupCodeResponseParser implements PersonalIdApiResponseParser{
public class ConfirmBackupCodeResponseParser implements PersonalIdApiResponseParser {
/**
* Parses and sets values on the given PersonalIdSessionData instance.
*
Expand All @@ -20,28 +20,30 @@ public class ConfirmBackupCodeResponseParser implements PersonalIdApiResponsePar
*/
@Override
public void parse(JSONObject json, PersonalIdSessionData sessionData) throws JSONException {
String username = JsonExtensions.optStringSafe(json, "username", null);
String dbKey = JsonExtensions.optStringSafe(json, "db_key", null);
String password = JsonExtensions.optStringSafe(json, "password", null);

Objects.requireNonNull(username);
Objects.requireNonNull(dbKey);
Objects.requireNonNull(password);
if (username.isEmpty() || dbKey.isEmpty() || password.isEmpty()) {
throw new IllegalStateException(
"Any of the fields amongst username, db_key or password cannot be empty");
}

sessionData.setPersonalId(username);
sessionData.setDbKey(dbKey);
sessionData.setOauthPassword(password);

if (json.has("attempts_left")) {
if (json.has("attempts_left")) { // whenever wrong code is entered, server responed with attempts_left
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: typo on responed vs responds

sessionData.setAttemptsLeft(json.getInt("attempts_left"));
}
if (json.has("error_code")) {
} else if (json.has("error_code")) { // whenever account is locked, server responed with error_code
sessionData.setSessionFailureCode(json.getString("error_code"));
} else { // whenever backup code is correct
String username = JsonExtensions.optStringSafe(json, "username", null);
String dbKey = JsonExtensions.optStringSafe(json, "db_key", null);
String password = JsonExtensions.optStringSafe(json, "password", null);

Objects.requireNonNull(username);
Objects.requireNonNull(dbKey);
Objects.requireNonNull(password);
if (username.isEmpty() || dbKey.isEmpty() || password.isEmpty()) {
throw new IllegalStateException(
"Any of the fields amongst username, db_key or password cannot be empty");
}

sessionData.setPersonalId(username);
sessionData.setDbKey(dbKey);
sessionData.setOauthPassword(password);

sessionData.setInvitedUser(json.optBoolean("invited_user", false));
}
sessionData.setInvitedUser(json.optBoolean("invited_user", false));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.commcare.android.database.connect.models.PersonalIdSessionData
import org.json.JSONException
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
Expand All @@ -31,8 +32,6 @@ class ConfirmBackupCodeResponseParserTest {
JSONObject().apply {
put("username", "test-user-123")
put("db_key", "db-key-456")
put("attempts_left", 3)
put("error_code", "invalid_code")
put("password", "test-password")
put("invited_user", true)
}
Expand All @@ -43,12 +42,49 @@ class ConfirmBackupCodeResponseParserTest {
// Assert
assertEquals("test-user-123", sessionData.personalId)
assertEquals("db-key-456", sessionData.dbKey)
assertEquals(3, sessionData.attemptsLeft)
assertEquals("invalid_code", sessionData.sessionFailureCode)
assertEquals("test-password", sessionData.oauthPassword)
assertTrue(sessionData.invitedUser)
}

@Test
fun testParseForWrongBackupCodeResponse() {
// Arrange
val json =
JSONObject().apply {
put("attempts_left", 3)
}

// Act
parser.parse(json, sessionData)

// Assert
assertEquals(null, sessionData.personalId)
assertEquals(null, sessionData.dbKey)
assertEquals(null, sessionData.oauthPassword)
assertEquals(3, sessionData.attemptsLeft)
assertEquals(null, sessionData.sessionFailureCode)
assertFalse(sessionData.invitedUser)
}

@Test
fun testParseForLockedAccountResponse() {
// Arrange
val json =
JSONObject().apply {
put("error_code", "LOCKED_ACCOUNT")
}

// Act
parser.parse(json, sessionData)

// Assert
assertEquals(null, sessionData.personalId)
assertEquals(null, sessionData.dbKey)
assertEquals(null, sessionData.oauthPassword)
assertEquals("LOCKED_ACCOUNT", sessionData.sessionFailureCode)
assertFalse(sessionData.invitedUser)
}

@Test(expected = NullPointerException::class)
fun testParseWithMissingUsername() {
// Arrange - Missing required username field
Expand Down