#!/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

from dcache import (
    grpc_add_captcha,
    grpc_add_vote,
    grpc_captcha_exists,
    grpc_rename_captcha,
)
from dcache import grpc_delete_captcha

def delete_captcha(key):
    grpc_delete_captcha(key)


def add_captcha(key):
    grpc_add_captcha(key)


def rename_captcha(key, new_key):
    grpc_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):
        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)
    assert exists is True
    delete_captcha(key)
    assert captcha_exists(key) is False
    print("[*] Delete captcha works")


async def rename_captcha_works():
    key = "rename_captcha_works"
    new_key = "new_key_rename_captcha_works"
    register(key)
    exists = captcha_exists(key)
    assert exists is True
    rename_captcha(key, new_key)
    print(captcha_exists(key))
    assert captcha_exists(key) is False
    assert captcha_exists(new_key) is True
    print("[*] Rename captcha works")
