2018-10-26 07:05:22 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2018-10-26 07:05:22 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2023-02-08 16:29:49 -05:00
|
|
|
from typing import List, Optional
|
Disallow untyped defs in synapse._scripts (#12422)
Of note:
* No untyped defs in `register_new_matrix_user`
This one might be contraversial. `request_registration` has three
dependency-injection arguments used for testing. I'm removing the
injection of the `requests` module and using `unitest.mock.patch` in the
test cases instead.
Doing `reveal_type(requests)` and `reveal_type(requests.get)` before the
change:
```
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "Any"
synapse/_scripts/register_new_matrix_user.py:46: note: Revealed type is "Any"
```
And after:
```
synapse/_scripts/register_new_matrix_user.py:44: note: Revealed type is "types.ModuleType"
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "def (url: Union[builtins.str, builtins.bytes], params: Union[Union[_typeshed.SupportsItems[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], typing.Iterable[Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]]], builtins.str, builtins.bytes], None] =, data: Union[Any, None] =, headers: Union[Any, None] =, cookies: Union[Any, None] =, files: Union[Any, None] =, auth: Union[Any, None] =, timeout: Union[Any, None] =, allow_redirects: builtins.bool =, proxies: Union[Any, None] =, hooks: Union[Any, None] =, stream: Union[Any, None] =, verify: Union[Any, None] =, cert: Union[Any, None] =, json: Union[Any, None] =) -> requests.models.Response"
```
* Drive-by comment in `synapse.storage.types`
* No untyped defs in `synapse_port_db`
This was by far the most painful. I'm happy to break this up into
smaller pieces for review if it's not managable as-is.
2022-04-11 07:41:55 -04:00
|
|
|
from unittest.mock import Mock, patch
|
2018-10-26 07:05:22 -04:00
|
|
|
|
|
|
|
from synapse._scripts.register_new_matrix_user import request_registration
|
2023-02-08 16:29:49 -05:00
|
|
|
from synapse.types import JsonDict
|
2018-10-26 07:05:22 -04:00
|
|
|
|
|
|
|
from tests.unittest import TestCase
|
|
|
|
|
|
|
|
|
|
|
|
class RegisterTestCase(TestCase):
|
2023-02-08 16:29:49 -05:00
|
|
|
def test_success(self) -> None:
|
2018-10-26 07:05:22 -04:00
|
|
|
"""
|
|
|
|
The script will fetch a nonce, and then generate a MAC with it, and then
|
|
|
|
post that MAC.
|
|
|
|
"""
|
|
|
|
|
2023-02-08 16:29:49 -05:00
|
|
|
def get(url: str, verify: Optional[bool] = None) -> Mock:
|
2018-10-26 07:05:22 -04:00
|
|
|
r = Mock()
|
|
|
|
r.status_code = 200
|
|
|
|
r.json = lambda: {"nonce": "a"}
|
|
|
|
return r
|
|
|
|
|
2023-02-08 16:29:49 -05:00
|
|
|
def post(
|
|
|
|
url: str, json: Optional[JsonDict] = None, verify: Optional[bool] = None
|
|
|
|
) -> Mock:
|
2018-10-26 07:05:22 -04:00
|
|
|
# Make sure we are sent the correct info
|
2023-02-08 16:29:49 -05:00
|
|
|
assert json is not None
|
2018-10-26 07:05:22 -04:00
|
|
|
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
|
2022-05-23 07:23:26 -04:00
|
|
|
out: List[str] = []
|
|
|
|
err_code: List[int] = []
|
2018-10-26 07:05:22 -04:00
|
|
|
|
Disallow untyped defs in synapse._scripts (#12422)
Of note:
* No untyped defs in `register_new_matrix_user`
This one might be contraversial. `request_registration` has three
dependency-injection arguments used for testing. I'm removing the
injection of the `requests` module and using `unitest.mock.patch` in the
test cases instead.
Doing `reveal_type(requests)` and `reveal_type(requests.get)` before the
change:
```
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "Any"
synapse/_scripts/register_new_matrix_user.py:46: note: Revealed type is "Any"
```
And after:
```
synapse/_scripts/register_new_matrix_user.py:44: note: Revealed type is "types.ModuleType"
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "def (url: Union[builtins.str, builtins.bytes], params: Union[Union[_typeshed.SupportsItems[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], typing.Iterable[Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]]], builtins.str, builtins.bytes], None] =, data: Union[Any, None] =, headers: Union[Any, None] =, cookies: Union[Any, None] =, files: Union[Any, None] =, auth: Union[Any, None] =, timeout: Union[Any, None] =, allow_redirects: builtins.bool =, proxies: Union[Any, None] =, hooks: Union[Any, None] =, stream: Union[Any, None] =, verify: Union[Any, None] =, cert: Union[Any, None] =, json: Union[Any, None] =) -> requests.models.Response"
```
* Drive-by comment in `synapse.storage.types`
* No untyped defs in `synapse_port_db`
This was by far the most painful. I'm happy to break this up into
smaller pieces for review if it's not managable as-is.
2022-04-11 07:41:55 -04:00
|
|
|
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,
|
|
|
|
)
|
2018-10-26 07:05:22 -04:00
|
|
|
|
|
|
|
# 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, [])
|
|
|
|
|
2023-02-08 16:29:49 -05:00
|
|
|
def test_failure_nonce(self) -> None:
|
2018-10-26 07:05:22 -04:00
|
|
|
"""
|
|
|
|
If the script fails to fetch a nonce, it throws an error and quits.
|
|
|
|
"""
|
|
|
|
|
2023-02-08 16:29:49 -05:00
|
|
|
def get(url: str, verify: Optional[bool] = None) -> Mock:
|
2018-10-26 07:05:22 -04:00
|
|
|
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
|
2022-05-23 07:23:26 -04:00
|
|
|
out: List[str] = []
|
|
|
|
err_code: List[int] = []
|
2018-10-26 07:05:22 -04:00
|
|
|
|
Disallow untyped defs in synapse._scripts (#12422)
Of note:
* No untyped defs in `register_new_matrix_user`
This one might be contraversial. `request_registration` has three
dependency-injection arguments used for testing. I'm removing the
injection of the `requests` module and using `unitest.mock.patch` in the
test cases instead.
Doing `reveal_type(requests)` and `reveal_type(requests.get)` before the
change:
```
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "Any"
synapse/_scripts/register_new_matrix_user.py:46: note: Revealed type is "Any"
```
And after:
```
synapse/_scripts/register_new_matrix_user.py:44: note: Revealed type is "types.ModuleType"
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "def (url: Union[builtins.str, builtins.bytes], params: Union[Union[_typeshed.SupportsItems[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], typing.Iterable[Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]]], builtins.str, builtins.bytes], None] =, data: Union[Any, None] =, headers: Union[Any, None] =, cookies: Union[Any, None] =, files: Union[Any, None] =, auth: Union[Any, None] =, timeout: Union[Any, None] =, allow_redirects: builtins.bool =, proxies: Union[Any, None] =, hooks: Union[Any, None] =, stream: Union[Any, None] =, verify: Union[Any, None] =, cert: Union[Any, None] =, json: Union[Any, None] =) -> requests.models.Response"
```
* Drive-by comment in `synapse.storage.types`
* No untyped defs in `synapse_port_db`
This was by far the most painful. I'm happy to break this up into
smaller pieces for review if it's not managable as-is.
2022-04-11 07:41:55 -04:00
|
|
|
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,
|
|
|
|
)
|
2018-10-26 07:05:22 -04:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2023-02-08 16:29:49 -05:00
|
|
|
def test_failure_post(self) -> None:
|
2018-10-26 07:05:22 -04:00
|
|
|
"""
|
|
|
|
The script will fetch a nonce, and then if the final POST fails, will
|
|
|
|
report an error and quit.
|
|
|
|
"""
|
|
|
|
|
2023-02-08 16:29:49 -05:00
|
|
|
def get(url: str, verify: Optional[bool] = None) -> Mock:
|
2018-10-26 07:05:22 -04:00
|
|
|
r = Mock()
|
|
|
|
r.status_code = 200
|
|
|
|
r.json = lambda: {"nonce": "a"}
|
|
|
|
return r
|
|
|
|
|
2023-02-08 16:29:49 -05:00
|
|
|
def post(
|
|
|
|
url: str, json: Optional[JsonDict] = None, verify: Optional[bool] = None
|
|
|
|
) -> Mock:
|
2018-10-26 07:05:22 -04:00
|
|
|
# Make sure we are sent the correct info
|
2023-02-08 16:29:49 -05:00
|
|
|
assert json is not None
|
2018-10-26 07:05:22 -04:00
|
|
|
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
|
2022-05-23 07:23:26 -04:00
|
|
|
out: List[str] = []
|
|
|
|
err_code: List[int] = []
|
2018-10-26 07:05:22 -04:00
|
|
|
|
Disallow untyped defs in synapse._scripts (#12422)
Of note:
* No untyped defs in `register_new_matrix_user`
This one might be contraversial. `request_registration` has three
dependency-injection arguments used for testing. I'm removing the
injection of the `requests` module and using `unitest.mock.patch` in the
test cases instead.
Doing `reveal_type(requests)` and `reveal_type(requests.get)` before the
change:
```
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "Any"
synapse/_scripts/register_new_matrix_user.py:46: note: Revealed type is "Any"
```
And after:
```
synapse/_scripts/register_new_matrix_user.py:44: note: Revealed type is "types.ModuleType"
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "def (url: Union[builtins.str, builtins.bytes], params: Union[Union[_typeshed.SupportsItems[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], typing.Iterable[Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]]], builtins.str, builtins.bytes], None] =, data: Union[Any, None] =, headers: Union[Any, None] =, cookies: Union[Any, None] =, files: Union[Any, None] =, auth: Union[Any, None] =, timeout: Union[Any, None] =, allow_redirects: builtins.bool =, proxies: Union[Any, None] =, hooks: Union[Any, None] =, stream: Union[Any, None] =, verify: Union[Any, None] =, cert: Union[Any, None] =, json: Union[Any, None] =) -> requests.models.Response"
```
* Drive-by comment in `synapse.storage.types`
* No untyped defs in `synapse_port_db`
This was by far the most painful. I'm happy to break this up into
smaller pieces for review if it's not managable as-is.
2022-04-11 07:41:55 -04:00
|
|
|
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,
|
|
|
|
)
|
2018-10-26 07:05:22 -04:00
|
|
|
|
|
|
|
# Exit was called
|
|
|
|
self.assertEqual(err_code, [1])
|
|
|
|
|
|
|
|
# We got an error message
|
|
|
|
self.assertIn("ERROR! Received 500 Broken", out)
|
|
|
|
self.assertNotIn("Success!", out)
|