Skip to content

Commit

Permalink
fix origin trials GET request (#4612)
Browse files Browse the repository at this point in the history
Co-authored-by: DanielRyanSmith <[email protected]>
  • Loading branch information
DanielRyanSmith and DanielRyanSmith authored Dec 7, 2024
1 parent 1ac4356 commit 2eec2fe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions api/origin_trials_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ def do_get(self, **kwargs):
A list of data on all public origin trials.
"""
try:
trials_list = GetOriginTrialsResponse.from_dict(origin_trials_client.get_trials_list())
trials_list = origin_trials_client.get_trials_list()
except requests.exceptions.RequestException:
self.abort(500, 'Error obtaining origin trial data from API')
except KeyError:
self.abort(500, 'Malformed response from origin trials API')

return trials_list
return GetOriginTrialsResponse.from_dict({
'origin_trials': trials_list
})

def _validate_creation_args(
self, body: dict) -> dict[str, str]:
Expand Down
30 changes: 30 additions & 0 deletions api/origin_trials_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
from unittest import mock

import flask
import requests
import werkzeug.exceptions # Flask HTTP stuff.

import testing_config # Must be imported before the module under test.
from api import origin_trials_api
from chromestatus_openapi.models import GetOriginTrialsResponse
from internals.core_models import FeatureEntry, MilestoneSet, Stage
from internals.review_models import Gate, Vote
from internals.user_models import AppUser
Expand Down Expand Up @@ -137,6 +139,34 @@ def tearDown(self):
for entity in kind.query():
entity.key.delete()

@mock.patch('framework.origin_trials_client.get_trials_list')
def test_get__valid(self, mock_get_trials_list):
"""A list of public trials is returned."""
testing_config.sign_in('[email protected]', 1234567890)
mock_get_trials_list.return_value = [
{'id': '123', 'display_name': 'Example Trial'}]
with test_app.test_request_context(
self.request_path, method='GET', json={}):
result = self.handler.do_get()
self.assertEqual(GetOriginTrialsResponse.from_dict({
'origin_trials': [
{'id': '123', 'display_name': 'Example Trial'}
]
}), result)

@mock.patch('logging.exception')
@mock.patch('logging.error')
@mock.patch('framework.origin_trials_client.get_trials_list')
def test_get__invalid(
self, mock_get_trials_list, mock_log_error, mock_log_exception):
"""A request error from the origin trials API raises the correct exception."""
testing_config.sign_in('[email protected]', 1234567890)
mock_get_trials_list.side_effect = requests.exceptions.RequestException
with test_app.test_request_context(
self.request_path, method='GET', json={}):
with self.assertRaises(werkzeug.exceptions.InternalServerError):
self.handler.do_get()

def mock_chromium_file_return_value_generator(self, *args, **kwargs):
"""Returns mock milestone info based on input."""
if args == ('https://chromium.googlesource.com/chromium/src/+/main/third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom?format=TEXT',):
Expand Down

0 comments on commit 2eec2fe

Please sign in to comment.