|
| 1 | +/* |
| 2 | + * JavaScript tracker for Snowplow: tests/functional/helpers.js |
| 3 | + * |
| 4 | + * Significant portions copyright 2010 Anthon Pang. Remainder copyright |
| 5 | + * 2012-2014 Snowplow Analytics Ltd. All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are |
| 9 | + * met: |
| 10 | + * |
| 11 | + * * Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * * Redistributions in binary form must reproduce the above copyright |
| 15 | + * notice, this list of conditions and the following disclaimer in the |
| 16 | + * documentation and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * * Neither the name of Anthon Pang nor Snowplow Analytics Ltd nor the |
| 19 | + * names of their contributors may be used to endorse or promote products |
| 20 | + * derived from this software without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | + */ |
| 34 | + |
| 35 | +define([ |
| 36 | + 'intern!object', |
| 37 | + 'intern/chai!assert', |
| 38 | + 'intern/dojo/node!fs', |
| 39 | + 'intern/dojo/node!querystring', |
| 40 | + 'intern/dojo/node!lodash' |
| 41 | +], function(registerSuite, assert, fs, querystring, lodash) { |
| 42 | + |
| 43 | + function parseLog () { |
| 44 | + return fs.readFileSync('./tracker.log', 'utf8').split('\n'); |
| 45 | + } |
| 46 | + |
| 47 | + function querystringFromLine(line) { |
| 48 | + return line ? JSON.parse(line) : {}; |
| 49 | + } |
| 50 | + |
| 51 | + function requestMatchesExpectedQuerystring(actual, expected) { |
| 52 | + if (!actual) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + return !lodash.any(lodash.keys(expected), function (key) { |
| 56 | + if (expected[key] !== actual[key]) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + return false; |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + function checkExistenceOfExpectedQuerystring(expected) { |
| 64 | + var lines = parseLog(); |
| 65 | + |
| 66 | + return lodash.any(lines, function (line) { |
| 67 | + if (requestMatchesExpectedQuerystring(querystringFromLine(line), expected)) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + return false; |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + registerSuite({ |
| 75 | + |
| 76 | + name: 'Test that request_recorder logs meet expectations', |
| 77 | + |
| 78 | + 'Check existence of page view in log': function() { |
| 79 | + assert.isTrue(checkExistenceOfExpectedQuerystring({ |
| 80 | + e: 'pv', |
| 81 | + p: 'mob', |
| 82 | + aid: 'CFe23a', |
| 83 | + uid: 'Malcolm', |
| 84 | + page: 'My Title', |
| 85 | + cx: 'eyJzY2hlbWEiOiJpZ2x1OmNvbS5zbm93cGxvd2FuYWx5dGljcy5zbm93cGxvdy9jb250ZXh0cy9qc29uc2NoZW1hLzEtMC0wIiwiZGF0YSI6W3sic2NoZW1hIjoiaWdsdTpjb20uZXhhbXBsZV9jb21wYW55L3VzZXIvanNvbnNjaGVtYS8yLTAtMCIsImRhdGEiOnsidXNlclR5cGUiOiJ0ZXN0ZXIifX1dfQ' |
| 86 | + }), 'A page view should be detected'); |
| 87 | + }, |
| 88 | + |
| 89 | + 'Check nonexistence of nonexistent event types in log': function() { |
| 90 | + assert.isFalse(checkExistenceOfExpectedQuerystring({ |
| 91 | + e: 'ad' |
| 92 | + }), 'No nonexistent event type should be detected'); |
| 93 | + }, |
| 94 | + |
| 95 | + 'Check a structured event was sent': function () { |
| 96 | + assert.isTrue(checkExistenceOfExpectedQuerystring({ |
| 97 | + e: 'se', |
| 98 | + se_ca: 'Mixes', |
| 99 | + se_ac: 'Play', |
| 100 | + se_la: 'MRC/fabric-0503-mix', |
| 101 | + se_va: '0.0' |
| 102 | + }), 'A structured event should be detected'); |
| 103 | + }, |
| 104 | + |
| 105 | + 'Check an unstructured event was sent': function () { |
| 106 | + assert.isTrue(checkExistenceOfExpectedQuerystring({ |
| 107 | + e: 'ue', |
| 108 | + ue_px: 'eyJzY2hlbWEiOiJpZ2x1OmNvbS5zbm93cGxvd2FuYWx5dGljcy5zbm93cGxvdy91bnN0cnVjdF9ldmVudC9qc29uc2NoZW1hLzEtMC0wIiwiZGF0YSI6eyJzY2hlbWEiOiJpZ2x1OmNvbS5hY21lX2NvbXBhbnkvdmlld2VkX3Byb2R1Y3QvanNvbnNjaGVtYS81LTAtMCIsImRhdGEiOnsicHJvZHVjdElkIjoiQVNPMDEwNDMifX19' |
| 109 | + }), 'An unstructured event should be detected'); |
| 110 | + }, |
| 111 | + |
| 112 | + 'Check a transaction event was sent': function () { |
| 113 | + assert.isTrue(checkExistenceOfExpectedQuerystring({ |
| 114 | + e: 'tr', |
| 115 | + tr_id: 'order-123', |
| 116 | + tr_af: 'acme', |
| 117 | + tr_tt: '8000', |
| 118 | + tr_tx: '100', |
| 119 | + tr_ci: 'phoenix', |
| 120 | + tr_st: 'arizona', |
| 121 | + tr_co: 'USA', |
| 122 | + tr_cu: 'JPY' |
| 123 | + }), 'A transaction event should be detected'); |
| 124 | + }, |
| 125 | + |
| 126 | + 'Check a transaction item event was sent': function () { |
| 127 | + assert.isTrue(checkExistenceOfExpectedQuerystring({ |
| 128 | + e: 'ti', |
| 129 | + ti_id: 'order-123', |
| 130 | + ti_sk: '1001', |
| 131 | + ti_nm: 'Blue t-shirt', |
| 132 | + ti_ca: 'clothing', |
| 133 | + ti_pr: '2000', |
| 134 | + ti_qu: '2', |
| 135 | + ti_cu: 'JPY' |
| 136 | + }), 'A transaction item event should be detected'); |
| 137 | + } |
| 138 | + }); |
| 139 | +}); |
0 commit comments