Skip to content

Commit

Permalink
Merge pull request #3020 from L015H4CK/feature-api-update-planner
Browse files Browse the repository at this point in the history
Add API functionality to update planners
  • Loading branch information
elegantmoose committed Aug 16, 2024
2 parents b93877d + 41eb7b4 commit 5521615
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/api/v2/handlers/planner_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def add_routes(self, app: web.Application):
router = app.router
router.add_get('/planners', self.get_planners)
router.add_get('/planners/{planner_id}', self.get_planner_by_id)
router.add_patch('/planners/{planner_id}', self.update_planner)

@aiohttp_apispec.docs(tags=['planners'],
summary='Retrieve planners',
Expand Down Expand Up @@ -49,3 +50,20 @@ async def get_planners(self, request: web.Request):
async def get_planner_by_id(self, request: web.Request):
planner = await self.get_object(request)
return web.json_response(planner)

@aiohttp_apispec.docs(tags=['planners'],
summary='Updates an existing planner.',
description='Updates a planner based on the `PlannerSchema` value provided in the message body.',
parameters=[{
'in': 'path',
'name': 'planner_id',
'schema': {'type': 'string'},
'required': 'true',
'description': 'UUID of the Planner to be updated'
}])
@aiohttp_apispec.request_schema(PlannerSchema(partial=True))
@aiohttp_apispec.response_schema(PlannerSchema(partial=True),
description='JSON dictionary representation of the replaced Planner.')
async def update_planner(self, request: web.Request):
planner = await self.update_on_disk_object(request)
return web.json_response(planner.display)
8 changes: 8 additions & 0 deletions app/objects/c_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class PlannerSchema(ma.Schema):
allow_repeatable_abilities = ma.fields.Boolean()
plugin = ma.fields.String(load_default=None)

@ma.pre_load
def fix_id(self, data, **_):
if 'planner_id' in data:
data['id'] = data.pop('planner_id')
return data

@ma.post_load()
def build_planner(self, data, **kwargs):
return None if kwargs.get('partial') is True else Planner(**data)
Expand Down Expand Up @@ -54,6 +60,8 @@ def store(self, ram):
existing.update('stopping_conditions', self.stopping_conditions)
existing.update('params', self.params)
existing.update('plugin', self.plugin)
existing.update('description', self.description)
existing.update('allow_repeatable_abilities', self.allow_repeatable_abilities)
return existing

async def which_plugin(self):
Expand Down
23 changes: 23 additions & 0 deletions tests/api/v2/handlers/test_planners_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ def expected_test_planner_dump(test_planner):
return test_planner.display_schema.dump(test_planner)


@pytest.fixture
def updated_planner(test_planner):
planner_dict = test_planner.schema.dump(test_planner)
planner_dict.update(dict(description="a test planner with updated description"))
return planner_dict


class TestPlannersApi:
async def test_get_planners(self, api_v2_client, api_cookies, test_planner, expected_test_planner_dump):
resp = await api_v2_client.get('/api/v2/planners', cookies=api_cookies)
Expand Down Expand Up @@ -57,3 +64,19 @@ async def test_planner_defaults(self, api_v2_client, api_cookies, test_planner,
assert len(planners_list) == 2
assert planners_list[0]["id"] == "456"
assert planners_list[0]["name"][0] > planners_list[1]["name"][0] # prove that this wasn't an alphabetical sort

async def test_update_planner(self, api_v2_client, api_cookies, test_planner, updated_planner, mocker):
with mocker.patch('app.api.v2.managers.base_api_manager.BaseApiManager.strip_yml') as mock_strip_yml:
mock_strip_yml.return_value = [test_planner.schema.dump(test_planner)]
resp = await api_v2_client.patch('/api/v2/planners/123', cookies=api_cookies, json=updated_planner)
assert resp.status == HTTPStatus.OK
planner = (await BaseService.get_service('data_svc').locate('planners'))[0]
assert planner.description == updated_planner["description"]

async def test_unauthorized_update_planner(self, api_v2_client, updated_planner):
resp = await api_v2_client.patch('/api/v2/planners/123', json=updated_planner)
assert resp.status == HTTPStatus.UNAUTHORIZED

async def test_update_nonexistent_planner(self, api_v2_client, api_cookies, updated_planner):
resp = await api_v2_client.patch('/api/v2/planners/999', cookies=api_cookies, json=updated_planner)
assert resp.status == HTTPStatus.NOT_FOUND

0 comments on commit 5521615

Please sign in to comment.