sync from local backup
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
import json
|
||||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
from rest_framework.test import APIClient
|
||||
from rest_framework import status
|
||||
from django.core.cache import caches
|
||||
|
||||
default_cache = caches['default']
|
||||
|
||||
|
||||
class TestSliderCaptchaAPIView(TestCase):
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
self.generate_url = reverse('slider-captcha-generate')
|
||||
self.verify_url = reverse('slider-captcha-verify')
|
||||
|
||||
def test_generate_captcha(self):
|
||||
response = self.client.get(self.generate_url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
data = response.data['data']
|
||||
self.assertIn('captcha_key', data)
|
||||
self.assertIn('bg_image', data)
|
||||
self.assertIn('slider_image', data)
|
||||
self.assertIn('y_position', data)
|
||||
|
||||
def test_verify_captcha_success(self):
|
||||
gen_response = self.client.get(self.generate_url)
|
||||
captcha_key = gen_response.data['data']['captcha_key']
|
||||
|
||||
cached = default_cache.get(f'slider_captcha_{captcha_key}')
|
||||
correct_x = cached['x_position']
|
||||
|
||||
verify_data = {
|
||||
'captcha_key': captcha_key,
|
||||
'x_position': correct_x
|
||||
}
|
||||
response = self.client.post(
|
||||
self.verify_url,
|
||||
data=json.dumps(verify_data),
|
||||
content_type='application/json'
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertTrue(response.data['data']['verified'])
|
||||
|
||||
def test_verify_captcha_failure(self):
|
||||
gen_response = self.client.get(self.generate_url)
|
||||
captcha_key = gen_response.data['data']['captcha_key']
|
||||
|
||||
verify_data = {
|
||||
'captcha_key': captcha_key,
|
||||
'x_position': 999
|
||||
}
|
||||
response = self.client.post(
|
||||
self.verify_url,
|
||||
data=json.dumps(verify_data),
|
||||
content_type='application/json'
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertFalse(response.data['data']['verified'])
|
||||
|
||||
def test_verify_expired_captcha(self):
|
||||
verify_data = {
|
||||
'captcha_key': 'non-existent-key',
|
||||
'x_position': 100
|
||||
}
|
||||
response = self.client.post(
|
||||
self.verify_url,
|
||||
data=json.dumps(verify_data),
|
||||
content_type='application/json'
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_verify_with_trajectory(self):
|
||||
gen_response = self.client.get(self.generate_url)
|
||||
captcha_key = gen_response.data['data']['captcha_key']
|
||||
|
||||
cached = default_cache.get(f'slider_captcha_{captcha_key}')
|
||||
correct_x = cached['x_position']
|
||||
|
||||
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': 200, 'y': 149, 't': 400},
|
||||
{'x': correct_x, 'y': 150, 't': 500},
|
||||
]
|
||||
|
||||
verify_data = {
|
||||
'captcha_key': captcha_key,
|
||||
'x_position': correct_x,
|
||||
'trajectory': trajectory
|
||||
}
|
||||
response = self.client.post(
|
||||
self.verify_url,
|
||||
data=json.dumps(verify_data),
|
||||
content_type='application/json'
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertTrue(response.data['data']['verified'])
|
||||
|
||||
def test_verify_missing_fields(self):
|
||||
response = self.client.post(
|
||||
self.verify_url,
|
||||
data=json.dumps({}),
|
||||
content_type='application/json'
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
Reference in New Issue
Block a user