|
2 | 2 |
|
3 | 3 | from __future__ import absolute_import, unicode_literals
|
4 | 4 |
|
| 5 | +import calendar |
5 | 6 | import os
|
| 7 | +from datetime import timedelta |
6 | 8 |
|
7 | 9 | try:
|
8 | 10 | from selenium import webdriver
|
|
16 | 18 |
|
17 | 19 | from django.test import LiveServerTestCase
|
18 | 20 | from django.test.utils import override_settings
|
| 21 | +from django.utils import timezone |
19 | 22 | from django.utils.unittest import skipIf, skipUnless
|
20 | 23 |
|
21 | 24 | from debug_toolbar.settings import PANELS_DEFAULTS
|
@@ -46,10 +49,11 @@ def setUp(self):
|
46 | 49 | "#djDebugPanelList li a.{}".format(self.panel_class)
|
47 | 50 | )
|
48 | 51 | self.panel = self.selenium.find_element_by_id(self.panel_class)
|
| 52 | + WebDriverWait(self.selenium, timeout=10).until( |
| 53 | + lambda selenium: self.panel_trigger.is_displayed()) |
49 | 54 |
|
50 | 55 | def tearDown(self):
|
51 | 56 | self.selenium.delete_all_cookies()
|
52 |
| - self.selenium.refresh() |
53 | 57 |
|
54 | 58 | def test_show_toolbar(self):
|
55 | 59 | toolbar = self.selenium.find_element_by_id('djDebug')
|
@@ -278,10 +282,16 @@ def tearDownClass(cls):
|
278 | 282 |
|
279 | 283 | def setUp(self):
|
280 | 284 | self.selenium.get(self.live_server_url + '/execute_sql/')
|
| 285 | + self.panel_class = "HeadersPanel" |
| 286 | + self.panel_trigger = self.selenium.find_element( |
| 287 | + By.CSS_SELECTOR, |
| 288 | + "#djDebugPanelList li a.{}".format(self.panel_class) |
| 289 | + ) |
| 290 | + WebDriverWait(self.selenium, timeout=10).until( |
| 291 | + lambda selenium: self.panel_trigger.is_displayed()) |
281 | 292 |
|
282 | 293 | def tearDown(self):
|
283 | 294 | self.selenium.delete_all_cookies()
|
284 |
| - self.selenium.refresh() |
285 | 295 |
|
286 | 296 | def test_show_toolbar(self):
|
287 | 297 | self.selenium.execute_script("djdt.close()")
|
@@ -336,3 +346,57 @@ def test_hide_toolbar(self):
|
336 | 346 | cookie = self.selenium.get_cookie('djdt')
|
337 | 347 | self.assertEquals(cookie['name'], 'djdt')
|
338 | 348 | self.assertEquals(cookie['value'], 'hide')
|
| 349 | + |
| 350 | + def test_cookie_get_none(self): |
| 351 | + null_cookie = self.selenium.execute_script( |
| 352 | + "return djdt.cookie.get('test')" |
| 353 | + ) |
| 354 | + self.assertIsNone(null_cookie) |
| 355 | + |
| 356 | + def test_cookie_get(self): |
| 357 | + key = str("test") |
| 358 | + value = str("val") |
| 359 | + path = str("/") |
| 360 | + expires = calendar.timegm( |
| 361 | + (timezone.now() + timedelta(days=10)).timetuple() |
| 362 | + ) |
| 363 | + domain = str("localhost") |
| 364 | + self.selenium.add_cookie({ |
| 365 | + "name": key, |
| 366 | + "value": value, |
| 367 | + "expiry": expires, |
| 368 | + "path": path, |
| 369 | + "domain": domain, |
| 370 | + }) |
| 371 | + actual_value = self.selenium.execute_script( |
| 372 | + "return djdt.cookie.get('{}')".format(key) |
| 373 | + ) |
| 374 | + self.assertEquals(actual_value, value) |
| 375 | + |
| 376 | + def test_cookie_set(self): |
| 377 | + key = str("test") |
| 378 | + value = str("val") |
| 379 | + path = str("/") |
| 380 | + expires = 3 |
| 381 | + domain = str("localhost") |
| 382 | + expires_lower_bound = timezone.now().date() + timedelta(days=expires) |
| 383 | + expires_upper_bound = expires_lower_bound + timedelta(days=1) |
| 384 | + self.selenium.execute_script( |
| 385 | + "djdt.cookie.set('{}','{}',{{'path':'{}','expires':{},'domain':'{}'}})" |
| 386 | + .format(key, value, path, expires, domain) |
| 387 | + ) |
| 388 | + cookie = self.selenium.get_cookie(key) |
| 389 | + self.assertEquals(cookie['name'], key) |
| 390 | + self.assertEquals(cookie['value'], value) |
| 391 | + self.assertEquals(cookie['path'], path) |
| 392 | + self.assertEquals(cookie['domain'], domain) |
| 393 | + # Verify the expiration date is close to the value we passed in. |
| 394 | + # The method calculates the current time, so it's difficult to compare. |
| 395 | + self.assertGreaterEqual( |
| 396 | + cookie['expiry'], |
| 397 | + calendar.timegm(expires_lower_bound.timetuple()) |
| 398 | + ) |
| 399 | + self.assertLessEqual( |
| 400 | + cookie['expiry'], |
| 401 | + calendar.timegm(expires_upper_bound.timetuple()) |
| 402 | + ) |
0 commit comments