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: | diff --git a/porkbun_ddns/porkbun_ddns.py b/porkbun_ddns/porkbun_ddns.py index 438d61f..2306554 100644 --- a/porkbun_ddns/porkbun_ddns.py +++ b/porkbun_ddns/porkbun_ddns.py @@ -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..76ab52f 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 error message + self.assertEqual(str(context.exception), 'Failed to obtain IP Addresses!') + if __name__ == '__main__': unittest.main()