From 1b14e12cc175eaab5fa1c3004ee2fccd0a2577da Mon Sep 17 00:00:00 2001 From: filip Date: Sat, 2 Mar 2024 11:00:23 +0100 Subject: [PATCH 1/4] get public ip method refactor - response status diff 200 try another api - test fetch public ip --- porkbun_ddns/porkbun_ddns.py | 14 +++++++++++--- porkbun_ddns/test/test_porkbun_ddns.py | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/porkbun_ddns/porkbun_ddns.py b/porkbun_ddns/porkbun_ddns.py index 438d61f..b6f7cd5 100644 --- a/porkbun_ddns/porkbun_ddns.py +++ b/porkbun_ddns/porkbun_ddns.py @@ -5,7 +5,7 @@ import ipaddress import urllib.request from urllib.error import HTTPError, URLError -from porkbun_ddns.helpers import get_ips_from_fritzbox +from helpers import get_ips_from_fritzbox logger = logging.getLogger('porkbun_ddns') @@ -85,8 +85,16 @@ def get_public_ips(self) -> list: else: if self.ipv4: try: - public_ips.append(urllib.request.urlopen( - 'https://v4.ident.me').read().decode('utf8')) + response = urllib.request.urlopen('https://v4.ident.me') + if response.getcode() == 200: + public_ips.append(response.read().decode('utf-8')) + else: + logger.warning("Failed to retrieve IPv4 Address! HTTP status code: {}".format(response.code())) + alternative_response = urllib.request.urlopen('https://api.ipify.org/') + if alternative_response.getcode() == 200: + public_ips.append(alternative_response.read().decode('utf-8')) + else: + logger.warning("Failed to retrieve IPv4 Address! HTTP status code: {}".format(response.code())) except URLError: logger.warning("Can't reach IPv4 Address! Check IPv4 connectivity!") if self.ipv6: diff --git a/porkbun_ddns/test/test_porkbun_ddns.py b/porkbun_ddns/test/test_porkbun_ddns.py index ff51f24..a29eb23 100644 --- a/porkbun_ddns/test/test_porkbun_ddns.py +++ b/porkbun_ddns/test/test_porkbun_ddns.py @@ -1,5 +1,5 @@ import unittest -from unittest.mock import patch +from unittest.mock import patch, MagicMock from ..porkbun_ddns import PorkbunDDNS, PorkbunDDNS_Error import logging @@ -159,6 +159,23 @@ def test_record_overwrite_alias_and_cname(self, mocker=None): 'INFO:porkbun_ddns:Creating AAAA-Record for my-domain.local with content: ' '0000:0000:0000:0000:0000:0000:0000:0001, Status: SUCCESS']) + @patch('urllib.request.urlopen') + def test_urlopen_returns_500(self, mock_urlopen): + # Set up the mock to return a response with status code 500 + mock_response = MagicMock() + mock_response.getcode.return_value = 500 + mock_urlopen.return_value = mock_response + + # Instantiate your class or call the method that uses urllib.request.urlopen() + porkbun_ddns = PorkbunDDNS(valid_config, domain='example.com', ipv4=True, ipv6=False) + + # Now when you call the method that uses urllib.request.urlopen(), it will get the mocked response + with self.assertRaises(PorkbunDDNS_Error) as context: + porkbun_ddns.get_public_ips() + + # Verify that the exception has the expected status code + self.assertEqual(context.exception, PorkbunDDNS_Error('Failed to obtain IP Addresses!')) + if __name__ == '__main__': unittest.main() From 8a6290723299a9f1a1e5cd9d6511a340e68086f3 Mon Sep 17 00:00:00 2001 From: filip Date: Sat, 2 Mar 2024 11:05:39 +0100 Subject: [PATCH 2/4] import fix --- porkbun_ddns/porkbun_ddns.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/porkbun_ddns/porkbun_ddns.py b/porkbun_ddns/porkbun_ddns.py index b6f7cd5..2306554 100644 --- a/porkbun_ddns/porkbun_ddns.py +++ b/porkbun_ddns/porkbun_ddns.py @@ -5,7 +5,7 @@ import ipaddress import urllib.request from urllib.error import HTTPError, URLError -from helpers import get_ips_from_fritzbox +from porkbun_ddns.helpers import get_ips_from_fritzbox logger = logging.getLogger('porkbun_ddns') From 48286dcd569bc53a9755c721d71d98c1d71bcfc0 Mon Sep 17 00:00:00 2001 From: Nils Stein <31704359+mietzen@users.noreply.github.com> Date: Sat, 2 Mar 2024 13:08:49 +0100 Subject: [PATCH 3/4] docker user must be lower case --- .github/workflows/docker.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 864c0fd..0ae6e14 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -59,7 +59,7 @@ jobs: - name: Build env: - DOCKER_USER: ${{ vars.DOCKER_HUB_USERNAME || 'USER' }} + DOCKER_USER: ${{ vars.DOCKER_HUB_USERNAME || 'user' }} BUILD_NR: ${{ github.run_number }} PLATFORM: ${{ matrix.platforms }} VERSION: ${{ needs.Setup.outputs.version }} @@ -79,7 +79,7 @@ jobs: - name: Test env: - DOCKER_USER: ${{ vars.DOCKER_HUB_USERNAME || 'USER' }} + DOCKER_USER: ${{ vars.DOCKER_HUB_USERNAME || 'user' }} BUILD_NR: ${{ github.run_number }} PLATFORM: ${{ matrix.platforms }} VERSION: ${{ needs.Setup.outputs.version }} @@ -99,13 +99,13 @@ jobs: if: github.event_name == 'release' uses: docker/login-action@v2 with: - username: ${{ vars.DOCKER_HUB_USERNAME || 'USER' }} + username: ${{ vars.DOCKER_HUB_USERNAME || 'user' }} password: ${{ secrets.DOCKER_HUB_DEPLOY_KEY }} - name: Push Images if: github.event_name == 'release' env: - DOCKER_USER: ${{ vars.DOCKER_HUB_USERNAME || 'USER' }} + DOCKER_USER: ${{ vars.DOCKER_HUB_USERNAME || 'user' }} BUILD_NR: ${{ github.run_number }} PLATFORM: ${{ matrix.platforms }} VERSION: ${{ needs.Setup.outputs.version }} @@ -135,12 +135,12 @@ jobs: - name: Login to Docker Hub uses: docker/login-action@v2 with: - username: ${{ vars.DOCKER_HUB_USERNAME || 'USER' }} + username: ${{ vars.DOCKER_HUB_USERNAME || 'user' }} password: ${{ secrets.DOCKER_HUB_DEPLOY_KEY }} - name: Create and push shared manifest env: - DOCKER_USER: ${{ vars.DOCKER_HUB_USERNAME || 'USER' }} + DOCKER_USER: ${{ vars.DOCKER_HUB_USERNAME || 'user' }} BUILD_NR: ${{ github.run_number }} VERSION: ${{ needs.Setup.outputs.version }} run: | From d73bc380a93d9ac529cea4b89a7776a955bfa1f8 Mon Sep 17 00:00:00 2001 From: Nils Stein <31704359+mietzen@users.noreply.github.com> Date: Sat, 2 Mar 2024 13:24:33 +0100 Subject: [PATCH 4/4] fix test --- porkbun_ddns/test/test_porkbun_ddns.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/porkbun_ddns/test/test_porkbun_ddns.py b/porkbun_ddns/test/test_porkbun_ddns.py index a29eb23..76ab52f 100644 --- a/porkbun_ddns/test/test_porkbun_ddns.py +++ b/porkbun_ddns/test/test_porkbun_ddns.py @@ -173,8 +173,8 @@ def test_urlopen_returns_500(self, mock_urlopen): with self.assertRaises(PorkbunDDNS_Error) as context: porkbun_ddns.get_public_ips() - # Verify that the exception has the expected status code - self.assertEqual(context.exception, PorkbunDDNS_Error('Failed to obtain IP Addresses!')) + # Verify that the exception has the expected error message + self.assertEqual(str(context.exception), 'Failed to obtain IP Addresses!') if __name__ == '__main__':