dom/payment/tests/unit/test_paymanager_get_payment_request.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 "use strict";
michael@0 5
michael@0 6 function getPaymentHelper() {
michael@0 7 let error;
michael@0 8 let paym = newPaymentModule();
michael@0 9
michael@0 10 paym.PaymentManager.paymentFailed = function paymentFailed(aRequestId,
michael@0 11 errorMsg) {
michael@0 12 error = errorMsg;
michael@0 13 };
michael@0 14
michael@0 15 return {
michael@0 16 get paymentModule() {
michael@0 17 return paym;
michael@0 18 },
michael@0 19 get error() {
michael@0 20 return error;
michael@0 21 }
michael@0 22 };
michael@0 23 }
michael@0 24
michael@0 25 function run_test() {
michael@0 26 run_next_test();
michael@0 27 }
michael@0 28
michael@0 29 function testGetPaymentRequest(paymentProviders, test) {
michael@0 30 let helper = getPaymentHelper();
michael@0 31 let paym = helper.paymentModule;
michael@0 32
michael@0 33 paym.PaymentManager.registeredProviders = paymentProviders;
michael@0 34
michael@0 35 let ret = paym.PaymentManager.getPaymentRequestInfo("", test.jwt);
michael@0 36 if (!test.result) {
michael@0 37 test.ret ? do_check_true(ret) : do_check_false(ret);
michael@0 38 }
michael@0 39 if (test.error !== null) {
michael@0 40 do_check_eq(helper.error, test.error);
michael@0 41 } else {
michael@0 42 do_check_eq(typeof ret, "object");
michael@0 43 do_check_eq(ret.jwt, test.jwt);
michael@0 44 do_check_eq(ret.type, test.result.type);
michael@0 45 do_check_eq(ret.providerName, test.result.providerName);
michael@0 46 }
michael@0 47 }
michael@0 48
michael@0 49 add_test(function test_successfull_request() {
michael@0 50 let providers = {};
michael@0 51 let type = "mock/payments/inapp/v1";
michael@0 52 providers[type] = {
michael@0 53 name: "mockprovider",
michael@0 54 description: "Mock Payment Provider",
michael@0 55 uri: "https://mockpayprovider.phpfogapp.com/?req=",
michael@0 56 requestMethod: "GET"
michael@0 57 };
michael@0 58
michael@0 59 // Payload
michael@0 60 // {
michael@0 61 // "aud": "mockpayprovider.phpfogapp.com",
michael@0 62 // "iss": "Enter you app key here!",
michael@0 63 // "request": {
michael@0 64 // "name": "Piece of Cake",
michael@0 65 // "price": "10.50",
michael@0 66 // "priceTier": 1,
michael@0 67 // "productdata": "transaction_id=86",
michael@0 68 // "currencyCode": "USD",
michael@0 69 // "description": "Virtual chocolate cake to fill your virtual tummy"
michael@0 70 // },
michael@0 71 // "exp": 1352232792,
michael@0 72 // "iat": 1352229192,
michael@0 73 // "typ": "mock/payments/inapp/v1"
michael@0 74 // }
michael@0 75 let jwt = "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJhdWQiOiAibW9j" +
michael@0 76 "a3BheXByb3ZpZGVyLnBocGZvZ2FwcC5jb20iLCAiaXNzIjogIkVudGVyI" +
michael@0 77 "HlvdSBhcHAga2V5IGhlcmUhIiwgInJlcXVlc3QiOiB7Im5hbWUiOiAiUG" +
michael@0 78 "llY2Ugb2YgQ2FrZSIsICJwcmljZSI6ICIxMC41MCIsICJwcmljZVRpZXI" +
michael@0 79 "iOiAxLCAicHJvZHVjdGRhdGEiOiAidHJhbnNhY3Rpb25faWQ9ODYiLCAi" +
michael@0 80 "Y3VycmVuY3lDb2RlIjogIlVTRCIsICJkZXNjcmlwdGlvbiI6ICJWaXJ0d" +
michael@0 81 "WFsIGNob2NvbGF0ZSBjYWtlIHRvIGZpbGwgeW91ciB2aXJ0dWFsIHR1bW" +
michael@0 82 "15In0sICJleHAiOiAxMzUyMjMyNzkyLCAiaWF0IjogMTM1MjIyOTE5Miw" +
michael@0 83 "gInR5cCI6ICJtb2NrL3BheW1lbnRzL2luYXBwL3YxIn0.QZxc62USCy4U" +
michael@0 84 "IyKIC1TKelVhNklvk-Ou1l_daKntaFI";
michael@0 85
michael@0 86 testGetPaymentRequest(providers, {
michael@0 87 jwt: jwt,
michael@0 88 ret: true,
michael@0 89 error: null,
michael@0 90 result: {
michael@0 91 type: type,
michael@0 92 providerName: providers[type].name
michael@0 93 }
michael@0 94 });
michael@0 95
michael@0 96 run_next_test();
michael@0 97 });
michael@0 98
michael@0 99 add_test(function test_successfull_request_html_description() {
michael@0 100 let providers = {};
michael@0 101 let type = "mozilla/payments/pay/v1";
michael@0 102 providers[type] = {
michael@0 103 name: "webpay",
michael@0 104 description: "Mozilla Payment Provider",
michael@0 105 uri: "https://marketplace.firefox.com/mozpay/?req=",
michael@0 106 requestMethod: "GET"
michael@0 107 };
michael@0 108
michael@0 109 // Payload
michael@0 110 // {
michael@0 111 // "aud": "marketplace.firefox.com",
michael@0 112 // "iss": "marketplace-dev.allizom.org",
michael@0 113 // "request": {
michael@0 114 // "name": "Krupa's paid app 1",
michael@0 115 // "chargebackURL": "http://localhost:8002/telefonica/services/webpay/"
michael@0 116 // "chargeback",
michael@0 117 // "postbackURL": "http://localhost:8002/telefonica/services/webpay/"
michael@0 118 // "postback",
michael@0 119 // "productData": "addon_id=85&seller_uuid=d4855df9-6ce0-45cd-81cb-"
michael@0 120 // "cf8737e1e7aa&contrib_uuid=201868b7ac2cda410a99b3"
michael@0 121 // "ed4c11a8ea",
michael@0 122 // "pricePoint": 1,
michael@0 123 // "id": "maude:85",
michael@0 124 // "description": "This app has been automatically generated by <a href="
michael@0 125 // "\"http://outgoing.mozilla.org/v1/ba7f373ae16789eff3ab"
michael@0 126 // "fd95ca8d3c15d18dc9009afa204dc43f85a55b1f6ef1/http%3A/"
michael@0 127 // "/testmanifest.com\" rel=\"nofollow\">testmanifest.com"
michael@0 128 // "</a>"
michael@0 129 // },
michael@0 130 // "exp": 1358379147,
michael@0 131 // "iat": 1358375547,
michael@0 132 // "typ": "mozilla/payments/pay/v1"
michael@0 133 // }
michael@0 134 let jwt = "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJhdWQiOiAibWFya2V0cGx" +
michael@0 135 "hY2UuZmlyZWZveC5jb20iLCAiaXNzIjogIm1hcmtldHBsYWNlLWRldi5hbGxpem9" +
michael@0 136 "tLm9yZyIsICJyZXF1ZXN0IjogeyJuYW1lIjogIktydXBhJ3MgcGFpZCBhcHAgMSI" +
michael@0 137 "sICJjaGFyZ2ViYWNrVVJMIjogImh0dHA6Ly9sb2NhbGhvc3Q6ODAwMi90ZWxlZm9" +
michael@0 138 "uaWNhL3NlcnZpY2VzL3dlYnBheS9jaGFyZ2ViYWNrIiwgInBvc3RiYWNrVVJMIjo" +
michael@0 139 "gImh0dHA6Ly9sb2NhbGhvc3Q6ODAwMi90ZWxlZm9uaWNhL3NlcnZpY2VzL3dlYnB" +
michael@0 140 "heS9wb3N0YmFjayIsICJwcm9kdWN0RGF0YSI6ICJhZGRvbl9pZD04NSZzZWxsZXJ" +
michael@0 141 "fdXVpZD1kNDg1NWRmOS02Y2UwLTQ1Y2QtODFjYi1jZjg3MzdlMWU3YWEmY29udHJ" +
michael@0 142 "pYl91dWlkPTIwMTg2OGI3YWMyY2RhNDEwYTk5YjNlZDRjMTFhOGVhIiwgInByaWN" +
michael@0 143 "lUG9pbnQiOiAxLCAiaWQiOiAibWF1ZGU6ODUiLCAiZGVzY3JpcHRpb24iOiAiVGh" +
michael@0 144 "pcyBhcHAgaGFzIGJlZW4gYXV0b21hdGljYWxseSBnZW5lcmF0ZWQgYnkgPGEgaHJ" +
michael@0 145 "lZj1cImh0dHA6Ly9vdXRnb2luZy5tb3ppbGxhLm9yZy92MS9iYTdmMzczYWUxNjc" +
michael@0 146 "4OWVmZjNhYmZkOTVjYThkM2MxNWQxOGRjOTAwOWFmYTIwNGRjNDNmODVhNTViMWY" +
michael@0 147 "2ZWYxL2h0dHAlM0EvL3Rlc3RtYW5pZmVzdC5jb21cIiByZWw9XCJub2ZvbGxvd1w" +
michael@0 148 "iPnRlc3RtYW5pZmVzdC5jb208L2E-In0sICJleHAiOiAxMzU4Mzc5MTQ3LCAiaWF" +
michael@0 149 "0IjogMTM1ODM3NTU0NywgInR5cCI6ICJtb3ppbGxhL3BheW1lbnRzL3BheS92MSJ" +
michael@0 150 "9.kgSt636OSRBezMGtm9QLeDxlEOevL4xcOoDj8VRJyD8";
michael@0 151
michael@0 152 testGetPaymentRequest(providers, {
michael@0 153 jwt: jwt,
michael@0 154 ret: true,
michael@0 155 error: null,
michael@0 156 result: {
michael@0 157 type: type,
michael@0 158 providerName: providers[type].name
michael@0 159 }
michael@0 160 });
michael@0 161
michael@0 162 run_next_test();
michael@0 163 });
michael@0 164
michael@0 165 add_test(function test_empty_jwt() {
michael@0 166 testGetPaymentRequest(null, {
michael@0 167 jwt: "",
michael@0 168 ret: true,
michael@0 169 error: "INTERNAL_ERROR_CALL_WITH_MISSING_JWT"
michael@0 170 });
michael@0 171
michael@0 172 run_next_test();
michael@0 173 });
michael@0 174
michael@0 175 add_test(function test_wrong_segments_count() {
michael@0 176 // 1 segment JWT
michael@0 177 let OneSegJwt = "eyJhbGciOiJIUzI1NiJ9";
michael@0 178 testGetPaymentRequest(null, {
michael@0 179 jwt: OneSegJwt,
michael@0 180 ret: true,
michael@0 181 error: "PAY_REQUEST_ERROR_WRONG_SEGMENTS_COUNT"
michael@0 182 });
michael@0 183
michael@0 184 // 2 segments JWT
michael@0 185 let TwoSegJwt = "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIwNTg2NDkwMTM2NTY2N" +
michael@0 186 "zU1ODY2MSIsImF1ZCI6Ikdvb2dsZSIsInR5cCI6Imdvb2dsZS9" +
michael@0 187 "wYXltZW50cy9pbmFwcC9pdGVtL3YxIiwiaWF0IjoxMzUyMjIwM" +
michael@0 188 "jEyLCJleHAiOjEzNTIzMDY2MTIsInJlcXVlc3QiOnsiY3VycmV" +
michael@0 189 "uY3lDb2RlIjoiVVNEIiwicHJpY2UiOiIzLjAwIiwibmFtZSI6I" +
michael@0 190 "kdvbGQgU3RhciIsInNlbGxlckRhdGEiOiJzb21lIG9wYXF1ZSB" +
michael@0 191 "kYXRhIiwiZGVzY3JpcHRpb24iOiJBIHNoaW5pbmcgYmFkZ2Ugb" +
michael@0 192 "2YgZGlzdGluY3Rpb24ifX0";
michael@0 193
michael@0 194 testGetPaymentRequest(null, {
michael@0 195 jwt: TwoSegJwt,
michael@0 196 ret: true,
michael@0 197 error: "PAY_REQUEST_ERROR_WRONG_SEGMENTS_COUNT"
michael@0 198 });
michael@0 199
michael@0 200 run_next_test();
michael@0 201 });
michael@0 202
michael@0 203 add_test(function test_empty_payload() {
michael@0 204 let EmptyPayloadJwt = "eyJhbGciOiJIUzI1NiJ9..eyJpc3MiOiIwNTg2NDkwMTM2NTY2N";
michael@0 205
michael@0 206 testGetPaymentRequest(null, {
michael@0 207 jwt: EmptyPayloadJwt,
michael@0 208 ret: true,
michael@0 209 error: "PAY_REQUEST_ERROR_EMPTY_PAYLOAD"
michael@0 210 });
michael@0 211
michael@0 212 run_next_test();
michael@0 213 });
michael@0 214
michael@0 215 add_test(function test_missing_typ_parameter() {
michael@0 216 // Payload
michael@0 217 // {
michael@0 218 // "iss": "640ae477-df33-45cd-83b8-6f1f910a6494",
michael@0 219 // "iat": 1361203745,
michael@0 220 // "request": {
michael@0 221 // "description": "detailed description",
michael@0 222 // "id": "799db970-7afa-4028-bdb7-8b045eb8babc",
michael@0 223 // "postbackURL": "http://inapp-pay-test.farmdev.com/postback",
michael@0 224 // "productData": "transaction_id=58",
michael@0 225 // "pricePoint": 1,
michael@0 226 // "chargebackURL": "http://inapp-pay-test.farmdev.com/chargeback",
michael@0 227 // "name": "The Product"
michael@0 228 // },
michael@0 229 // "aud": "marketplace-dev.allizom.org",
michael@0 230 // "exp": 1361207345
michael@0 231 // }
michael@0 232 let missingTypJwt = "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9." +
michael@0 233 "eyJpc3MiOiAiNjQwYWU0NzctZGYzMy00NWNkLTgzY" +
michael@0 234 "jgtNmYxZjkxMGE2NDk0IiwgImlhdCI6IDEzNjEyMD" +
michael@0 235 "M3NDUsICJyZXF1ZXN0IjogeyJkZXNjcmlwdGlvbiI" +
michael@0 236 "6ICJkZXRhaWxlZCBkZXNjcmlwdGlvbiIsICJpZCI6" +
michael@0 237 "ICI3OTlkYjk3MC03YWZhLTQwMjgtYmRiNy04YjA0N" +
michael@0 238 "WViOGJhYmMiLCAicG9zdGJhY2tVUkwiOiAiaHR0cD" +
michael@0 239 "ovL2luYXBwLXBheS10ZXN0LmZhcm1kZXYuY29tL3B" +
michael@0 240 "vc3RiYWNrIiwgInByb2R1Y3REYXRhIjogInRyYW5z" +
michael@0 241 "YWN0aW9uX2lkPTU4IiwgInByaWNlUG9pbnQiOiAxL" +
michael@0 242 "CAiY2hhcmdlYmFja1VSTCI6ICJodHRwOi8vaW5hcH" +
michael@0 243 "AtcGF5LXRlc3QuZmFybWRldi5jb20vY2hhcmdlYmF" +
michael@0 244 "jayIsICJuYW1lIjogIlRoZSBQcm9kdWN0In0sICJh" +
michael@0 245 "dWQiOiAibWFya2V0cGxhY2UtZGV2LmFsbGl6b20ub" +
michael@0 246 "3JnIiwgImV4cCI6IDEzNjEyMDczNDV9.KAHsJX1Hy" +
michael@0 247 "fmwNvAckdVUqlpPvdHggpx9yX276TWacRg";
michael@0 248 testGetPaymentRequest(null, {
michael@0 249 jwt: missingTypJwt,
michael@0 250 ret: true,
michael@0 251 error: "PAY_REQUEST_ERROR_NO_TYP_PARAMETER"
michael@0 252 });
michael@0 253
michael@0 254 run_next_test();
michael@0 255 });
michael@0 256
michael@0 257 add_test(function test_missing_request_parameter() {
michael@0 258 // Payload
michael@0 259 // {
michael@0 260 // "iss": "Enter you app key here!",
michael@0 261 // "iat": 1352225299,
michael@0 262 // "typ": "mock/payments/inapp/v1",
michael@0 263 // "aud": "mockpayprovider.phpfogapp.com",
michael@0 264 // "exp": 1352228899
michael@0 265 // }
michael@0 266 let missingRequestJwt = "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9." +
michael@0 267 "eyJpc3MiOiAiRW50ZXIgeW91IGFwcCBrZXkgaGVyZ" +
michael@0 268 "SEiLCAiaWF0IjogMTM1MjIyNTI5OSwgInR5cCI6IC" +
michael@0 269 "Jtb2NrL3BheW1lbnRzL2luYXBwL3YxIiwgImF1ZCI" +
michael@0 270 "6ICJtb2NrcGF5cHJvdmlkZXIucGhwZm9nYXBwLmNv" +
michael@0 271 "bSIsICJleHAiOiAxMzUyMjI4ODk5fQ.yXGinvZiUs" +
michael@0 272 "v9JWvdfM6zPD0iOX9DgCPcIwIbCrL4tcs";
michael@0 273
michael@0 274 testGetPaymentRequest(null, {
michael@0 275 jwt: missingRequestJwt,
michael@0 276 ret: true,
michael@0 277 error: "PAY_REQUEST_ERROR_NO_REQUEST_PARAMETER"
michael@0 278 });
michael@0 279
michael@0 280 run_next_test();
michael@0 281 });
michael@0 282
michael@0 283 add_test(function test_jwt_decoding_error() {
michael@0 284 let wrongJwt = "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.^eyJhdWQiOiAibW9" +
michael@0 285 "a3BheXByb3ZpZGVyLnBocGZvZ2FwcC5jb20iLCAiaXNzIjogIkVudGVyI" +
michael@0 286 "HlvdSBhcHAga2V5IGhlcmUhIiwgInJlcXVlc3QiOiB7Im5hbWUiOiAiUG" +
michael@0 287 "llY2Ugb2YgQ2FrZSIsICJwcmljZSI6ICIxMC41MCIsICJwcmljZVRpZXI" +
michael@0 288 "iOiAxLCAicHJvZHVjdGRhdGEiOiAidHJhbnNhY3Rpb25faWQ9ODYiLCAi" +
michael@0 289 "Y3VycmVuY3lDb2RlIjogIlVTRCIsICJkZXNjcmlwdGlvbiI6ICJWaXJ0d" +
michael@0 290 "WFsIGNob2NvbGF0ZSBjYWtlIHRvIGZpbGwgeW91ciB2aXJ0dWFsIHR1bW" +
michael@0 291 "15In0sICJleHAiOiAxMzUyMjMyNzkyLCAiaWF0IjogMTM1MjIyOTE5Miw" +
michael@0 292 "gInR5cCI6ICJtb2NrL3BheW1lbnRzL2luYXBwL3YxIn0.QZxc62USCy4U" +
michael@0 293 "IyKIC1TKelVhNklvk-Ou1l_daKntaFI";
michael@0 294
michael@0 295 testGetPaymentRequest(null, {
michael@0 296 jwt: wrongJwt,
michael@0 297 ret: true,
michael@0 298 error: "PAY_REQUEST_ERROR_ERROR_DECODING_JWT"
michael@0 299 });
michael@0 300
michael@0 301 run_next_test();
michael@0 302 });
michael@0 303
michael@0 304 add_test(function test_non_https_provider() {
michael@0 305 let providers = {};
michael@0 306 let type = "mock/payments/inapp/v1";
michael@0 307 providers[type] = {
michael@0 308 name: "mockprovider",
michael@0 309 description: "Mock Payment Provider",
michael@0 310 uri: "http://mockpayprovider.phpfogapp.com/?req=",
michael@0 311 requestMethod: "GET"
michael@0 312 };
michael@0 313
michael@0 314 // Payload
michael@0 315 // {
michael@0 316 // "aud": "mockpayprovider.phpfogapp.com",
michael@0 317 // "iss": "Enter you app key here!",
michael@0 318 // "request": {
michael@0 319 // "name": "Piece of Cake",
michael@0 320 // "price": "10.50",
michael@0 321 // "priceTier": 1,
michael@0 322 // "productdata": "transaction_id=86",
michael@0 323 // "currencyCode": "USD",
michael@0 324 // "description": "Virtual chocolate cake to fill your virtual tummy"
michael@0 325 // },
michael@0 326 // "exp": 1352232792,
michael@0 327 // "iat": 1352229192,
michael@0 328 // "typ": "mock/payments/inapp/v1"
michael@0 329 // }
michael@0 330 let jwt = "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.eyJhdWQiOiAibW9j" +
michael@0 331 "a3BheXByb3ZpZGVyLnBocGZvZ2FwcC5jb20iLCAiaXNzIjogIkVudGVyI" +
michael@0 332 "HlvdSBhcHAga2V5IGhlcmUhIiwgInJlcXVlc3QiOiB7Im5hbWUiOiAiUG" +
michael@0 333 "llY2Ugb2YgQ2FrZSIsICJwcmljZSI6ICIxMC41MCIsICJwcmljZVRpZXI" +
michael@0 334 "iOiAxLCAicHJvZHVjdGRhdGEiOiAidHJhbnNhY3Rpb25faWQ9ODYiLCAi" +
michael@0 335 "Y3VycmVuY3lDb2RlIjogIlVTRCIsICJkZXNjcmlwdGlvbiI6ICJWaXJ0d" +
michael@0 336 "WFsIGNob2NvbGF0ZSBjYWtlIHRvIGZpbGwgeW91ciB2aXJ0dWFsIHR1bW" +
michael@0 337 "15In0sICJleHAiOiAxMzUyMjMyNzkyLCAiaWF0IjogMTM1MjIyOTE5Miw" +
michael@0 338 "gInR5cCI6ICJtb2NrL3BheW1lbnRzL2luYXBwL3YxIn0.QZxc62USCy4U" +
michael@0 339 "IyKIC1TKelVhNklvk-Ou1l_daKntaFI";
michael@0 340
michael@0 341 testGetPaymentRequest(providers, {
michael@0 342 jwt: jwt,
michael@0 343 ret: true,
michael@0 344 error: "INTERNAL_ERROR_NON_HTTPS_PROVIDER_URI"
michael@0 345 });
michael@0 346
michael@0 347 run_next_test();
michael@0 348 });

mercurial