Skip to content

Commit

Permalink
Merged Pull Request '#40 feature/cookies->main : FEAT: The enable coo…
Browse files Browse the repository at this point in the history
…kies option for JavaScriptBuilderElement is now configurable per-request.'

FEAT: The enable cookies option for JavaScriptBuilderElement is now configurable per-request.
  • Loading branch information
Automation51D authored May 3, 2024
2 parents 222c734 + 9348a84 commit ec10275
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 22 deletions.
59 changes: 59 additions & 0 deletions fiftyone_pipeline_core/src/fiftyone_pipeline_core/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# *********************************************************************
# This Original Work is copyright of 51 Degrees Mobile Experts Limited.
# Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
# Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
#
# This Original Work is licensed under the European Union Public Licence
# (EUPL) v.1.2 and is subject to its terms as set out below.
#
# If a copy of the EUPL was not distributed with this file, You can obtain
# one at https://opensource.org/licenses/EUPL-1.2.
#
# The "Compatible Licences" set out in the Appendix to the EUPL (as may be
# amended by the European Commission) shall be deemed incompatible for
# the purposes of the Work and the provisions of the compatibility
# clause in Article 5 of the EUPL shall not apply.
#
# If using the Work as, or as part of, a network application, by
# including the attribution notice(s) required under Article 5 of the EUPL
# in the end user terms of the application under an appropriate heading,
# such notice(s) shall fulfill the requirements of that article.
# *********************************************************************

class Constants():

"""!
The string used to split evidence name parts
"""
EVIDENCE_SEPARATOR = "."

"""!
Used to prefix evidence that is obtained from HTTP headers
"""
EVIDENCE_HTTPHEADER_PREFIX = "header"

"""!
Used to prefix evidence that is obtained from HTTP bookies
"""
EVIDENCE_COOKIE_PREFIX = "cookie"

"""!
Used to prefix evidence that is obtained from an HTTP request"s
query string or is passed into the pipeline for off-line
processing.
"""
EVIDENCE_QUERY_PREFIX = "query"

"""!
The suffix used when the JavaScriptBuilderElement
"enable cookies" parameter is supplied as evidence.
"""
EVIDENCE_ENABLE_COOKIES_SUFFIX = "fod-js-enable-cookies"

"""!
The complete key to be used when the
JavaScriptBuilderElement "enable cookies"
parameter is supplied as part of the query
string.
"""
EVIDENCE_ENABLE_COOKIES = EVIDENCE_QUERY_PREFIX + EVIDENCE_SEPARATOR + EVIDENCE_ENABLE_COOKIES_SUFFIX
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .flowelement import FlowElement
from .evidence_keyfilter import EvidenceKeyFilter
from .elementdata_dictionary import ElementDataDictionary
from .constants import Constants


