Mholloway has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/332782 )

Change subject: Add CreateAccountInfoClientTest
......................................................................

Add CreateAccountInfoClientTest

Bug: T152396
Change-Id: Ic7305e3ef9ee9db745d120f595e63fa9961690d9
---
A app/src/test/java/org/wikipedia/createaccount/CreateAccountInfoClientTest.java
A app/src/test/res/raw/api_error.json
A app/src/test/res/raw/create_account_info.json
3 files changed, 206 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/82/332782/1

diff --git 
a/app/src/test/java/org/wikipedia/createaccount/CreateAccountInfoClientTest.java
 
b/app/src/test/java/org/wikipedia/createaccount/CreateAccountInfoClientTest.java
new file mode 100644
index 0000000..2fa77c5
--- /dev/null
+++ 
b/app/src/test/java/org/wikipedia/createaccount/CreateAccountInfoClientTest.java
@@ -0,0 +1,83 @@
+package org.wikipedia.createaccount;
+
+import android.support.annotation.NonNull;
+
+import com.google.gson.stream.MalformedJsonException;
+
+import org.junit.Test;
+import org.wikipedia.dataclient.mwapi.MwApiErrorException;
+import org.wikipedia.dataclient.mwapi.MwQueryResponse;
+import org.wikipedia.dataclient.retrofit.RetrofitException;
+import org.wikipedia.test.MockWebServerTest;
+
+import retrofit2.Call;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.isA;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+public class CreateAccountInfoClientTest extends MockWebServerTest {
+    @NonNull private CreateAccountInfoClient subject = new 
CreateAccountInfoClient();
+
+    @Test public void testRequestSuccess() throws Throwable {
+        enqueueFromFile("create_account_info.json");
+
+        CreateAccountInfoClient.Callback cb = 
mock(CreateAccountInfoClient.Callback.class);
+        Call<MwQueryResponse<CreateAccountInfo>> call = request(cb);
+
+        server().takeRequest();
+        assertCallbackSuccess(call, cb);
+    }
+
+    @Test public void testRequestResponseApiError() throws Throwable {
+        enqueueFromFile("api_error.json");
+
+        CreateAccountInfoClient.Callback cb = 
mock(CreateAccountInfoClient.Callback.class);
+        Call<MwQueryResponse<CreateAccountInfo>> call = request(cb);
+
+        server().takeRequest();
+        assertCallbackFailure(call, cb, MwApiErrorException.class);
+    }
+
+    @Test public void testRequestResponse404() throws Throwable {
+        enqueue404();
+
+        CreateAccountInfoClient.Callback cb = 
mock(CreateAccountInfoClient.Callback.class);
+        Call<MwQueryResponse<CreateAccountInfo>> call = request(cb);
+
+        server().takeRequest();
+        assertCallbackFailure(call, cb, RetrofitException.class);
+    }
+
+    @Test public void testRequestResponseMalformed() throws Throwable {
+        server().enqueue("┏━┓ ︵  /(^.^/)");
+
+        CreateAccountInfoClient.Callback cb = 
mock(CreateAccountInfoClient.Callback.class);
+        Call<MwQueryResponse<CreateAccountInfo>> call = request(cb);
+
+        server().takeRequest();
+        assertCallbackFailure(call, cb, MalformedJsonException.class);
+    }
+
+    private void assertCallbackSuccess(@NonNull 
Call<MwQueryResponse<CreateAccountInfo>> call,
+                                       @NonNull 
CreateAccountInfoClient.Callback cb) {
+        verify(cb).success(eq(call), any(CreateAccountInfoResult.class));
+        //noinspection unchecked
+        verify(cb, never()).failure(any(Call.class), any(Throwable.class));
+    }
+
+    private void assertCallbackFailure(@NonNull 
Call<MwQueryResponse<CreateAccountInfo>> call,
+                                       @NonNull 
CreateAccountInfoClient.Callback cb,
+                                       @NonNull Class<? extends Throwable> 
throwable) {
+        //noinspection unchecked
+        verify(cb, never()).success(any(Call.class), 
any(CreateAccountInfoResult.class));
+        verify(cb).failure(eq(call), isA(throwable));
+    }
+
+    private Call<MwQueryResponse<CreateAccountInfo>> request(@NonNull 
CreateAccountInfoClient.Callback cb) {
+        return subject.request(service(CreateAccountInfoClient.Service.class), 
cb);
+    }
+}
diff --git a/app/src/test/res/raw/api_error.json 
b/app/src/test/res/raw/api_error.json
new file mode 100644
index 0000000..c83abb1
--- /dev/null
+++ b/app/src/test/res/raw/api_error.json
@@ -0,0 +1,8 @@
+{
+  "error": {
+    "code": "unknown_action",
+    "info": "Unrecognized value for parameter \"action\": oscillate.",
+    "*": "See https://en.wikipedia.org/w/api.php for API usage."
+  },
+  "servedby": "mw1286"
+}
\ No newline at end of file
diff --git a/app/src/test/res/raw/create_account_info.json 
b/app/src/test/res/raw/create_account_info.json
new file mode 100644
index 0000000..e59b380
--- /dev/null
+++ b/app/src/test/res/raw/create_account_info.json
@@ -0,0 +1,115 @@
+{
+  "batchcomplete": "",
+  "query": {
+    "authmanagerinfo": {
+      "canauthenticatenow": "",
+      "cancreateaccounts": "",
+      "preservedusername": "",
+      "requests": [
+        {
+          "id": "CaptchaAuthenticationRequest",
+          "metadata": {
+            "type": "image",
+            "mime": "image/png"
+          },
+          "required": "required",
+          "provider": "CaptchaAuthenticationRequest",
+          "account": "CaptchaAuthenticationRequest",
+          "fields": {
+            "captchaId": {
+              "type": "hidden",
+              "value": "389780711",
+              "label": "CAPTCHA ID",
+              "help": "This value should be sent back unchanged."
+            },
+            "captchaInfo": {
+              "type": "null",
+              "value": 
"/w/index.php?title=Special:Captcha/image&wpCaptchaId=389780711",
+              "label": "To help protect against automated account creation, 
please enter the words that appear below in the box 
([[Special:Captcha/help|more info]]):",
+              "help": "Description of the CAPTCHA."
+            },
+            "captchaWord": {
+              "type": "string",
+              "label": "CAPTCHA",
+              "help": "Solution of the CAPTCHA."
+            }
+          }
+        },
+        {
+          "id": "MediaWiki\\Auth\\PasswordAuthenticationRequest",
+          "metadata": {},
+          "required": "primary-required",
+          "provider": "Password-based authentication",
+          "account": "",
+          "fields": {
+            "username": {
+              "type": "string",
+              "label": "Username",
+              "help": "Username for authentication."
+            },
+            "password": {
+              "type": "password",
+              "label": "Password",
+              "help": "Password for authentication.",
+              "sensitive": ""
+            },
+            "retype": {
+              "type": "password",
+              "label": "Retype password:",
+              "help": "Password again to confirm.",
+              "sensitive": ""
+            }
+          }
+        },
+        {
+          "id": "CampaignsAuthenticationRequest",
+          "metadata": {},
+          "required": "optional",
+          "provider": "CampaignsAuthenticationRequest",
+          "account": "CampaignsAuthenticationRequest",
+          "fields": {
+            "campaign": {
+              "type": "hidden",
+              "value": "",
+              "label": "Campaign",
+              "help": "Identifies the campaign leading to an account 
creation.",
+              "optional": ""
+            }
+          }
+        },
+        {
+          "id": "MediaWiki\\Auth\\UsernameAuthenticationRequest",
+          "metadata": {},
+          "required": "required",
+          "provider": "MediaWiki\\Auth\\UsernameAuthenticationRequest",
+          "account": "MediaWiki\\Auth\\UsernameAuthenticationRequest",
+          "fields": {
+            "username": {
+              "type": "string",
+              "label": "Username",
+              "help": "Username for authentication."
+            }
+          }
+        },
+        {
+          "id": "MediaWiki\\Auth\\UserDataAuthenticationRequest",
+          "metadata": {},
+          "required": "required",
+          "provider": "MediaWiki\\Auth\\UserDataAuthenticationRequest",
+          "account": "MediaWiki\\Auth\\UserDataAuthenticationRequest",
+          "fields": {
+            "email": {
+              "type": "string",
+              "label": "Email",
+              "help": "Email address",
+              "optional": ""
+            }
+          }
+        }
+      ]
+    },
+    "tokens": {
+      "createaccounttoken": "0a696a98f61c08f0c9491cf15453e3a0587fa3b6+\\"
+    }
+  }
+}
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/332782
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic7305e3ef9ee9db745d120f595e63fa9961690d9
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Mholloway <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to