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 @@ -20,9 +20,21 @@ public class CompleteProfileResponseParser implements PersonalIdApiResponseParse
*/
@Override
public void parse(JSONObject json, PersonalIdSessionData sessionData) throws JSONException {
sessionData.setPersonalId(Objects.requireNonNull(JsonExtensions.optStringSafe(json, "username", null)));
sessionData.setDbKey(Objects.requireNonNull(JsonExtensions.optStringSafe(json, "db_key", null)));
sessionData.setOauthPassword(Objects.requireNonNull(JsonExtensions.optStringSafe(json, "password", null)));
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);
Comment on lines +27 to +29
Copy link
Contributor

Choose a reason for hiding this comment

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

if (username.isEmpty() || dbKey.isEmpty() || password.isEmpty()) {
throw new IllegalStateException(
Copy link
Contributor

Choose a reason for hiding this comment

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

"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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Objects;

/**
* Parses a JSON response from the confirm backup code API call
* and populates a PersonalIdSessionData instance.
Expand All @@ -18,15 +20,28 @@ public class ConfirmBackupCodeResponseParser implements PersonalIdApiResponsePar
*/
@Override
public void parse(JSONObject json, PersonalIdSessionData sessionData) throws JSONException {
sessionData.setPersonalId(JsonExtensions.optStringSafe(json, "username", null));
sessionData.setDbKey(JsonExtensions.optStringSafe(json, "db_key", null));
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);
Comment on lines +27 to +29
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to add the text here as reason i.e. Objects.requireNonNull(username,"username cannot be null");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

think that will be redundant in simple cases like this where it's quite evident that the username is null which is why there is an NPE here.

if (username.isEmpty() || dbKey.isEmpty() || password.isEmpty()) {
throw new IllegalStateException(
Copy link
Contributor

Choose a reason for hiding this comment

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

PersonalIdApiHandler doesn't handle IllegalStateException so application might crash here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we do want to crash in these scenarios as it's critical info for app to process a successful sign-in

"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")) {
sessionData.setAttemptsLeft(json.getInt("attempts_left"));
}
if (json.has("error_code")) {
sessionData.setSessionFailureCode(json.getString("error_code"));
}
sessionData.setOauthPassword(JsonExtensions.optStringSafe(json, "password", null));
sessionData.setInvitedUser(json.optBoolean("invited_user", false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.commcare.connect.network.base.BaseApiHandler;
import org.commcare.connect.network.base.BaseApiResponseParser;
import org.commcare.google.services.analytics.FirebaseAnalyticsUtil;
import org.commcare.utils.JsonExtensions;
import org.javarosa.core.io.StreamsUtil;
import org.javarosa.core.services.Logger;
import org.json.JSONException;
Expand All @@ -20,8 +21,8 @@ public class ReportIntegrityResponseParser<T> implements BaseApiResponseParser<T
@Override
public T parse(int responseCode, @NonNull InputStream responseData, @Nullable Object anyInputObject) throws IOException,JSONException {
JSONObject json = new JSONObject(new String(StreamsUtil.inputStreamToByteArray(responseData)));
FirebaseAnalyticsUtil.reportPersonalIdHeartbeatIntegritySubmission(((String)anyInputObject),
json.optString("result_code", "NoCodeFromServer"));
String resultCode = JsonExtensions.optStringSafe(json, "result_code", "NoCodeFromServer");
FirebaseAnalyticsUtil.reportPersonalIdHeartbeatIntegritySubmission(((String)anyInputObject), resultCode);
return (T)Boolean.TRUE;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package org.commcare.connect.network.connectId.parser

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.commcare.CommCareTestApplication
import org.commcare.android.database.connect.models.PersonalIdSessionData
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config

@Config(application = CommCareTestApplication::class)
@RunWith(AndroidJUnit4::class)
class AddOrVerifyNameParserTest {
private lateinit var parser: AddOrVerifyNameParser
private lateinit var sessionData: PersonalIdSessionData

@Before
fun setUp() {
parser = AddOrVerifyNameParser()
sessionData = PersonalIdSessionData()
}

@Test
fun testParseCompleteValidResponse() {
// Arrange
val photoBase64 =
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
val json =
JSONObject().apply {
put("account_exists", true)
put(
"photo",
photoBase64,
)
}

// Act
parser.parse(json, sessionData)

// Assert
assertEquals(true, sessionData.accountExists)
assertEquals(
photoBase64,
sessionData.photoBase64,
)
}

@Test
fun testParseWithDefaultValues() {
// Arrange
val json = JSONObject()
// Not setting any fields to test default values

// Act
parser.parse(json, sessionData)

// Assert
assertEquals(false, sessionData.accountExists)
assertNull(sessionData.photoBase64)
}

@Test
fun testParseWithPartialData() {
// Arrange
val json =
JSONObject().apply {
put("account_exists", true)
// Missing photo field
}

// Act
parser.parse(json, sessionData)

// Assert
assertEquals(true, sessionData.accountExists)
assertNull(sessionData.photoBase64)
}

@Test
fun testParseWithEmptyStrings() {
// Arrange
val json =
JSONObject().apply {
put("photo", "")
}

// Act
parser.parse(json, sessionData)

// Assert
assertEquals("", sessionData.photoBase64)
assertEquals(false, sessionData.accountExists)
}

@Test
fun testParseWithNullValues() {
// Arrange
val json =
JSONObject().apply {
put("account_exists", JSONObject.NULL)
put("photo", JSONObject.NULL)
}

// Act
parser.parse(json, sessionData)

// Assert
assertEquals(false, sessionData.accountExists) // optBoolean returns false for null
assertNull(sessionData.photoBase64)
}

@Test
fun testParseWithLargePhotoData() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if this test is different from other test except the string length

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that is right, the purpose of this test is to do a explicit check on very large photo data which is a real possibility with base 64 photo data.

// Arrange - Test with a larger photo string
val largePhotoData = StringBuilder()
repeat(100000) {
largePhotoData.append("a")
}

val json =
JSONObject().apply {
put("account_exists", true)
put("photo", largePhotoData.toString())
}

// Act
parser.parse(json, sessionData)

// Assert
assertEquals(true, sessionData.accountExists)
assertEquals(largePhotoData.toString(), sessionData.photoBase64)
assertEquals(100000, sessionData.photoBase64?.length)
}

@Test(expected = NullPointerException::class)
fun testParseWithNullJSON() {
// Act - Should throw NullPointerException when trying to parse null JSON
parser.parse(null, sessionData)
}

@Test(expected = NullPointerException::class)
fun testParseWithNullSessionData() {
// Arrange
val json =
JSONObject().apply {
put("account_exists", true)
}

// Act - Should throw NullPointerException when sessionData is null
parser.parse(json, null)
}
}
Loading