def test_index(self): """Test Function to check customer index page""" response = self.client.get('/') self.assertTrue(response.context['loginform'], LoginForm()) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'frontend/index.html') request = self.factory.get('/') request.user = self.user request.session = {} response = index(request) self.assertEqual(response.status_code, 200)
def test_given_empty_request_renders_html_content_with_div_id_app(self): http_request = {} http_response = index(http_request) self.assertEqual(http_response.charset, "utf-8") decoded_html = http_response.content.decode("utf8") self.assertTrue( decoded_html.startswith('<!DOCTYPE html>\n<html lang="en">')) self.assertIn("<head>", decoded_html) self.assertIn("<title>Questling</title>", decoded_html) self.assertIn("</head>", decoded_html) self.assertIn("<body>", decoded_html) self.assertIn('<div id="app"></div>', decoded_html) self.assertIn("<body>", decoded_html) self.assertTrue(decoded_html.endswith("</html>"))
def login(request): if request.method == 'POST': form = UserLoginForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] password = form.cleaned_data['password'] user = authenticate(email=email, password=password) django_login(request, user) if user.is_admin: return redirect('/admin') return index(request) form = UserLoginForm() context = {'form': form} return render(request, 'frontend/index.html', context)
def register(request): if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): form.save() email = form.cleaned_data['email'] password = form.cleaned_data['password1'] user = authenticate(email=email, password=password) if user: django_login(request, user) return index(request) context = { 'userLoginForm': UserLoginForm(), 'userCreationForm': UserCreationForm() } return render(request, 'frontend/index.html', context)
def checkout_done(request, user_id): user = User.objects.get(id=user_id) basket = Basket.objects.get(user=user) order = Order(customer=user) order.save() for item in basket.basketelement_set.all(): orderelement = OrderElement(order=order, book=item.book, price=item.price) orderelement.save() order.address = request.POST.get('address') order_status = OrderStatus.objects.get(name='Новый') order.order_status = order_status order.save() basket.delete() for item in basket.basketelement_set.all(): item.delete() return index(request)
def logout(request): django_logout(request) return index(request)
def test_given_empty_request_renders_status_ok(self): http_request = {} http_response = index(http_request) self.assertEqual(http_response.status_code, 200) self.assertEqual(http_response.reason_phrase, "OK")