Skip to content

Commit 32604ea

Browse files
authored
Add API test resource (#30)
1 parent 150949c commit 32604ea

8 files changed

Lines changed: 112 additions & 22 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ All notable changes in **python-transip** are documented below.
33

44
## [Unreleased]
55
### Added
6-
- This CHANGELOG file to be able to list all notable changes for each version of **python-transip**.
6+
- This `CHANGELOG.md` file to be able to list all notable changes for each version of **python-transip**.
7+
- The `transip.v6.objects.ApiTestService` service to allow calling the test resource to make sure everything is working.
78

89
[Unreleased]: https://github.com/roaldnefs/python-transip/compare/v0.3.0...HEAD

tests/fixtures/general.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
[
2+
{
3+
"method": "GET",
4+
"url": "https://api.transip.nl/v6/api-test",
5+
"json": {"ping": "pong"},
6+
"status": 200,
7+
"content_type": "application/json"
8+
},
29
{
310
"method": "GET",
411
"url": "https://api.transip.nl/v6/products",

tests/services/test_api_test.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2021 Roald Nefs <info@roaldnefs.com>
4+
#
5+
# This file is part of python-transip.
6+
#
7+
# python-transip is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# python-transip is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser General Public License
18+
# along with python-transip. If not, see <https://www.gnu.org/licenses/>.
19+
20+
import responses # type: ignore
21+
import unittest
22+
23+
from transip import TransIP
24+
25+
from tests.utils import load_responses_fixtures
26+
27+
28+
class ApiTestTest(unittest.TestCase):
29+
"""Test the ApiTestService."""
30+
31+
client: TransIP
32+
33+
@classmethod
34+
def setUpClass(cls) -> None:
35+
"""Set up a minimal TransIP client for using the API test services."""
36+
cls.client = TransIP(access_token='ACCESS_TOKEN')
37+
38+
def setUp(self) -> None:
39+
"""Setup mocked responses for the '/api-test' endpoint."""
40+
load_responses_fixtures("general.json")
41+
42+
@responses.activate
43+
def test_test(self) -> None:
44+
"""Test if the API test returns True."""
45+
self.assertTrue(self.client.api_test.test()) # type: ignore

tests/services/test_domains.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2020, 2012 Roald Nefs <info@roaldnefs.com>
3+
# Copyright (C) 2020, 2021 Roald Nefs <info@roaldnefs.com>
44
#
55
# This file is part of python-transip.
66
#
@@ -43,13 +43,13 @@ def setUp(self) -> None:
4343

4444
@responses.activate
4545
def test_get(self) -> None:
46-
domain: Domain = self.client.domains.get("example.com")
46+
domain: Domain = self.client.domains.get("example.com") # type: ignore
4747

4848
assert domain.get_id() == "example.com" # type: ignore
4949

5050
@responses.activate
5151
def test_contacts_list(self) -> None:
52-
domain: Domain = self.client.domains.get("example.com")
52+
domain: Domain = self.client.domains.get("example.com") # type: ignore
5353
contacts: List[Domain] = domain.contacts.list() # type: ignore
5454
contact: Domain = contacts[0]
5555

@@ -58,7 +58,7 @@ def test_contacts_list(self) -> None:
5858

5959
@responses.activate
6060
def test_nameservers_list(self) -> None:
61-
domain: Domain = self.client.domains.get("example.com")
61+
domain: Domain = self.client.domains.get("example.com") # type: ignore
6262
nameservers: List[Nameserver] = domain.nameservers.list() # type: ignore
6363
nameserver: Nameserver = nameservers[0]
6464

@@ -67,7 +67,7 @@ def test_nameservers_list(self) -> None:
6767

6868
@responses.activate
6969
def test_dns_list(self) -> None:
70-
domain: Domain = self.client.domains.get("example.com")
70+
domain: Domain = self.client.domains.get("example.com") # type: ignore
7171
entries: List[DnsEntry] = domain.dns.list() # type: ignore
7272
entry: DnsEntry = entries[0]
7373

@@ -82,7 +82,7 @@ def test_dns_create(self) -> None:
8282
"type": "A",
8383
"content": "127.0.0.1"
8484
}
85-
domain: Domain = self.client.domains.get("example.com")
85+
domain: Domain = self.client.domains.get("example.com") # type: ignore
8686
domain.dns.create(dns_entry_data) # type: ignore
8787

8888
assert len(responses.calls) == 2

tests/services/test_invoices.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def setUp(self) -> None:
4141

4242
@responses.activate
4343
def test_list(self) -> None:
44-
invoices: List[Invoice] = self.client.invoices.list()
44+
invoices: List[Invoice] = self.client.invoices.list() # type: ignore
4545
invoice: Invoice = invoices[0]
4646

4747
assert len(invoices) == 1
@@ -50,6 +50,6 @@ def test_list(self) -> None:
5050
@responses.activate
5151
def test_get(self) -> None:
5252
invoice_id: str = "F0000.1911.0000.0004"
53-
invoice: Invoice = self.client.invoices.get(invoice_id)
53+
invoice: Invoice = self.client.invoices.get(invoice_id) # type: ignore
5454

5555
assert invoice.get_id() == "F0000.1911.0000.0004" # type: ignore

tests/services/test_ssh_keys.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def setUp(self) -> None:
4242

4343
@responses.activate
4444
def test_list(self) -> None:
45-
ssh_keys: List[SshKey] = self.client.ssh_keys.list()
45+
ssh_keys: List[SshKey] = self.client.ssh_keys.list() # type: ignore
4646
ssh_key: SshKey = ssh_keys[0]
4747

4848
assert len(ssh_keys) == 1
@@ -51,22 +51,22 @@ def test_list(self) -> None:
5151
@responses.activate
5252
def test_get(self) -> None:
5353
ssh_key_id: int = 123
54-
ssh_key: SshKey = self.client.ssh_keys.get(ssh_key_id)
54+
ssh_key: SshKey = self.client.ssh_keys.get(ssh_key_id) # type: ignore
5555

5656
assert ssh_key.get_id() == 123 # type: ignore
5757

5858
@responses.activate
5959
def test_delete(self) -> None:
6060
ssh_key_id: int = 123
6161
try:
62-
self.client.ssh_keys.delete(ssh_key_id)
62+
self.client.ssh_keys.delete(ssh_key_id) # type: ignore
6363
except Exception as exc:
6464
assert False, f"'transip.TransIP.ssh_keys.delete' raised an exception {exc}"
6565

6666
@responses.activate
6767
def test_delete_object(self) -> None:
6868
ssh_key_id: int = 123
69-
ssh_key: SshKey = self.client.ssh_keys.get(ssh_key_id)
69+
ssh_key: SshKey = self.client.ssh_keys.get(ssh_key_id) # type: ignore
7070

7171
try:
7272
ssh_key.delete() # type: ignore
@@ -79,7 +79,7 @@ def test_create(self) -> None:
7979
"sshKey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDf2pxWX/yhUBDyk2LPhvRtI0LnVO8PyR5Zt6AHrnhtLGqK+8YG9EMlWbCCWrASR+Q1hFQG example",
8080
"description": "Jim key"
8181
}
82-
self.client.ssh_keys.create(ssh_key_data)
82+
self.client.ssh_keys.create(ssh_key_data) # type: ignore
8383

8484
assert len(responses.calls) == 1
8585

@@ -91,6 +91,8 @@ def test_update(self) -> None:
9191
}
9292

