Example #1
0
        public void JavaScriptBuilder_VerifyParameters(string userAgent, string lat, string lon)
        {
            _javaScriptBuilderElement =
                new JavaScriptBuilderElementBuilder(_loggerFactory)
                .SetEndpoint("/json")
                .Build();

            var flowData = new Mock <IFlowData>();

            Configure(flowData, null, "localhost", "https", userAgent, lat, lon);

            IJavaScriptBuilderElementData result = null;

            flowData.Setup(d => d.GetOrAdd(
                               It.IsAny <ITypedKey <IJavaScriptBuilderElementData> >(),
                               It.IsAny <Func <IPipeline, IJavaScriptBuilderElementData> >()))
            .Returns <ITypedKey <IJavaScriptBuilderElementData>, Func <IPipeline, IJavaScriptBuilderElementData> >((k, f) =>
            {
                result = f(flowData.Object.Pipeline);
                return(result);
            });

            _javaScriptBuilderElement.Process(flowData.Object);

            Assert.IsTrue(result.JavaScript.Contains(userAgent),
                          $"JavaScript does not contain expected user agent query parameter '{userAgent}'.");
            Assert.IsTrue(result.JavaScript.Contains(lat),
                          $"JavaScript does not contain expected user agent query parameter '{lat}'.");
            Assert.IsTrue(result.JavaScript.Contains(lon),
                          $"JavaScript does not contain expected user agent query parameter '{lon}'.");
        }
Example #2
0
        public void JavaScriptBuilder_VerifyObjName()
        {
            _javaScriptBuilderElement =
                new JavaScriptBuilderElementBuilder(_loggerFactory)
                .SetEndpoint("/json")
                .Build();

            var flowData = new Mock <IFlowData>();

            Configure(flowData, jsObjName: "testObj");

            IJavaScriptBuilderElementData result = null;

            flowData.Setup(d => d.GetOrAdd(
                               It.IsAny <ITypedKey <IJavaScriptBuilderElementData> >(),
                               It.IsAny <Func <IPipeline, IJavaScriptBuilderElementData> >()))
            .Returns <ITypedKey <IJavaScriptBuilderElementData>, Func <IPipeline, IJavaScriptBuilderElementData> >((k, f) =>
            {
                result = f(flowData.Object.Pipeline);
                return(result);
            });

            _javaScriptBuilderElement.Process(flowData.Object);

            IJavaScriptExecutor js = _driver;

            // Run the JavaScript content from the cloud service and bind to
            // window so we can check it later.
            js.ExecuteScript($"{result.JavaScript}; window.testObj = testObj;");
        }
Example #3
0
        public void JavaScriptBuilder_VerifyUrl()
        {
            _javaScriptBuilderElement =
                new JavaScriptBuilderElementBuilder(_loggerFactory)
                .SetEndpoint("/json")
                .Build();

            var flowData = new Mock <IFlowData>();

            Configure(flowData);

            IJavaScriptBuilderElementData result = null;

            flowData.Setup(d => d.GetOrAdd(
                               It.IsAny <ITypedKey <IJavaScriptBuilderElementData> >(),
                               It.IsAny <Func <IPipeline, IJavaScriptBuilderElementData> >()))
            .Returns <ITypedKey <IJavaScriptBuilderElementData>, Func <IPipeline, IJavaScriptBuilderElementData> >((k, f) =>
            {
                result = f(flowData.Object.Pipeline);
                return(result);
            });

            _javaScriptBuilderElement.Process(flowData.Object);

            string expectedUrl = "https://localhost/json";

            Assert.IsTrue(result.JavaScript.Contains(expectedUrl),
                          $"JavaScript does not contain expected URL '{expectedUrl}'.");
        }
