예제 #1
0
def test_base_modify_config():
    user = User()
    user.modify_config("invalid", 80, "invalid", "http://", False)
    with pytest.raises(ConnectError):
        user.lists()
    assert isinstance(User().lists(), Contents)
    user.modify_config("invalid", 80, "invalid", "http://", True)
    with pytest.raises(ConnectError):
        user.lists()
        User().lists()
    assert user.modify_config("localhost", 8008, admin_access_token, "http://")
    assert isinstance(user.lists(), Contents)
    assert isinstance(User().lists(), Contents)
 def __init__(self,
              server_addr=None,
              server_port=443,
              access_token=None,
              server_protocol=None,
              suppress_exception=False):
     super().__init__(server_addr, server_port, access_token,
                      server_protocol, suppress_exception)
     if server_addr is not None and access_token is not None:
         self.user = User(server_addr, server_port, access_token,
                          server_protocol, suppress_exception)
         self.client = ClientAPI(server_addr, server_port, access_token,
                                 server_protocol, suppress_exception)
     else:
         self.user = User()
         self.client = ClientAPI()
     self._create_alias()
예제 #3
0
    def admin_login(protocol: str,
                    host: str,
                    port: str,
                    username: str = None,
                    password: str = None,
                    suppress_exception: bool = False,
                    no_admin: bool = False) -> str:
        """Login and get an access token

        Args:
            protocol (str): "http://" or "https://". Defaults to None. # noqa: E501
            host (str): homeserver address. Defaults to None.
            port (int): homeserver listening port. Defaults to None.
            username (str, optional): just username. Defaults to None.
            password (str, optional): just password. Defaults to None.
            suppress_exception (bool, optional): suppress exception or not, if not return False and the error in dict. Defaults to False. # noqa: E501

        Returns:
            str: access token
        """
        if username is None:
            username = input("Enter a username: "******"identifier": {
                "type": "m.id.user",
                "user": username
            },
            "type": "m.login.password",
            "password": password,
            "initial_device_display_name": "matrix-synapse-admin"
        }
        http = Client()
        base_url = f"{protocol}{host}:{port}"
        while True:
            resp = http.post(f"{base_url}{ClientAPI.BASE_PATH}/login",
                             json=login_data)
            data = resp.json()
            if "errcode" in data and data["errcode"] == "M_LIMIT_EXCEEDED":
                time.sleep(data["retry_after_ms"] / 1000)
                continue
            if resp.status_code == 200:
                access_token = data["access_token"]
                if not no_admin:
                    resp = User(host, port, access_token,
                                protocol).query(username)
                    if "errcode" not in resp:
                        return data["access_token"]
                    else:
                        data = resp
                else:
                    return access_token
            if suppress_exception:
                return False, data
            else:
                raise SynapseException(data["errcode"], data["error"])
예제 #4
0
def test_base_read_config():
    user = User()
    user.config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    "../synapse_test/api.cfg")
    user.modify_config("invalid", 80, "invalid", "http://", True)
    user.read_config(user.config_path)
    assert user.server_addr == "invalid"
    assert user.server_port == 80
    assert user.access_token == "invalid"
예제 #5
0
def test_base_create_config():
    """TODO: interactive"""
    base_handler.config_path = config_path
    if os.path.isfile(config_path):
        os.remove(config_path)
    assert base_handler.create_config("http://", "localhost", 8008,
                                      admin_access_token, True)
    assert os.path.isfile(config_path)
    assert isinstance(User().lists(), Contents)
    os.remove(config_path)

    assert base_handler.create_config("http://", "localhost", 8008,
                                      admin_access_token, False)
    assert not os.path.isfile(config_path)

    assert base_handler.create_config("http://", "localhost", 8008,
                                      admin_access_token, True)
    assert os.path.isfile(config_path)
예제 #6
0
import time
from synapse_admin import User, Room
from synapse_admin.client import ClientAPI
from synapse_admin.base import SynapseException, Utility
from yaml import load, CLoader

with open("synapse_test/admin.token", "r") as f:
    admin_access_token = f.read().replace("\n", "")

with open("synapse_test/user.token", "r") as f:
    user_access_token = f.read().replace("\n", "")

config = load(open("synapse_test/homeserver.yaml", "r"), Loader=CLoader)

conn = ("localhost", 8008, admin_access_token, "http://")
user_handler = User(*conn)


def test_user_list():
    exist_users = []
    for user in user_handler.lists():
        exist_users.append(user["name"][1:])
    assert exist_users == ["admin1:localhost", "test1:localhost"]


def test_user_create():
    user_handler.create("test2",
                        password="******",
                        displayname="Test 2",
                        admin=False,
                        threepids=[{