fix: ructf format api responses
This commit is contained in:
1 parent
1c1d174476
commit
e838a733ea
2 files changed
+184
-6
No files matched your search
@@ -1,14 +1,18 @@
|
||||
import requests
|
||||
import logging
|
||||
import math
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
from models import FlagStatus, SubmitResult
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
RESPONSES = {
|
||||
FlagStatus.QUEUED: ['timeout', 'game not started', 'try again later', 'game over', 'is not up',
|
||||
'no such flag'],
|
||||
FlagStatus.ACCEPTED: ['accepted', 'congrat'],
|
||||
FlagStatus.QUEUED: ['timeout', 'game not started', 'game is paused', 'temporarily unavailable',
|
||||
'cannot submit flags while service is down', 'try again later', 'game over',
|
||||
'is not up', 'no such flag'],
|
||||
FlagStatus.ACCEPTED: ['accepted', 'congrat', 'already stolen'],
|
||||
FlagStatus.REJECTED: ['bad', 'wrong', 'expired', 'unknown', 'your own',
|
||||
'too old', 'not in database', 'already submitted', 'invalid flag'],
|
||||
}
|
||||
@@ -21,17 +25,65 @@ RESPONSES = {
|
||||
|
||||
|
||||
TIMEOUT = 5
|
||||
_retry_after_until = 0.0
|
||||
|
||||
|
||||
def _response_error(response):
|
||||
try:
|
||||
payload = response.json()
|
||||
except ValueError:
|
||||
return ''
|
||||
if isinstance(payload, dict) and isinstance(payload.get('error'), str):
|
||||
return payload['error'].strip()
|
||||
return ''
|
||||
|
||||
|
||||
def _retry_after(response, config):
|
||||
fallback = max(float(config.get('SUBMIT_PERIOD', 2)), 0.1)
|
||||
try:
|
||||
return max(float(response.headers.get('Retry-After', '')), 0.0)
|
||||
except (TypeError, ValueError):
|
||||
return fallback
|
||||
|
||||
|
||||
def _queued(flags, message):
|
||||
for item in flags:
|
||||
yield SubmitResult(item.flag, FlagStatus.QUEUED, message)
|
||||
|
||||
|
||||
def submit_flags(flags, config):
|
||||
global _retry_after_until
|
||||
|
||||
now = time.monotonic()
|
||||
if now < _retry_after_until:
|
||||
remaining = math.ceil(_retry_after_until - now)
|
||||
yield from _queued(flags, f'Ограничение частоты, повтор через {remaining} с')
|
||||
return
|
||||
|
||||
r = requests.put(config['SYSTEM_URL'],
|
||||
headers={'X-Team-Token': config['SYSTEM_TOKEN']},
|
||||
json=[item.flag for item in flags], timeout=TIMEOUT)
|
||||
|
||||
if r.status_code == 429:
|
||||
delay = _retry_after(r, config)
|
||||
_retry_after_until = max(_retry_after_until, time.monotonic() + delay)
|
||||
message = _response_error(r) or 'Превышен лимит отправки флагов'
|
||||
logger.warning('%s; повтор не раньше чем через %.2f с', message, delay)
|
||||
yield from _queued(flags, message)
|
||||
return
|
||||
|
||||
r.raise_for_status()
|
||||
payload = r.json()
|
||||
if not isinstance(payload, list):
|
||||
raise ValueError('Skipper вернул ответ в неожиданном формате')
|
||||
|
||||
unknown_responses = set()
|
||||
for item in r.json():
|
||||
for item in payload:
|
||||
if (not isinstance(item, dict) or not isinstance(item.get('flag'), str)
|
||||
or not isinstance(item.get('msg'), str)):
|
||||
raise ValueError('Skipper вернул некорректный результат сдачи флага')
|
||||
response = item['msg'].strip()
|
||||
response = response.replace('[{}] '.format(item['flag']), '')
|
||||
response = response.removeprefix('[{}] '.format(item['flag']))
|
||||
|
||||
response_lower = response.lower()
|
||||
for status, substrings in RESPONSES.items():
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
import sys
|
||||
import types
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from unittest import mock
|
||||
|
||||
APP_DIR = Path(__file__).resolve().parents[1] / 'app'
|
||||
sys.path.insert(0, str(APP_DIR))
|
||||
|
||||
requests_stub = types.ModuleType('requests')
|
||||
requests_stub.put = mock.Mock()
|
||||
sys.modules['requests'] = requests_stub
|
||||
|
||||
from models import FlagStatus
|
||||
from protocols import ructf_http
|
||||
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, status_code, payload, headers=None, error=None):
|
||||
self.status_code = status_code
|
||||
self.payload = payload
|
||||
self.headers = headers or {}
|
||||
self.error = error
|
||||
|
||||
def json(self):
|
||||
return self.payload
|
||||
|
||||
def raise_for_status(self):
|
||||
if self.error:
|
||||
raise self.error
|
||||
|
||||
|
||||
def flags(count):
|
||||
return [SimpleNamespace(flag=f'{chr(65 + index)}' + '0' * 30 + '=')
|
||||
for index in range(count)]
|
||||
|
||||
|
||||
def result(flag, message):
|
||||
return {'flag': flag.flag, 'msg': f'[{flag.flag}] {message}'}
|
||||
|
||||
|
||||
class RuctfHttpTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
ructf_http._retry_after_until = 0.0
|
||||
requests_stub.put.reset_mock()
|
||||
requests_stub.put.return_value = None
|
||||
requests_stub.put.side_effect = None
|
||||
self.config = {
|
||||
'SYSTEM_URL': 'http://skipper.test/api/v1/flags',
|
||||
'SYSTEM_TOKEN': 'token',
|
||||
'SUBMIT_PERIOD': 2,
|
||||
}
|
||||
|
||||
def test_skipper_messages_have_terminal_or_retry_status(self):
|
||||
batch = flags(8)
|
||||
messages = (
|
||||
'Flag accepted! Earned 15.75 flag points!',
|
||||
'Flag already stolen',
|
||||
'Flag is your own',
|
||||
'Flag is too old',
|
||||
'Flag is invalid or too old.',
|
||||
'Cannot submit flags while service is down',
|
||||
'game is paused',
|
||||
'Game is temporarily unavailable.',
|
||||
)
|
||||
expected = (
|
||||
FlagStatus.ACCEPTED,
|
||||
FlagStatus.ACCEPTED,
|
||||
FlagStatus.REJECTED,
|
||||
FlagStatus.REJECTED,
|
||||
FlagStatus.REJECTED,
|
||||
FlagStatus.QUEUED,
|
||||
FlagStatus.QUEUED,
|
||||
FlagStatus.QUEUED,
|
||||
)
|
||||
requests_stub.put.return_value = FakeResponse(
|
||||
200,
|
||||
[result(flag, message) for flag, message in zip(batch, messages)],
|
||||
)
|
||||
|
||||
actual = list(ructf_http.submit_flags(batch, self.config))
|
||||
|
||||
self.assertEqual(tuple(item.status for item in actual), expected)
|
||||
|
||||
def test_retry_after_stops_requests_until_deadline(self):
|
||||
batch = flags(1)
|
||||
rate_limited = FakeResponse(
|
||||
429,
|
||||
{'error': 'rate limited'},
|
||||
headers={'Retry-After': '5'},
|
||||
)
|
||||
accepted = FakeResponse(200, [result(batch[0], 'Flag accepted! Earned 1 flag points!')])
|
||||
requests_stub.put.side_effect = [rate_limited, accepted]
|
||||
|
||||
with mock.patch.object(ructf_http.time, 'monotonic', side_effect=(100, 100, 103, 106)):
|
||||
first = list(ructf_http.submit_flags(batch, self.config))
|
||||
second = list(ructf_http.submit_flags(batch, self.config))
|
||||
third = list(ructf_http.submit_flags(batch, self.config))
|
||||
|
||||
self.assertEqual(first[0].status, FlagStatus.QUEUED)
|
||||
self.assertEqual(second[0].status, FlagStatus.QUEUED)
|
||||
self.assertEqual(third[0].status, FlagStatus.ACCEPTED)
|
||||
self.assertEqual(requests_stub.put.call_count, 2)
|
||||
|
||||
def test_invalid_success_payload_is_rejected(self):
|
||||
batch = flags(1)
|
||||
requests_stub.put.return_value = FakeResponse(200, {'error': 'unexpected'})
|
||||
|
||||
with self.assertRaisesRegex(ValueError, 'неожиданном формате'):
|
||||
list(ructf_http.submit_flags(batch, self.config))
|
||||
|
||||
def test_request_error_is_propagated(self):
|
||||
batch = flags(1)
|
||||
requests_stub.put.return_value = FakeResponse(
|
||||
503,
|
||||
{'error': 'maintenance'},
|
||||
error=RuntimeError('503 Service Unavailable'),
|
||||
)
|
||||
|
||||
with self.assertRaisesRegex(RuntimeError, '503 Service Unavailable'):
|
||||
list(ructf_http.submit_flags(batch, self.config))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user