Skip to content

Commit 448d978

Browse files
桁数、入力チェックルールの変更
1 parent f79ac9b commit 448d978

File tree

3 files changed

+78
-40
lines changed

3 files changed

+78
-40
lines changed

src/common/settings.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,24 @@
8888
},
8989
'fraud_user': {
9090
'reason': {
91-
'type': 'string',
91+
'type': ['string'],
9292
},
9393
'plagiarism_url': {
9494
'type': 'string',
9595
'format': 'uri',
96+
'pattern': r'^(https?|http?)://',
97+
'minLength': 1,
9698
'maxLength': 2048
9799
},
98100
'plagiarism_description': {
99101
'type': 'string',
100-
'maxLength': 65535
102+
'minLength': 1,
103+
'maxLength': 1000
101104
},
102105
'illegal_content': {
103106
'type': 'string',
104-
'maxLength': 65535
107+
'minLength': 1,
108+
'maxLength': 1000
105109
},
106110

107111
}

src/handlers/me/articles/fraud/create/me_articles_fraud_create.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ def get_schema(self):
2525
'properties': {
2626
'reason': {'enum': settings.FRAUD_REASONS}
2727
}
28+
},
29+
{
30+
'properties': {
31+
'reason': {'enum': settings.FRAUD_NEED_ORIGINAL_REASONS}
32+
},
33+
'anyOf': [
34+
{'required': ['plagiarism_url']},
35+
{'required': ['plagiarism_description']}
36+
]
37+
},
38+
{
39+
'properties': {
40+
'reason': {'enum': settings.FRAUD_NEED_DETAIL_REASONS}
41+
},
42+
'required': ['illegal_content']
2843
}
2944
],
3045
'required': ['article_id']
@@ -35,7 +50,6 @@ def validate_params(self):
3550
if self.event.get('pathParameters') is None:
3651
raise ValidationError('pathParameters is required')
3752
validate(self.params, self.get_schema())
38-
self.__validate_reason_dependencies(self.params)
3953
# relation
4054
DBUtil.validate_article_existence(
4155
self.dynamodb,
@@ -74,16 +88,3 @@ def __create_article_fraud_user(self, article_fraud_user_table):
7488
Item=article_fraud_user,
7589
ConditionExpression='attribute_not_exists(article_id)'
7690
)
77-
78-
def __validate_reason_dependencies(self, params):
79-
reason = params.get('reason', '')
80-
if reason in settings.FRAUD_NEED_ORIGINAL_REASONS:
81-
self.__validate_dependencies(params, ['plagiarism_url', 'plagiarism_description'])
82-
83-
if reason in settings.FRAUD_NEED_DETAIL_REASONS:
84-
self.__validate_dependencies(params, ['illegal_content'])
85-
86-
def __validate_dependencies(self, params, required_items):
87-
for item in required_items:
88-
if not params[item]:
89-
raise ValidationError("%s is required" % item)

tests/handlers/me/articles/fraud/create/test_me_articles_fraud_create.py

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ def test_main_ok_exist_article_id(self):
105105
target_article_id = params['pathParameters']['article_id']
106106
target_user_id = params['requestContext']['authorizer']['claims']['cognito:username']
107107
body = json.loads(params['body'])
108-
target_reason = body['reason']
109-
target_plagiarism_url = body['plagiarism_url']
110-
target_plagiarism_description = body['plagiarism_description']
111-
target_illegal_content = body['illegal_content']
108+
target_reason = body.get('reason')
109+
target_plagiarism_url = body.get('plagiarism_url')
110+
target_plagiarism_description = body.get('plagiarism_description')
111+
target_illegal_content = body.get('illegal_content')
112112

113113
article_fraud_user = self.get_article_fraud_user(target_article_id, target_user_id)
114114

@@ -202,65 +202,83 @@ def test_validation_article_id_min(self):
202202
self.assert_bad_request(params)
203203