Example #4
0
        public void JavaScriptBuilder_VerifySession()
        {
            _javaScriptBuilderElement =
                new JavaScriptBuilderElementBuilder(_loggerFactory).SetMinify(false).Build();
            var flowData = new Mock <IFlowData>();

            Configure(flowData);

            IJavaScriptBuilderElementData result = null;

            flowData.Setup(d => d.GetOrAdd(
                               It.IsAny <ITypedKey <IJavaScriptBuilderElementData> >(),
                               It.IsAny <Func <IPipeline, IJavaScriptBuilderElementData> >()))
            .Returns <ITypedKey <IJavaScriptBuilderElementData>, Func <IPipeline, IJavaScriptBuilderElementData> >((k, f) =>
            {
                result = f(flowData.Object.Pipeline);
                return(result);
            });

            _javaScriptBuilderElement.Process(flowData.Object);

            Assert.IsTrue(result.JavaScript.Contains("abcdefg-hijklmn-opqrst-uvwxyz"),
                          $"JavaScript does not contain expected session id 'abcdefg-hijklmn-opqrst-uvwxyz'.");
            Assert.IsTrue(result.JavaScript.Contains("var sequence = 1;"),
                          $"JavaScript does not contain expected sequence '1'.");
        }
Example #5
0
        public void JavaScriptBuilderElement_DelayExecution(bool minify)
        {
            _javaScriptBuilderElement =
                new JavaScriptBuilderElementBuilder(_loggerFactory)
                .SetMinify(minify)
                .Build();

            dynamic json = new JObject();

            var locationData = new JObject();

            locationData.Add(new JProperty("postcode", null));
            locationData.Add(new JProperty("postcodenullreason",
                                           "Evidence for this property has not been retrieved. Ensure the 'complete' method is called, passing the name of this property in the second parameter."));
            locationData.Add(new JProperty("postcodeevidenceproperties", new[] { "location.javascript" }));
            locationData.Add(new JProperty("javascript", "if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function() { // 51D replace this comment with callback function. }); }"));
            locationData.Add(new JProperty("javascriptdelayexecution", true));
            json["location"] = locationData;

            var flowData = new Mock <IFlowData>();

            Configure(flowData, json);

            IJavaScriptBuilderElementData result = null;

            flowData.Setup(d => d.GetOrAdd(
                               It.IsAny <ITypedKey <IJavaScriptBuilderElementData> >(),
                               It.IsAny <Func <IPipeline, IJavaScriptBuilderElementData> >()))
            .Returns <ITypedKey <IJavaScriptBuilderElementData>, Func <IPipeline, IJavaScriptBuilderElementData> >((k, f) =>
            {
                result = f(flowData.Object.Pipeline);
                return(result);
            });

            _javaScriptBuilderElement.Process(flowData.Object);

            if (minify == false)
            {
                Assert.IsTrue(result.JavaScript.Contains("getEvidencePropertiesFromObject"),
                              "Expected the generated JavaScript to contain the " +
                              "'getEvidencePropertiesFromObject' function but it does not.");
            }
            IJavaScriptExecutor js = _driver;

            // Attempt to evaluate the JavaScript.
            js.ExecuteScript($"{result.JavaScript}; window.fod = fod;");
            var jsObject = js.ExecuteScript("return fod.sessionId;");

            Assert.IsNotNull(jsObject);
        }
        public void JavaScriptBuilder_VerifyUrl()
        {
            _javaScriptBuilderElement =
                new JavaScriptBuilderElementBuilder(_loggerFactory)
                .SetEndpoint("/json")
                .Build();

            var flowData = new Mock <IFlowData>();

            flowData.Setup(d => d.Get <IJsonBuilderElementData>()).Returns(() =>
            {
                var d  = new JsonBuilderElementData(new Mock <ILogger <JsonBuilderElementData> >().Object, null);
                d.Json = @"{ ""test"": ""value"" }";
                return(d);
            });
            // Setup the TryGetEvidence methods that are used to get
            // host and protocol for the callback URL
            flowData.Setup(d => d.TryGetEvidence(Pipeline.JavaScriptBuilder.Constants.EVIDENCE_HOST_KEY, out It.Ref <string> .IsAny))
            .Callback(new GetValueCallback((string key, out string result) => { result = "localhost"; }));
            flowData.Setup(d => d.TryGetEvidence(Pipeline.JavaScriptBuilder.Constants.EVIDENCE_PROTOCOL, out It.Ref <string> .IsAny))
            .Callback(new GetValueCallback((string key, out string result) => { result = "https"; }));
            // Setup the evidence dictionary accessor.
            // This is used to get the query parameters to add to the
            // callback URL.
            flowData.Setup(d => d.GetEvidence().AsDictionary()).Returns(new Dictionary <string, object>()
            {
                { Pipeline.JavaScriptBuilder.Constants.EVIDENCE_HOST_KEY, "localhost" },
                { Pipeline.JavaScriptBuilder.Constants.EVIDENCE_PROTOCOL, "https" },
            });
            // Setup the GetOrAdd method to catch the data object that is
            // set by the element so we can check it's values.
            IJavaScriptBuilderElementData result = null;

            flowData.Setup(d => d.GetOrAdd(
                               It.IsAny <ITypedKey <IJavaScriptBuilderElementData> >(),
                               It.IsAny <Func <IPipeline, IJavaScriptBuilderElementData> >()))
            .Returns <ITypedKey <IJavaScriptBuilderElementData>, Func <IPipeline, IJavaScriptBuilderElementData> >((k, f) =>
            {
                result = f(flowData.Object.Pipeline);
                return(result);
            });

            _javaScriptBuilderElement.Process(flowData.Object);

            string expectedUrl = "https://localhost/json";

            Assert.IsTrue(result.JavaScript.Contains(expectedUrl),
                          $"JavaScript does not contain expected URL '{expectedUrl}'.");
        }
        public void JavaScriptBuilderElement_JavaScript(string key, string property, object value)
        {
            _javaScriptBuilderElement =
                new JavaScriptBuilderElementBuilder(_loggerFactory).Build();

            dynamic json = new JObject();

            if (value == null)
            {
                json[key] = new JObject(new JProperty(property, value), new JProperty(property + "nullreason", "No value set"));
                value     = "No value set";
            }
            else
            {
                json[key] = new JObject(new JProperty(property, value));
            }
            IJavaScriptBuilderElementData result = null;
            var flowData = new Mock <IFlowData>();

            flowData.Setup(d => d.Get <IJsonBuilderElementData>()).Returns(() =>
            {
                var d  = new JsonBuilderElementData(new Mock <ILogger <JsonBuilderElementData> >().Object, flowData.Object.Pipeline);
                d.Json = json.ToString();
                return(d);
            });

            flowData.Setup(d => d.GetAsString(It.IsAny <string>())).Returns("None");
            flowData.Setup(d => d.GetEvidence().AsDictionary()).Returns(new Dictionary <string, object>()
            {
                { Pipeline.JavaScriptBuilder.Constants.EVIDENCE_HOST_KEY, "localhost" },
                { Pipeline.JavaScriptBuilder.Constants.EVIDENCE_PROTOCOL, "https" },
            });
            flowData.Setup(d => d.Get(It.IsAny <string>())).Returns(_elementDataMock.Object);
            flowData.Setup(d => d.GetOrAdd(
                               It.IsAny <ITypedKey <IJavaScriptBuilderElementData> >(),
                               It.IsAny <Func <IPipeline, IJavaScriptBuilderElementData> >()))
            .Returns <ITypedKey <IJavaScriptBuilderElementData>, Func <IPipeline, IJavaScriptBuilderElementData> >((k, f) =>
            {
                result = f(flowData.Object.Pipeline);
                return(result);
            });
            _javaScriptBuilderElement.Process(flowData.Object);

            Assert.IsTrue(IsValidFodObject(result.JavaScript, key, property, value));
        }
