|
| 1 | +/* eslint-disable comma-spacing */ |
| 2 | +/* eslint-disable key-spacing */ |
| 3 | +/* eslint-disable quote-props */ |
| 4 | +/* eslint-disable object-curly-spacing */ |
| 5 | +/* eslint-disable cypress/no-unnecessary-waiting */ |
| 6 | +/* eslint-disable require-jsdoc */ |
| 7 | +var Countly = require("../../lib/countly"); |
| 8 | +var hp = require("../support/helper"); |
| 9 | + |
| 10 | +function initMain() { |
| 11 | + Countly.init({ |
| 12 | + app_key: "YOUR_APP_KEY", |
| 13 | + url: "https://try.count.ly", |
| 14 | + tests: true, |
| 15 | + max_events: -1, |
| 16 | + debug: true |
| 17 | + }); |
| 18 | +} |
| 19 | +// a convenience function to test wrong status code options |
| 20 | +function variedStatusCodeTestPack(validationFunction, response, result) { |
| 21 | + expect(validationFunction(response)).to.equal(result); |
| 22 | + expect(validationFunction(-500, response)).to.equal(result); |
| 23 | + expect(validationFunction(-400, response)).to.equal(result); |
| 24 | + expect(validationFunction(-301, response)).to.equal(result); |
| 25 | + expect(validationFunction(-300, response)).to.equal(result); |
| 26 | + expect(validationFunction(-201, response)).to.equal(result); |
| 27 | + expect(validationFunction(-200, response)).to.equal(result); |
| 28 | + expect(validationFunction(-100, response)).to.equal(result); |
| 29 | + expect(validationFunction(0, response)).to.equal(result); |
| 30 | + expect(validationFunction(100, response)).to.equal(result); |
| 31 | + expect(validationFunction(300, response)).to.equal(result); |
| 32 | + expect(validationFunction(301, response)).to.equal(result); |
| 33 | + expect(validationFunction(400, response)).to.equal(result); |
| 34 | + expect(validationFunction(500, response)).to.equal(result); |
| 35 | +} |
| 36 | + |
| 37 | +function fakeResponseTestPack(validationFunction, result) { |
| 38 | + variedStatusCodeTestPack(validationFunction, numberResponse, result); |
| 39 | + variedStatusCodeTestPack(validationFunction, stringResponse, result); |
| 40 | + variedStatusCodeTestPack(validationFunction, arrayResponse1, result); |
| 41 | + variedStatusCodeTestPack(validationFunction, arrayResponse2, result); |
| 42 | + variedStatusCodeTestPack(validationFunction, arrayResponse3, result); |
| 43 | + variedStatusCodeTestPack(validationFunction, arrayResponse4, result); |
| 44 | + variedStatusCodeTestPack(validationFunction, objectResponse1, result); |
| 45 | + variedStatusCodeTestPack(validationFunction, objectResponse2, result); |
| 46 | + variedStatusCodeTestPack(validationFunction, objectResponse3, result); |
| 47 | + variedStatusCodeTestPack(validationFunction, objectResponse4, result); |
| 48 | + variedStatusCodeTestPack(validationFunction, nullResponse, result); |
| 49 | + variedStatusCodeTestPack(validationFunction, undefinedResponse, result); |
| 50 | +} |
| 51 | + |
| 52 | +function fakeResponseKeyTestPack(validationFunction, response, result) { |
| 53 | + expect(validationFunction(200, response)).to.equal(result); |
| 54 | + expect(validationFunction(201, response)).to.equal(result); |
| 55 | +} |
| 56 | +// responses, stringified from actual parsed responses from the server |
| 57 | +const enableRatingResponse = JSON.stringify([{"_id":"619b8dd77730596209194f7e","popup_header_text":"hohoho","popup_comment_callout":"Add comment","popup_email_callout":"Contact me via e-mail","popup_button_callout":"Submit feedback","popup_thanks_message":"Thank you for your feedback","trigger_position":"bleft","trigger_bg_color":"13B94D","trigger_font_color":"FFFFFF","trigger_button_text":"Feedback","target_devices":{"phone":false,"desktop":true,"tablet":false},"target_page":"all","target_pages":["/"],"is_active":"true","hide_sticker":false,"app_id":"6181431e09e272efa5f64305","contact_enable":"true","comment_enable":"true","trigger_size":"l","type":"rating","ratings_texts":["Very dissatisfied","Somewhat dissatisfied","Neither satisfied Nor Dissatisfied","Somewhat Satisfied","Very Satisfied"],"status":true,"targeting":null,"timesShown":22,"ratingsCount":4,"ratingsSum":13}]); |
| 58 | +const popupResponse = JSON.stringify({"_id":"619b8dd77730596209194f7e","popup_header_text":"hohoho","popup_comment_callout":"Add comment","popup_email_callout":"Contact me via e-mail","popup_button_callout":"Submit feedback","popup_thanks_message":"Thank you for your feedback","trigger_position":"bleft","trigger_bg_color":"13B94D","trigger_font_color":"FFFFFF","trigger_button_text":"Feedback","target_devices":{"phone":false,"desktop":true,"tablet":false},"target_page":"all","target_pages":["/"],"is_active":"true","hide_sticker":false,"app_id":"6181431e09e272efa5f64305","contact_enable":"true","comment_enable":"true","trigger_size":"l","type":"rating","ratings_texts":["Very dissatisfied","Somewhat dissatisfied","Neither satisfied Nor Dissatisfied","Somewhat Satisfied","Very Satisfied"],"status":true,"targeting":null,"timesShown":23,"ratingsCount":4,"ratingsSum":13}); |
| 59 | +const remoteConfigResponse = JSON.stringify({"Nightfox":{"test":"250 mg"},"firefox":{"clen":"20 mg"}}); |
| 60 | + |
| 61 | +// fake responses for other testing purposes |
| 62 | +const numberResponse = 551; |
| 63 | +const stringResponse = "551"; |
| 64 | +const arrayResponse1 = []; |
| 65 | +const arrayResponse2 = [{}]; |
| 66 | +// passing response for isResponseValidBroad |
| 67 | +const arrayResponse3 = "[{}]"; |
| 68 | +// passing response for isResponseValidBroad |
| 69 | +const arrayResponse4 = "[]"; |
| 70 | +const objectResponse1 = {}; |
| 71 | +const objectResponse2 = {[5]:{}}; |
| 72 | +const objectResponse3 = "{[]}"; |
| 73 | +// passing response for isResponseValid and isResponseValidBroad |
| 74 | +const objectResponse4 = "{}"; |
| 75 | +const nullResponse = null; |
| 76 | +const undefinedResponse = undefined; |
| 77 | + |
| 78 | +describe("Response validation tests ", () => { |
| 79 | + // enableRating call => only isResponseValidBroad field should yield true |
| 80 | + it("isResponseValid, enableRatingResponse", () => { |
| 81 | + hp.haltAndClearStorage(() => { |
| 82 | + initMain(); |
| 83 | + variedStatusCodeTestPack(Countly._internals.isResponseValid, enableRatingResponse, false); |
| 84 | + expect(Countly._internals.isResponseValid(200, enableRatingResponse)).to.equal(false); |
| 85 | + expect(Countly._internals.isResponseValid(201, enableRatingResponse)).to.equal(false); |
| 86 | + }); |
| 87 | + }); |
| 88 | + it("isResponseValid, enableRatingResponse", () => { |
| 89 | + hp.haltAndClearStorage(() => { |
| 90 | + initMain(); |
| 91 | + variedStatusCodeTestPack(Countly._internals.isResponseValidBroad, enableRatingResponse, false); |
| 92 | + expect(Countly._internals.isResponseValidBroad(200, enableRatingResponse)).to.equal(true); |
| 93 | + expect(Countly._internals.isResponseValidBroad(201, enableRatingResponse)).to.equal(true); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + // popup call => both isResponseValidBroad and isResponseValid fields should yield true |
| 98 | + it("isResponseValid, popupResponse", () => { |
| 99 | + hp.haltAndClearStorage(() => { |
| 100 | + initMain(); |
| 101 | + variedStatusCodeTestPack(Countly._internals.isResponseValid, popupResponse, false); |
| 102 | + expect(Countly._internals.isResponseValid(200, popupResponse)).to.equal(false); |
| 103 | + expect(Countly._internals.isResponseValid(201, popupResponse)).to.equal(false); |
| 104 | + }); |
| 105 | + }); |
| 106 | + it("isResponseValidBroad, popupResponse", () => { |
| 107 | + hp.haltAndClearStorage(() => { |
| 108 | + initMain(); |
| 109 | + variedStatusCodeTestPack(Countly._internals.isResponseValidBroad, popupResponse, false); |
| 110 | + expect(Countly._internals.isResponseValidBroad(200, popupResponse)).to.equal(true); |
| 111 | + expect(Countly._internals.isResponseValidBroad(201, popupResponse)).to.equal(true); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + // remoteconfig call => both isResponseValidBroad and isResponseValid fields should yield true |
| 116 | + it("isResponseValid, remoteConfigResponse", () => { |
| 117 | + hp.haltAndClearStorage(() => { |
| 118 | + initMain(); |
| 119 | + variedStatusCodeTestPack(Countly._internals.isResponseValid, remoteConfigResponse, false); |
| 120 | + expect(Countly._internals.isResponseValid(200, remoteConfigResponse)).to.equal(false); |
| 121 | + expect(Countly._internals.isResponseValid(201, remoteConfigResponse)).to.equal(false); |
| 122 | + }); |
| 123 | + }); |
| 124 | + it("isResponseValidBroad, remoteConfigResponse", () => { |
| 125 | + hp.haltAndClearStorage(() => { |
| 126 | + initMain(); |
| 127 | + variedStatusCodeTestPack(Countly._internals.isResponseValidBroad, remoteConfigResponse, false); |
| 128 | + expect(Countly._internals.isResponseValidBroad(200, remoteConfigResponse)).to.equal(true); |
| 129 | + expect(Countly._internals.isResponseValidBroad(201, remoteConfigResponse)).to.equal(true); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + // fake response calls => both isResponseValidBroad and isResponseValid fields should yield true |
| 134 | + it("isResponseValid, fake responses", () => { |
| 135 | + hp.haltAndClearStorage(() => { |
| 136 | + initMain(); |
| 137 | + fakeResponseTestPack(Countly._internals.isResponseValid, false); |
| 138 | + fakeResponseKeyTestPack(Countly._internals.isResponseValid, numberResponse, false); |
| 139 | + fakeResponseKeyTestPack(Countly._internals.isResponseValid, stringResponse, false); |
| 140 | + fakeResponseKeyTestPack(Countly._internals.isResponseValid, arrayResponse1, false); |
| 141 | + fakeResponseKeyTestPack(Countly._internals.isResponseValid, arrayResponse2, false); |
| 142 | + fakeResponseKeyTestPack(Countly._internals.isResponseValid, arrayResponse3, false); |
| 143 | + fakeResponseKeyTestPack(Countly._internals.isResponseValid, arrayResponse4, false); |
| 144 | + fakeResponseKeyTestPack(Countly._internals.isResponseValid, objectResponse1, false); |
| 145 | + fakeResponseKeyTestPack(Countly._internals.isResponseValid, objectResponse2, false); |
| 146 | + fakeResponseKeyTestPack(Countly._internals.isResponseValid, objectResponse3, false); |
| 147 | + fakeResponseKeyTestPack(Countly._internals.isResponseValid, objectResponse4, false); |
| 148 | + fakeResponseKeyTestPack(Countly._internals.isResponseValid, nullResponse, false); |
| 149 | + fakeResponseKeyTestPack(Countly._internals.isResponseValid, undefinedResponse, false); |
| 150 | + }); |
| 151 | + }); |
| 152 | + it("isResponseValidBroad, fake responses", () => { |
| 153 | + hp.haltAndClearStorage(() => { |
| 154 | + initMain(); |
| 155 | + fakeResponseTestPack(Countly._internals.isResponseValidBroad, false); |
| 156 | + fakeResponseKeyTestPack(Countly._internals.isResponseValidBroad, numberResponse, false); |
| 157 | + fakeResponseKeyTestPack(Countly._internals.isResponseValidBroad, stringResponse, false); |
| 158 | + fakeResponseKeyTestPack(Countly._internals.isResponseValidBroad, arrayResponse1, false); |
| 159 | + fakeResponseKeyTestPack(Countly._internals.isResponseValidBroad, arrayResponse2, false); |
| 160 | + fakeResponseKeyTestPack(Countly._internals.isResponseValidBroad, arrayResponse3, true); |
| 161 | + fakeResponseKeyTestPack(Countly._internals.isResponseValidBroad, arrayResponse4, true); |
| 162 | + fakeResponseKeyTestPack(Countly._internals.isResponseValidBroad, objectResponse1, false); |
| 163 | + fakeResponseKeyTestPack(Countly._internals.isResponseValidBroad, objectResponse2, false); |
| 164 | + fakeResponseKeyTestPack(Countly._internals.isResponseValidBroad, objectResponse3, false); |
| 165 | + fakeResponseKeyTestPack(Countly._internals.isResponseValidBroad, objectResponse4, true); |
| 166 | + fakeResponseKeyTestPack(Countly._internals.isResponseValidBroad, nullResponse, false); |
| 167 | + fakeResponseKeyTestPack(Countly._internals.isResponseValidBroad, undefinedResponse, false); |
| 168 | + }); |
| 169 | + }); |
| 170 | +}); |
0 commit comments