Skip to content

Commit

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

from typing import Optional

import requests
Expand Down Expand Up @@ -98,7 +109,6 @@ def create_access_key(self, name: Optional[str] = None, password: Optional[str]
Raises:
ValidationError: If the server response is not 201 or if there's an issue with the request.
"""
# Create request_data dictionary only with non-None values
request_data = {
"name": name,
"password": password,
Expand All @@ -110,7 +120,7 @@ def create_access_key(self, name: Optional[str] = None, password: Optional[str]
if request_data:
request_data = AccessKeyCreateRequest(**request_data).model_dump(mode="json")
else:
request_data = {}
request_data = None # Use None instead of an empty dictionary

response = self._request("POST", "access-keys", json_data=request_data)

Expand Down
4 changes: 2 additions & 2 deletions pyoutlineapi/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
Copyright (c) 2024 Denis Rozhnovskiy <pytelemonbot@mail.ru>
This file is part of the PyOutline project.
This file is part of the PyOutlineAPI project.
PyOutline is a Python package for interacting with the Outline VPN Server.
PyOutlineAPI is a Python package for interacting with the Outline VPN Server.
Licensed under the MIT License. See the LICENSE file for more details.
Expand Down
4 changes: 2 additions & 2 deletions pyoutlineapi/logger.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
Copyright (c) 2024 Denis Rozhnovskiy <pytelemonbot@mail.ru>
This file is part of the PyOutline project.
This file is part of the PyOutlineAPI project.
PyOutline is a Python package for interacting with the Outline VPN Server.
PyOutlineAPI is a Python package for interacting with the Outline VPN Server.
Licensed under the MIT License. See the LICENSE file for more details.
Expand Down
4 changes: 2 additions & 2 deletions pyoutlineapi/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
Copyright (c) 2024 Denis Rozhnovskiy <pytelemonbot@mail.ru>
This file is part of the PyOutline project.
This file is part of the PyOutlineAPI project.
PyOutline is a Python package for interacting with the Outline VPN Server.
PyOutlineAPI is a Python package for interacting with the Outline VPN Server.
Licensed under the MIT License. See the LICENSE file for more details.
Expand Down
48 changes: 17 additions & 31 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
"""
Copyright (c) 2024 Denis Rozhnovskiy <pytelemonbot@mail.ru>
This file is part of the PyOutlineAPI project.
PyOutlineAPI 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

import requests

from pyoutlineapi import models as models, client as pyoutline_client, exceptions as exceptions
from pyoutlineapi.exceptions import ValidationError


class TestPyOutlineWrapper(unittest.TestCase):
Expand Down Expand Up @@ -36,43 +48,17 @@ def test_get_server_info(self, mock_request):
self.assertEqual(server_info.portForNewAccessKeys, 8080)
mock_request.assert_called_once_with("GET", f"{self.api_url}/server", json=None, verify=True)

@patch('pyoutlineapi.client.requests.Session.request')
def test_create_access_key(self, mock_request):
"""Test the create_access_key method."""
def test_create_access_key_invalid_data(self, mock_request):
"""Test create_access_key with invalid data."""
mock_response = Mock()
mock_response.status_code = 201
mock_response.status_code = 400
mock_response.json.return_value = {
"id": "key_id",
"name": "Test Key",
"password": "secret_password",
"port": 1234,
"method": "method",
"accessUrl": "https://example.com"
}
mock_request.return_value = mock_response

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")

# 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)
with self.assertRaises(ValidationError):
self.api.create_access_key(name="Test Key", password="secret_password", port=1234)

@patch('pyoutlineapi.client.requests.Session.request')
def test_get_access_keys(self, mock_request):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
"""
Copyright (c) 2024 Denis Rozhnovskiy <pytelemonbot@mail.ru>
This file is part of the PyOutlineAPI project.
PyOutlineAPI 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 pyoutlineapi.exceptions import APIError, HTTPError, RequestError, ValidationError

Expand Down
5 changes: 2 additions & 3 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"""
Copyright (c) 2024 Denis Rozhnovskiy <pytelemonbot@mail.ru>
This file is part of the PyOutline project.
This file is part of the PyOutlineAPI project.
PyOutline is a Python package for interacting with the Outline VPN Server.
PyOutlineAPI is a Python package for interacting with the Outline VPN Server.
Licensed under the MIT License. See the LICENSE file for more details.
"""

import io
import logging
import unittest
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
Copyright (c) 2024 Denis Rozhnovskiy <pytelemonbot@mail.ru>
This file is part of the PyOutline project.
This file is part of the PyOutlineAPI project.
PyOutline is a Python package for interacting with the Outline VPN Server.
PyOutlineAPI is a Python package for interacting with the Outline VPN Server.
Licensed under the MIT License. See the LICENSE file for more details.
Expand Down

0 comments on commit f37a447

Please sign in to comment.