Skip to content

Commit

Permalink
Completed the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
orenlab committed Aug 28, 2024
1 parent 5e4070c commit 9194cf7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 54 deletions.
43 changes: 21 additions & 22 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
"""
Copyright (c) 2024 Denis Rozhnovskiy <pytelemonbot@mail.ru>
This file is part of the PyOutline project.
PyOutline is a Python package for interacting with the Outline VPN Server.
Licensed under the MIT License. See the LICENSE file for more details.
"""

import unittest
from unittest.mock import patch, Mock

Expand All @@ -17,7 +6,7 @@
from pyoutlineapi import models as models, client as pyoutline_client, exceptions as exceptions


class TestPyOutline(unittest.TestCase):
class TestPyOutlineWrapper(unittest.TestCase):
def setUp(self):
"""Setup for test cases."""
self.api_url = "https://api.example.com"
Expand Down Expand Up @@ -62,15 +51,28 @@ def test_create_access_key(self, mock_request):
}
mock_request.return_value = mock_response

access_key = self.api.create_access_key()
access_key = self.api.create_access_key(name="Test Key", password="secret_password", port=1234)
self.assertIsInstance(access_key, models.AccessKey)
self.assertEqual(access_key.id, "key_id")
self.assertEqual(access_key.name, "Test Key")
self.assertEqual(access_key.password.get_secret_value(), "secret_password")
self.assertEqual(access_key.port, 1234)
self.assertEqual(access_key.method, "method")
self.assertEqual(access_key.accessUrl.get_secret_value(), "https://example.com")
mock_request.assert_called_once_with("POST", f"{self.api_url}/access-keys", json=None, verify=True)

# Print the actual call arguments for debugging
print("Actual call arguments:", mock_request.call_args)

# Ensure the correct URL and parameters are used
mock_request.assert_called_once_with(
"POST",
f"{self.api_url}/access-keys",
json={
"name": "Test Key",
"password": "secret_password",
"port": 1234
},
verify=True)

@patch('pyoutlineapi.client.requests.Session.request')
def test_get_access_keys(self, mock_request):
Expand Down Expand Up @@ -189,12 +191,10 @@ def test_get_server_info_invalid_data(self, mock_request):
"""Test handling of invalid data from get_server_info."""
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {
"unexpectedField": "unexpectedValue"
}
mock_response.json.return_value = {"unexpectedField": "unexpectedValue"}
mock_request.return_value = mock_response

with self.assertRaises(ValueError):
with self.assertRaises(exceptions.ValidationError):
self.api.get_server_info()

@patch('pyoutlineapi.client.requests.Session.request')
Expand All @@ -206,14 +206,13 @@ def test_create_access_key_invalid_data(self, mock_request):
"id": "key_id",
"name": "Test Key",
"password": "secret_password",
"port": 1234,
"method": "method"
"port": 1234
# Missing 'accessUrl'
}
mock_request.return_value = mock_response

with self.assertRaises(ValueError):
self.api.create_access_key()
with self.assertRaises(exceptions.ValidationError):
self.api.create_access_key(name="Test Key", password="secret_password", port=1234)


if __name__ == '__main__':
Expand Down
64 changes: 32 additions & 32 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
"""
Copyright (c) 2024 Denis Rozhnovskiy <pytelemonbot@mail.ru>
This file is part of the PyOutline project.
PyOutline is a Python package for interacting with the Outline VPN Server.
Licensed under the MIT License. See the LICENSE file for more details.
"""

from pyoutlineapi import exceptions as e


def test_api_error():
error = e.APIError("API general error")
assert str(error) == "API general error"
assert isinstance(error, Exception)


def test_request_error():
error = e.RequestError("Connection failed")
assert str(error) == "An error occurred while requesting data: Connection failed"
assert isinstance(error, e.APIError)
assert isinstance(error, Exception)


def test_validation_error():
error = e.ValidationError("Invalid data format")
assert str(error) == "Validation error occurred: Invalid data format"
assert isinstance(error, e.APIError)
assert isinstance(error, Exception)
import unittest
from pyoutlineapi.exceptions import APIError, HTTPError, RequestError, ValidationError

class TestAPIError(unittest.TestCase):
def test_api_error_initialization(self):
"""Test initialization of APIError."""
error = APIError("Test API Error")
self.assertEqual(str(error), "Test API Error")
self.assertEqual(error.message, "Test API Error")

class TestHTTPError(unittest.TestCase):
def test_http_error_initialization(self):
"""Test initialization of HTTPError."""
error = HTTPError(404, "Not Found")
self.assertEqual(str(error), "HTTP error occurred: 404 - Not Found")
self.assertEqual(error.status_code, 404)
self.assertEqual(error.message, "Not Found")

class TestRequestError(unittest.TestCase):
def test_request_error_initialization(self):
"""Test initialization of RequestError."""
error = RequestError("Connection failed")
self.assertEqual(str(error), "An error occurred while requesting data: Connection failed")

class TestValidationError(unittest.TestCase):
def test_validation_error_initialization(self):
"""Test initialization of ValidationError."""
error = ValidationError("Invalid data format")
self.assertEqual(str(error), "Validation error occurred: Invalid data format")

if __name__ == '__main__':
unittest.main()

0 comments on commit 9194cf7

Please sign in to comment.