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

@ -1,16 +1,16 @@
#!/bin/env /usr/bin/python3 #!/bin/env /usr/bin/python3
# # Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net> # # Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the # published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version. # License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details. # GNU Affero General Public License for more details.
# #
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from asyncio import sleep from asyncio import sleep
@ -21,9 +21,11 @@ from mcaptcha import register
from dcache import grpc_add_vote, grpc_get_visitor_count from dcache import grpc_add_vote, grpc_get_visitor_count
def incr(key): def incr(key):
return grpc_add_vote(key) return grpc_add_vote(key)
def get_count(key): def get_count(key):
try: try:
count = grpc_get_visitor_count(key) count = grpc_get_visitor_count(key)
@ -31,10 +33,12 @@ def get_count(key):
except: except:
return 0 return 0
def assert_count(expect, key): def assert_count(expect, key):
count = get_count(key) count = get_count(key)
assert count == expect assert count == expect
async def incr_one_works(): async def incr_one_works():
try: try:
key = "incr_one" key = "incr_one"
@ -74,7 +78,7 @@ async def difficulty_works():
register(key) register(key)
data = incr(key) data = incr(key)
assert data.difficulty_factor == 50 assert data.difficulty_factor == 50
for _ in range(501): for _ in range(501):
incr(key) incr(key)
data = incr(key) data = incr(key)

View file

@ -8,6 +8,7 @@ from dcache_py.dcache_pb2_grpc import DcacheServiceStub
host = "localhost:9001" host = "localhost:9001"
def grpc_add_vote(captcha_id: str): def grpc_add_vote(captcha_id: str):
with grpc.insecure_channel(host) as channel: with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel) stub = DcacheServiceStub(channel)
@ -34,7 +35,6 @@ def grpc_add_captcha(captcha_id: str):
), ),
) )
resp = stub.AddCaptcha(msg) resp = stub.AddCaptcha(msg)
return resp return resp
@ -47,7 +47,7 @@ def grpc_captcha_exists(captcha_id: str):
return resp.exists return resp.exists
def grpc_rename_captcha( captcha_id: str, new_id: str): def grpc_rename_captcha(captcha_id: str, new_id: str):
with grpc.insecure_channel(host) as channel: with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel) stub = DcacheServiceStub(channel)
@ -55,7 +55,7 @@ def grpc_rename_captcha( captcha_id: str, new_id: str):
resp = stub.RenameCaptcha(msg) resp = stub.RenameCaptcha(msg)
def grpc_delete_captcha( captcha_id: str): def grpc_delete_captcha(captcha_id: str):
with grpc.insecure_channel(host) as channel: with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel) stub = DcacheServiceStub(channel)
@ -74,10 +74,10 @@ def grpc_add_challenge(token: str, key: str):
with grpc.insecure_channel(host) as channel: with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel) stub = DcacheServiceStub(channel)
msg = dcache.CacheResultRequest( msg = dcache.CacheResultRequest(
token= token, token=token,
key= key, key=key,
duration = 5, duration=5,
) )
stub.CacheResult(msg) stub.CacheResult(msg)
@ -85,44 +85,42 @@ def grpc_get_challenge(token: str, key: str):
with grpc.insecure_channel(host) as channel: with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel) stub = DcacheServiceStub(channel)
msg = dcache.RetrievePowRequest( msg = dcache.RetrievePowRequest(
token= token, token=token,
key= key, key=key,
) )
return stub.VerifyCaptchaResult(msg) return stub.VerifyCaptchaResult(msg)
def grpc_delete_challenge(token: str): def grpc_delete_challenge(token: str):
with grpc.insecure_channel(host) as channel: with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel) stub = DcacheServiceStub(channel)
msg = dcache.DeleteCaptchaResultRequest( msg = dcache.DeleteCaptchaResultRequest(
token= token, token=token,
) )
stub.DeleteCaptchaResult(msg) stub.DeleteCaptchaResult(msg)
def grpc_add_pow(token: str, string: str): def grpc_add_pow(token: str, string: str):
with grpc.insecure_channel(host) as channel: with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel) stub = DcacheServiceStub(channel)
msg = dcache.CachePowRequest( msg = dcache.CachePowRequest(
key= token, key=token, string=string, duration=5, difficulty_factor=500
string= string, )
duration = 5,
difficulty_factor= 500
)
return stub.CachePow(msg) return stub.CachePow(msg)
def grpc_get_pow(token: str, string: str): def grpc_get_pow(token: str, string: str):
with grpc.insecure_channel(host) as channel: with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel) stub = DcacheServiceStub(channel)
msg = dcache.RetrievePowRequest( msg = dcache.RetrievePowRequest(token=string, key=token)
token= string, resp = stub.RetrievePow(msg)
key= token)
resp= stub.RetrievePow(msg)
return resp return resp
def grpc_delete_pow(string: str): def grpc_delete_pow(string: str):
with grpc.insecure_channel(host) as channel: with grpc.insecure_channel(host) as channel:
stub = DcacheServiceStub(channel) stub = DcacheServiceStub(channel)
msg = dcache.DeletePowRequest( msg = dcache.DeletePowRequest(
string= string, string=string,
) )
stub.DeletePow(msg) stub.DeletePow(msg)

View file

@ -1,28 +1,34 @@
#!/bin/env /usr/bin/python3 #!/bin/env /usr/bin/python3
# #
# Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net> # Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the # published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version. # License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details. # GNU Affero General Public License for more details.
# #
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
import json import json
import utils 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 dcache import grpc_delete_captcha
from stub import get_stub from stub import get_stub
def delete_captcha(key): def delete_captcha(key):
grpc_delete_captcha(key) grpc_delete_captcha(key)
@ -30,6 +36,7 @@ def delete_captcha(key):
def add_captcha(key): def add_captcha(key):
grpc_add_captcha(key) grpc_add_captcha(key)
def rename_captcha(key, new_key): def rename_captcha(key, new_key):
grpc_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): def captcha_exists(key):
return grpc_captcha_exists(captcha_id=key) return grpc_captcha_exists(captcha_id=key)
def register(key): def register(key):
if captcha_exists(key): if captcha_exists(key):
delete_captcha(key) delete_captcha(key)
add_captcha(key) add_captcha(key)
async def captcha_exists_works(): async def captcha_exists_works():
key = "captcha_delete_works" key = "captcha_delete_works"
if captcha_exists(key): if captcha_exists(key):
@ -51,12 +60,14 @@ async def captcha_exists_works():
assert captcha_exists(key) is True assert captcha_exists(key) is True
print("[*] Captcha delete works") print("[*] Captcha delete works")
async def register_captcha_works(): async def register_captcha_works():
key = "register_captcha_works" key = "register_captcha_works"
register(key) register(key)
assert captcha_exists(key) is True assert captcha_exists(key) is True
print("[*] Add captcha works") print("[*] Add captcha works")
async def delete_captcha_works(): async def delete_captcha_works():
key = "delete_captcha_works" key = "delete_captcha_works"
register(key) register(key)

View file

@ -1,17 +1,17 @@
#!/bin/env /usr/bin/python3 #!/bin/env /usr/bin/python3
# #
# Copyright (C) 2023 Aravinth Manivannan <realaravinth@batsense.net> # Copyright (C) 2023 Aravinth Manivannan <realaravinth@batsense.net>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the # published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version. # License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details. # GNU Affero General Public License for more details.
# #
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from asyncio import sleep from asyncio import sleep
@ -25,32 +25,36 @@ from dcache import grpc_add_pow, grpc_get_pow, grpc_delete_pow
# 4. Read pow # 4. Read pow
# 5. Read expired pow # 5. Read expired pow
def add_pow(captcha, pow): def add_pow(captcha, pow):
"""Add pow to """ """Add pow to"""
try : try:
res = grpc_add_pow(captcha, pow) res = grpc_add_pow(captcha, pow)
return res return res
except Exception as e: except Exception as e:
return e return e
def get_pow_from(captcha, pow): def get_pow_from(captcha, pow):
"""Add pow to """ """Add pow to"""
try : try:
res = grpc_get_pow(captcha, pow) res = grpc_get_pow(captcha, pow)
if res.HasField('result'): if res.HasField("result"):
return res.result return res.result
else: else:
return None return None
except Exception as e: except Exception as e:
return e return e
def delete_pow(captcha, pow): def delete_pow(captcha, pow):
"""Add pow to """ """Add pow to"""
try : try:
grpc_delete_pow(pow) grpc_delete_pow(pow)
except Exception as e: except Exception as e:
return e return e
async def add_pow_works(): async def add_pow_works():
"""Test: Add pow""" """Test: Add pow"""
try: try:
@ -66,6 +70,7 @@ async def add_pow_works():
except Exception as e: except Exception as e:
raise e raise e
async def pow_ttl_works(): async def pow_ttl_works():
"""Test: pow TTL""" """Test: pow TTL"""
try: try:
@ -102,14 +107,13 @@ async def delete_pow_works():
try: try:
pow_name = "delete_pow" pow_name = "delete_pow"
key = "delete_pow_key" key = "delete_pow_key"
#pow = get_pow(pow_name) # pow = get_pow(pow_name)
add_pow(key, pow_name) add_pow(key, pow_name)
delete_pow(key, pow_name) delete_pow(key, pow_name)
error = get_pow_from(key, pow_name) error = get_pow_from(key, pow_name)
assert error is None assert error is None
print("[*] Delete pow works") print("[*] Delete pow works")
except Exception as e: except Exception as e:
raise e raise e

View file

@ -1,17 +1,17 @@
#!/bin/env /usr/bin/python3 #!/bin/env /usr/bin/python3
# #
# Copyright (C) 2023 Aravinth Manivannan <realaravinth@batsense.net> # Copyright (C) 2023 Aravinth Manivannan <realaravinth@batsense.net>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the # published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version. # License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details. # GNU Affero General Public License for more details.
# #
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from asyncio import sleep from asyncio import sleep
@ -28,36 +28,40 @@ from dcache import grpc_add_challenge, grpc_get_challenge, grpc_delete_challenge
COMMANDS = { COMMANDS = {
"ADD" :"MCAPTCHA_CACHE.ADD_result", "ADD": "MCAPTCHA_CACHE.ADD_result",
"GET" :"MCAPTCHA_CACHE.GET_result", "GET": "MCAPTCHA_CACHE.GET_result",
"DEL" :"MCAPTCHA_CACHE.DELETE_result" "DEL": "MCAPTCHA_CACHE.DELETE_result",
} }
result_NOT_FOUND = "result not found" result_NOT_FOUND = "result not found"
DUPLICATE_result = "result already exists" DUPLICATE_result = "result already exists"
REDIS_OK = bytes("OK", 'utf-8') REDIS_OK = bytes("OK", "utf-8")
def add_result(captcha, result): def add_result(captcha, result):
"""Add result to """ """Add result to"""
try : try:
grpc_add_challenge(captcha,result) grpc_add_challenge(captcha, result)
except Exception as e:
return e
def get_result_from(captcha, result):
"""Add result to """
try :
return grpc_get_challenge(captcha,result)
except Exception as e: except Exception as e:
return e return e
def get_result_from(captcha, result):
"""Add result to"""
try:
return grpc_get_challenge(captcha, result)
except Exception as e:
return e
def delete_result(captcha, result): def delete_result(captcha, result):
"""Add result to """ """Add result to"""
try : try:
grpc_delete_challenge(captcha) grpc_delete_challenge(captcha)
except Exception as e: except Exception as e:
return e return e
async def add_result_works(): async def add_result_works():
"""Test: Add result""" """Test: Add result"""
try: try:
@ -72,6 +76,7 @@ async def add_result_works():
except Exception as e: except Exception as e:
raise e raise e
async def result_ttl_works(): async def result_ttl_works():
"""Test: result TTL""" """Test: result TTL"""
try: try:
@ -82,7 +87,7 @@ async def result_ttl_works():
await sleep(5 + 2) await sleep(5 + 2)
error = get_result_from(key, result_name) error = get_result_from(key, result_name)
# assert str(error) == result_NOT_FOUND # assert str(error) == result_NOT_FOUND
print("[*] result TTL works") print("[*] result TTL works")
except Exception as e: except Exception as e:

View file

@ -1,16 +1,16 @@
#!/bin/env /usr/bin/python3 #!/bin/env /usr/bin/python3
# Copyright (C) 2023 Aravinth Manivannan <realaravinth@batsense.net> # Copyright (C) 2023 Aravinth Manivannan <realaravinth@batsense.net>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the # published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version. # License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details. # GNU Affero General Public License for more details.
# #
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from threading import Thread from threading import Thread
@ -18,13 +18,15 @@ import asyncio
import importlib.util import importlib.util
import sys import sys
sys.path.append('/home/atm/code/mcaptcha/dcache/')
sys.path.append("/home/atm/code/mcaptcha/dcache/")
import bucket import bucket
import mcaptcha import mcaptcha
import result import result
import pow import pow
class Runner(object): class Runner(object):
__fn = [ __fn = [
bucket.incr_one_works, bucket.incr_one_works,
@ -46,12 +48,11 @@ class Runner(object):
__tasks = [] __tasks = []
async def __register(self): async def __register(self):
""" Register functions to be run""" """Register functions to be run"""
for fn in self.__fn: for fn in self.__fn:
task = asyncio.create_task(fn()) task = asyncio.create_task(fn())
self.__tasks.append(task) self.__tasks.append(task)
async def run(self): async def run(self):
"""Wait for registered functions to finish executing""" """Wait for registered functions to finish executing"""
await self.__register() await self.__register()
@ -59,5 +60,6 @@ class Runner(object):
await task await task
"""Runs in separate threads""" """Runs in separate threads"""
def __init__(self): def __init__(self):
super(Runner, self).__init__() super(Runner, self).__init__()

View file

@ -1,17 +1,17 @@
#!/bin/env /usr/bin/python3 #!/bin/env /usr/bin/python3
# #
# Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net> # Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the # published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version. # License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details. # GNU Affero General Public License for more details.
# #
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
import asyncio import asyncio
@ -25,5 +25,6 @@ async def main():
await runner.run() await runner.run()
print("All tests passed") print("All tests passed")
if __name__ == "__main__": if __name__ == "__main__":
asyncio.run(main()) asyncio.run(main())