def test_calls_auth_login_with_user_if_there_is_one( self, mock_login, mock_auth): self.request.method = 'POST' mock_a_login = mock_login.return_value mock_a_login.is_valid.return_value = True login_view(self.request) mock_auth.login.assert_called_once()
def test_renders_correct_html(self, mock_render, mock_login, mock_auth): self.request.method = 'GET' mock_a_login = mock_login.return_value login_view(self.request) # response.client = Client() mock_render.assert_called_once_with(self.request, 'accounts/login.html', {'form': mock_a_login})
def test_redirects_to_home_page(self, mock_login, mock_auth): self.request.method = 'POST' mock_auth.authenticate.return_value = True mock_a_login = mock_login.return_value mock_a_login.is_valid.return_value = True response = login_view(self.request) response.client = Client() self.assertRedirects(response, '/')
def display_cart(request): if request.user.is_authenticated: if request.method == 'GET': user_id = request.user.id cart_items = CartItem.objects.filter(user_id=user_id,order_id__isnull=True) return render(request, 'myCart/index.html',{'cart_items':cart_items}) else: return login_view(request)
def test_redirect_homepage_if_already_loggedin(self, mock_login, mock_auth): self.request.method = 'POST' self.request.user = Mock() self.request.user(data=self.request.POST) self.assertTrue(self.request.user.is_authenticated()) response = login_view(self.request) response.client = Client() self.assertRedirects(response, '/')
def test_login_home_view(self): auction = AuctionFactory.create() request_factory = RequestFactory() request = request_factory.post('/accounts/login/', data={ 'username': auction.seller, 'password': "******" }) setattr(request, 'session', 'session') messages = FallbackStorage(request) setattr(request, '_messages', messages) middleware = SessionMiddleware() middleware.process_request(request) response = login_view(request)
def food_upload(request): if request.user.is_authenticated: if request.method == 'POST': form = FoodForm(request.POST, request.FILES) if form.is_valid(): food = form.save(commit=False) food.user = request.user food.created_at = timezone.now() food.save() context = {'form': form} # messages.success(request, 'your post was uploaded') return redirect('/eatin/') else: messages.error(request, 'oops something went wrong, try again') form = FoodForm() context = {"form": form} return render(request, 'cook/index.html', context) else: print("you are not logged in") return login_view(request)
def test_login(self): request = self.factory.get('/login/') response = login_view(request) self.assertEqual(response.status_code, 200)
def test_doesnt_login_if_loginform_not_valid(self, mock_login, mock_auth): self.request.method = 'POST' mock_a_login = mock_login.return_value mock_a_login.is_valid.return_value = False login_view(self.request) mock_auth.login.assert_not_called()