Add try/catch when fetching user

This commit is contained in:
Nathan Chapman 2022-05-12 21:48:18 -06:00
parent 28e6c9001b
commit 63a15b8935
2 changed files with 63 additions and 3 deletions

View File

@ -7,7 +7,9 @@ from django.conf import settings
from selenium.webdriver.firefox.webdriver import WebDriver from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import WebDriverException from selenium.common.exceptions import (
WebDriverException, NoSuchElementException
)
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support import expected_conditions as EC
@ -42,7 +44,62 @@ class CouponTests(StaticLiveServerTestCase):
session_id = self.browser.get_cookie('sessionid') session_id = self.browser.get_cookie('sessionid')
self.assertTrue(session_id) self.assertTrue(session_id)
def test_apply_coupon_to_order(self): def test_apply_coupon_to_order_for_non_registered_user(self):
# Add item to cart
self.browser.get(self.live_server_url + '/products/1/')
self.browser.find_element_by_xpath(
'//input[@value="Add to cart"]'
).click()
self.assertEqual(
self.browser.find_element_by_class_name('cart__count').text,
'1'
)
# Add coupon code
coupon_input = self.browser.find_element_by_id('id_code')
coupon_input.send_keys('MAY2022')
self.browser.find_element_by_xpath('//input[@value="Apply"]').click()
self.browser.find_element_by_xpath(
'//a[contains(text(), "Proceed to Checkout")]'
).click()
# Add 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('Peter Templer')
email_input = self.browser.find_element_by_id('id_email')
email_input.send_keys('peter@example.com')
street_address_1_input = self.browser.find_element_by_name(
'street_address_1'
)
street_address_1_input.send_keys('1579 Talon Dr')
city_input = self.browser.find_element_by_name('city')
city_input.send_keys('Logan')
state_select = select = Select(
self.browser.find_element_by_name('state')
)
state_select.select_by_value('UT')
postal_code_input = self.browser.find_element_by_name('postal_code')
postal_code_input.send_keys('84321')
self.browser.find_element_by_xpath(
'//input[@value="Continue to Payment"]'
).click()
self.assertEqual(
self.browser.title,
'Checkout | Port Townsend Roasting Co.'
)
# Check there is not an exception
with self.assertRaises(NoSuchElementException):
self.browser.find_element_by_css_selector(
'.messages p'
).text
def test_apply_used_coupon_to_order_returns_message(self):
# Add item to cart # Add item to cart
self.browser.get(self.live_server_url + '/products/1/') self.browser.get(self.live_server_url + '/products/1/')
self.browser.find_element_by_xpath( self.browser.find_element_by_xpath(

View File

@ -227,7 +227,10 @@ class OrderCreateView(CreateView):
coupon = Coupon.objects.get( coupon = Coupon.objects.get(
code=self.request.session.get('coupon_code') code=self.request.session.get('coupon_code')
) )
user = get_object_or_404(User, email=address['email']) try:
user = User.objects.get(email=address['email'])
except ObjectDoesNotExist:
user = None
if user in coupon.users.all(): if user in coupon.users.all():
del self.request.session['coupon_code'] del self.request.session['coupon_code']
messages.warning(request, 'Coupon already used.') messages.warning(request, 'Coupon already used.')