chore: linting

This commit is contained in:
Aravinth Manivannan 2023-12-31 01:28:59 +05:30
parent e548a532a0
commit 240b5ec13a
Signed by: realaravinth
GPG key ID: F8F50389936984FF
7 changed files with 98 additions and 73 deletions

View file

@ -21,9 +21,11 @@ from mcaptcha import register
from dcache import grpc_add_vote, grpc_get_visitor_count
def incr(key):
return grpc_add_vote(key)
def get_count(key):
try:
count = grpc_get_visitor_count(key)
@ -31,10 +33,12 @@ def get_count(key):
except:
return 0
def assert_count(expect, key):
count = get_count(key)
assert count == expect
async def incr_one_works():
try:
key = "incr_one"

View file

@ -8,6 +8,7 @@ from dcache_py.dcache_pb2_grpc import DcacheServiceStub
host = "localhost:9001"
def grpc_add_vote(captcha_id: str):
with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel)
@ -34,7 +35,6 @@ def grpc_add_captcha(captcha_id: str):
),
)
resp = stub.AddCaptcha(msg)
return resp
@ -90,6 +90,7 @@ def grpc_get_challenge(token: str, key: str):
)
return stub.VerifyCaptchaResult(msg)
def grpc_delete_challenge(token: str):
with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel)
@ -98,14 +99,12 @@ def grpc_delete_challenge(token: str):
)
stub.DeleteCaptchaResult(msg)
def grpc_add_pow(token: str, string: str):
with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel)
msg = dcache.CachePowRequest(
key= token,
string= string,
duration = 5,
difficulty_factor= 500
key=token, string=string, duration=5, difficulty_factor=500
)
return stub.CachePow(msg)
@ -113,12 +112,11 @@ def grpc_add_pow(token: str, string: str):
def grpc_get_pow(token: str, string: str):
with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel)
msg = dcache.RetrievePowRequest(
token= string,
key= token)
msg = dcache.RetrievePowRequest(token=string, key=token)
resp = stub.RetrievePow(msg)
return resp
def grpc_delete_pow(string: str):
with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel)

View file

@ -18,11 +18,17 @@
import json
import utils
from dcache import grpc_add_captcha, grpc_add_vote, grpc_captcha_exists, grpc_rename_captcha
from dcache import (
grpc_add_captcha,
grpc_add_vote,
grpc_captcha_exists,
grpc_rename_captcha,
)
from dcache import grpc_delete_captcha
from stub import get_stub
def delete_captcha(key):
grpc_delete_captcha(key)
@ -30,6 +36,7 @@ def delete_captcha(key):
def add_captcha(key):
grpc_add_captcha(key)
def rename_captcha(key, new_key):
grpc_rename_captcha(key, new_key)
@ -37,11 +44,13 @@ def rename_captcha(key, new_key):
def captcha_exists(key):
return grpc_captcha_exists(captcha_id=key)
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):
@ -51,12 +60,14 @@ async def captcha_exists_works():
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)

View file

@ -25,6 +25,7 @@ from dcache import grpc_add_pow, grpc_get_pow, grpc_delete_pow
# 4. Read pow
# 5. Read expired pow
def add_pow(captcha, pow):
"""Add pow to"""
try:
@ -33,17 +34,19 @@ def add_pow(captcha, pow):
except Exception as e:
return e
def get_pow_from(captcha, pow):
"""Add pow to"""
try:
res = grpc_get_pow(captcha, pow)
if res.HasField('result'):
if res.HasField("result"):
return res.result
else:
return None
except Exception as e:
return e
def delete_pow(captcha, pow):
"""Add pow to"""
try:
@ -51,6 +54,7 @@ def delete_pow(captcha, pow):
except Exception as e:
return e
async def add_pow_works():
"""Test: Add pow"""
try:
@ -66,6 +70,7 @@ async def add_pow_works():
except Exception as e:
raise e
async def pow_ttl_works():
"""Test: pow TTL"""
try:
@ -109,7 +114,6 @@ async def delete_pow_works():
error = get_pow_from(key, pow_name)
assert error is None
print("[*] Delete pow works")
except Exception as e:
raise e

View file

@ -30,12 +30,13 @@ from dcache import grpc_add_challenge, grpc_get_challenge, grpc_delete_challenge
COMMANDS = {
"ADD": "MCAPTCHA_CACHE.ADD_result",
"GET": "MCAPTCHA_CACHE.GET_result",
"DEL" :"MCAPTCHA_CACHE.DELETE_result"
"DEL": "MCAPTCHA_CACHE.DELETE_result",
}
result_NOT_FOUND = "result not found"
DUPLICATE_result = "result already exists"
REDIS_OK = bytes("OK", 'utf-8')
REDIS_OK = bytes("OK", "utf-8")
def add_result(captcha, result):
"""Add result to"""
@ -44,6 +45,7 @@ def add_result(captcha, result):
except Exception as e:
return e
def get_result_from(captcha, result):
"""Add result to"""
try:
@ -51,6 +53,7 @@ def get_result_from(captcha, result):
except Exception as e:
return e
def delete_result(captcha, result):
"""Add result to"""
try:
@ -58,6 +61,7 @@ def delete_result(captcha, result):
except Exception as e:
return e
async def add_result_works():
"""Test: Add result"""
try:
@ -72,6 +76,7 @@ async def add_result_works():
except Exception as e:
raise e
async def result_ttl_works():
"""Test: result TTL"""
try:

View file

@ -18,13 +18,15 @@ import asyncio
import importlib.util
import sys
sys.path.append('/home/atm/code/mcaptcha/dcache/')
sys.path.append("/home/atm/code/mcaptcha/dcache/")
import bucket
import mcaptcha
import result
import pow
class Runner(object):
__fn = [
bucket.incr_one_works,
@ -51,7 +53,6 @@ class Runner(object):
task = asyncio.create_task(fn())
self.__tasks.append(task)
async def run(self):
"""Wait for registered functions to finish executing"""
await self.__register()
@ -59,5 +60,6 @@ class Runner(object):
await task
"""Runs in separate threads"""
def __init__(self):
super(Runner, self).__init__()

View file

@ -25,5 +25,6 @@ async def main():
await runner.run()
print("All tests passed")
if __name__ == "__main__":
asyncio.run(main())