Remove old profile page

This commit is contained in:
Lacey Williams Henschel
2023-08-04 13:11:06 -07:00
parent eeb615ceff
commit ebe07d249e
6 changed files with 16 additions and 155 deletions

View File

@@ -1,8 +1,4 @@
import tempfile
from django.contrib.auth import get_user_model
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import override_settings
User = get_user_model()
@@ -74,44 +70,3 @@ def test_signup_post(tp, db):
)
tp.response_302(res)
assert User.objects.filter(email="testerson@example.com").exists()
def test_profile_photo_auth(tp, db):
"""
POST /users/me/photo/
Canary test that the photo upload page is protected.
"""
res = tp.post("profile-photo")
tp.response_302(res)
assert "/accounts/login" in res.url
@override_settings(MEDIA_ROOT=tempfile.gettempdir())
def test_profile_photo_update_success(tp, user, client):
"""
POST /users/me/photo
Confirm that user can update their profile photo
"""
old_image = user.image
client.force_login(user)
image = SimpleUploadedFile(
"/image/fpo/user.png", b"file_content", content_type="image/png"
)
res = tp.post("profile-photo", data={"image": image})
tp.response_302(res)
assert "/users/me/" in res.url
user.refresh_from_db()
assert user.image != old_image
def test_profile_photo_github_auth(tp, db):
"""
POST /users/me/update-github-photo/
Canary test that the github photo update page is protected.
"""
res = tp.post("profile-photo-github")
tp.response_302(res)
assert "/accounts/login" in res.url

View File

@@ -67,23 +67,23 @@ def test_preferences_post_clears_options(
@pytest.mark.django_db
def test_new_current_user_profile_not_authenticated(tp, user):
tp.assertLoginRequired("profile-account-new")
tp.assertLoginRequired("profile-account")
@pytest.mark.django_db
def test_new_current_user_profile_view_get(user, tp):
def test_current_user_profile_view_get(user, tp):
with tp.login(user):
response = tp.assertGoodView(tp.reverse("profile-account-new"), verbose=True)
response = tp.assertGoodView(tp.reverse("profile-account"), verbose=True)
assert isinstance(response.context["change_password_form"], ChangePasswordForm)
assert isinstance(response.context["profile_photo_form"], UserProfilePhotoForm)
assert isinstance(response.context["profile_preferences_form"], PreferencesForm)
@pytest.mark.django_db
def test_new_current_user_profile_view_post_valid_password(user, tp):
def test_current_user_profile_view_post_valid_password(user, tp):
with tp.login(user):
response = tp.post(
tp.reverse("profile-account-new"),
tp.reverse("profile-account"),
data={
"email": user.email,
"oldpassword": "password",
@@ -98,11 +98,11 @@ def test_new_current_user_profile_view_post_valid_password(user, tp):
@pytest.mark.django_db
def test_new_current_user_profile_view_post_invalid_password(user, tp):
def test_current_user_profile_view_post_invalid_password(user, tp):
old_password = "password"
with tp.login(user):
response = tp.post(
tp.reverse("profile-account-new"),
tp.reverse("profile-account"),
data={
"email": user.email,
"oldpassword": "not the right password",
@@ -117,13 +117,13 @@ def test_new_current_user_profile_view_post_invalid_password(user, tp):
@pytest.mark.django_db
def test_new_current_user_profile_view_update_name(user, tp):
def test_current_user_profile_view_update_name(user, tp):
new_first_name = "Tester"
new_last_name = "Testerson"
with tp.login(user):
response = tp.post(
tp.reverse("profile-account-new"),
tp.reverse("profile-account"),
data={
"email": user.email,
"first_name": new_first_name,
@@ -139,7 +139,7 @@ def test_new_current_user_profile_view_update_name(user, tp):
@pytest.mark.django_db
def test_new_current_user_profile_view_post_valid_photo(user, tp):
def test_current_user_profile_view_post_valid_photo(user, tp):
"""Test that a user can upload a new profile picture."""
# Create a temporary image file for testing
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_image:
@@ -154,7 +154,7 @@ def test_new_current_user_profile_view_post_valid_photo(user, tp):
with tp.login(user):
response = tp.post(
tp.reverse("profile-account-new"),
tp.reverse("profile-account"),
data={
"image": uploaded_file,
},
@@ -170,7 +170,7 @@ def test_new_current_user_profile_view_post_valid_photo(user, tp):
@pytest.mark.django_db
@pytest.mark.parametrize("user_type", ["user", "moderator_user"])
@pytest.mark.parametrize("form_field", PreferencesForm.Meta.fields)
def test_new_current_user_profile_view_post_valid_preferences(
def test_current_user_profile_view_post_valid_preferences(
user_type, form_field, tp, request, assert_messages
):
user = request.getfixturevalue(user_type)
@@ -183,7 +183,7 @@ def test_new_current_user_profile_view_post_valid_preferences(
with tp.login(user):
response = tp.post(
tp.reverse("profile-account-new"),
tp.reverse("profile-account"),
data={**new_preferences, "update_preferences": "Update Preeferences"},
follow=True,
)

View File

@@ -4,7 +4,6 @@ from django.contrib.messages.views import SuccessMessageMixin
from django.http import HttpResponseRedirect
from django.urls import reverse_lazy
from django.views.generic import DetailView, UpdateView
from django.views.generic.edit import FormView
from django.views.generic.base import TemplateView
from allauth.account.forms import ChangePasswordForm
@@ -71,54 +70,6 @@ class ProfileView(DetailView):
return context
class CurrentUserProfileView(LoginRequiredMixin, ProfileView):
def get_object(self):
return self.request.user
class ProfilePhotoUploadView(LoginRequiredMixin, FormView):
"""Allows a user to change their profile photo"""
template_name = "users/photo_upload.html"
form_class = UserProfilePhotoForm
success_url = reverse_lazy("profile-account")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
user = self.request.user
context["user_has_gh_username"] = bool(user.github_username)
return context
def get_success_url(self, **kwargs):
return reverse_lazy("profile-account")
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST, request.FILES, instance=self.request.user)
if form.is_valid():
form.save()
messages.success(request, "Your profile photo has been updated")
return super().form_valid(form)
else:
return super().form_invalid()
class ProfilePhotoGitHubUpdateView(LoginRequiredMixin, UpdateView):
"""Allow a user to sync their profile photo to their current GitHub photo."""
http_method_names = ["post"]
def get_object(self, queryset=None):
return self.request.user
def get_success_url(self, **kwargs):
return reverse_lazy("profile-user", args=[self.request.user.pk])
def post(self, request, *args, **kwargs):
user = self.get_object()
tasks.update_user_github_photo.delay(user.pk)
return HttpResponseRedirect(self.get_success_url())
class ProfilePreferencesView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
form_class = PreferencesForm
template_name = "users/profile_preferences.html"
@@ -129,10 +80,10 @@ class ProfilePreferencesView(LoginRequiredMixin, SuccessMessageMixin, UpdateView
return self.request.user.preferences
class NewCurrentUserProfileView(LoginRequiredMixin, SuccessMessageMixin, TemplateView):
class CurrentUserProfileView(LoginRequiredMixin, SuccessMessageMixin, TemplateView):
template_name = "users/profile_new.html"
success_message = "Your profile was successfully updated."
success_url = reverse_lazy("profile-account-new")
success_url = reverse_lazy("profile-account")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)