def test_get_success_url(self, user: User, rf: RequestFactory): view = UserUpdateView() request = rf.get("/fake-url/") request.user = user view.request = request assert view.get_success_url() == f"/users/{user.username}/"
def test_get_object(self, user: User, rf: RequestFactory): view = UserUpdateView() request = rf.get("/fake-url/") request.user = user view.request = request assert view.get_object() == user
def test_get_object(self, user: settings.AUTH_USER_MODEL, request_factory: RequestFactory): view = UserUpdateView() request = request_factory.get("/fake-url/") request.user = user view.request = request assert view.get_object() == user
def test_get_success_url(self, user: settings.AUTH_USER_MODEL, request_factory: RequestFactory): view = UserUpdateView() request = request_factory.get("/fake-url/") request.user = user view.request = request assert view.get_success_url() == f"/users/{user.username}/"
def test_get_success_url(self, user: User, rf: RequestFactory): """[summary] Test UserUpdateView return the correct /users/{user.id}/ (user detail) """ view = UserUpdateView() request = rf.get("/fake-url/") request.user = user view.request = request assert view.get_success_url() == f"/users/{user.id}/"
def test_form_valid(self, user: User, rf: RequestFactory): view = UserUpdateView() request = rf.get("/fake-url/") # Add the session/message middleware to the request SessionMiddleware(self.dummy_get_response).process_request(request) MessageMiddleware(self.dummy_get_response).process_request(request) request.user = user view.request = request # Initialize the form form = UserChangeForm() form.cleaned_data = [] view.form_valid(form) messages_sent = [m.message for m in messages.get_messages(request)] assert messages_sent == ["Information successfully updated"]
def test_update_object(self, user: User, rf: RequestFactory): request = rf.post({"name": "Other name"}) # type: ignore from django.contrib.messages.storage.fallback import FallbackStorage setattr(request, "session", "session") messages = FallbackStorage(request) setattr(request, "_messages", messages) request.user = user response = UserUpdateView.as_view()(request) assert response.status_code == 302
def test_form_valid(self, user: User, request_factory: RequestFactory): form_data = {"name": "John Doe"} request = request_factory.post(reverse("users:update"), form_data) request.user = user session_middleware = SessionMiddleware() session_middleware.process_request(request) msg_middleware = MessageMiddleware() msg_middleware.process_request(request) response = UserUpdateView.as_view()(request) user.refresh_from_db() assert response.status_code == 302 assert user.name == form_data["name"]