Handle 404 error on key get api call

This commit is contained in:
s3lph 2022-08-07 16:02:53 +02:00
parent 95a2c481b7
commit eccd08a8bc
2 changed files with 25 additions and 5 deletions

View file

@ -3,8 +3,10 @@ from typing import List, Optional
import base64 import base64
import json import json
import logging
import os import os
import ssl import ssl
import urllib.error
import urllib.request import urllib.request
from multischleuder.types import SchleuderKey, SchleuderList, SchleuderSubscriber from multischleuder.types import SchleuderKey, SchleuderList, SchleuderSubscriber
@ -129,8 +131,12 @@ class SchleuderApi:
# Key Management # Key Management
def get_key(self, fpr: str, schleuder: SchleuderList) -> SchleuderKey: def get_key(self, fpr: str, schleuder: SchleuderList) -> Optional[SchleuderKey]:
key = self.__request('keys/{}.json', list_id=schleuder.id, fmt=[fpr]) try:
key = self.__request('keys/{}.json', list_id=schleuder.id, fmt=[fpr])
except urllib.error.HTTPError as e:
logging.exception(e)
return None
return SchleuderKey.from_api(schleuder.id, **key) return SchleuderKey.from_api(schleuder.id, **key)
def post_key(self, key: SchleuderKey, schleuder: SchleuderList): def post_key(self, key: SchleuderKey, schleuder: SchleuderList):

View file

@ -1,5 +1,6 @@
import unittest import unittest
import urllib.error
from datetime import datetime from datetime import datetime
from unittest.mock import patch, MagicMock from unittest.mock import patch, MagicMock
@ -111,7 +112,7 @@ _KEY_RESPONSE = '''
class TestSchleuderApi(unittest.TestCase): class TestSchleuderApi(unittest.TestCase):
def _mock_api(self, mock, nokey=False): def _mock_api(self, mock, nokey=False, error=None):
m = MagicMock() m = MagicMock()
m.getcode.return_value = 200 m.getcode.return_value = 200
@ -133,8 +134,14 @@ class TestSchleuderApi(unittest.TestCase):
m.read = read m.read = read
m.__enter__.return_value = m m.__enter__.return_value = m
mock.return_value = m mock.return_value = m
return SchleuderApi('https://localhost:4443', api = SchleuderApi('https://localhost:4443',
'86cf2676d065dc902548e563ab22b57868ed2eb6') '86cf2676d065dc902548e563ab22b57868ed2eb6')
if error is not None:
def __request(*args, **kwargs):
raise error
api._SchleuderApi__request = __request
return api
@patch('urllib.request.urlopen') @patch('urllib.request.urlopen')
def test_get_lists(self, mock): def test_get_lists(self, mock):
@ -282,6 +289,13 @@ class TestSchleuderApi(unittest.TestCase):
self.assertEqual(42, key.schleuder) self.assertEqual(42, key.schleuder)
self.assertIn('-----BEGIN PGP PUBLIC KEY BLOCK-----', key.blob) self.assertIn('-----BEGIN PGP PUBLIC KEY BLOCK-----', key.blob)
@patch('urllib.request.urlopen')
def test_get_key_404(self, mock):
url = 'https://localhost:4443/keys/ADB9BC679FF53CC8EF66FAC39348FDAB7A7663FA.json?list_id=42'
api = self._mock_api(mock, error=urllib.error.HTTPError(url=url, code=404, msg='Not Found', hdrs={}, fp=None))
key = api.get_key('ADB9BC679FF53CC8EF66FAC39348FDAB7A7663FA', SchleuderList(42, '', ''))
self.assertIsNone(key)
@patch('urllib.request.urlopen') @patch('urllib.request.urlopen')
def test_post_key(self, mock): def test_post_key(self, mock):
api = self._mock_api(mock) api = self._mock_api(mock)