1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
"""
Tests for the SnapEngage template tags and filters.
"""
import pytest
from django.contrib.auth.models import AnonymousUser, User
from django.template import Context
from django.test.utils import override_settings
from django.utils import translation
from utils import TagTestCase
from analytical.templatetags.snapengage import (
BUTTON_LOCATION_BOTTOM,
BUTTON_LOCATION_LEFT,
BUTTON_LOCATION_RIGHT,
BUTTON_LOCATION_TOP,
BUTTON_STYLE_DEFAULT,
BUTTON_STYLE_LIVE,
BUTTON_STYLE_NONE,
FORM_POSITION_TOP_LEFT,
SnapEngageNode,
)
from analytical.utils import AnalyticalException
WIDGET_ID = 'ec329c69-0bf0-4db8-9b77-3f8150fb977e'
@override_settings(
SNAPENGAGE_WIDGET_ID=WIDGET_ID,
SNAPENGAGE_BUTTON=BUTTON_STYLE_DEFAULT,
SNAPENGAGE_BUTTON_LOCATION=BUTTON_LOCATION_LEFT,
SNAPENGAGE_BUTTON_OFFSET='55%',
)
class SnapEngageTestCase(TagTestCase):
"""
Tests for the ``snapengage`` template tag.
"""
def test_tag(self):
r = self.render_tag('snapengage', 'snapengage')
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","0","55%");' in r
)
def test_node(self):
r = SnapEngageNode().render(Context())
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","0","55%");' in r
)
@override_settings(SNAPENGAGE_WIDGET_ID=None)
def test_no_site_id(self):
with pytest.raises(AnalyticalException):
SnapEngageNode()
@override_settings(SNAPENGAGE_WIDGET_ID='abc')
def test_wrong_site_id(self):
with pytest.raises(AnalyticalException):
SnapEngageNode()
def test_no_button(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_button': BUTTON_STYLE_NONE,
}
)
)
assert 'SnapABug.init("ec329c69-0bf0-4db8-9b77-3f8150fb977e")' in r
with override_settings(SNAPENGAGE_BUTTON=BUTTON_STYLE_NONE):
r = SnapEngageNode().render(Context())
assert 'SnapABug.init("ec329c69-0bf0-4db8-9b77-3f8150fb977e")' in r
def test_live_button(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_button': BUTTON_STYLE_LIVE,
}
)
)
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","0","55%",true);'
in r
)
with override_settings(SNAPENGAGE_BUTTON=BUTTON_STYLE_LIVE):
r = SnapEngageNode().render(Context())
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","0","55%",true);'
in r
)
def test_custom_button(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_button': 'http://www.example.com/button.png',
}
)
)
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","0","55%");' in r
)
assert 'SnapABug.setButton("http://www.example.com/button.png");' in r
with override_settings(SNAPENGAGE_BUTTON='http://www.example.com/button.png'):
r = SnapEngageNode().render(Context())
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","0","55%");'
in r
)
assert 'SnapABug.setButton("http://www.example.com/button.png");' in r
def test_button_location_right(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_button_location': BUTTON_LOCATION_RIGHT,
}
)
)
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","1","55%");' in r
)
with override_settings(SNAPENGAGE_BUTTON_LOCATION=BUTTON_LOCATION_RIGHT):
r = SnapEngageNode().render(Context())
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","1","55%");'
in r
)
def test_button_location_top(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_button_location': BUTTON_LOCATION_TOP,
}
)
)
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","2","55%");' in r
)
with override_settings(SNAPENGAGE_BUTTON_LOCATION=BUTTON_LOCATION_TOP):
r = SnapEngageNode().render(Context())
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","2","55%");'
in r
)
def test_button_location_bottom(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_button_location': BUTTON_LOCATION_BOTTOM,
}
)
)
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","3","55%");' in r
)
with override_settings(SNAPENGAGE_BUTTON_LOCATION=BUTTON_LOCATION_BOTTOM):
r = SnapEngageNode().render(Context())
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","3","55%");'
in r
)
def test_button_offset(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_button_location_offset': '30%',
}
)
)
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","0","30%");' in r
)
with override_settings(SNAPENGAGE_BUTTON_LOCATION_OFFSET='30%'):
r = SnapEngageNode().render(Context())
assert (
'SnapABug.addButton("ec329c69-0bf0-4db8-9b77-3f8150fb977e","0","30%");'
in r
)
def test_button_effect(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_button_effect': '-4px',
}
)
)
assert 'SnapABug.setButtonEffect("-4px");' in r
with override_settings(SNAPENGAGE_BUTTON_EFFECT='-4px'):
r = SnapEngageNode().render(Context())
assert 'SnapABug.setButtonEffect("-4px");' in r
def test_form_position(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_form_position': FORM_POSITION_TOP_LEFT,
}
)
)
assert 'SnapABug.setChatFormPosition("tl");' in r
with override_settings(SNAPENGAGE_FORM_POSITION=FORM_POSITION_TOP_LEFT):
r = SnapEngageNode().render(Context())
assert 'SnapABug.setChatFormPosition("tl");' in r
def test_form_top_position(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_form_top_position': 40,
}
)
)
assert 'SnapABug.setFormTopPosition(40);' in r
with override_settings(SNAPENGAGE_FORM_TOP_POSITION=40):
r = SnapEngageNode().render(Context())
assert 'SnapABug.setFormTopPosition(40);' in r
def test_domain(self):
r = SnapEngageNode().render(Context({'snapengage_domain': 'example.com'}))
assert 'SnapABug.setDomain("example.com");' in r
with override_settings(SNAPENGAGE_DOMAIN='example.com'):
r = SnapEngageNode().render(Context())
assert 'SnapABug.setDomain("example.com");' in r
def test_secure_connection(self):
r = SnapEngageNode().render(Context({'snapengage_secure_connection': True}))
assert 'SnapABug.setSecureConnexion();' in r
with override_settings(SNAPENGAGE_SECURE_CONNECTION=True):
r = SnapEngageNode().render(Context())
assert 'SnapABug.setSecureConnexion();' in r
def test_show_offline(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_show_offline': False,
}
)
)
assert 'SnapABug.allowOffline(false);' in r
with override_settings(SNAPENGAGE_SHOW_OFFLINE=False):
r = SnapEngageNode().render(Context())
assert 'SnapABug.allowOffline(false);' in r
def test_proactive_chat(self):
r = SnapEngageNode().render(Context({'snapengage_proactive_chat': False}))
assert 'SnapABug.allowProactiveChat(false);' in r
def test_screenshot(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_screenshots': False,
}
)
)
assert 'SnapABug.allowScreenshot(false);' in r
with override_settings(SNAPENGAGE_SCREENSHOTS=False):
r = SnapEngageNode().render(Context())
assert 'SnapABug.allowScreenshot(false);' in r
def test_offline_screenshots(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_offline_screenshots': False,
}
)
)
assert 'SnapABug.showScreenshotOption(false);' in r
with override_settings(SNAPENGAGE_OFFLINE_SCREENSHOTS=False):
r = SnapEngageNode().render(Context())
assert 'SnapABug.showScreenshotOption(false);' in r
def test_sounds(self):
r = SnapEngageNode().render(Context({'snapengage_sounds': False}))
assert 'SnapABug.allowChatSound(false);' in r
with override_settings(SNAPENGAGE_SOUNDS=False):
r = SnapEngageNode().render(Context())
assert 'SnapABug.allowChatSound(false);' in r
@override_settings(SNAPENGAGE_READONLY_EMAIL=False)
def test_email(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_email': 'test@example.com',
}
)
)
assert 'SnapABug.setUserEmail("test@example.com");' in r
def test_email_readonly(self):
r = SnapEngageNode().render(
Context(
{
'snapengage_email': 'test@example.com',
'snapengage_readonly_email': True,
}
)
)
assert 'SnapABug.setUserEmail("test@example.com",true);' in r
with override_settings(SNAPENGAGE_READONLY_EMAIL=True):
r = SnapEngageNode().render(
Context(
{
'snapengage_email': 'test@example.com',
}
)
)
assert 'SnapABug.setUserEmail("test@example.com",true);' in r
@override_settings(ANALYTICAL_AUTO_IDENTIFY=True)
def test_identify(self):
r = SnapEngageNode().render(
Context(
{
'user': User(username='test', email='test@example.com'),
}
)
)
assert 'SnapABug.setUserEmail("test@example.com");' in r
@override_settings(ANALYTICAL_AUTO_IDENTIFY=True)
def test_identify_anonymous_user(self):
r = SnapEngageNode().render(
Context(
{
'user': AnonymousUser(),
}
)
)
assert 'SnapABug.setUserEmail(' not in r
def test_language(self):
r = SnapEngageNode().render(Context({'snapengage_locale': 'fr'}))
assert 'SnapABug.setLocale("fr");' in r
with override_settings(SNAPENGAGE_LOCALE='fr'):
r = SnapEngageNode().render(Context())
assert 'SnapABug.setLocale("fr");' in r
def test_automatic_language(self):
real_get_language = translation.get_language
try:
translation.get_language = lambda: 'fr-ca'
r = SnapEngageNode().render(Context())
assert 'SnapABug.setLocale("fr_CA");' in r
finally:
translation.get_language = real_get_language
|