mirror of
https://github.com/KevinMidboe/cloudflare-ddns.git
synced 2025-10-29 01:20:17 +00:00
Unit tests for valid IP, public IP & CF API requests
This commit is contained in:
20
.drone.yml
20
.drone.yml
@@ -1,3 +1,21 @@
|
||||
---
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: Test
|
||||
|
||||
|
||||
platform:
|
||||
os: linux
|
||||
arch: amd64
|
||||
|
||||
steps:
|
||||
- name: Run unit tests
|
||||
image: python:3.10-alpine
|
||||
commands:
|
||||
- pip install -r requirements.txt
|
||||
- pip install -r requirements-test.txt
|
||||
- python3 -m unittest tests/test_*.py -v
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
type: docker
|
||||
@@ -99,6 +117,6 @@ volumes:
|
||||
|
||||
---
|
||||
kind: signature
|
||||
hmac: d3088aaf784f4eaac3223f43a86a19bfccff416fd854351c527d785002ae2c26
|
||||
hmac: 65e2dbf1e3ea133f91518bee0532663a3e3941d05c8335114141910dcca8f660
|
||||
|
||||
...
|
||||
|
||||
2
tests/__init__.py
Normal file
2
tests/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from src.logger import logger
|
||||
logger.setLevel('WARNING')
|
||||
80
tests/test_cloudflare_dns_record_ip.py
Normal file
80
tests/test_cloudflare_dns_record_ip.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import re
|
||||
import unittest
|
||||
import responses
|
||||
from src.main import cloudflareDDNS
|
||||
|
||||
MOCK_IP = '44.208.147.61'
|
||||
CLOUDFLARE_GET_RECORDS_URL = re.compile(
|
||||
r"https\:\/\/api.cloudflare.com\/client\/v4\/zones\/\w*\/dns_records\?type\=A")
|
||||
CLOUDFLARE_ADDR_RECORD_EXISTS_RESPONSE = {
|
||||
'success': True,
|
||||
'result': [{
|
||||
'content': MOCK_IP,
|
||||
'name': 'addr.',
|
||||
'id': 'id',
|
||||
'ttl': 86400,
|
||||
'proxied': True
|
||||
}]
|
||||
}
|
||||
CLOUDFLARE_ADDR_RECORD_NONEXISTANT_RESPONSE = {
|
||||
'success': True,
|
||||
'result': [{
|
||||
'content': MOCK_IP,
|
||||
'name': None,
|
||||
'id': 'id',
|
||||
'ttl': 86400,
|
||||
'proxied': True
|
||||
}]
|
||||
}
|
||||
CLOUDFLARE_500_RESPONSE = {
|
||||
'success': False,
|
||||
'errors': 'someerror'
|
||||
}
|
||||
|
||||
|
||||
class TestCloudflareDNSRecordIP(unittest.TestCase):
|
||||
|
||||
@responses.activate
|
||||
def test_successfull_response(self):
|
||||
responses.add(responses.GET, CLOUDFLARE_GET_RECORDS_URL,
|
||||
json=CLOUDFLARE_ADDR_RECORD_EXISTS_RESPONSE, status=200)
|
||||
|
||||
ip = cloudflareDDNS()
|
||||
|
||||
self.assertEqual(MOCK_IP, ip)
|
||||
|
||||
@responses.activate
|
||||
def test_addr_record_exists(self):
|
||||
responses.add(responses.GET, CLOUDFLARE_GET_RECORDS_URL,
|
||||
json=CLOUDFLARE_ADDR_RECORD_NONEXISTANT_RESPONSE,
|
||||
status=200)
|
||||
|
||||
self.assertRaises(Exception, cloudflareDDNS)
|
||||
|
||||
@responses.activate
|
||||
def test_cloudflare_500_response(self):
|
||||
responses.add(responses.GET, CLOUDFLARE_GET_RECORDS_URL,
|
||||
json=CLOUDFLARE_500_RESPONSE,
|
||||
status=500)
|
||||
|
||||
self.assertRaises(Exception, cloudflareDDNS)
|
||||
|
||||
@responses.activate
|
||||
def test_cloudflare_empty_200_response(self):
|
||||
responses.add(responses.GET, CLOUDFLARE_GET_RECORDS_URL,
|
||||
json={},
|
||||
status=500)
|
||||
|
||||
self.assertRaises(Exception, cloudflareDDNS)
|
||||
|
||||
@responses.activate
|
||||
def test_cloudflare_empty_500_response(self):
|
||||
responses.add(responses.GET, CLOUDFLARE_GET_RECORDS_URL,
|
||||
json={},
|
||||
status=500)
|
||||
|
||||
self.assertRaises(Exception, cloudflareDDNS)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
33
tests/test_ip_address.py
Normal file
33
tests/test_ip_address.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import unittest
|
||||
from src.main import validIP
|
||||
|
||||
MOCK_IP = "44.208.147.61"
|
||||
|
||||
|
||||
class TestIPAddress(unittest.TestCase):
|
||||
def test_valid_ip(self):
|
||||
self.assertTrue(validIP(MOCK_IP))
|
||||
|
||||
def test_invalid_ip(self):
|
||||
ip = "256.0.0.1"
|
||||
self.assertFalse(validIP(ip))
|
||||
|
||||
def test_invalid_format(self):
|
||||
ip = "192.168.1"
|
||||
self.assertFalse(validIP(ip))
|
||||
|
||||
def test_empty_string(self):
|
||||
ip = ""
|
||||
self.assertFalse(validIP(ip))
|
||||
|
||||
def test_error_looking_string(self):
|
||||
ip = "upstream connect error or disconnect/reset before headers. reset reason: connection timeout."
|
||||
self.assertFalse(validIP(ip))
|
||||
|
||||
def test_none(self):
|
||||
ip = None
|
||||
self.assertFalse(validIP(ip))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
40
tests/test_public_ip.py
Normal file
40
tests/test_public_ip.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import unittest
|
||||
import responses
|
||||
from src.main import publicAddress
|
||||
|
||||
MOCK_IP = '44.208.147.61'
|
||||
MOCK_TIMEOUT = 'upstream connect error or disconnect/reset before headers. reset reason: connection timeout.'
|
||||
|
||||
|
||||
class TestPublicAddress(unittest.TestCase):
|
||||
|
||||
@responses.activate
|
||||
def test_successfull_response(self):
|
||||
responses.add(responses.GET, 'https://ifconfig.me',
|
||||
body=MOCK_IP, status=200)
|
||||
|
||||
ip = publicAddress()
|
||||
|
||||
self.assertEqual(MOCK_IP, ip)
|
||||
|
||||
@responses.activate
|
||||
def test_timeout_response(self):
|
||||
responses.add(responses.GET, 'https://ifconfig.me',
|
||||
body=MOCK_TIMEOUT, status=500)
|
||||
|
||||
ip = publicAddress()
|
||||
|
||||
self.assertIsNone(ip)
|
||||
|
||||
@responses.activate
|
||||
def test_mangled_response(self):
|
||||
responses.add(responses.GET, 'https://ifconfig.me',
|
||||
body='123.22', status=200)
|
||||
|
||||
ip = publicAddress()
|
||||
|
||||
self.assertIsNone(ip)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user