diff --git a/ecommerce/coupons/tests/test_views.py b/ecommerce/coupons/tests/test_views.py index 2ec4ba1c369..641dc721286 100644 --- a/ecommerce/coupons/tests/test_views.py +++ b/ecommerce/coupons/tests/test_views.py @@ -441,10 +441,16 @@ def test_sku_not_provided(self): def test_invalid_voucher_code(self): """ Verify an error is returned when voucher does not exist. """ code = FuzzyText().fuzz().upper() + expected_message = 'No voucher found with code {code}'.format(code=code) url = format_url(base=self.redeem_url, params={'code': code, 'sku': self.stock_record.partner_sku}) - response = self.client.get(url) - msg = 'No voucher found with code {code}'.format(code=code) - self.assertEqual(response.context['error'], msg) + response = self.client.get(url, follow=True) + + messages = [] + messages += response.context['messages'] + + self.assertEqual(messages[0].tags, 'error') + self.assertEqual(messages[0].message, expected_message) + self.assert_redemption_page_redirects(self.get_coupon_redeem_success_expected_redirect_url()) def test_no_product(self): """ Verify an error is returned when a stock record for the provided SKU doesn't exist. """ diff --git a/ecommerce/coupons/views.py b/ecommerce/coupons/views.py index f195c66ef22..848654f939b 100644 --- a/ecommerce/coupons/views.py +++ b/ecommerce/coupons/views.py @@ -169,16 +169,26 @@ def get(self, request): # pylint: disable=too-many-statements if not sku: return render(request, template_name, {'error': _('SKU not provided.')}) + try: + product = StockRecord.objects.get(partner_sku=sku).product + except StockRecord.DoesNotExist: + return render(request, template_name, {'error': _('The product does not exist.')}) + try: voucher = Voucher.objects.get(code=code) except Voucher.DoesNotExist: + try: + basket = prepare_basket(request, [product]) + except AlreadyPlacedOrderException: + msg = _('You have already purchased {course} seat.').format(course=product.course.name) + return render(request, template_name, {'error': msg}) + msg = 'No voucher found with code {code}'.format(code=code) - return render(request, template_name, {'error': _(msg)}) + messages.error(self.request, _(msg)) - try: - product = StockRecord.objects.get(partner_sku=sku).product - except StockRecord.DoesNotExist: - return render(request, template_name, {'error': _('The product does not exist.')}) + redirect_url = get_payment_microfrontend_or_basket_url(self.request) + "?coupon_redeem_redirect=1" + redirect_url = add_stripe_flag_to_url(redirect_url, self.request) + return HttpResponseRedirect(redirect_url) valid_voucher, msg, hide_error_message = voucher_is_valid(voucher, [product], request) if not valid_voucher: