Skip to content

Commit

Permalink
Issue 90 create api view to modify user preferences (#100)
Browse files Browse the repository at this point in the history
* Getting started

* Trailing slash

* Finish view

* Wrote tests

* Adding bio

* More testing

* Requested changes
  • Loading branch information
SafetyInObscurity authored Jan 18, 2025
1 parent a5399e1 commit 0971ff1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
7 changes: 7 additions & 0 deletions server/bingo/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer
from django.contrib.auth import get_user_model
from django.contrib.auth.password_validation import validate_password
from .models import BingoGrid, Challenge
Expand Down Expand Up @@ -80,3 +81,9 @@ class BingoGridSerializer(serializers.ModelSerializer):
class Meta:
model = BingoGrid
fields = ['grid_id', 'challenges']


class UpdatePreferencesSerializer(ModelSerializer):
class Meta:
model = User
fields = ["avatar", "bio", "visibility"]
50 changes: 50 additions & 0 deletions server/bingo/tests/test_user_model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.db.utils import IntegrityError
from django.test import TestCase
from django.urls import reverse
from ..models import User
from django.core.exceptions import ValidationError
from datetime import date
from rest_framework.test import APIClient


class UsersTest(TestCase):
Expand Down Expand Up @@ -63,3 +65,51 @@ def test_email_uniqueness(self):
password="A string of random characters",
email="test@example.com"
)


class UpdatePreferencesTest(TestCase):
def setUp(self):
self.user = User.objects.create(
username="Test",
password="A generic, valid password",
email="test@example.com",
first_name="Jane",
last_name="Doe",
birthdate=date(1, 1, 1),
avatar=0,
visibility=User.Visibility.BLUECREW
)
self.client = APIClient()
self.client.force_authenticate(self.user)

def request(self, avatar=1, visibility=User.Visibility.FRIENDS, bio="words"):
return self.client.put(
reverse("update_preferences"),
{"avatar": avatar, "visibility": visibility, "bio": bio}
)

def test_updates(self):
self.assertEqual(self.request().status_code, 200)
self.assertEqual(self.user.avatar, 1)
self.assertEqual(self.user.visibility, User.Visibility.FRIENDS)
self.assertEqual(self.user.bio, "words")

def test_invalid(self):
self.assertEqual(self.request(-1, User.Visibility.BLUECREW).status_code, 400)
self.assertEqual(self.request('a', User.Visibility.PUBLIC).status_code, 400)
self.assertEqual(self.request(5, 3).status_code, 400)
self.assertEqual(self.request(bio="!"*301).status_code, 400)

def test_subsequent(self):
self.assertEqual(self.request(5, 2, "").status_code, 200)
self.assertEqual(self.request(3, 1, "other words").status_code, 200)
self.assertEqual(self.user.visibility, 1)
self.assertEqual(self.user.avatar, 3)
self.assertEqual(self.user.bio, "other words")

def test_unauthenitcated(self):
response = APIClient().put(
reverse("update_preferences"),
{"avatar": 1, "visibility": User.Visibility.FRIENDS, "bio": "Stuff and things"}
)
self.assertEqual(response.status_code, 401)
2 changes: 1 addition & 1 deletion server/bingo/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('register/', views.register_user, name='register_user'),
path('update-preferences/', views.update_user_preferences, name='update_preferences'),
path('leaderboard/', views.get_leaderboard, name='get_leaderboard'),
path('user/me/', views.get_current_user, name='current_user'),
path('delete-friendship/<int:friendship_id>/',
Expand All @@ -23,5 +24,4 @@
path('bingo-grid/', views.get_bingo_grid, name='get_bingo_grid'),
path('accept-friendship/<int:friendship_id>/',
views.accept_friendship, name='accept_friendship'),

]
13 changes: 12 additions & 1 deletion server/bingo/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework import status, permissions
from .serializers import UserRegisterSerializer, UserProfileSerializer, LeaderboardUserSerializer, BingoGridSerializer
from .serializers import UserRegisterSerializer, UserProfileSerializer, LeaderboardUserSerializer, BingoGridSerializer, UpdatePreferencesSerializer
from django.shortcuts import get_object_or_404
from django.core.exceptions import ObjectDoesNotExist
from rest_framework.decorators import api_view, permission_classes
Expand Down Expand Up @@ -44,6 +44,17 @@ def get_current_user(request):
return Response(serializer.data, status=status.HTTP_200_OK)


@api_view(['PUT'])
@permission_classes([IsAuthenticated])
def update_user_preferences(request):
serializer = UpdatePreferencesSerializer(instance=request.user, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(status=status.HTTP_200_OK)
else:
return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)


@api_view(['POST'])
@permission_classes([IsAuthenticated])
def start_challenge(request):
Expand Down

0 comments on commit 0971ff1

Please sign in to comment.