Brion VIBBER has submitted this change and it was merged. Change subject: Add a CaptchaEditResult with data about the Captcha ......................................................................
Add a CaptchaEditResult with data about the Captcha Also adds a test that triggers a Captcha. Change-Id: Ic5331e2356ceff2b40ef95833305fb407c3392a8 --- A wikipedia-it/src/main/java/org/wikipedia/test/TriggerEditCaptchaTest.java M wikipedia/src/main/java/org/wikipedia/Site.java A wikipedia/src/main/java/org/wikipedia/editing/CaptchaEditResult.java M wikipedia/src/main/java/org/wikipedia/editing/DoEditTask.java 4 files changed, 84 insertions(+), 1 deletion(-) Approvals: Brion VIBBER: Verified; Looks good to me, approved diff --git a/wikipedia-it/src/main/java/org/wikipedia/test/TriggerEditCaptchaTest.java b/wikipedia-it/src/main/java/org/wikipedia/test/TriggerEditCaptchaTest.java new file mode 100644 index 0000000..00eacb7 --- /dev/null +++ b/wikipedia-it/src/main/java/org/wikipedia/test/TriggerEditCaptchaTest.java @@ -0,0 +1,48 @@ + +package org.wikipedia.test; + +import android.content.Intent; +import android.test.ActivityUnitTestCase; +import org.wikipedia.PageTitle; +import org.wikipedia.Site; +import org.wikipedia.WikipediaApp; +import org.wikipedia.editing.CaptchaEditResult; +import org.wikipedia.editing.DoEditTask; +import org.wikipedia.editing.EditingResult; +import org.wikipedia.editing.FetchSectionWikitextTask; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +public class TriggerEditCaptchaTest extends ActivityUnitTestCase<TestDummyActivity> { + private static final int TASK_COMPLETION_TIMEOUT = 20000; + + public TriggerEditCaptchaTest() { + super(TestDummyActivity.class); + } + + public void testCaptchaTrigger() throws Throwable { + startActivity(new Intent(), null, null); + final PageTitle title = new PageTitle(null, "Test_page_for_app_testing/Captcha", new Site("test.wikipedia.org")); + final String wikitext = "== Section 2 ==\n\nEditing by inserting an external link https://" + System.currentTimeMillis(); + final CountDownLatch completionLatch = new CountDownLatch(1); + runTestOnUiThread(new Runnable() { + @Override + public void run() { + new DoEditTask(getInstrumentation().getTargetContext(), title, wikitext, 0) { + @Override + public void onFinish(EditingResult result) { + assertNotNull(result); + assertTrue(result instanceof CaptchaEditResult); + CaptchaEditResult captchaResult = (CaptchaEditResult) result; + String captchaUrl = captchaResult.getCaptchaUrl(new Site("test.wikipedia.org")); + assertTrue(captchaUrl.startsWith(WikipediaApp.PROTOCOL + "://test.wikipedia.org/w/index.php?title=Special:Captcha/image")); + completionLatch.countDown(); + } + }.execute(); + } + }); + assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS)); + } +} + diff --git a/wikipedia/src/main/java/org/wikipedia/Site.java b/wikipedia/src/main/java/org/wikipedia/Site.java index dbe8a14..bc072c3 100644 --- a/wikipedia/src/main/java/org/wikipedia/Site.java +++ b/wikipedia/src/main/java/org/wikipedia/Site.java @@ -59,6 +59,10 @@ return ((Site)o).getDomain().equals(domain); } + public String getFullUrl(String path) { + return WikipediaApp.PROTOCOL + "://" + getDomain() + path; + } + /** * Regex to match internal relative wikilinks. * diff --git a/wikipedia/src/main/java/org/wikipedia/editing/CaptchaEditResult.java b/wikipedia/src/main/java/org/wikipedia/editing/CaptchaEditResult.java new file mode 100644 index 0000000..f4bdeb4 --- /dev/null +++ b/wikipedia/src/main/java/org/wikipedia/editing/CaptchaEditResult.java @@ -0,0 +1,23 @@ +package org.wikipedia.editing; + +import org.wikipedia.Site; + +// Handles only Image Captchas +public class CaptchaEditResult extends EditingResult { + private final String captchaId; + private final String captchaPath; + + public CaptchaEditResult(String captchaId, String captchaPath) { + super("Failure"); + this.captchaId = captchaId; + this.captchaPath = captchaPath; + } + + public String getCaptchaId() { + return captchaId; + } + + public String getCaptchaUrl(Site site) { + return site.getFullUrl(captchaPath); + } +} diff --git a/wikipedia/src/main/java/org/wikipedia/editing/DoEditTask.java b/wikipedia/src/main/java/org/wikipedia/editing/DoEditTask.java index b9a9c6a..dde50de 100644 --- a/wikipedia/src/main/java/org/wikipedia/editing/DoEditTask.java +++ b/wikipedia/src/main/java/org/wikipedia/editing/DoEditTask.java @@ -51,9 +51,17 @@ JSONObject errorJSON = resultJSON.optJSONObject("error"); throw new EditingException(errorJSON.optString("code"), errorJSON.optString("info")); } - String status = resultJSON.optJSONObject("edit").optString("result"); + JSONObject edit = resultJSON.optJSONObject("edit"); + String status = edit.optString("result"); if (status.equals("Success")) { return new SuccessEditResult(); + } else if (status.equals("Failure")) { + if (edit.has("captcha")) { + return new CaptchaEditResult( + edit.optJSONObject("captcha").optString("id"), + edit.optJSONObject("captcha").optString("url") + ); + } } // Handle other type of return codes here throw new RuntimeException("Failure happens"); -- To view, visit https://gerrit.wikimedia.org/r/109293 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ic5331e2356ceff2b40ef95833305fb407c3392a8 Gerrit-PatchSet: 1 Gerrit-Project: apps/android/wikipedia Gerrit-Branch: master Gerrit-Owner: Yuvipanda <yuvipa...@gmail.com> Gerrit-Reviewer: Brion VIBBER <br...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits