Integration tests #4
7 changed files with 98 additions and 73 deletions
|
@ -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"
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
@ -90,6 +90,7 @@ def grpc_get_challenge(token: str, key: str):
|
||||||
)
|
)
|
||||||
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)
|
||||||
|
@ -98,14 +99,12 @@ def grpc_delete_challenge(token: str):
|
||||||
)
|
)
|
||||||
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)
|
||||||
|
|
||||||
|
@ -113,12 +112,11 @@ def grpc_add_pow(token: str, string: str):
|
||||||
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,
|
|
||||||
key= token)
|
|
||||||
resp = stub.RetrievePow(msg)
|
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)
|
||||||
|
|
|
@ -18,11 +18,17 @@
|
||||||
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)
|
||||||
|
|
|
@ -25,6 +25,7 @@ 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:
|
||||||
|
@ -33,17 +34,19 @@ def add_pow(captcha, pow):
|
||||||
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:
|
||||||
|
@ -51,6 +54,7 @@ def delete_pow(captcha, 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:
|
||||||
|
@ -109,7 +114,6 @@ async def delete_pow_works():
|
||||||
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
|
||||||
|
|
|
@ -30,12 +30,13 @@ 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"""
|
||||||
|
@ -44,6 +45,7 @@ def add_result(captcha, result):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return e
|
return e
|
||||||
|
|
||||||
|
|
||||||
def get_result_from(captcha, result):
|
def get_result_from(captcha, result):
|
||||||
"""Add result to"""
|
"""Add result to"""
|
||||||
try:
|
try:
|
||||||
|
@ -51,6 +53,7 @@ def get_result_from(captcha, result):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return e
|
return e
|
||||||
|
|
||||||
|
|
||||||
def delete_result(captcha, result):
|
def delete_result(captcha, result):
|
||||||
"""Add result to"""
|
"""Add result to"""
|
||||||
try:
|
try:
|
||||||
|
@ -58,6 +61,7 @@ def delete_result(captcha, result):
|
||||||
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:
|
||||||
|
|
|
@ -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,
|
||||||
|
@ -51,7 +53,6 @@ class Runner(object):
|
||||||
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__()
|
||||||
|
|
|
@ -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())
|
||||||
|
|
Loading…
Add table
Reference in a new issue