2021-06-06 14:05:14 +03:00
|
|
|
#!/bin/env /usr/bin/python3
|
|
|
|
#
|
|
|
|
# Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
2021-06-07 14:44:39 +03:00
|
|
|
import utils
|
|
|
|
from test import REDIS_URL
|
|
|
|
|
|
|
|
r = utils.connect(REDIS_URL)
|
|
|
|
utils.ping(r)
|
|
|
|
|
2021-06-06 14:05:14 +03:00
|
|
|
MCAPTCHA = {
|
2021-06-07 14:44:39 +03:00
|
|
|
"levels": [
|
2021-06-06 14:05:14 +03:00
|
|
|
{"visitor_threshold": 50, "difficulty_factor": 50},
|
|
|
|
{"visitor_threshold": 500, "difficulty_factor": 500}
|
|
|
|
],
|
|
|
|
"duration": 5
|
|
|
|
}
|
|
|
|
|
|
|
|
COMMANDS = {
|
|
|
|
"ADD_CAPTCHA": "MCAPTCHA_CACHE.ADD_CAPTCHA",
|
2021-06-07 14:44:39 +03:00
|
|
|
"DELETE_CAPTCHA": "MCAPTCHA_CACHE.DELETE_CAPTCHA",
|
|
|
|
"CAPTCHA_EXISTS": "MCAPTCHA_CACHE.CAPTCHA_EXISTS",
|
2021-06-06 14:05:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
payload = json.dumps(MCAPTCHA)
|
|
|
|
|
2021-06-07 14:44:39 +03:00
|
|
|
def delete_captcha(key):
|
|
|
|
r.execute_command(COMMANDS["DELETE_CAPTCHA"], key)
|
|
|
|
|
2021-06-06 14:05:14 +03:00
|
|
|
|
2021-06-07 14:44:39 +03:00
|
|
|
def add_captcha(key):
|
2021-06-06 14:05:14 +03:00
|
|
|
r.execute_command(COMMANDS["ADD_CAPTCHA"], key, payload)
|
2021-06-07 14:44:39 +03:00
|
|
|
|
|
|
|
|
|
|
|
def captcha_exists(key):
|
|
|
|
exists = r.execute_command(COMMANDS["CAPTCHA_EXISTS"], key)
|
|
|
|
if exists == 0:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if exists == 1:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def register(key):
|
|
|
|
if captcha_exists(key):
|
|
|
|
delete_captcha(key)
|
|
|
|
|
|
|
|
add_captcha(key)
|
|
|
|
|
|
|
|
async def captcha_exists_works():
|
|
|
|
key = "captcha_delete_works"
|
|
|
|
if captcha_exists(key):
|
|
|
|
delete_captcha(key)
|
|
|
|
assert captcha_exists(key) is False
|
|
|
|
register(key)
|
|
|
|
assert captcha_exists(key) is True
|
|
|
|
print("Captcha delete works")
|
|
|
|
|
|
|
|
async def register_captcha_works():
|
|
|
|
key = "register_captcha_works"
|
|
|
|
register(key)
|
|
|
|
assert captcha_exists(key) is True
|
|
|
|
print("Add captcha works")
|
|
|
|
|
|
|
|
async def delete_captcha_works():
|
|
|
|
key = "delete_captcha_works"
|
|
|
|
register(key)
|
|
|
|
exists = captcha_exists(key)
|
|
|
|
print("captcha exists stauts", exists)
|
|
|
|
assert exists is True
|
|
|
|
delete_captcha(key)
|
|
|
|
assert captcha_exists(key) is False
|
|
|
|
print("Delete captcha works")
|