Ejemplo n.º 1
0
 def test_service_callable_raise_api_exception_strict_mode_off(self):
     # patch where the class is located.
     with mock.patch('watson_transformer.service.nlu.IAMAuthenticator'):
         with mock.patch(
                 'watson_transformer.service.nlu.NaturalLanguageUnderstandingV1'
         ) as mock_nlu_api:
             # arrange
             mock_nlu_api.return_value.analyze.side_effect = ApiException(
                 'NLU API raise exception.')  # mock nlu.analyze()
             nlu = NLU(token='foo',
                       endpoint='http://www.foo.com/bar',
                       strict_mode=False,
                       features='foo')
             for value in ['      ', '    _', 'one two']:
                 # act
                 response = nlu(value)
                 # assert
                 assert response == None
                 assert nlu.strict_mode == False
Ejemplo n.º 2
0
    def test_create_rules_example(self):
        """
        create_rules request example
        """
        try:
            # begin-create_rules

            target_resource_model = {
                'service_name': service_name,
                'resource_kind': 'service'
            }

            rule_required_config_model = {
                'description': 'Public access check',
                'property': 'public_access_enabled',
                'operator': 'is_true'
            }

            enforcement_action_model = {'action': 'disallow'}

            rule_request_model = {
                'account_id': account_id,
                'name': 'Disable public access',
                'description':
                'Ensure that public access to account resources is disabled.',
                'target': {
                    'service_name': service_name,
                    'resource_kind': 'service'
                },
                'required_config': {
                    'description':
                    'Public access check',
                    'and': [{
                        'property': 'public_access_enabled',
                        'operator': 'is_false'
                    }]
                },
                'enforcement_actions': [enforcement_action_model],
                'labels': [test_label]
            }

            create_rule_request_model = {
                'request_id': '3cebc877-58e7-44a5-a292-32114fa73558',
                'rule': {
                    'account_id':
                    account_id,
                    'name':
                    'Disable public access',
                    'description':
                    'Ensure that public access to account resources is disabled.',
                    'labels': [test_label],
                    'target': {
                        'service_name': service_name,
                        'resource_kind': 'service'
                    },
                    'required_config': {
                        'description':
                        'Public access check',
                        'and': [{
                            'property': 'public_access_enabled',
                            'operator': 'is_false'
                        }]
                    },
                    'enforcement_actions': [{
                        'action': 'disallow'
                    }, {
                        'action': 'audit_log'
                    }]
                }
            }

            detailed_response = configuration_governance_service.create_rules(
                rules=[create_rule_request_model],
                transaction_id=transaction_id)
            create_rules_response = detailed_response.get_result()
            if detailed_response.status_code == 207:
                for responseEntry in create_rules_response['rules']:
                    if responseEntry['status_code'] > 299:
                        raise ApiException(
                            code=responseEntry['errors'][0]['code'],
                            message=responseEntry['errors'][0]['message'])

            print(json.dumps(create_rules_response, indent=2))

            # end-create_rules

            global rule_id_link
            rule_id_link = create_rules_response['rules'][0]['rule']['rule_id']
        except ApiException as e:
            pytest.fail(str(e))
Ejemplo n.º 3
0
def test_api_exception():
    """Test APIException class"""
    responses.add(responses.GET,
                  'https://test.com',
                  status=500,
                  body=json.dumps({'error': 'sorry', 'msg': 'serious error'}),
                  content_type='application/json')

    mock_response = requests.get('https://test.com')
    exception = ApiException(500, http_response=mock_response)
    assert exception is not None
    assert exception.message == 'sorry'

    responses.add(responses.GET,
                  'https://test-again.com',
                  status=500,
                  body=json.dumps({
                      "errors": [
                          {
                              "message": "sorry again",
                          }],
                  }),
                  content_type='application/json')
    mock_response = requests.get('https://test-again.com')
    exception = ApiException(500, http_response=mock_response)
    assert exception.message == 'sorry again'

    responses.add(responses.GET,
                  'https://test-once-more.com',
                  status=500,
                  body=json.dumps({'message': 'sorry once more'}),
                  content_type='application/json')
    mock_response = requests.get('https://test-once-more.com')
    exception = ApiException(500, http_response=mock_response)
    assert exception.message == 'sorry once more'

    responses.add(responses.GET,
                  'https://test-msg.com',
                  status=500,
                  body=json.dumps({'msg': 'serious error'}),
                  content_type='application/json')
    mock_response = requests.get('https://test-msg.com')
    exception = ApiException(500, http_response=mock_response)
    assert exception.message == 'Internal Server Error'

    responses.add(responses.GET,
                  'https://test-errormessage.com',
                  status=500,
                  body=json.dumps({'errorMessage': 'IAM error message'}),
                  content_type='application/json')
    mock_response = requests.get('https://test-errormessage.com')
    exception = ApiException(500, http_response=mock_response)
    assert exception.message == 'IAM error message'

    responses.add(responses.GET,
                  'https://test-for-text.com',
                  status=500,
                  headers={'X-Global-Transaction-ID': 'xx'},
                  body="plain text error")
    mock_response = requests.get('https://test-for-text.com')
    exception = ApiException(500, http_response=mock_response)
    assert exception.message == 'plain text error'
    assert str(exception) == 'Error: plain text error, Code: 500 , X-global-transaction-id: xx'