-
-
Notifications
You must be signed in to change notification settings - Fork 45
Tests for Personal ID Signup Parsers + More Validations on mandatory fields #3402
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
Changes from all commits
1f51135
58b10de
113c20c
06ca6a4
bc75a51
1927fea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| if (username.isEmpty() || dbKey.isEmpty() || password.isEmpty()) { | ||
| throw new IllegalStateException( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
| } | ||
conroy-ricketts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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");
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PersonalIdApiHandler doesn't handle
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
| } | ||
conroy-ricketts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
conroy-ricketts marked this conversation as resolved.
Show resolved
Hide resolved
|
| 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() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as https://github.com/dimagi/commcare-android/pull/3402/files#r2498579701