This repository has been archived by the owner on Sep 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added first iintegration tests -- mock http server
- Loading branch information
1 parent
2715f49
commit 4ad2963
Showing
8 changed files
with
165 additions
and
9 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
pymatrix_tests/integration_tests/helpers/mock_matrix_server.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import http.server | ||
import io | ||
import sys | ||
|
||
class StaticResponseHTTPRequestHandler(http.server.BaseHTTPRequestHandler): | ||
""" | ||
Returns a static string response with a predetermined HTTP status code. | ||
""" | ||
status_code = None | ||
response_body = None | ||
|
||
def setup_response(status_code, response_body): | ||
StaticResponseHTTPRequestHandler.status_code = status_code | ||
StaticResponseHTTPRequestHandler.response_body = response_body | ||
|
||
def __init__(self, request, client_address, server): | ||
if(StaticResponseHTTPRequestHandler.status_code is None | ||
or StaticResponseHTTPRequestHandler.response_body is None): | ||
raise Exception("Error no status code or response body") | ||
super().__init__(request, client_address, server) | ||
|
||
def send_static_response(self): | ||
self.send_response_only(StaticResponseHTTPRequestHandler.status_code) | ||
self.send_header("Content-Type", "application/json") | ||
self.end_headers() | ||
self.wfile.write(StaticResponseHTTPRequestHandler.response_body \ | ||
.encode(sys.getfilesystemencoding())) | ||
|
||
|
||
def do_GET(self): | ||
self.send_static_response() | ||
def do_POST(self): | ||
self.send_static_response() | ||
def do_PUT(self): | ||
self.send_static_response() | ||
def do_DELETE(self): | ||
self.send_static_response() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from pymatrix_tests.framework.fixture import TestClassBase, testmethod | ||
from pymatrix_tests.framework.asserts import Assert | ||
from pymatrix_tests.integration_tests.helpers.mock_matrix_server import StaticResponseHTTPRequestHandler | ||
import pymatrix.client | ||
import pymatrix.specification.base | ||
import pymatrix.specification.r0.login | ||
import asyncio | ||
import http | ||
import http.server | ||
import threading | ||
|
||
server_hostname = "localhost" | ||
server_port = 49993 | ||
|
||
class LoginTests(TestClassBase): | ||
|
||
client = None | ||
server = None | ||
worker_thread = None | ||
|
||
def test_method_init(self): | ||
self.server = http.server.HTTPServer((server_hostname, server_port), StaticResponseHTTPRequestHandler) | ||
self.server.timeout = 0.1 # will hang for .1 second max | ||
self.worker_thread = threading.Thread(target=self.server.handle_request) | ||
self.worker_thread.start() | ||
|
||
|
||
self.client = pymatrix.client.ClientFactory.get_client() | ||
asyncio.get_event_loop(). \ | ||
run_until_complete(self.client.connect(server_hostname, server_port)) | ||
|
||
def test_method_cleanup(self): | ||
asyncio.get_event_loop().run_until_complete(self.client.logout()) | ||
self.worker_thread.join() | ||
self.server.server_close() | ||
|
||
# securely reset the state | ||
StaticResponseHTTPRequestHandler.response_body = None | ||
StaticResponseHTTPRequestHandler.status_code = None | ||
|
||
@testmethod | ||
def T_login_should_succeed(self): | ||
# arrange | ||
username = "local_username" | ||
password = "correct_password" | ||
response_home_server = "localhost" | ||
response_user_id = "@{}:{}".format(username, response_home_server) | ||
response_access_token = "ABCDE123456" | ||
response_device_id = "DEVICE123" | ||
response_json = "{{\"user_id\": \"{}\", \ | ||
\"access_token\": \"{}\", \ | ||
\"home_server\": \"{}\", \ | ||
\"device_id\": \"{}\"}}" \ | ||
.format(response_user_id, response_access_token, | ||
response_home_server, response_device_id) | ||
StaticResponseHTTPRequestHandler.setup_response( | ||
http.HTTPStatus.OK, response_json) | ||
|
||
# act | ||
response = asyncio.get_event_loop() \ | ||
.run_until_complete(self.client.login(username, password)) | ||
|
||
# assert | ||
assert isinstance(response, | ||
pymatrix.specification.r0.login.LoginResponseMessage) | ||
assert response.user_id == response_user_id | ||
assert response.home_server == response_home_server | ||
|
||
@testmethod | ||
def T_login_should_fail(self): | ||
# arrange | ||
username = "karthanistyr" | ||
password = "wrongpassword" | ||
errcode = "M_WRONG_PASSWORD" | ||
error = "Error: wrong password" | ||
response_json = "{{\"errcode\": \"{}\", \"error\": \"{}\"}}" \ | ||
.format(errcode, error) | ||
StaticResponseHTTPRequestHandler.setup_response( | ||
http.HTTPStatus.FORBIDDEN, response_json) | ||
|
||
loop = asyncio.get_event_loop() | ||
|
||
# act | ||
response = asyncio.get_event_loop() \ | ||
.run_until_complete(self.client.login(username, password)) | ||
|
||
# assert | ||
assert isinstance(response, | ||
pymatrix.specification.base.ErrorMessageBase) | ||
assert response.error == error | ||
assert response.errcode == errcode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters