def test_tag_count(): tag1 = TagFactory(name="tag1") tag2 = TagFactory(name="tag2") trigger = TriggerFactory() trigger.tags.add(tag1, tag2) trigger_model_admin = TriggerModelAdmin() result = trigger_model_admin.tags_count(trigger) assert result == "2 tag(s)"
def test_passive_view(client, site): response = client.get(site.root_page.url) assert response.status_code == 200 tag_functional = TagFactory( name="functional lazy", active=False, tag_loading=Tag.INSTANT_LOAD, content='<script>console.log("{{ state }}")</script>', ) tag_analytical = TagFactory( name="analytical lazy", active=False, tag_loading=Tag.INSTANT_LOAD, tag_type="analytical", content='<script>console.log("{{ state }}")</script>', ) tag_traceable = TagFactory( name="traceable lazy", active=False, tag_loading=Tag.INSTANT_LOAD, tag_type="traceable", content='<script>console.log("{{ state }}")</script>', ) assert tag_functional in Tag.objects.passive().sorted() assert tag_analytical in Tag.objects.passive().sorted() assert tag_traceable in Tag.objects.passive().sorted() trigger = TriggerFactory(pattern="[?&]state=(?P<state>\S+)") trigger.tags.add(tag_functional) trigger.tags.add(tag_analytical) trigger.tags.add(tag_traceable) client.cookies = SimpleCookie({"wtm_functional": "true"}) response = client.get("/?state=1") assert response.status_code == 200 assert b'console.log("1")' in response.content client.cookies = SimpleCookie({"wtm_analytical": "true"}) response = client.get("/?state=2") assert response.status_code == 200 assert b'console.log("2")' in response.content client.cookies = SimpleCookie({"wtm_traceable": "true"}) response = client.get("/?state=3") assert response.status_code == 200 assert b'console.log("3")' in response.content
def test_tag_create(): produced_tag = TagFactory() tag = Tag( name="functional instant", content='<script>console.log("functional instant")</script>', ) assert produced_tag.name == tag.name assert produced_tag.tag_type == tag.tag_type assert produced_tag.content == get_expected_content(tag.name)
def test_no_description_display(): constant = ConstantFactory() constant_model_admin = ConstantModelAdmin() description = constant_model_admin.name_display(constant) assert constant.name == description variable = VariableFactory(key="var") variable_model_admin = VariableModelAdmin() description = variable_model_admin.name_display(variable) assert variable.name == description tag = TagFactory() tag_model_admin = TagModelAdmin() description = tag_model_admin.name_display(tag) assert tag.name == description trigger = TriggerFactory() trigger_model_admin = TriggerModelAdmin() description = trigger_model_admin.name_display(trigger) assert trigger.name == description
def test_page_tags(client, site): response = client.get(site.root_page.url) assert response.status_code == 200 tag_necessary = TagFactory( name="necessary instant", auto_load=False, tag_loading=Tag.INSTANT_LOAD, content='<script>console.log("necessary")</script>', ) tag_preferences = TagFactory( name="preferences instant", auto_load=False, tag_loading=Tag.INSTANT_LOAD, tag_type="preferences", content='<script>console.log("preferences")</script>', ) tag_statistics = TagFactory( name="statistics instant", auto_load=False, tag_loading=Tag.INSTANT_LOAD, tag_type="statistics", content='<script>console.log("statistics")</script>', ) tag_marketing = TagFactory( name="marketing instant", auto_load=False, tag_loading=Tag.INSTANT_LOAD, tag_type="marketing", content='<script>console.log("marketing")</script>', ) assert tag_necessary in Tag.objects.passive().sorted() assert tag_preferences in Tag.objects.passive().sorted() assert tag_statistics in Tag.objects.passive().sorted() assert tag_marketing in Tag.objects.passive().sorted() page = TaggableContentPageFactory(parent=site.root_page, slug="tagged-page") page.wtm_tags.add(tag_necessary) page.wtm_tags.add(tag_preferences) page.wtm_tags.add(tag_statistics) page.wtm_tags.add(tag_marketing) page.save() client.cookies = SimpleCookie({"wtm": "necessary:true"}) response = client.get(page.get_url()) assert response.status_code == 200 assert b'console.log("necessary")' in response.content client.cookies = SimpleCookie({"wtm": "preferences:true"}) response = client.get(page.get_url()) assert response.status_code == 200 assert b'console.log("preferences")' in response.content client.cookies = SimpleCookie({"wtm": "statistics:true"}) response = client.get(page.get_url()) assert response.status_code == 200 assert b'console.log("statistics")' in response.content client.cookies = SimpleCookie({"wtm": "marketing:true"}) response = client.get(page.get_url()) assert response.status_code == 200 assert b'console.log("marketing")' in response.content
def test_page_tags(client, site): tag_necessary = TagFactory( name="necessary lazy", auto_load=False, tag_loading=Tag.LAZY_LOAD, content='<script>console.log("necessary")</script>', ) tag_preferences = TagFactory( name="preferences lazy", auto_load=False, tag_loading=Tag.LAZY_LOAD, tag_type="preferences", content='<script>console.log("preferences")</script>', ) tag_statistics = TagFactory( name="statistics lazy", auto_load=False, tag_loading=Tag.LAZY_LOAD, tag_type="statistics", content='<script>console.log("statistics")</script>', ) tag_marketing = TagFactory( name="marketing lazy", auto_load=False, tag_loading=Tag.LAZY_LOAD, tag_type="marketing", content='<script>console.log("marketing")</script>', ) assert tag_necessary in Tag.objects.passive().sorted() assert tag_preferences in Tag.objects.passive().sorted() assert tag_statistics in Tag.objects.passive().sorted() assert tag_marketing in Tag.objects.passive().sorted() page = TaggableContentPageFactory(parent=site.root_page, slug="tagged-page") page.wtm_tags.add(tag_necessary) page.wtm_tags.add(tag_preferences) page.wtm_tags.add(tag_statistics) page.wtm_tags.add(tag_marketing) page.save() assert len(page.wtm_tags.all()) == 4 response = client.post( "/wtm/lazy/", json.dumps({"pathname": page.get_url()}), content_type="application/json", ) data = response.json() assert response.status_code == 200 assert "tags" in data results = ['console.log("necessary")', 'console.log("preferences")'] for tag in data.get("tags", []): assert tag["string"].strip() in results client.cookies = SimpleCookie({"wtm": "preferences:true"}) response = client.post( "/wtm/lazy/", json.dumps({"pathname": page.get_url()}), content_type="application/json", ) data = response.json() assert response.status_code == 200 assert "tags" in data results = ['console.log("necessary")', 'console.log("preferences")'] for tag in data.get("tags", []): assert tag["string"].strip() in results client.cookies = SimpleCookie({"wtm": "preferences:false|statistics:true"}) response = client.post( "/wtm/lazy/", json.dumps({"pathname": page.get_url()}), content_type="application/json", ) data = response.json() assert response.status_code == 200 assert "tags" in data results = ['console.log("necessary")', 'console.log("statistics")'] for tag in data.get("tags", []): assert tag["string"].strip() in results client.cookies = SimpleCookie({"wtm": "preferences:false|marketing:true"}) response = client.post( "/wtm/lazy/", json.dumps({"pathname": page.get_url()}), content_type="application/json", ) data = response.json() assert response.status_code == 200 assert "tags" in data results = ['console.log("necessary")', 'console.log("marketing")'] for tag in data.get("tags", []): assert tag["string"].strip() in results
def test_passive_tags(client, site): tag_necessary = TagFactory( name="necessary lazy", auto_load=False, tag_loading=Tag.LAZY_LOAD, content= '<script>console.log("necessary: {{ trigger_value }}")</script>', ) tag_preferences = TagFactory( name="preferences lazy", auto_load=False, tag_loading=Tag.LAZY_LOAD, tag_type="preferences", content= '<script>console.log("preferences: {{ trigger_value }}")</script>', ) tag_statistics = TagFactory( name="statistics lazy", auto_load=False, tag_loading=Tag.LAZY_LOAD, tag_type="statistics", content= '<script>console.log("statistics: {{ trigger_value }}")</script>', ) tag_marketing = TagFactory( name="marketing lazy", auto_load=False, tag_loading=Tag.LAZY_LOAD, tag_type="marketing", content= '<script>console.log("marketing: {{ trigger_value }}")</script>', ) assert tag_necessary in Tag.objects.passive().sorted() assert tag_preferences in Tag.objects.passive().sorted() assert tag_statistics in Tag.objects.passive().sorted() assert tag_marketing in Tag.objects.passive().sorted() trigger = TriggerFactory() trigger.tags.add(tag_necessary) trigger.tags.add(tag_preferences) trigger.tags.add(tag_statistics) trigger.tags.add(tag_marketing) response = client.post( "/wtm/lazy/", json.dumps({ "pathname": "/", "search": "" }), content_type="application/json", ) data = response.json() assert response.status_code == 200 assert "tags" in data assert len(data["tags"]) == 0 response = client.post( "/wtm/lazy/", json.dumps({ "pathname": "/", "search": "", "trigger": { "slug": "trigger", "type": Trigger.TYPE_FORM_SUBMIT, "value": "1", }, }), content_type="application/json", ) data = response.json() assert response.status_code == 200 assert "tags" in data assert 'console.log("necessary: 1")' in data["tags"][0]["string"] client.cookies = SimpleCookie({"wtm": "preferences:true"}) response = client.post( "/wtm/lazy/", json.dumps({ "pathname": "/", "search": "", "trigger": { "slug": "trigger", "type": Trigger.TYPE_FORM_SUBMIT, "value": "2", }, }), content_type="application/json", ) data = response.json() assert response.status_code == 200 assert "tags" in data assert 'console.log("preferences: 2")' in data["tags"][1]["string"] client.cookies = SimpleCookie({"wtm": "preferences:false|statistics:true"}) response = client.post( "/wtm/lazy/", json.dumps({ "pathname": "/", "search": "", "trigger": { "slug": "trigger", "type": Trigger.TYPE_FORM_SUBMIT, "value": "3", }, }), content_type="application/json", ) data = response.json() assert response.status_code == 200 assert "tags" in data assert 'console.log("statistics: 3")' in data["tags"][1]["string"] client.cookies = SimpleCookie({"wtm": "preferences:false|marketing:true"}) response = client.post( "/wtm/lazy/", json.dumps({ "pathname": "/", "search": "", "trigger": { "slug": "trigger", "type": Trigger.TYPE_FORM_SUBMIT, "value": "4", }, }), content_type="application/json", ) data = response.json() assert response.status_code == 200 assert "tags" in data assert 'console.log("marketing: 4")' in data["tags"][1]["string"]
def test_passive_tags(client, site): tag_functional = TagFactory( name="functional lazy", active=False, tag_loading=Tag.LAZY_LOAD, content='<script>console.log("{{ state }}")</script>', ) tag_analytical = TagFactory( name="analytical lazy", active=False, tag_loading=Tag.LAZY_LOAD, tag_type="analytical", content='<script>console.log("{{ state }}")</script>', ) tag_traceable = TagFactory( name="traceable lazy", active=False, tag_loading=Tag.LAZY_LOAD, tag_type="traceable", content='<script>console.log("{{ state }}")</script>', ) assert tag_functional in Tag.objects.passive().sorted() assert tag_analytical in Tag.objects.passive().sorted() assert tag_traceable in Tag.objects.passive().sorted() trigger = TriggerFactory(pattern="[?&]state=(?P<state>\S+)") trigger.tags.add(tag_functional) trigger.tags.add(tag_analytical) trigger.tags.add(tag_traceable) response = client.post( "/wtm/lazy/", json.dumps({"pathname": "/", "search": ""}), content_type="application/json", ) data = response.json() assert response.status_code == 200 assert "tags" in data assert len(data["tags"]) == 0 response = client.post( "/wtm/lazy/", json.dumps({"pathname": "/", "search": "?state=1"}), content_type="application/json", ) data = response.json() assert response.status_code == 200 assert "tags" in data assert len(data["tags"]) == 1 assert 'console.log("1")' in data["tags"][0]["string"] client.cookies = SimpleCookie({"wtm_analytical": "unset"}) response = client.post( "/wtm/lazy/", json.dumps({"pathname": "/", "search": "?state=2"}), content_type="application/json", ) data = response.json() assert response.status_code == 200 assert "tags" in data assert len(data["tags"]) == 2 assert 'console.log("2")' in data["tags"][1]["string"] response = client.post( "/wtm/lazy/", json.dumps({"consent": "true", "pathname": "/", "search": "?state=3"}), content_type="application/json", ) data = response.json() assert response.status_code == 200 assert "tags" in data assert len(data["tags"]) == 3 assert 'console.log("3")' in data["tags"][2]["string"]
def test_page_tags(client, site): response = client.get(site.root_page.url) assert response.status_code == 200 tag_functional = TagFactory( name="functional instant", auto_load=False, tag_loading=Tag.INSTANT_LOAD, content='<script>console.log("functional")</script>', ) tag_analytical = TagFactory( name="analytical instant", auto_load=False, tag_loading=Tag.INSTANT_LOAD, tag_type="analytical", content='<script>console.log("analytical")</script>', ) tag_delayed = TagFactory( name="delayed instant", auto_load=False, tag_loading=Tag.INSTANT_LOAD, tag_type="delayed", content='<script>console.log("delayed")</script>', ) tag_traceable = TagFactory( name="traceable instant", auto_load=False, tag_loading=Tag.INSTANT_LOAD, tag_type="traceable", content='<script>console.log("traceable")</script>', ) assert tag_functional in Tag.objects.passive().sorted() assert tag_analytical in Tag.objects.passive().sorted() assert tag_delayed in Tag.objects.passive().sorted() assert tag_traceable in Tag.objects.passive().sorted() page = TaggableContentPageFactory(parent=site.root_page, slug="tagged-page") page.wtm_tags.add(tag_functional) page.wtm_tags.add(tag_analytical) page.wtm_tags.add(tag_delayed) page.wtm_tags.add(tag_traceable) page.save() client.cookies = SimpleCookie({"wtm": "functional:true"}) response = client.get(page.get_url()) assert response.status_code == 200 assert b'console.log("functional")' in response.content client.cookies = SimpleCookie({"wtm": "analytical:true"}) response = client.get(page.get_url()) assert response.status_code == 200 assert b'console.log("analytical")' in response.content client.cookies = SimpleCookie({"wtm": "delayed:true"}) response = client.get(page.get_url()) assert response.status_code == 200 assert b'console.log("delayed")' in response.content client.cookies = SimpleCookie({"wtm": "traceable:true"}) response = client.get(page.get_url()) assert response.status_code == 200 assert b'console.log("traceable")' in response.content