Esempio n. 1
0
    def test_failure_nonce(self):
        """
        If the script fails to fetch a nonce, it throws an error and quits.
        """
        def get(url, verify=None):
            r = Mock()
            r.status_code = 404
            r.reason = "Not Found"
            r.json = lambda: {"not": "error"}
            return r

        requests = Mock()
        requests.get = get

        # The fake stdout will be written here
        out = []
        err_code = []

        request_registration(
            "user",
            "pass",
            "matrix.org",
            "shared",
            admin=False,
            requests=requests,
            _print=out.append,
            exit=err_code.append,
        )

        # Exit was called
        self.assertEqual(err_code, [1])

        # We got an error message
        self.assertIn("ERROR! Received 404 Not Found", out)
        self.assertNotIn("Success!", out)
Esempio n. 2
0
    def test_failure_post(self):
        """
        The script will fetch a nonce, and then if the final POST fails, will
        report an error and quit.
        """

        def get(url, verify=None):
            r = Mock()
            r.status_code = 200
            r.json = lambda: {"nonce": "a"}
            return r

        def post(url, json=None, verify=None):
            # Make sure we are sent the correct info
            self.assertEqual(json["username"], "user")
            self.assertEqual(json["password"], "pass")
            self.assertEqual(json["nonce"], "a")
            # We want a 40-char hex MAC
            self.assertEqual(len(json["mac"]), 40)

            r = Mock()
            # Then 500 because we're jerks
            r.status_code = 500
            r.reason = "Broken"
            return r

        requests = Mock()
        requests.get = get
        requests.post = post

        # The fake stdout will be written here
        out = []
        err_code = []

        request_registration(
            "user",
            "pass",
            "matrix.org",
            "shared",
            admin=False,
            requests=requests,
            _print=out.append,
            exit=err_code.append,
        )

        # Exit was called
        self.assertEqual(err_code, [1])

        # We got an error message
        self.assertIn("ERROR! Received 500 Broken", out)
        self.assertNotIn("Success!", out)
Esempio n. 3
0
    def test_failure_post(self):
        """
        The script will fetch a nonce, and then if the final POST fails, will
        report an error and quit.
        """
        def get(url, verify=None):
            r = Mock()
            r.status_code = 200
            r.json = lambda: {"nonce": "a"}
            return r

        def post(url, json=None, verify=None):
            # Make sure we are sent the correct info
            self.assertEqual(json["username"], "user")
            self.assertEqual(json["password"], "pass")
            self.assertEqual(json["nonce"], "a")
            # We want a 40-char hex MAC
            self.assertEqual(len(json["mac"]), 40)

            r = Mock()
            # Then 500 because we're jerks
            r.status_code = 500
            r.reason = "Broken"
            return r

        requests = Mock()
        requests.get = get
        requests.post = post

        # The fake stdout will be written here
        out: List[str] = []
        err_code: List[int] = []

        with patch("synapse._scripts.register_new_matrix_user.requests",
                   requests):
            request_registration(
                "user",
                "pass",
                "matrix.org",
                "shared",
                admin=False,
                _print=out.append,
                exit=err_code.append,
            )

        # Exit was called
        self.assertEqual(err_code, [1])

        # We got an error message
        self.assertIn("ERROR! Received 500 Broken", out)
        self.assertNotIn("Success!", out)
Esempio n. 4
0
    def test_success(self):
        """
        The script will fetch a nonce, and then generate a MAC with it, and then
        post that MAC.
        """

        def get(url, verify=None):
            r = Mock()
            r.status_code = 200
            r.json = lambda: {"nonce": "a"}
            return r

        def post(url, json=None, verify=None):
            # Make sure we are sent the correct info
            self.assertEqual(json["username"], "user")
            self.assertEqual(json["password"], "pass")
            self.assertEqual(json["nonce"], "a")
            # We want a 40-char hex MAC
            self.assertEqual(len(json["mac"]), 40)

            r = Mock()
            r.status_code = 200
            return r

        requests = Mock()
        requests.get = get
        requests.post = post

        # The fake stdout will be written here
        out = []
        err_code = []

        request_registration(
            "user",
            "pass",
            "matrix.org",
            "shared",
            admin=False,
            requests=requests,
            _print=out.append,
            exit=err_code.append,
        )

        # We should get the success message making sure everything is OK.
        self.assertIn("Success!", out)

        # sys.exit shouldn't have been called.
        self.assertEqual(err_code, [])
