Ejemplo n.º 1
0
class ClientTestCase(unittest.TestCase):
    CONFIGS = get_configs()

    bad_message = {
        CONFIGS.get('RESPONSE'): 400,
        CONFIGS.get('ERROR'): 'Bad request'
    }

    good_message = {
        CONFIGS.get('RESPONSE'): 200,
        CONFIGS.get('ALERT'): 'Привет, клиент!'
    }

    test_error_message = f'400: Bad request'
    test_correct_message = f'200: OK, Привет, клиент!'

    def test_error_response(self):
        self.assertEqual(
            Client.check_response(Client(), self.bad_message, self.CONFIGS),
            self.test_error_message)

    def test_correct_response(self):
        self.assertEqual(
            Client.check_response(Client(), self.good_message, self.CONFIGS),
            self.test_correct_message)
Ejemplo n.º 2
0
class Test_Socket:
    CONFIGS = get_configs()

    def __init__(self, test_message):
        self.test_message = test_message
        self.encoded_message = None
        self.received_message = None

    def send(self, message_to_send):
        self.encoded_message = pickle.dumps(self.test_message)
        self.received_message = message_to_send

    def recv(self, max_len):
        pickle_test_message = pickle.dumps(self.test_message)
        return pickle_test_message
Ejemplo n.º 3
0
class UtilsTestCase(unittest.TestCase):
    CONFIGS = get_configs()

    # CONFIGS['TIME'] = 123456789

    def test_get_config_default_port(self):
        self.assertEqual(self.CONFIGS['DEFAULT_PORT'], 7777)

    def test_get_config_default_encoding(self):
        self.assertEqual(self.CONFIGS['ENCODING'], 'utf-8')

    def test_get_config_connections(self):
        self.assertLessEqual(self.CONFIGS['MAX_CONNECTIONS'], 5)

    def test_get_config_max_package_length(self):
        self.assertLessEqual(self.CONFIGS['MAX_PACKAGE_LENGTH'], 1024)

    def setUp(self):
        self.server_addr = self.CONFIGS['DEFAULT_IP_ADDRESS']
        self.server_port = self.CONFIGS['DEFAULT_PORT']
        self.socket = socket(AF_INET, SOCK_STREAM)

    def tearDown(self):
        self.socket.close()

    def test_send_message(self):
        test_message_from_client = {
            'action': 'presence',
            'time': 123456789,
            'type': 'status',
            'user': {
                'account_name': 'di-mario',
                'status': 'Привет, сервер!'
            }
        }

        test_socket = TestSocket(test_message_from_client)
        send_message(test_socket, test_message_from_client, self.CONFIGS)
        self.assertEqual(test_socket.encoded_message,
                         test_socket.received_message)

    def test_get_message(self):
        test_message = {"response": "200", "alert": "test"}
        test_socket = TestSocket(test_message)
        self.assertEqual(get_message(test_socket, self.CONFIGS), {
            'response': '200',
            'alert': 'test'
        })
Ejemplo n.º 4
0
class ServerTestCase(unittest.TestCase):

    CONFIGS = get_configs()

    test_error_message = {
        CONFIGS.get('RESPONSE'): 400,
        CONFIGS.get('ERROR'): 'Bad request'
    }

    test_correct_message = {
        CONFIGS.get('RESPONSE'): 200,
        CONFIGS.get('ALERT'): 'Привет, клиент!'
    }

    def test_check_not_full_message(self):
        self.assertEqual(
            check_presence_message({
                'action': 'presence',
                'type': 'status'
            }, self.CONFIGS), self.test_error_message)

    def test_check_correct_message(self):
        test_message = {
            'action': 'presence',
            'time': 'Sat Feb 13 02:17:51 2021',
            'type': 'status',
            'user': {
                'account_name': 'di-mario',
                'status': 'Привет, сервер!'
            }
        }
        self.assertEqual(check_presence_message(test_message, self.CONFIGS),
                         self.test_correct_message)

    def test_check_message_wrong_user(self):
        test_message = {
            'action': 'presence',
            'time': 'Sat Feb 13 02:17:51 2021',
            'type': 'status',
            'user': {
                'account_name': 'Dima',
                'status': 'Привет, сервер!'
            }
        }
        self.assertEqual(check_presence_message(test_message, self.CONFIGS),
                         self.test_error_message)
Ejemplo n.º 5
0
class TestSocket:
    CONFIGS = get_configs()

    def __init__(self, test_message):
        self.test_message = test_message
        self.encoded_message = None
        self.received_message = None

    def send(self, message_to_send):
        json_test_message = json.dumps(self.test_message)
        self.encoded_message = json_test_message.encode(
            self.CONFIGS.get('ENCODING'))
        self.received_message = message_to_send

    def recv(self, max_len):
        json_test_message = json.dumps(self.test_message)
        return json_test_message.encode(self.CONFIGS.get('ENCODING'))
Ejemplo n.º 6
0
import base64
import json

from Cryptodome.Cipher import PKCS1_OAEP
from Cryptodome.PublicKey import RSA
from PyQt5.QtWidgets import QMainWindow, qApp, QMessageBox
from PyQt5.QtGui import QStandardItemModel, QStandardItem, QBrush, QColor
from PyQt5.QtCore import pyqtSlot, Qt
from client.add_contact import AddContactDialog
from client.del_contact import DelContactDialog
from client.main_window_conv import Ui_MainWindow
from common.errors import ServerError
from common.utils import get_configs
from log.client_log import client_logger

CONFIGS = get_configs()


class ClientMainWindow(QMainWindow):
    """
    Класс - основное окно пользователя.
    Содержит всю основную логику работы клиентского модуля.
    Конфигурация окна создана в QTDesigner и загружается из
    конвертированого файла main_window_conv.py
    """
    def __init__(self, database, transport, keys):
        super().__init__()
        # основные переменные
        self.database = database
        self.transport = transport