Example #8
0
        public void JavaScriptBuilderElement_JavaScript(bool minify, string key, string property, object value)
        {
            _javaScriptBuilderElement =
                new JavaScriptBuilderElementBuilder(_loggerFactory)
                .SetMinify(minify)
                .Build();

            dynamic json = new JObject();

            if (value == null)
            {
                json[key] = new JObject(new JProperty(property, value), new JProperty(property + "nullreason", "No value set"));
                value     = "No value set";
            }
            else
            {
                json[key] = new JObject(new JProperty(property, value));
            }

            var flowData = new Mock <IFlowData>();

            Configure(flowData, json);

            IJavaScriptBuilderElementData result = null;

            flowData.Setup(d => d.GetOrAdd(
                               It.IsAny <ITypedKey <IJavaScriptBuilderElementData> >(),
                               It.IsAny <Func <IPipeline, IJavaScriptBuilderElementData> >()))
            .Returns <ITypedKey <IJavaScriptBuilderElementData>, Func <IPipeline, IJavaScriptBuilderElementData> >((k, f) =>
            {
                result = f(flowData.Object.Pipeline);
                return(result);
            });

            _javaScriptBuilderElement.Process(flowData.Object);

            Assert.IsTrue(IsValidFodObject(result.JavaScript, key, property, value));
        }
