Skip to content

Commit 4c075e5

Browse files
committed
Add a validation process to authlite util.
1 parent ec43d0c commit 4c075e5

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

src/common/authlete_util.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import requests
44
import settings
55
from record_not_found_error import RecordNotFoundError
6+
from jsonschema import ValidationError
67

78

89
class AuthleteUtil:
@@ -24,9 +25,11 @@ def is_accessible_client(client_id, user_id):
2425

2526
return developer == user_id
2627

27-
# 404以外はALIS上では異常な状態であるため、システムエラーとして扱い、検知対象にする
28+
# 400, 404以外はALIS上では異常な状態であるため、システムエラーとして扱い、検知対象にする
2829
@staticmethod
2930
def verify_valid_response(response, request_client_id=None):
31+
if response.status_code == 400:
32+
raise ValidationError('Please check the input parameters')
3033
if request_client_id and response.status_code == 404:
3134
raise RecordNotFoundError('{0} is not found.'.format(request_client_id))
3235

tests/common/test_authlete_util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import settings
99
from authlete_util import AuthleteUtil
1010
from record_not_found_error import RecordNotFoundError
11+
from jsonschema import ValidationError
1112

1213

1314
class TestAuthleteUtil(TestCase):
@@ -90,6 +91,11 @@ def test_verify_valid_response(self):
9091
'request_client_id': '12345',
9192
'exception': False
9293
},
94+
{
95+
'status_code': 400,
96+
'request_client_id': '12345',
97+
'exception': ValidationError
98+
},
9399
{
94100
'status_code': 404,
95101
'request_client_id': None,
@@ -121,6 +127,10 @@ def test_verify_valid_response(self):
121127
with self.assertRaises(Exception):
122128
AuthleteUtil.verify_valid_response(response, case['request_client_id'])
123129

130+
if case['exception'] is ValidationError:
131+
with self.assertRaises(ValidationError):
132+
AuthleteUtil.verify_valid_response(response, case['request_client_id'])
133+
124134
if case['exception'] is RecordNotFoundError:
125135
with self.assertRaises(RecordNotFoundError):
126136
AuthleteUtil.verify_valid_response(response, case['request_client_id'])

tests/handlers/me/applications/create/test_me_applications_create.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,40 @@ def test_main_ok_type_native(self):
8989
self.assertEqual('PUBLIC', json.loads(responses.calls[0].request.body).get('clientType'))
9090
self.assertEqual('NONE', json.loads(responses.calls[0].request.body).get('tokenAuthMethod'))
9191

92+
@responses.activate
93+
def test_main_ng_authlete_api_response_400(self):
94+
params = {
95+
'body': {
96+
'name': 'あ' * 80,
97+
'description': 'A' * 180,
98+
'application_type': 'NATIVE',
99+
'redirect_urls': ['http://example.com/1']
100+
},
101+
'requestContext': {
102+
'authorizer': {
103+
'claims': {
104+
'cognito:username': 'user01',
105+
'phone_number_verified': 'true',
106+
'email_verified': 'true'
107+
}
108+
}
109+
}
110+
}
111+
112+
params['body'] = json.dumps(params['body'])
113+
114+
# 400 が返却されるように mock 化
115+
responses.add(responses.POST, settings.AUTHLETE_CLIENT_ENDPOINT + '/create',
116+
json={"resultCode": "A031208", "resultMessage": "error_message"}, status=400)
117+
118+
response = MeApplicationsCreate(params, {}).main()
119+
120+
logging.fatal(response)
121+
122+
self.assertEqual(response['statusCode'], 400)
123+
self.assertEqual(json.loads(response['body']),
124+
{"message": "Invalid parameter: Please check the input parameters"})
125+
92126
@patch('requests.post', MagicMock(side_effect=requests.exceptions.RequestException()))
93127
def test_main_with_exception(self):
94128
params = {

tests/handlers/me/applications/update/test_me_applications_update.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,47 @@ def test_main_ok(self):
5858
self.assertEqual(response['statusCode'], 200)
5959
self.assertEqual(json.loads(response['body']), {"developer": "user01"})
6060

61+
@responses.activate
62+
def test_main_ng_authlete_api_response_400(self):
63+
params = {
64+
'pathParameters': {
65+
'client_id': '123456789'
66+
},
67+
'body': {
68+
'name': 'あ' * 80,
69+
'description': 'A' * 180,
70+
'redirect_urls': ['http://example.com/1']
71+
},
72+
'requestContext': {
73+
'authorizer': {
74+
'claims': {
75+
'cognito:username': 'user01',
76+
'phone_number_verified': 'true',
77+
'email_verified': 'true'
78+
}
79+
}
80+
}
81+
}
82+
83+
params['body'] = json.dumps(params['body'])
84+
85+
# 400 が返却されるように mock 化
86+
responses.add(responses.POST,
87+
settings.AUTHLETE_CLIENT_ENDPOINT + '/update/' + params['pathParameters']['client_id'],
88+
json={"resultCode": "A031233", "resultMessage": "error_message"}, status=400)
89+
# AuthleteUtilで呼ばれるAPI callをmockする
90+
responses.add(responses.GET, settings.AUTHLETE_CLIENT_ENDPOINT + '/get/' + params['pathParameters']['client_id'],
91+
json={'developer': "user01"}, status=200)
92+
# アプリケーション情報取得で呼ばれるAPI callをmockする
93+
responses.add(responses.GET, settings.AUTHLETE_CLIENT_ENDPOINT + '/get/' + params['pathParameters']['client_id'],
94+
json={'developer': "user01"}, status=200)
95+
96+
response = MeApplicationUpdate(params, {}).main()
97+
98+
self.assertEqual(response['statusCode'], 400)
99+
self.assertEqual(json.loads(response['body']),
100+
{"message": "Invalid parameter: Please check the input parameters"})
101+
61102
@patch('requests.post', MagicMock(side_effect=requests.exceptions.RequestException()))
62103
def test_main_with_exception(self):
63104
params = {

0 commit comments

Comments
 (0)