Skip to content

Commit df662da

Browse files
特定の理由の時に依存項目の入力チェック
(jsonschemaでやりたくて色々調べたが、draft-07で初めてifが登場してた)
1 parent 4cdee2f commit df662da

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,25 @@ def get_schema(self):
1818
'reason': {
1919
'type': 'string',
2020
'enum': settings.FRAUD_REASONS
21-
}
21+
},
22+
'plagiarism_url': {
23+
'type': 'string',
24+
},
25+
'plagiarism_description': {
26+
'type': 'string'
27+
},
28+
'illegal_content': {
29+
'type': 'string'
30+
},
2231
},
2332
'required': ['article_id']
2433
}
2534

2635
def validate_params(self):
27-
if not self.event.get('body'):
36+
if self.event.get('pathParameters') is None:
2837
raise ValidationError('Request parameter is required')
29-
3038
validate(self.params, self.get_schema())
39+
self.__validate_reason_dependencies(self.params)
3140
# relation
3241
DBUtil.validate_article_existence(
3342
self.dynamodb,
@@ -63,7 +72,16 @@ def __create_article_fraud_user(self, article_fraud_user_table):
6372
ConditionExpression='attribute_not_exists(article_id)'
6473
)
6574

66-
def __validate_plagiarism(self):
67-
# TODO:
75+
def __validate_reason_dependencies(self, params):
76+
reason = params.get('reason', '')
77+
if reason in settings.FRAUD_NEED_ORIGINAL_REASONS:
78+
self.__validate_dependencies(params, ['plagiarism_url', 'plagiarism_description'])
79+
80+
if reason in settings.FRAUD_NEED_DETAIL_REASONS:
81+
self.__validate_dependencies(params, ['illegal_content'])
6882

83+
def __validate_dependencies(self, params, required_items):
84+
for item in required_items:
85+
if not params[item]:
86+
raise ValidationError("%s is required" % item)
6987

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,16 @@ def setUpClass(cls):
2020
{
2121
'article_id': 'testid000000',
2222
'user_id': 'test01',
23-
'reason': 'violence',
2423
'created_at': 1520150272
2524
},
2625
{
2726
'article_id': 'testid000001',
2827
'user_id': 'test01',
29-
'reason': 'violence',
3028
'created_at': 1520150273
3129
},
3230
{
3331
'article_id': 'testid000002',
3432
'user_id': 'test02',
35-
'reason': 'violence',
3633
'created_at': 1520150273
3734
}
3835
]
@@ -87,7 +84,6 @@ def test_main_ok_exist_article_id(self):
8784
'pathParameters': {
8885
'article_id': self.article_fraud_user_table_items[0]['article_id']
8986
},
90-
'body': json.dumps({'reason': self.article_fraud_user_table_items[0]['reason']}),
9187
'requestContext': {
9288
'authorizer': {
9389
'claims': {
@@ -107,14 +103,12 @@ def test_main_ok_exist_article_id(self):
107103

108104
target_article_id = params['pathParameters']['article_id']
109105
target_user_id = params['requestContext']['authorizer']['claims']['cognito:username']
110-
target_reason = json.loads(params['body'])['reason']
111106

112107
article_fraud_user = self.get_article_fraud_user(target_article_id, target_user_id)
113108

114109
expected_items = {
115110
'article_id': target_article_id,
116111
'user_id': target_user_id,
117-
'reason': target_reason,
118112
'created_at': 1520150272000003
119113
}
120114

@@ -129,7 +123,6 @@ def test_call_validate_article_existence(self):
129123
'pathParameters': {
130124
'article_id': 'testid000002'
131125
},
132-
'body': json.dumps({'reason': self.article_fraud_user_table_items[0]['reason']}),
133126
'requestContext': {
134127
'authorizer': {
135128
'claims': {
@@ -154,7 +147,6 @@ def test_main_ng_exist_user_id(self):
154147
'pathParameters': {
155148
'article_id': self.article_fraud_user_table_items[0]['article_id']
156149
},
157-
'body': json.dumps({'reason': self.article_fraud_user_table_items[0]['reason']}),
158150
'requestContext': {
159151
'authorizer': {
160152
'claims': {

0 commit comments

Comments
 (0)