Esempio n. 5
0
    def test_success(self):
        """
        The script will fetch a nonce, and then generate a MAC with it, and then
        post that MAC.
        """
        def get(url, verify=None):
            r = Mock()
            r.status_code = 200
            r.json = lambda: {"nonce": "a"}
            return r

        def post(url, json=None, verify=None):
            # Make sure we are sent the correct info
            self.assertEqual(json["username"], "user")
            self.assertEqual(json["password"], "pass")
            self.assertEqual(json["nonce"], "a")
            # We want a 40-char hex MAC
            self.assertEqual(len(json["mac"]), 40)

            r = Mock()
            r.status_code = 200
            return r

        requests = Mock()
        requests.get = get
        requests.post = post

        # The fake stdout will be written here
        out: List[str] = []
        err_code: List[int] = []

        with patch("synapse._scripts.register_new_matrix_user.requests",
                   requests):
            request_registration(
                "user",
                "pass",
                "matrix.org",
                "shared",
                admin=False,
                _print=out.append,
                exit=err_code.append,
            )

        # We should get the success message making sure everything is OK.
        self.assertIn("Success!", out)

        # sys.exit shouldn't have been called.
        self.assertEqual(err_code, [])
Esempio n. 6
0
def run_and_test():
    """Launch the bot and its tests."""
    # Start the server, and wait for it
    LOGGER.info("Spawning synapse")
    srv = Popen([
        "python",
        "-m",
        "synapse.app.homeserver",
        "--config-path",
        "/srv/homeserver.yaml",
    ])
    if not wait_available(f"{MATRIX_URL}/_matrix/client/r0/login", "flows"):
        return False

    # Register a user for the bot.
    LOGGER.info("Registering the bot")
    with open("/srv/homeserver.yaml") as f:
        secret = yaml.safe_load(f.read()).get("registration_shared_secret",
                                              None)
    request_registration(MATRIX_ID, MATRIX_PW, MATRIX_URL, secret, admin=True)

    # Start the bot, and wait for it
    LOGGER.info("Spawning the bot")
    bot = Popen(["coverage", "run", "-m", "matrix_webhook", "-vvvvv"])
    if not wait_available(BOT_URL, "status"):
        return False

    # Run the main unittest module
    LOGGER.info("Runnig unittests")
    ret = main(module=None, exit=False).result.wasSuccessful()

    LOGGER.info("Stopping synapse")
    srv.terminate()

    # TODO Check what the bot says when the server is offline
    # print(bot_req({'data': 'bye'}, KEY), {'status': 200, 'ret': 'OK'})

    LOGGER.info("Stopping the bot")
    bot.terminate()

    LOGGER.info("Processing coverage")
    for cmd in ["report", "html", "xml"]:
        run(["coverage", cmd])
    return ret
Esempio n. 7
0
    def test_failure_nonce(self):
        """
        If the script fails to fetch a nonce, it throws an error and quits.
        """

        def get(url, verify=None):
            r = Mock()
            r.status_code = 404
            r.reason = "Not Found"
            r.json = lambda: {"not": "error"}
            return r

        requests = Mock()
        requests.get = get

        # The fake stdout will be written here
        out = []
        err_code = []

        request_registration(
            "user",
            "pass",
            "matrix.org",
            "shared",
            admin=False,
            requests=requests,
            _print=out.append,
            exit=err_code.append,
        )

        # Exit was called
        self.assertEqual(err_code, [1])

        # We got an error message
        self.assertIn("ERROR! Received 404 Not Found", out)
        self.assertNotIn("Success!", out)