package com.authy.api;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

public class TestPhoneVerification extends TestApiBase {

    private PhoneVerification client;

    private String getStartSuccessResponse(final String message){
        return "{\n" +
                "    \"carrier\": \"Pinger - Bandwidth.com - Sybase365\",\n" +
                "    \"is_cellphone\": false,\n" +
                "    \"message\": \""+ message + "\",\n" +
                "    \"seconds_to_expire\": 599,\n" +
                "    \"uuid\": \"bec828c0-b535-0135-8e26-1226b57fac04\",\n" +
                "    \"success\": true\n" +
                "}";
    }

    final private String startInvalidNumberResponse = "{\n" +
            "    \"error_code\": \"60033\",\n" +
            "    \"message\": \"Phone number is invalid\",\n" +
            "    \"errors\": {\n" +
            "        \"message\": \"Phone number is invalid\"\n" +
            "    },\n" +
            "    \"success\": false\n" +
            "}";

    final private String checkIncorrectVerificationResponse = "{\n" +
            "    \"error_code\": \"60022\",\n" +
            "    \"message\": \"Verification code is incorrect\",\n" +
            "    \"errors\": {\n" +
            "        \"message\": \"Verification code is incorrect\"\n" +
            "    },\n" +
            "    \"success\": false\n" +
            "}";

    @Before
    public void setUp() {
        client = new PhoneVerification(testHost, testApiKey, true);
    }

    @Test
    public void testContentTypeToBeJson() {
        Assert.assertEquals("application/json", client.getContentType());
    }

    @Test
    public void testVerificationStartEs() {
        stubFor(post(urlPathEqualTo("/protected/json/phones/verification/start"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withHeader("Content-Type", "application/json;charset=utf-8")
                        .withBody(getStartSuccessResponse("Llamada a +1 775-461-5609 fue iniciada."))));

        Params params = new Params();
        params.setAttribute("locale", "es");
        Verification result = client.start("775-461-5609", "1", "call", params);

        verify(postRequestedFor(urlPathEqualTo("/protected/json/phones/verification/start"))
                .withHeader("X-Authy-API-Key", equalTo(testApiKey))
                .withRequestBody(equalToJson("{\"country_code\": \"1\", \"phone_number\": \"775-461-5609\", \"locale\": \"es\", \"via\": \"call\"}", true, true)));
        Assert.assertEquals("Llamada a +1 775-461-5609 fue iniciada.", result.getMessage());
        Assert.assertEquals("true", result.getSuccess());
    }

    @Test
    public void testVerificationStartEn() {
        stubFor(post(urlPathEqualTo("/protected/json/phones/verification/start"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withHeader("Content-Type", "application/json;charset=utf-8")
                        .withBody(getStartSuccessResponse("Text message sent to +1 775-461-5609."))));

        Params params = new Params();
        params.setAttribute("locale", "en");
        Verification result = client.start("775-461-5609", "1", "sms", params);

        verify(postRequestedFor(urlPathEqualTo("/protected/json/phones/verification/start"))
                .withHeader("X-Authy-API-Key", equalTo(testApiKey))
                .withRequestBody(equalToJson("{\"country_code\": \"1\", \"phone_number\": \"775-461-5609\", \"locale\": \"en\", \"via\": \"sms\"}", true, true)));
        String msg = "Text message sent to +1 775-461-5609.";
        Assert.assertEquals(msg, result.getMessage());
        Assert.assertEquals("true", result.getSuccess());
    }

    @Test
    public void testVerificationStartEnInvalid() {
        stubFor(post(urlPathEqualTo("/protected/json/phones/verification/start"))
                .willReturn(aResponse()
                        .withStatus(400)
                        .withHeader("Content-Type", "application/json")
                        .withBody(startInvalidNumberResponse)));

        Params params = new Params();
        params.setAttribute("locale", "en");

        Verification result = client.start("282-23", "1", "sms", params);
        Assert.assertEquals("Phone number is invalid", result.getMessage());
        Assert.assertEquals("false", result.getSuccess());
    }

    @Test
    public void testVerificationCheckIncorrectCode() {
        stubFor(get(urlPathEqualTo("/protected/json/phones/verification/check"))
                .willReturn(aResponse()
                        .withStatus(401)
                        .withHeader("Content-Type", "application/json")
                        .withBody(checkIncorrectVerificationResponse)));

        Verification result = client.check("775-461-5609", "1", "2061");
        Assert.assertEquals("Verification code is incorrect", result.getMessage());
        Assert.assertEquals("false", result.getSuccess());
    }
}

@@ -1478,24 +1478,220 @@
       %22%7D%22;%0A%0A
+    final private String checkCorrectVerificationResponse = %22%7B%5Cn%22 +%0A            %22    %5C%22message%5C%22: %5C%22Verification code is correct.%5C%22,%5Cn%22 +%0A            %22    %5C%22success%5C%22: true%5Cn%22 +%0A            %22%7D%22;%0A%0A
     @Before%0A
@@ -4715,32 +4715,618 @@
 cess());%0A    %7D%0A%0A
+    @Test%0A    public void testVerificationCheckSuccess()%7B%0A        stubFor(get(urlPathEqualTo(%22/protected/json/phones/verification/check%22))%0A                .willReturn(aResponse()%0A                        .withStatus(200)%0A                        .withHeader(%22Content-Type%22, %22application/json%22)%0A                        .withBody(checkCorrectVerificationResponse)));%0A%0A        Verification result = client.check(%22775-461-5609%22, %221%22, %222061%22);%0A%0A        Assert.assertEquals(%22Verification code is correct.%22, result.getMessage());%0A        Assert.assertEquals(%22true%22, result.getSuccess());%0A    %7D%0A%0A
     @Test%0A    pu
@@ -5751,24 +5751,25 @@
 %22, %222061%22);%0A
+%0A
         Asse

Add test for phone verification check success