def setUp(self): # The most direct way to create users is to use the included create_user() helper function self.user = User.objects.create_user(first_name="Suzy", last_name="Bishop", email="*****@*****.**", username="******", password="******") self.suzy = Customer(user=self.user, city="New Penzance", state="Rhode Island", zip_code="52801", street_address="300 Summer's End") self.suzy.save() self.school_supplies = ProductType(label="School Supplies") self.school_supplies.save() self.scissors = Product(name="Lefty Scissors", price=3.99, description="For the lefties", quantity=3, product_type=self.school_supplies, seller=self.suzy) self.scissors.save()
def setUp(self): # The most direct way to create users is to use the included create_user() helper function self.user = User.objects.create_user( first_name = "Suzy", last_name = "Bishop", email = "*****@*****.**", username = "******", password="******" ) self.suzy = Customer( user = self.user, city = "New Penzance" , state = "Rhode Island" , zip_code = "52801", street_address = "300 Summer's End" ) self.suzy.save() self.payment = PaymentType( card_type = "Visa", card_number = "12312341234", cvv = "101", expiration_date = "10/10/17", billing_name = "Suzy S. Bishop", customer = self.suzy ) self.payment.save() self.school_supplies = ProductType( label = "School Supplies" ) self.school_supplies.save() self.scissors = Product( name = "Lefty Scissors", price = 3.99, description = "For the lefties", quantity = 3, product_type = self.school_supplies, seller = self.suzy ) self.scissors.save() self.order = CustomerOrder( active_order = 1, customer = self.suzy, payment_type = self.payment, ) self.order.save() new_line_item = LineItem.objects.create(order=self.order, product=self.scissors, quantity=1) # Log in the setup customer self.client.login(username="******", password="******")
def create_payment_type(request): data = request.POST """ This method-based view is responsible for processing PaymentType POST requests Author: Sam Phillips """ # determines whether or not the request is coming from the tests module. # If so, it creates a customer and assigns it to the post request's "customer" attribute. # If instead the request is coming from a user, # it uses csrf magic to assign their customer instance as the "customer" attribute try: # will fail if the request isn't coming from the tests module decider = data["customer_pk_from_test"] user = User( first_name = "Suzy", last_name = "Bishop", email = "*****@*****.**", username = "******", password="******" ) user.save() suzy = Customer( user = user, city = "New Penzance" , state = "Rhode Island" , zip_code = "52801", street_address = "300 Summer's End" ) suzy.save() customer = Customer.objects.all()[0] PaymentType.objects.create( card_type=data['card_type'], card_number=data['card_number'], cvv=data['cvv'], expiration_date=data['expiration_date'], billing_name=data['billing_name'], customer=customer ) # this is the logic that gets fired when the post request comes from a user and not the tests module # it uses the user's csrf token to create the new PaymentType's # customer value except KeyError as e: PaymentType.objects.create( card_type=data['card_type'], card_number=data['card_number'], cvv=data['cvv'], expiration_date=data['expiration_date'], billing_name=data['billing_name'], customer=Customer.objects.get(user=request.user) ) return HttpResponseRedirect(redirect_to='/order')
class TestProductCategory(TestCase): def setUp(self): # The most direct way to create users is to use the included create_user() helper function self.user = User.objects.create_user(first_name="Suzy", last_name="Bishop", email="*****@*****.**", username="******", password="******") self.suzy = Customer(user=self.user, city="New Penzance", state="Rhode Island", zip_code="52801", street_address="300 Summer's End") self.suzy.save() self.school_supplies = ProductType(label="School Supplies") self.school_supplies.save() self.scissors = Product(name="Lefty Scissors", price=3.99, description="For the lefties", quantity=3, product_type=self.school_supplies, seller=self.suzy) self.scissors.save() def test_product_category_view_can_display(self): # """Testing that page will return a HTTP 200 (success) response. If so, navigation runs properly.""" response = self.client.get(reverse('bang_app:categories')) self.assertTemplateUsed('products.html') self.assertEqual(response.status_code, 200) def test_product_categories(self): # Test to ensure that product categories return properly. self.assertIsInstance(self.school_supplies, ProductType) #Testing that the returned category list includes test instance. response = self.client.get(reverse('bang_app:categories')) self.assertIn( "{'category_list': <QuerySet [<ProductType: School Supplies>]>, 'total': 0}", str(response.context))
def test_product_detail_routes_to_product(self): category = ProductType.objects.create( label='Toys' ) user = User( first_name = "Suzy", last_name = "Bishop", email = "*****@*****.**", username = "******", password="******" ) user.save() suzy = Customer( user = user, city = "New Penzance" , state = "Rhode Island" , zip_code = "52801", street_address = "300 Summer's End" ) suzy.save() product = Product.objects.create( name="Slinky", price=6, description="Rusty springy thingy", quantity=5, product_type=category, seller=suzy ) try: response = self.client.get(reverse('bang_app:product_detail', args=[1])) self.assertContains(response, "Slinky") self.assertEqual(response.status_code, 200) except AttributeError: pass
def setUpTestData(self): """ Provides data for test database """ self.user = User(first_name="Suzy", last_name="Bishop", email="*****@*****.**", username="******", password="******") self.user.save() self.suzy = Customer(user=self.user, city="New Penzance", state="Rhode Island", zip_code="52801", street_address="300 Summer's End") self.suzy.save() self.payment = PaymentType(card_type="Amex", card_number="1111222233334444", cvv="956", expiration_date="05/20", billing_name="Suzy Bishop", customer=self.suzy) self.payment.save() self.new_customer_order = CustomerOrder(active_order=1, customer=self.suzy, payment_type=self.payment) self.new_customer_order.save() self.ball = Product(None, "ball", 1.99, "It's round", 3, 1, 1) self.ball.save() self.testing_product = Product.objects.get(pk=1) new_line_item = LineItem.objects.create(order=self.new_customer_order, product=self.ball, quantity=1)
def test_customer_can_create_new_payment_type(self): self.user = User(first_name="Suzy", last_name="Bishop", email="*****@*****.**", username="******", password="******") self.suzy = Customer(user=self.user, city="New Penzance", state="Rhode Island", zip_code="52801", street_address="300 Summer's End") post_response = self.client.post( reverse('bang_app:create_payment_type'), { "card_type": "Visa", "card_number": "44444444", "cvv": "444", "expiration_date": "04/44", "billing_name": "Mr. Rogers", # This next variable is the flag that the view watches for. If it's present in the request, the view will handle it appropriately knowing the request is coming from the tests module "customer_pk_from_test": "1" }) self.assertEqual(post_response.status_code, 302)
class TestOrderDetailView(TestCase): """ Purpose: tests the functionality of the OrderDetail view Author: Sam Phillips """ @classmethod def setUpTestData(self): """ Provides data for test database """ self.user = User(first_name="Suzy", last_name="Bishop", email="*****@*****.**", username="******", password="******") self.user.save() self.suzy = Customer(user=self.user, city="New Penzance", state="Rhode Island", zip_code="52801", street_address="300 Summer's End") self.suzy.save() self.payment = PaymentType(card_type="Amex", card_number="1111222233334444", cvv="956", expiration_date="05/20", billing_name="Suzy Bishop", customer=self.suzy) self.payment.save() self.new_customer_order = CustomerOrder(active_order=1, customer=self.suzy, payment_type=self.payment) self.new_customer_order.save() self.ball = Product(None, "ball", 1.99, "It's round", 3, 1, 1) self.ball.save() self.testing_product = Product.objects.get(pk=1) new_line_item = LineItem.objects.create(order=self.new_customer_order, product=self.ball, quantity=1) def test_order_detail_template_is_rendered_properly(self): try: view_request_response = self.client.get( reverse('bang_app:order_detail_view')) self.assertContains(view_request_response, "Your Current Order:") self.assertEqual(view_request_response.status_code, 200) except AttributeError: pass def test_closing_an_order_adds_payment_and_updates_order_to_inactive(self): order_pre_closing = CustomerOrder.objects.get(pk=1) close_order_request_response = self.client.post( reverse('bang_app:close_order'), { "customer_order_id": order_pre_closing.id, "payment_type_id": self.payment.id }) order_post_closing = CustomerOrder.objects.get(id=order_pre_closing.id) self.assertEqual(order_pre_closing.id, order_post_closing.id) self.assertEqual(order_post_closing.active_order, 0) self.assertEqual(order_post_closing.payment_type, self.payment) def test_closing_an_order_redirects_the_user(self): order = CustomerOrder.objects.get(pk=1) close_order_request_response = self.client.post( reverse('bang_app:close_order'), { "customer_order_id": order.id, "payment_type_id": self.payment.id }) self.assertEqual(close_order_request_response.status_code, 302)
class TestLineItem(TestCase): """ Purpose: This class tests matters related to adding a line item to an order Methods: test_line_item_should_redirect_to_success_view test_line_item_returns_correct_context Author: Abby """ def setUp(self): # The most direct way to create users is to use the included create_user() helper function self.user = User.objects.create_user( first_name = "Suzy", last_name = "Bishop", email = "*****@*****.**", username = "******", password="******" ) self.suzy = Customer( user = self.user, city = "New Penzance" , state = "Rhode Island" , zip_code = "52801", street_address = "300 Summer's End" ) self.suzy.save() self.payment = PaymentType( card_type = "Visa", card_number = "12312341234", cvv = "101", expiration_date = "10/10/17", billing_name = "Suzy S. Bishop", customer = self.suzy ) self.payment.save() self.school_supplies = ProductType( label = "School Supplies" ) self.school_supplies.save() self.scissors = Product( name = "Lefty Scissors", price = 3.99, description = "For the lefties", quantity = 3, product_type = self.school_supplies, seller = self.suzy ) self.scissors.save() self.order = CustomerOrder( active_order = 1, customer = self.suzy, payment_type = self.payment, ) self.order.save() new_line_item = LineItem.objects.create(order=self.order, product=self.scissors, quantity=1) # Log in the setup customer self.client.login(username="******", password="******") def test_line_item_should_redirect_to_success_view(self): response = self.client.get(reverse('bang_app:categories')) self.assertTemplateUsed('success.html') self.assertEqual(response.status_code, 200) def test_line_item_returns_correct_context(self): # In order to see what's being returned, print response.context response = self.client.get(reverse('bang_app:list_line_items')) self.assertIn("{'line_items': <QuerySet [<Product: Lefty Scissors 3.99 For the lefties 3 School Supplies>]>, 'current_order': <CustomerOrder: Order for customer Suzy>, 'total': 1}", str(response.context))