Example #9
0
        public void JavaScriptBuilderElement_Promise(ExceptionType exceptionThrownByPromiseProperty, bool exceptionExpected)
        {
            _javaScriptBuilderElement =
                new JavaScriptBuilderElementBuilder(_loggerFactory).Build();

            var flowData = new Mock <IFlowData>();

            Configure(flowData);

            IJavaScriptBuilderElementData result = null;

            flowData.Setup(d => d.GetOrAdd(
                               It.IsAny <ITypedKey <IJavaScriptBuilderElementData> >(),
                               It.IsAny <Func <IPipeline, IJavaScriptBuilderElementData> >()))
            .Returns <ITypedKey <IJavaScriptBuilderElementData>, Func <IPipeline, IJavaScriptBuilderElementData> >((k, f) =>
            {
                result = f(flowData.Object.Pipeline);
                return(result);
            });

            switch (exceptionThrownByPromiseProperty)
            {
            case ExceptionType.PropertyMissingException:
                flowData.Setup(d => d.GetAs <IAspectPropertyValue <string> >("Promise"))
                .Throws(new PropertyMissingException("Problem!"));
                break;

            case ExceptionType.PipelineDataException:
                flowData.Setup(d => d.GetAs <IAspectPropertyValue <string> >("Promise"))
                .Throws(new PipelineDataException("Problem!"));
                break;

            case ExceptionType.InvalidCastException:
                flowData.Setup(d => d.GetAs <IAspectPropertyValue <string> >("Promise"))
                .Throws(new InvalidCastException("Problem!"));
                break;

            case ExceptionType.KeyNotFoundException:
                flowData.Setup(d => d.GetAs <IAspectPropertyValue <string> >("Promise"))
                .Throws(new KeyNotFoundException("Problem!"));
                break;

            case ExceptionType.Exception:
                flowData.Setup(d => d.GetAs <IAspectPropertyValue <string> >("Promise"))
                .Throws(new Exception("Problem!"));
                break;

            case ExceptionType.None:
                flowData.Setup(d => d.GetAs <IAspectPropertyValue <string> >("Promise"))
                .Returns(new AspectPropertyValue <string>("Full"));
                break;

            default:
                break;
            }

            Exception thrown = null;

            try
            {
                _javaScriptBuilderElement.Process(flowData.Object);
            }
            catch (Exception ex)
            {
                thrown = ex;
            }

            if (exceptionExpected)
            {
                Assert.IsNotNull(thrown, "Expected an exception to be " +
                                 "visible externally but it was not.");
            }
            else
            {
                Assert.IsNull(thrown, "Did not expect an exception " +
                              "to be visible externally but one was.");
                Assert.IsNotNull(result.JavaScript, "Expected JavaScript " +
                                 "output to be populated but it was not.");
                Assert.AreNotEqual("", result.JavaScript, "Expected " +
                                   "JavaScript output to be populated but it was not.");
            }
        }