Add urllib quote to address validation

This commit is contained in:
Nathan Chapman 2022-05-20 17:16:34 -06:00
parent c09e166381
commit 05506503bf
4 changed files with 48 additions and 11 deletions

View File

@ -3,9 +3,10 @@ import requests
import xmltodict
from lxml import etree
from usps import USPSApi
from usps import USPSApi as USPSApiBase
class USPSApiWithRate(USPSApi):
class USPSApi(USPSApiBase):
urls = {
'tracking': 'TrackV2{test}&XML={xml}',
'label': 'eVS{test}&XML={xml}',

View File

@ -1,4 +1,6 @@
import os, time
import os
import time
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
@ -8,6 +10,7 @@ from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
class AddressTests(StaticLiveServerTestCase):
fixtures = ['products.json']
@ -54,3 +57,35 @@ class AddressTests(StaticLiveServerTestCase):
).text,
'USPS: Address Not Found.'
)
def test_address_is_url_safe(self):
self.browser.get(self.live_server_url + '/checkout/address/')
self.assertEqual(
self.browser.title,
'Checkout | Port Townsend Roasting Co.'
)
full_name_input = self.browser.find_element_by_name("full_name")
full_name_input.send_keys('John Doe')
email_input = self.browser.find_element_by_id('id_email')
email_input.send_keys('john@example.com')
street_address_1_input = self.browser.find_element_by_name('street_address_1')
street_address_1_input.send_keys('4031 N Douglas HWY #B')
city_input = self.browser.find_element_by_name('city')
city_input.send_keys('Juneau')
state_select = select = Select(self.browser.find_element_by_name('state'))
state_select.select_by_value('AK')
postal_code_input = self.browser.find_element_by_name('postal_code')
postal_code_input.send_keys('99801')
self.browser.find_element_by_xpath('//input[@value="Continue to Payment"]').click()
# try:
# WebDriverWait(self.browser, 4).until(
# EC.presence_of_element_located((By.CLASS_NAME, 'errorlist'))
# )
# finally:
# self.browser.quit()
self.assertRegex(
self.browser.current_url,
'^http://localhost:[0-9]*/checkout/$'
)

View File

@ -8,7 +8,7 @@ from django.shortcuts import redirect, reverse
from django.urls import reverse_lazy
from core.models import Product, OrderLine, Coupon
from core.usps import USPSApiWithRate
from core.usps import USPSApi
from core import (
DiscountValueType,
VoucherType,
@ -125,7 +125,7 @@ class Cart:
usps_rate_request = self.build_usps_rate_request()
except TypeError as e:
return Decimal('0.00')
usps = USPSApiWithRate(settings.USPS_USER_ID, test=True)
usps = USPSApi(settings.USPS_USER_ID, test=True)
try:
validation = usps.get_rate(usps_rate_request)

View File

@ -1,6 +1,7 @@
import logging
import json
from requests import ConnectionError
from urllib.parse import quote
from django import forms
from django.conf import settings
from django.core.mail import EmailMessage
@ -74,12 +75,12 @@ class AddressForm(forms.Form):
def clean(self):
cleaned_data = super().clean()
address = Address(
name=cleaned_data.get('full_name'),
address_1=cleaned_data.get('street_address_1'),
address_2=cleaned_data.get('street_address_2'),
city=cleaned_data.get('city'),
state=cleaned_data.get('state'),
zipcode=cleaned_data.get('postal_code')
name=quote(cleaned_data.get('full_name')),
address_1=quote(cleaned_data.get('street_address_1')),
address_2=quote(cleaned_data.get('street_address_2')),
city=quote(cleaned_data.get('city')),
state=quote(cleaned_data.get('state')),
zipcode=quote(cleaned_data.get('postal_code'))
)
usps = USPSApi(settings.USPS_USER_ID, test=True)