sync from local backup
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import json
|
||||
from django.test import TestCase
|
||||
from django.core.cache import caches
|
||||
from utils.slider_captcha import (
|
||||
generate_slider_captcha,
|
||||
verify_slider_captcha,
|
||||
SliderCaptchaError
|
||||
)
|
||||
|
||||
default_cache = caches['default']
|
||||
|
||||
|
||||
class TestGenerateSliderCaptcha(TestCase):
|
||||
def test_generate_returns_required_keys(self):
|
||||
result = generate_slider_captcha()
|
||||
self.assertIn('captcha_key', result)
|
||||
self.assertIn('bg_image', result)
|
||||
self.assertIn('slider_image', result)
|
||||
self.assertIn('y_position', result)
|
||||
|
||||
def test_generate_returns_valid_captcha_key(self):
|
||||
result = generate_slider_captcha()
|
||||
captcha_key = result['captcha_key']
|
||||
self.assertIsInstance(captcha_key, str)
|
||||
self.assertEqual(len(captcha_key), 36)
|
||||
|
||||
def test_generate_returns_base64_images(self):
|
||||
result = generate_slider_captcha()
|
||||
self.assertTrue(result['bg_image'].startswith('data:image/png;base64,'))
|
||||
self.assertTrue(result['slider_image'].startswith('data:image/png;base64,'))
|
||||
|
||||
def test_generate_stores_position_in_cache(self):
|
||||
result = generate_slider_captcha()
|
||||
captcha_key = result['captcha_key']
|
||||
cached_data = default_cache.get(f'slider_captcha_{captcha_key}')
|
||||
self.assertIsNotNone(cached_data)
|
||||
self.assertIn('x_position', cached_data)
|
||||
self.assertIn('y_position', cached_data)
|
||||
self.assertIn('timestamp', cached_data)
|
||||
|
||||
|
||||
class TestVerifySliderCaptcha(TestCase):
|
||||
def setUp(self):
|
||||
self.captcha_data = generate_slider_captcha()
|
||||
self.captcha_key = self.captcha_data['captcha_key']
|
||||
cached = default_cache.get(f'slider_captcha_{self.captcha_key}')
|
||||
self.correct_x = cached['x_position']
|
||||
|
||||
def test_verify_correct_position(self):
|
||||
result = verify_slider_captcha(self.captcha_key, self.correct_x)
|
||||
self.assertTrue(result)
|
||||
|
||||
def test_verify_wrong_position(self):
|
||||
result = verify_slider_captcha(self.captcha_key, self.correct_x + 50)
|
||||
self.assertFalse(result)
|
||||
|
||||
def test_verify_expired_captcha(self):
|
||||
default_cache.delete(f'slider_captcha_{self.captcha_key}')
|
||||
with self.assertRaises(SliderCaptchaError):
|
||||
verify_slider_captcha(self.captcha_key, self.correct_x)
|
||||
|
||||
def test_verify_tolerance(self):
|
||||
result = verify_slider_captcha(self.captcha_key, self.correct_x + 3)
|
||||
self.assertTrue(result)
|
||||
|
||||
def test_verify_clears_cache_after_success(self):
|
||||
verify_slider_captcha(self.captcha_key, self.correct_x)
|
||||
cached = default_cache.get(f'slider_captcha_{self.captcha_key}')
|
||||
self.assertIsNone(cached)
|
||||
|
||||
|
||||
class TestVerifySliderTrajectory(TestCase):
|
||||
def setUp(self):
|
||||
self.captcha_data = generate_slider_captcha()
|
||||
self.captcha_key = self.captcha_data['captcha_key']
|
||||
cached = default_cache.get(f'slider_captcha_{self.captcha_key}')
|
||||
self.correct_x = cached['x_position']
|
||||
|
||||
def test_verify_with_valid_trajectory(self):
|
||||
from utils.slider_captcha import verify_slider_trajectory
|
||||
|
||||
trajectory = [
|
||||
{'x': 0, 'y': 150, 't': 0},
|
||||
{'x': 50, 'y': 152, 't': 100},
|
||||
{'x': 100, 'y': 148, 't': 200},
|
||||
{'x': 150, 'y': 151, 't': 300},
|
||||
{'x': self.correct_x, 'y': 150, 't': 400},
|
||||
]
|
||||
result = verify_slider_trajectory(self.captcha_key, self.correct_x, trajectory)
|
||||
self.assertTrue(result)
|
||||
|
||||
def test_verify_with_suspicious_trajectory(self):
|
||||
from utils.slider_captcha import verify_slider_trajectory
|
||||
|
||||
trajectory = [
|
||||
{'x': 0, 'y': 150, 't': 0},
|
||||
{'x': 100, 'y': 150, 't': 100},
|
||||
{'x': 200, 'y': 150, 't': 200},
|
||||
]
|
||||
result = verify_slider_trajectory(self.captcha_key, self.correct_x, trajectory)
|
||||
self.assertFalse(result)
|
||||
Reference in New Issue
Block a user