class JavaScriptBuilderEvidenceKeyFilter(EvidenceKeyFilter):
Expand Down Expand Up @@ -80,7 +81,11 @@ def __init__(self, settings = {} ):
* callback url. This can be overriden with header.host evidence.
* @param {string} options.endpoint The endpoint of the client side
* callback url
* @param {boolean} options.enable_cookies whether cookies should be enabled
* @param {boolean} options.enable_cookies Whether the client JavaScript
* stored results of client side processing in cookies. This can also
* be set per request, using the "query.fod-js-enable-cookies" evidence key.
* For more details on personal data policy,
* see http://51degrees.com/terms/client-services-privacy-policy/
* @param {boolean} options.minify Whether to minify the JavaScript
"""
Expand Down Expand Up @@ -166,6 +171,12 @@ def process_internal(self, flowdata):
variables["_host"] = host
variables["_protocol"] = protocol

enableCookiesVal = flowdata.evidence.get(Constants.EVIDENCE_ENABLE_COOKIES)
if enableCookiesVal:
variables["_enableCookies"] = enableCookiesVal.lower() == "true"

variables["_enableCookies"]

query_params = self.get_evidence_key_filter().filter_evidence(flowdata.evidence.get_all())
variables["_sessionId"] = query_params["query.session-id"] if "query.session-id" in query_params else None
variables["_sequence"] = query_params["query.sequence"] if "query.sequence" in query_params else None
Expand All @@ -187,7 +198,7 @@ def process_internal(self, flowdata):

for param, paramvalue in query_params.items():

paramkey = param.split(".")[1]
paramkey = param.split(".")[1]

query[paramkey] = paramvalue

Expand Down
99 changes: 79 additions & 20 deletions fiftyone_pipeline_core/tests/test_javascriptbuilder.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# *********************************************************************
# This Original Work is copyright of 51 Degrees Mobile Experts Limited.
# Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
# Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
#
# This Original Work is licensed under the European Union Public Licence
# (EUPL) v.1.2 and is subject to its terms as set out below.
#
# If a copy of the EUPL was not distributed with this file, You can obtain
# one at https://opensource.org/licenses/EUPL-1.2.
#
# The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
# amended by the European Commission) shall be deemed incompatible for
# the purposes of the Work and the provisions of the compatibility
# clause in Article 5 of the EUPL shall not apply.
#
# If using the Work as, or as part of, a network application, by
# including the attribution notice(s) required under Article 5 of the EUPL
# in the end user terms of the application under an appropriate heading,
# such notice(s) shall fulfill the requirements of that article.
# *********************************************************************
# This Original Work is copyright of 51 Degrees Mobile Experts Limited.
# Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
# Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
#
# This Original Work is licensed under the European Union Public Licence
# (EUPL) v.1.2 and is subject to its terms as set out below.
#
# If a copy of the EUPL was not distributed with this file, You can obtain
# one at https://opensource.org/licenses/EUPL-1.2.
#
# The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
# amended by the European Commission) shall be deemed incompatible for
# the purposes of the Work and the provisions of the compatibility
# clause in Article 5 of the EUPL shall not apply.
#
# If using the Work as, or as part of, a network application, by
# including the attribution notice(s) required under Article 5 of the EUPL
# in the end user terms of the application under an appropriate heading,
# such notice(s) shall fulfill the requirements of that article.
# *********************************************************************

import unittest
Expand All @@ -26,6 +26,8 @@
from fiftyone_pipeline_core.pipelinebuilder import PipelineBuilder
from fiftyone_pipeline_core.elementdata_dictionary import ElementDataDictionary
from fiftyone_pipeline_core.aspectproperty_value import AspectPropertyValue
from fiftyone_pipeline_core.constants import Constants
from parameterized import parameterized


class TestEngine(FlowElement):
Expand Down Expand Up @@ -82,6 +84,30 @@ def __init__(self, minify = None):
.add(TestEngine())\
.build()

class CookieEngine(FlowElement):

def __init__(self):

super(CookieEngine, self).__init__()

self.datakey = "cookie"

self.properties = {
"javascript" : {
"type" : "javascript"
}
}


def process_internal(self, flowdata):

contents = {}

contents["javascript"] = "document.cookie = \"some cookie value\""

data = ElementDataDictionary(self, contents)

flowdata.set_element_data(data)

class DelayedExecutionEngine1(FlowElement):

Expand Down Expand Up @@ -312,3 +338,36 @@ def test_jsonbundler_when_delayed_execution_multiple(self):

actual = flowdata.jsonbundler.json["delayedexecutiontest3"]
self.assertEqual(actual, expected)

"""!
Test various configurations for enabling cookies to verify
that cookies are/aren't written for each configuration.
The source JavaScript contains code to set a cookie. The JSBuilder
element should replace this if the config says that cookies are not
enabled.
"""
@parameterized.expand([
[False, False, False],
[True, False, False],
[False, True, True],
[True, True, True]
])
def testJavaScriptBuilder_Cookies(self, enableInConfig, enableInEvidence, expectCookie):

# Generate minified javascript

jsSettings = {'enable_cookies' : enableInConfig}
pipelineSettings = {'javascript_builder_settings' : jsSettings}
Pipeline = PipelineBuilder(pipelineSettings)\
.add(CookieEngine())\
.build()
FlowData = Pipeline.create_flowdata()
FlowData.evidence.add(Constants.EVIDENCE_ENABLE_COOKIES, str(enableInEvidence))
FlowData.process()
js = FlowData.javascriptbuilder.javascript

if expectCookie:
self.assertEqual(2, js.count("document.cookie"))
else:
self.assertEqual(1, js.count("document.cookie"))

0 comments on commit ec10275

Please sign in to comment.