9393
try:
94-
self.client.ssh_keys.update(ssh_key_id, ssh_key_data)
94+
self.client.ssh_keys.update( # type: ignore
95+
ssh_key_id, ssh_key_data
96+
)
9597
except Exception as exc:
9698
assert False, f"'transip.TransIP.ssh_keys.update' raised an exception {exc}"

transip/__init__.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
# along with python-transip. If not, see <https://www.gnu.org/licenses/>.
1919
"""Wrapper for the TransIP API."""
2020

21-
from typing import Dict, Optional, Any, Type, Union
21+
from typing import Dict, Optional, Any, Type, Union, TYPE_CHECKING
22+
from types import ModuleType
2223

2324
import importlib
2425
import requests
@@ -36,6 +37,12 @@
3637
__license__ = "LGPL3"
3738

3839

40+
if TYPE_CHECKING:
41+
# Imports only needed for type checking. These will not be imported at
42+
# runtime.
43+
from transip.base import ApiService
44+
45+
3946
class TransIP:
4047
"""Represents a TransIP server connection.
4148
@@ -76,17 +83,28 @@ def __init__(
7683
self._set_auth_info()
7784

7885
# Dynamically import the services for the specified API version
79-
objects = importlib.import_module(f"transip.v{api_version}.objects")
86+
objects: ModuleType = (
87+
importlib.import_module(f"transip.v{api_version}.objects")
88+
)
8089

81-
self.availability_zones: Type[Any] = (
90+
self.api_test: Type['ApiService'] = (
91+
objects.ApiTestService(self) # type: ignore
92+
)
93+
self.availability_zones: Type['ApiService'] = (
8294
objects.AvailabilityZoneService(self) # type: ignore
8395
)
84-
self.domains: Type[Any] = objects.DomainService(self) # type: ignore
85-
self.invoices: Type[Any] = (
96+
self.domains: Type['ApiService'] = (
97+
objects.DomainService(self) # type: ignore
98+
)
99+
self.invoices: Type['ApiService'] = (
86100
objects.InvoiceService(self) # type: ignore
87101
)
88-
self.ssh_keys: Type[Any] = objects.SshKeyService(self) # type: ignore
89-
self.vpss: Type[Any] = objects.VpsService(self) # type: ignore
102+
self.ssh_keys: Type['ApiService'] = (
103+
objects.SshKeyService(self) # type: ignore
104+
)
105+
self.vpss: Type['ApiService'] = (
106+
objects.VpsService(self) # type: ignore
107+
)
90108

91109
@property
92110
def url(self) -> str:

transip/v6/objects.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@
2727
)
2828

2929

30+
class ApiTestService(ApiService):
31+
32+
_path: str = "/api-test"
33+
34+
def test(self):
35+
"""
36+
A simple test to make sure everything is working.
37+
38+
Returns:
39+
bool: True if everything is working, False otherwise.
40+
"""
41+
response = self.client.get(f"{self.path}")
42+
if response.get('ping') == 'pong':
43+
return True
44+
return False
45+
46+
3047
class AvailabilityZone(ApiObject):
3148

3249
_id_attr: str = "name"

0 commit comments

Comments
 (0)