<reponame>Cpt-Meow/API
import json.decoder
import string
import random
from requests import Response
from datetime import datetime


class BaseCase:
    # идем передавать объект ответа, который получаем после запроса
    def get_cookie(self, response: Response, cookie_name):
        assert cookie_name in response.cookies, f"cannot find cookies with name {cookie_name} in the last response"
        return response.cookies[cookie_name]

    def get_header(self, response: Response, headers_name):
        assert headers_name in response.headers, f"cannot find headers with name {headers_name} in the last response"
        return response.headers[headers_name]

    def get_json_value(self, response: Response, name):
        try:
            response_as_dict = response.json()
        except json.decoder.JSONDecodeError:
            assert False, f"Response is not in JSON format. Response text is '{response.text}'"

        assert name in response_as_dict, f"response JSON doesn't have key '{name}'"
        return response_as_dict[name]

    def prepare_registration_data(self, email=None):
        if email is None:
            base_part = "learnqa"
            domain = "example.com"
            random_part = datetime.now().strftime("%m%d%Y%H%M%S")
            email = f"{base_part}{random_part}@{domain}"

        return {
            'password': '<PASSWORD>',
            'username': 'learnqa',
            'firstName': 'learnqa',
            'lastName': 'learnqa',
            'email': email
        }

    def prepare_incomplete_registration_data(self, excluded_parameter):

        base_part = "learnqa"
        domain = "example.com"
        random_part = datetime.now().strftime("%m%d%Y%H%M%S")
        email = f"{base_part}{random_part}@{domain}"

        data = {
            'password': '<PASSWORD>',
            'username': 'learnqa',
            'firstName': 'learnqa',
            'lastName': 'learnqa',
            'email': email
        }

        del data[excluded_parameter]

        return data

    def get_random_username(self, maxlen):
        symbol = string.ascii_letters
        return "".join([random.choice(symbol) for i in range(random.randrange(maxlen))])


import json.decoder
import string
import random
from requests import Response
from datetime import datetime


class BaseCase:
    # идем передавать объект ответа, который получаем после запроса
    def get_cookie(self, response: Response, cookie_name):
        assert cookie_name in response.cookies, f"cannot find cookies with name {cookie_name} in the last response"
        return response.cookies[cookie_name]

    def get_header(self, response: Response, headers_name):
        assert headers_name in response.headers, f"cannot find headers with name {headers_name} in the last response"
        return response.headers[headers_name]

    def get_json_value(self, response: Response, name):
        try:
            response_as_dict = response.json()
        except json.decoder.JSONDecodeError:
            assert False, f"Response is not in JSON format. Response text is '{response.text}'"

        assert name in response_as_dict, f"response JSON doesn't have key '{name}'"
        return response_as_dict[name]

    def prepare_registration_data(self, email=None):
        if email is None:
            base_part = "learnqa"
            domain = "example.com"
            random_part = datetime.now().strftime("%m%d%Y%H%M%S")
            email = f"{base_part}{random_part}@{domain}"

        return {
            'password': '<PASSWORD>',
            'username': 'learnqa',
            'firstName': 'learnqa',
            'lastName': 'learnqa',
            'email': email
        }

    def prepare_incomplete_registration_data(self, excluded_parameter):

        base_part = "learnqa"
        domain = "example.com"
        random_part = datetime.now().strftime("%m%d%Y%H%M%S")
        email = f"{base_part}{random_part}@{domain}"

        data = {
            'password': '<PASSWORD>',
            'username': 'learnqa',
            'firstName': 'learnqa',
            'lastName': 'learnqa',
            'email': email
        }

        del data[excluded_parameter]

        return data

    def get_random_username(self, maxlen):
        symbol = string.ascii_letters
        return "".join([random.choice(symbol) for i in range(random.randrange(maxlen))])
# идем передавать объект ответа, который получаем после запроса