def test_already_added_user_id(self): event = { 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', } } } } # usersテーブルとcognitoの両方にユーザが存在する場合 with patch('me_external_provider_user_create.UserUtil') as user_mock: event['body'] = json.dumps({'user_id': 'username02'}) user_mock.get_cognito_user_info.return_value = { 'Username': '******', 'UserAttributes': {} } response = MeExternalProviderUserCreate(event=event, context="", dynamodb=dynamodb).main() self.assertEqual(response['statusCode'], 400) # usersテーブルのみにユーザが存在する場合(想定されるデータ不整合のため、そのような場合でも意図したエラーを返すことのテスト) with patch('me_external_provider_user_create.UserUtil') as user_mock: event['body'] = json.dumps({'user_id': 'username02'}) user_mock.get_cognito_user_info.side_effect = RecordNotFoundError( 'Record Not Found') response = MeExternalProviderUserCreate(event=event, context="", dynamodb=dynamodb).main() self.assertEqual(response['statusCode'], 400) # cognitoのみにユーザが存在する場合(想定されるデータ不整合のため、そのような場合でも意図したエラーを返すことのテスト) with patch('me_external_provider_user_create.UserUtil') as user_mock: event['body'] = json.dumps({'user_id': 'onlycognito'}) user_mock.get_cognito_user_info.return_value = { 'Username': '******', 'UserAttributes': {} } response = MeExternalProviderUserCreate(event=event, context="", dynamodb=dynamodb).main() self.assertEqual(response['statusCode'], 400)
def test_main_ok(self): with patch('me_external_provider_user_create.UserUtil') as user_mock, \ patch('me_external_provider_user_create.CryptoUtil') as crypto_mock: event = { 'body': { 'user_id': 'username01', }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', } } } } event['body'] = json.dumps(event['body']) user_mock.check_try_to_register_as_twitter_user.return_value = False user_mock.check_try_to_register_as_line_user.return_value = False user_mock.check_try_to_register_as_yahoo_user.return_value = False user_mock.check_try_to_register_as_facebook_user.return_value = False crypto_mock.decrypt_password.return_value = 'password' user_mock.create_external_provider_user.return_value = { 'AuthenticationResult': { 'AccessToken': 'aaaaa', 'IdToken': 'bbbbb', 'RefreshToken': 'ccccc' } } user_mock.wallet_initialization.return_value = None user_mock.force_non_verified_phone.return_value = None user_mock.add_user_id_to_external_provider_user.return_value = None user_mock.delete_external_provider_id_cognito_user.return_value = True user_mock.has_user_id.return_value = True user_mock.add_user_profile.return_value = None response = MeExternalProviderUserCreate(event=event, context="", dynamodb=dynamodb).main() self.assertEqual(response['statusCode'], 200) self.assertEqual( json.loads(response['body']), { 'access_token': 'aaaaa', 'id_token': 'bbbbb', 'refresh_token': 'ccccc', 'last_auth_user': '******', 'has_user_id': True, 'status': 'login' })
def test_main_ng_not_target_user(self): event = { 'body': { 'user_id': 'username01', }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', } } } } event['body'] = json.dumps(event['body']) response = MeExternalProviderUserCreate(event=event, context="", dynamodb=dynamodb).main() self.assertEqual(response['statusCode'], 400)
def test_invalid_yahoo_user_id(self): event = { 'body': { 'user_id': 'Yahoo-test', }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', } } } } event['body'] = json.dumps(event['body']) response = MeExternalProviderUserCreate(event=event, context="", dynamodb=dynamodb).main() self.assertEqual(response['statusCode'], 400)
def test_main_ok(self): with patch('me_external_provider_user_create.UserUtil.create_external_provider_user') as create_external_mock, \ patch('me_external_provider_user_create.UserUtil.get_cognito_user_info') as get_cognito_user_mock: create_external_mock.return_value = { 'AuthenticationResult': { 'AccessToken': 'aaaaa', 'IdToken': 'bbbbb', 'RefreshToken': 'ccccc' } } get_cognito_user_mock.side_effect = RecordNotFoundError( 'Record Not Found') event = { 'body': { 'user_id': 'username01', }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', } } } } event['body'] = json.dumps(event['body']) response = MeExternalProviderUserCreate(event=event, context="", dynamodb=dynamodb).main() self.assertEqual(response['statusCode'], 200) self.assertEqual( json.loads(response['body']), { 'access_token': 'aaaaa', 'id_token': 'bbbbb', 'refresh_token': 'ccccc', 'last_auth_user': '******', 'has_user_id': True, 'status': 'login' })
def test_already_exist_user_id(self): with patch('me_external_provider_user_create.UserUtil') as user_mock: event = { 'body': { 'user_id': 'existname', }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', } } } } event['body'] = json.dumps(event['body']) user_mock.get_cognito_user_info.side_effect = RecordNotFoundError( 'Record Not Found') response = MeExternalProviderUserCreate(event=event, context="", dynamodb=dynamodb).main() self.assertEqual(response['statusCode'], 400)
def lambda_handler(event, context): me_external_provider_user_create = MeExternalProviderUserCreate( event=event, context=context, dynamodb=dynamodb, cognito=cognito) return me_external_provider_user_create.main()