tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_basiccard.html (14792B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=1375345
      5 -->
      6 <head>
      7  <meta charset="utf-8">
      8  <title>Test for Bug 1375345</title>
      9  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     10  <script src="/tests/SimpleTest/SimpleTest.js"></script>
     11  <script type="application/javascript">
     12 
     13  "use strict";
     14  SimpleTest.waitForExplicitFinish();
     15 
     16  var gUrl = SimpleTest.getTestFileURL('BasiccardChromeScript.js');
     17  var gScript = SpecialPowers.loadChromeScript(gUrl);
     18 
     19  function testFailHandler(message) {
     20    ok(false, message);
     21  }
     22  function testPassHandler(message) {
     23    ok(true, message);
     24  }
     25  gScript.addMessageListener("test-fail", testFailHandler);
     26  gScript.addMessageListener("test-pass", testPassHandler);
     27 
     28  async function requestChromeAction(action, params) {
     29    await new Promise(resolve => {
     30      gScript.addMessageListener(`${action}-complete`, function completeListener() {
     31        gScript.removeMessageListener(`${action}-complete`, completeListener);
     32        resolve();
     33      });
     34      gScript.sendAsyncMessage(action, params);
     35    });
     36  }
     37 
     38  const errorNetworksMethods = [{
     39    supportedMethods: "basic-card",
     40    data: {
     41      supportedNetworks: ["myNetwork"],
     42    },
     43  }];
     44 
     45  const nullDataMethods = [{
     46    supportedMethods: "basic-card",
     47  }];
     48 
     49  const emptyDataMethods = [{
     50    supportedMethods: "basic-card",
     51    data: {},
     52  }];
     53 
     54  const unconvertableDataMethods = [{
     55    supportedMethods: "basic-card",
     56    data: "unconvertable data",
     57  }];
     58 
     59  const defaultMethods = [{
     60    supportedMethods: "basic-card",
     61    data: {
     62      supportedNetworks: ["unionpay", "visa", "mastercard", "amex", "discover",
     63                          "diners", "jcb", "mir",
     64      ],
     65    },
     66  }];
     67  const defaultDetails = {
     68    id: "test payment",
     69    total: {
     70      label: "Total",
     71      amount: {
     72        currency: "USD",
     73        value: "1.00"
     74      }
     75    },
     76    shippingOptions: [
     77      {
     78        id: "NormalShipping",
     79        label: "NormalShipping",
     80        amount: {
     81          currency: "USD",
     82          value: "10.00"
     83        },
     84        selected: true,
     85      },
     86      {
     87        id: "FastShipping",
     88        label: "FastShipping",
     89        amount: {
     90          currency: "USD",
     91          value: "30.00"
     92        },
     93        selected: false,
     94      },
     95    ],
     96  };
     97 
     98  const updateDetails = {
     99    total: {
    100      label: "Total",
    101      amount: {
    102        currency: "USD",
    103        value: "1.00"
    104      }
    105    },
    106    shippingOptions: [
    107      {
    108        id: "NormalShipping",
    109        label: "NormalShipping",
    110        amount: {
    111          currency: "USD",
    112          value: "10.00"
    113        },
    114        selected: true,
    115      },
    116      {
    117        id: "FastShipping",
    118        label: "FastShipping",
    119        amount: {
    120          currency: "USD",
    121          value: "30.00"
    122        },
    123        selected: false,
    124      },
    125    ],
    126    error: "",
    127  };
    128 
    129  const defaultOptions = {
    130    requestPayerName: true,
    131    requestPayerEmail: false,
    132    requestPayerPhone: false,
    133    requestShipping: true,
    134    shippingType: "shipping"
    135  };
    136 
    137  async function testBasicCardRequestWithErrorNetworks() {
    138    const testName = "testBasicCardRequestWithErrorNetworks";
    139    try {
    140      const request = new PaymentRequest(errorNetworksMethods, defaultDetails, defaultOptions);
    141      ok(false, `${testName}: Expected 'TypeError', but got success construction.`);
    142    } catch (e) {
    143      is(e.name, "TypeError", `${testName}: Expected TypeError, but got ${e.name}`);
    144    }
    145  }
    146 
    147  async function testBasicCardRequestWithUnconvertableData() {
    148    const testName = "testBasicCardRequestWithUnconvertableData";
    149    try {
    150      const request = new PaymentRequest(unconvertableDataMethods, defaultDetails, defaultOptions);
    151      ok(false, `${testName}: Expected 'TypeError', but got success construction.`);
    152    } catch (e) {
    153      is(e.name, "TypeError", `${testName}: Expected TypeError, but got ${e.name}`);
    154    }
    155  }
    156 
    157  async function testBasicCardRequestWithNullData() {
    158    const testName = "testBasicCardRequestWithNullData";
    159    try {
    160      const request = new PaymentRequest(nullDataMethods, defaultDetails, defaultOptions);
    161      ok(request, `${testName}: PaymentRequest should be constructed with null data BasicCardRequest.`);
    162    } catch (e) {
    163      ok(false, `${testName}: Unexpected error: ${e.name}`);
    164    }
    165  }
    166 
    167  async function testBasicCardRequestWithEmptyData() {
    168    const testName = "testBasicCardRequestWithEmptyData";
    169    try {
    170      const request = new PaymentRequest(emptyDataMethods, defaultDetails, defaultOptions);
    171      ok(request, `${testName}: PaymentRequest should be constructed with empty data BasicCardRequest.`);
    172    } catch (e) {
    173      ok(false, `${testName}: Unexpected error: ${e.name}`);
    174    }
    175  }
    176 
    177  async function testCanMakePaymentWithBasicCardRequest() {
    178    const testName = "testCanMakePaymentWithBasicCardRequest";
    179    const request = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
    180    try {
    181      const result = await request.canMakePayment();
    182      ok(result, `${testName}: canMakePayment() should be resolved with true.`);
    183    } catch (e) {
    184      ok(false, `${testName}: Unexpected error: ${e.name}`);
    185    }
    186  }
    187 
    188  async function testBasicCardSimpleResponse() {
    189    const testName = "testBasicCardSimpleResponse";
    190    await requestChromeAction("set-simple-ui-service", testName);
    191    const request = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
    192    const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
    193    try {
    194      const response = await request.show();
    195      ok(response.details, `${testName}: basiccard response should exists.`);
    196      ok(!response.details.cardholderName, `${testName}: response.details.cardholderName should not exist.`);
    197      is(response.details.cardNumber, "4916855166538720",
    198         `${testName}: response.details.cardNumber should be '4916855166538720'.`);
    199      ok(!response.details.expiryMonth, `${testName}: response.details.expiryMonth should not exist.`);
    200      ok(!response.details.expiryYear, `${testName}: response.details.expiryYear should be '2024'.`);
    201      ok(!response.details.cardSecurityCode, `${testName}: response.details.cardSecurityCode should not exist.`);
    202      ok(!response.details.billingAddress, `${testName}: response.details.billingAddress should not exist.`);
    203      await response.complete("success");
    204    } catch (e) {
    205      ok(false, `${testName}: Unexpected error: ${e.name}`);
    206    }
    207    await handler.destruct();
    208  }
    209 
    210  async function testBasicCardDetailedResponse() {
    211    const testName = "testBasicCardDetailedResponse";
    212    await requestChromeAction("set-detailed-ui-service", testName);
    213    const request = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
    214    const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
    215    try {
    216      const response = await request.show();
    217      ok(response.details, `${testName}: basiccard response should exists.`);
    218      ok(response.details.cardholderName, `${testName}: response.details.cardholderName should not exist.`);
    219      is(response.details.cardNumber, "4916855166538720",
    220         `${testName}: response.details.cardNumber should be '4916855166538720'.`);
    221      ok(response.details.expiryMonth, `${testName}: response.details.expiryMonth should not exist.`);
    222      ok(response.details.expiryYear, `${testName}: response.details.expiryYear should be '2024'.`);
    223      ok(response.details.cardSecurityCode, `${testName}: response.details.cardSecurityCode should not exist.`);
    224      ok(response.details.billingAddress, `${testName}: response.details.billingAddress should not exist.`);
    225      const billingAddress = response.details.billingAddress;
    226      is(billingAddress.country, "USA", `${testName}: country should be 'USA'.`);
    227      is(billingAddress.addressLine.length, 1, `${testName}: addressLine.length should be 1.`);
    228      is(billingAddress.addressLine[0], "Easton Ave", `${testName}: addressLine[0] should be 'Easton Ave'.`);
    229      is(billingAddress.region, "CA", `${testName}: region should be 'CA'.`);
    230      is(billingAddress.regionCode, "CA", `${testName}: regionCode should be 'CA'.`);
    231      is(billingAddress.city, "San Bruno", `${testName}: city should be 'San Bruno'.`);
    232      is(billingAddress.dependentLocality, "", `${testName}: dependentLocality should be empty.`);
    233      is(billingAddress.postalCode, "94066", `${testName}: postalCode should be '94066'.`);
    234      is(billingAddress.sortingCode, "123456", `${testName}: sortingCode should be '123456'.`);
    235      is(billingAddress.organization, "", `${testName}: organization should be empty.`);
    236      is(billingAddress.recipient, "Bill A. Pacheco", `${testName}: recipient should be 'Bill A. Pacheco'.`);
    237      is(billingAddress.phone, "+14344413879", `${testName}: phone should be '+14344413879'.`);
    238      await response.complete("success");
    239    } catch (e) {
    240      ok(false, `${testName}: Unexpected error: ${e.name}`);
    241    }
    242    await handler.destruct();
    243  }
    244 
    245  async function testSpecialAddressResponse() {
    246    const testName = "testSpecialAddressResponse";
    247    await requestChromeAction("set-special-address-ui-service", testName);
    248    const request = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
    249    const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
    250    try {
    251      const response = await request.show();
    252      ok(response.details, `${testName}: BasiccardResponse should exist.`);
    253      ok(response.details.billingAddress,
    254         `${testName}: BasiccardResponse.billingAddress should exist.`);
    255      is(response.details.billingAddress.addressLine[0], ":$%@&*",
    256         `${testName}: AddressLine should be ':$%@&*'`);
    257      await response.complete("success");
    258    } catch (e) {
    259      ok(false, `${testName}: Unexpected error: ${e.name}`);
    260    }
    261    await handler.destruct();
    262  }
    263 
    264  async function testMethodChangeWithoutRequestBillingAddress() {
    265    const testName = "testMethodChangeWithoutRequestBillingAddress";
    266    await requestChromeAction("method-change-to-basic-card", testName);
    267    const request = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
    268    request.addEventListener("paymentmethodchange", async (event) => {
    269      is(event.methodName, "basic-card", `${testName}: PaymentMethodChangeEvent.methodName should be 'basic-card'.`)
    270      ok(event.methodDetails, `PaymentMethodChangeEvent.methodDetails should exist.`);
    271      ok(!event.methodDetails.billingAddress, `PaymentMethodChangeEvent.methodDetails.billingAddres should not exist.`);
    272      event.updateWith(updateDetails);
    273    });
    274    const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
    275    try {
    276      const response = await request.show();
    277      await response.complete("success");
    278    } catch (error) {
    279      ok(false, `${testName}: Unexpected error: ${error.name}`);
    280    }
    281    await handler.destruct();
    282  }
    283 
    284  async function testMethodChangeWithRequestBillingAddress() {
    285    const testName = "testMethodChangeWithRequestBillingAddress";
    286    await requestChromeAction("method-change-to-basic-card", testName);
    287    const options = {
    288      requestPayerName: true,
    289      requestBillingAddress: true,
    290      requestShipping: true,
    291      shippingType: "shipping",
    292    };
    293    const request = new PaymentRequest(defaultMethods, defaultDetails, options);
    294    request.addEventListener("paymentmethodchange", async (event) => {
    295      is(event.methodName, "basic-card", `${testName}: PaymentMethodChangeEvent.methodName should be 'basic-card'.`)
    296      ok(event.methodDetails, `PaymentMethodChangeEvent.methodDetails should exist.`);
    297      const billingAddress = event.methodDetails.billingAddress;
    298      is(billingAddress.country, "USA", `${testName}: country should be 'USA'.`);
    299      is(billingAddress.addressLine.length, 1, `${testName}: addressLine.length should be 1.`);
    300      is(billingAddress.addressLine[0], "Easton Ave", `${testName}: addressLine[0] should be 'Easton Ave'.`);
    301      is(billingAddress.region, "CA", `${testName}: region should be 'CA'.`);
    302      is(billingAddress.regionCode, "CA", `${testName}: regionCode should be 'CA'.`);
    303      is(billingAddress.city, "San Bruno", `${testName}: city should be 'San Bruno'.`);
    304      is(billingAddress.dependentLocality, "", `${testName}: dependentLocality should be empty.`);
    305      is(billingAddress.postalCode, "94066", `${testName}: postalCode should be '94066'.`);
    306      is(billingAddress.sortingCode, "123456", `${testName}: sortingCode should be '123456'.`);
    307      is(billingAddress.organization, "", `${testName}: organization should be empty.`);
    308      is(billingAddress.recipient, "Bill A. Pacheco", `${testName}: recipient should be 'Bill A. Pacheco'.`);
    309      is(billingAddress.phone, "+14344413879", `${testName}: phone should be '+14344413879'.`);
    310      event.updateWith(updateDetails);
    311    });
    312    const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true);
    313    try {
    314      const response = await request.show();
    315      await response.complete("success");
    316    } catch (error) {
    317      ok(false, `${testName}: Unexpected error: ${error.name}`);
    318    }
    319    await handler.destruct();
    320  }
    321 
    322 
    323  async function testBasicCardErrorResponse() {
    324    const testName = "testBasicCardErrorResponse";
    325    return requestChromeAction("error-response-test", testName);
    326  }
    327 
    328  async function teardown() {
    329    gScript.addMessageListener("teardown-complete", function teardownCompleteHandler() {
    330      gScript.removeMessageListener("teardown-complete", teardownCompleteHandler);
    331      gScript.removeMessageListener("test-fail", testFailHandler)
    332      gScript.destroy();
    333      SimpleTest.finish();
    334    });
    335    gScript.sendAsyncMessage("teardown");
    336  }
    337 
    338  async function runTests() {
    339    try {
    340      await testBasicCardRequestWithErrorNetworks();
    341      await testBasicCardRequestWithUnconvertableData();
    342      await testBasicCardRequestWithNullData();
    343      await testBasicCardRequestWithEmptyData();
    344      await testCanMakePaymentWithBasicCardRequest();
    345      await testBasicCardSimpleResponse();
    346      await testBasicCardDetailedResponse();
    347      await testSpecialAddressResponse();
    348      await testBasicCardErrorResponse();
    349      await testMethodChangeWithoutRequestBillingAddress();
    350      await testMethodChangeWithRequestBillingAddress()
    351      await teardown();
    352    } catch (e) {
    353      ok(false, `test_basiccard.html: Unexpected error: ${e.name}`);
    354      SimpleTest.finish();
    355    };
    356  }
    357 
    358  window.addEventListener('load', function() {
    359    SpecialPowers.pushPrefEnv({
    360      'set': [
    361        ['dom.payments.request.enabled', true],
    362      ]
    363    }, runTests);
    364  });
    365 
    366  </script>
    367 </head>
    368 <body>
    369 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1375345">Mozilla Bug 1375345</a>
    370 </body>
    371 </html>