Skip to content

Commit

Permalink
add validate remote data in SwitchesCopy (#34)
Browse files Browse the repository at this point in the history
* add switch_detail url api
* add validate remote data in SwitchesCopy
* up aiohttp and aiohttp-jinja2

authored-by: Pavel Peryakin <[email protected]>
  • Loading branch information
pashaandsik authored Mar 31, 2022
1 parent 5c8dc2e commit 0d7a2e6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
13 changes: 8 additions & 5 deletions auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import TYPE_CHECKING

import aiohttp_jinja2
from aiohttp.abc import StreamResponse
from aiohttp.web import HTTPFound, View, Response
from aiohttp_security import forget, remember
from marshmallow.exceptions import ValidationError
Expand All @@ -20,16 +21,18 @@ async def get(self, error: Optional[str] = None) -> Dict[str, str]:
return {'context': ''}

@aiohttp_jinja2.template('users/login.html')
async def error(self, error: str = 'Internal error') -> Dict[str, str]:
return {'context': '', 'error': error}
async def error(self) -> Dict[str, str]:
return {'context': '', 'error': 'Authorization failed'}

async def authorise(self, response_location: Response, login: str, password: str) -> Response:
async def authorise(
self, response_location: Response, login: str, password: str,
) -> StreamResponse:
if await check_credentials(self.request.app['db'], login, password):
await remember(self.request, response_location, login)
return response_location
return await self.error('Authorization failed')
return await self.error()

async def post(self) -> Response:
async def post(self) -> StreamResponse:
response_location = HTTPFound('/zbs/switches')
form_data = await self.request.post()
validated_data = self.validate_form_data(form_data)
Expand Down
6 changes: 6 additions & 0 deletions its_on/admin/views/switches.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
from its_on.admin.utils import get_switch_history, save_switch_history
from its_on.models import switches
from its_on.schemes import RemoteSwitchesDataSchema


class SwitchListAdminView(web.View):
Expand Down Expand Up @@ -193,6 +194,7 @@ async def post(self) -> Dict[str, Dict]:

class SwitchesCopyAdminView(web.View, CreateMixin):
validator = SwitchCopyFromAnotherItsOnAdminPostRequestSchema()
remote_validator = RemoteSwitchesDataSchema()
model = switches

@staticmethod
Expand All @@ -210,6 +212,10 @@ async def post(self) -> Dict[str, Exception]:
switches_data = await self._get_switches_data()
except (ClientConnectionError, ClientResponseError) as error:
return {'errors': error}
try:
switches_data = self.remote_validator.load(switches_data)
except ValidationError as error:
return {'errors': error}

for switch_data in switches_data['result']:
await self._create_or_update_switch(switch_data, update_existing)
Expand Down
24 changes: 23 additions & 1 deletion its_on/schemes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from marshmallow import Schema, fields
from marshmallow import Schema, fields, validate, EXCLUDE


class SwitchListRequestSchema(Schema):
Expand All @@ -25,3 +25,25 @@ class SwitchScheme(Schema):

class SwitchFullListResponseSchema(Schema):
result = fields.List(fields.Nested(SwitchScheme))


class SwitchRemoteDataSchema(Schema):
class Meta:
unknown = EXCLUDE

is_active = fields.Boolean()
version = fields.Int()
comment = fields.Str()
ttl = fields.Int(validate=validate.Range(min=1))
name = fields.Str()
groups = fields.List(fields.Str())
is_hidden = fields.Boolean()
created_at = fields.Str(allow_none=True)
updated_at = fields.Str(allow_none=True)


class RemoteSwitchesDataSchema(Schema):
class Meta:
unknown = EXCLUDE

result = fields.List(fields.Nested(SwitchRemoteDataSchema))
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
aiohttp==3.6.2
aiohttp==3.6.3
aiohttp-apispec==1.5.0
aiohttp-cors==0.7.0
aiohttp-jinja2==1.2.0
aiohttp-jinja2==1.5.0
aiohttp-security==0.4.0
aiohttp-session==2.8.0
aiodns==2.0.0
Expand Down
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ def get_switches_data_mocked_existing_switch(mocker):
'groups': ['soft_delete'],
'version': 2,
'comment': '',
'created_at': '2020-10-30T10:00:00+00:00',
'updated_at': '2020-10-30T10:00:00+00:00',
'created_at': '2020-10-30T10:00:12.000000+00:00',
'updated_at': '2020-10-30T10:00:12.000000+00:00',
'flag_url': 'http://test.ru/zbs/switches/10557',
},
],
}
Expand Down

0 comments on commit 0d7a2e6

Please sign in to comment.