Beispiel #1
0
def get_address_from_freeform(address):
    client = Client(auth_id=SMARTY_STREETS_AUTH_ID,
                    auth_token=SMARTY_STREETS_AUTH_TOKEN)
    response = client.street_address(str(address))

    if not response or not response.get('analysis', False) or \
      response['analysis'].get('active', 'N') != 'Y':
        raise ValidationError("could not validate freeform address",
                              payload={"address": address})

    return response
class TestClient(unittest.TestCase):

    def setUp(self):
        self.client = Client(auth_id='blah', auth_token='blibbidy')

    @responses.activate
    def test_input_error(self):
        responses.add(responses.POST, 'https://api.smartystreets.com/street-address',
                  body='', status=400,
                  content_type='application/json')
        self.assertRaises(SmartyStreetsInputError, self.client.street_addresses, [{}, {}])

    @responses.activate
    def test_auth_error(self):
        responses.add(responses.POST, 'https://api.smartystreets.com/street-address',
                      body='', status=401,
                      content_type='application/json')
        self.assertRaises(SmartyStreetsAuthError, self.client.street_addresses, [{}, {}])

    @responses.activate
    def test_payment_error(self):
        responses.add(responses.POST, 'https://api.smartystreets.com/street-address',
                      body='', status=402,
                      content_type='application/json')
        self.assertRaises(SmartyStreetsPaymentError, self.client.street_addresses, [{}, {}])

    @responses.activate
    def test_server_error(self):
        responses.add(responses.POST, 'https://api.smartystreets.com/street-address',
                      body='', status=500,
                      content_type='application/json')
        self.assertRaises(SmartyStreetsServerError, self.client.street_addresses, [{}, {}])

    @responses.activate
    def test_one_address(self):
        """Ensure singluar street address method returns an Address"""
        responses.add(responses.POST, 'https://api.smartystreets.com/street-address',
                      body='[{"street_address": "100 Main St"}]',
                      status=200, content_type='application/json')
        response = self.client.street_address({"street": "100 Main st"})
        self.assertIsInstance(response, Address)

    @responses.activate
    def test_addresses_response(self):
        """Ensure address return an AddressCollection"""
        responses.add(responses.POST, 'https://api.smartystreets.com/street-address',
                      body='[{"street_address": "100 Main St"}, {"street_address": "200 Main St"}]',
                      status=200, content_type='application/json')
        response = self.client.street_addresses([{"street": "100 Main st"},
                                                 {"street": "200 Main St"}])
        self.assertIsInstance(response, AddressCollection)
        self.assertEqual(2, len(response))
Beispiel #3
0
def get_address_components(address, city, state, zip):
    client = Client(auth_id=SMARTY_STREETS_AUTH_ID,
                    auth_token=SMARTY_STREETS_AUTH_TOKEN)

    # reassemble components into string
    # smarty streets specifically wants strings (not unicode) so...
    full_address = "%(address)s, %(city)s, %(state)s, %(zip)s" % \
        {'address': str(address), 'city': str(city), 'state': str(state), 'zip': str(zip)}
    response = client.street_address(str(full_address))

    if not response or not response.get('analysis', False) or \
      response['analysis'].get('active', 'N') != 'Y':
        raise ValidationError("could not validate address",
                              payload={
                                  "address": address,
                                  "city": city,
                                  "state": state,
                                  "zip": zip
                              })

    # merge county into components dict
    d = response['components']
    d['county_name'] = response['metadata']['county_name']
    return d
Beispiel #4
0
class TestStreetAddress(unittest.TestCase):
    def setUp(self):
        self.client = Client(auth_id='blah', auth_token='blibbidy')

    @responses.activate
    def test_input_error(self):
        responses.add(responses.POST,
                      'https://api.smartystreets.com/street-address',
                      body='',
                      status=400,
                      content_type='application/json')
        self.assertRaises(SmartyStreetsInputError,
                          self.client.street_addresses, [{}, {}])

    @responses.activate
    def test_auth_error(self):
        responses.add(responses.POST,
                      'https://api.smartystreets.com/street-address',
                      body='',
                      status=401,
                      content_type='application/json')
        self.assertRaises(SmartyStreetsAuthError, self.client.street_addresses,
                          [{}, {}])

    @responses.activate
    def test_payment_error(self):
        responses.add(responses.POST,
                      'https://api.smartystreets.com/street-address',
                      body='',
                      status=402,
                      content_type='application/json')
        self.assertRaises(SmartyStreetsPaymentError,
                          self.client.street_addresses, [{}, {}])

    @responses.activate
    def test_server_error(self):
        responses.add(responses.POST,
                      'https://api.smartystreets.com/street-address',
                      body='',
                      status=500,
                      content_type='application/json')
        self.assertRaises(SmartyStreetsServerError,
                          self.client.street_addresses, [{}, {}])

    @responses.activate
    def test_one_address(self):
        """Ensure singluar street address method returns an Address"""
        responses.add(responses.POST,
                      'https://api.smartystreets.com/street-address',
                      body='[{"street_address": "100 Main St"}]',
                      status=200,
                      content_type='application/json')
        response = self.client.street_address({"street": "100 Main st"})
        self.assertIsInstance(response, Address)

    @responses.activate
    def test_addresses_response(self):
        """Ensure address return an AddressCollection"""
        responses.add(
            responses.POST,
            'https://api.smartystreets.com/street-address',
            body=
            '[{"street_address": "100 Main St"}, {"street_address": "200 Main St"}]',
            status=200,
            content_type='application/json')
        response = self.client.street_addresses([{
            "street": "100 Main st"
        }, {
            "street": "200 Main St"
        }])
        self.assertIsInstance(response, AddressCollection)
        self.assertEqual(2, len(response))