204204
def test_validation_invalid_reason(self):
205-
params = {
205+
body = {
206206
'body': json.dumps({'reason': 'abcde'})
207207
}
208+
params = dict(self.get_parameters_other_than_body(), **body)
208209
self.assert_bad_request(params)
209210

210-
def test_validation_required_plagiarism_url_when_reason_is_plagiarism(self):
211-
params = {
211+
def test_validation_required_plagiarism_detail_when_reason_is_plagiarism(self):
212+
body = {
212213
'body': json.dumps(
213214
{
214215
'reason': 'plagiarism',
215216
'plagiarism_url': '',
217+
'plagiarism_description': ''
216218
}
217219
)
218220
}
221+
params = dict(self.get_parameters_other_than_body(), **body)
219222
self.assert_bad_request(params)
220223

221-
def test_validation_required_plagiarism_description_when_reason_is_plagiarism(self):
222-
params = {
224+
def test_validation_invalid_plagiarism_url_when_reason_is_plagiarism(self):
225+
body = {
223226
'body': json.dumps(
224227
{
225228
'reason': 'plagiarism',
226-
'plagiarism_url': 'http://test.com',
227-
'plagiarism_description': '',
229+
'plagiarism_url': 'aaa'
228230
}
229231
)
230232
}
233+
params = dict(self.get_parameters_other_than_body(), **body)
231234
self.assert_bad_request(params)
232235

233-
def test_validation_invalid_plagiarism_url_when_reason_is_plagiarism(self):
234-
params = {
236+
def test_validation_required_illegal_content_when_reason_is_illegal(self):
237+
body = {
235238
'body': json.dumps(
236239
{
237-
'reason': 'plagiarism',
238-
'plagiarism_url': 'aaa'
240+
'reason': 'illegal',
241+
'illegal_content': ''
239242
}
240243
)
241244
}
245+
params = dict(self.get_parameters_other_than_body(), **body)
242246
self.assert_bad_request(params)
243247

244-
def test_validation_required_illegal_content_when_reason_is_illegal(self):
245-
params = {
248+
def test_validation_required_illegal_content_when_reason_is_other(self):
249+
body = {
246250
'body': json.dumps(
247251
{
248-
'reason': 'illegal',
249-
'illegal_content': '',
252+
'reason': 'other',
253+
'illegal_content': ''
250254
}
251255
)
252256
}
257+
params = dict(self.get_parameters_other_than_body(), **body)
253258
self.assert_bad_request(params)
254259

255-
def test_validation_required_illegal_content_when_reason_is_other(self):
256-
params = {
260+
def test_validation_plagiarism_description_max(self):
261+
body = {
257262
'body': json.dumps(
258263
{
259-
'reason': 'other',
260-
'illegal_content': '',
264+
'reason': 'plagiarism',
265+
'plagiarism_description': u'あ' * 1001
261266
}
262267
)
263268
}
269+
params = dict(self.get_parameters_other_than_body(), **body)
270+
self.assert_bad_request(params)
271+
272+
def test_validation_illegal_content_max(self):
273+
body = {
274+
'body': json.dumps(
275+
{
276+
'reason': 'illegal',
277+
'illegal_content': u'あ' * 1001
278+
}
279+
)
280+
}
281+
params = dict(self.get_parameters_other_than_body(), **body)
264282
self.assert_bad_request(params)
265283

266284
def get_article_fraud_user(self, article_id, user_id):
@@ -269,3 +287,18 @@ def get_article_fraud_user(self, article_id, user_id):
269287
}
270288
article_fraud_user_table = self.dynamodb.Table(os.environ['ARTICLE_FRAUD_USER_TABLE_NAME'])
271289
return article_fraud_user_table.query(**query_params)['Items'][0]
290+
291+
def get_parameters_other_than_body(self):
292+
basic_params = {
293+
'pathParameters': {
294+
'article_id': self.article_fraud_user_table_items[1]['article_id']
295+
},
296+
'requestContext': {
297+
'authorizer': {
298+
'claims': {
299+
'cognito:username': 'test03'
300+
}
301+
}
302+
}
303+
}
304+
return basic_params

0 commit comments

Comments
 (0)