Skip to content

Commit

Permalink
fix:tests and view name
Browse files Browse the repository at this point in the history
  • Loading branch information
carlenee committed Feb 25, 2024
1 parent 8f5c51a commit 7444170
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 17 deletions.
58 changes: 44 additions & 14 deletions authentication/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def test_username_is_already_exist(self):
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()['msg'],"Username and/or email already taken!")

def test_missing_fields(self):
data = {
"username":"user1",
"password":"pass1",
}
response = self.client.post(REGISTER_LINK, json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()['msg'],"One or more fields are missing!")


class LoginTest(TestCase):
def setUp(self):
self.client = APIClient()
Expand All @@ -77,6 +87,13 @@ def test_login_failed(self):
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()['msg'],"Wrong username/password!")

def test_missing_fields(self):
data = {
"username":"user1"
}
response = self.client.post(LOGIN_LINK, json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 400)

class SendVerificationEmailTest(TestCase):
def setUp(self):
self.client = APIClient()
Expand All @@ -99,6 +116,11 @@ def test_valid_verification_token(self):
def test_invalid_verification_token(self):
response = self.client.post((EMAIL_VERIFICATION_LINK), {'token': 'invalid token'})
self.assertEqual(response.status_code, 400)

def test_missing_verification_token(self):
response = self.client.post((EMAIL_VERIFICATION_LINK), {})
self.assertEqual(response.status_code, 400)


class SendRecoverPasswordEmailTest(TestCase):
def setUp(self):
Expand All @@ -114,6 +136,10 @@ def test_sent_email_recover_password(self):
def test_sent_wrong_email_recover_password(self):
response = self.client.post((RECOVER_PASSWORD_LINK), {'email':'[email protected]'})
self.assertEqual(response.status_code, 400)

def test_sent_missing_email_recover_password(self):
response = self.client.post((RECOVER_PASSWORD_LINK), {})
self.assertEqual(response.status_code, 400)

def test_change_password(self):
token = account_token.make_token(self.user)
Expand All @@ -132,63 +158,67 @@ def test_change_password_wrong_token(self):
{'email':'[email protected]', 'token': 'wrong token', 'new_password':'newpass'})
self.assertEqual(response.status_code, 400)

def test_change_password_missing_fields(self):
response = self.client.put((RECOVER_PASSWORD_LINK),
{'email':'[email protected]', 'token': 'wrong token'})
self.assertEqual(response.status_code, 400)

class ProfileUpdateTest(TestCase):
def setUp(self):
self.client = APIClient()
self.user = AppUser.objects.create_user(email='[email protected]', username='testuser', password='test')
self.client.force_authenticate(user=self.user)
self.profile = Profile.objects.create(user=self.user, bio='Old bio')

def tearDown(self):
# Clean up any resources created during the test
Profile.objects.filter(user=self.user).delete()

def test_update_profile_with_valid_data(self):
# Test updating profile with valid data
data = {'bio': 'New bio'}
response = self.client.put(reverse('authentication:update_profile'), data)
response = self.client.put(reverse('authentication:profile'), data)
self.assertEqual(response.status_code, 200)
self.assertEqual(Profile.objects.get(user=self.user).bio, 'New bio')

def test_update_profile_with_empty_data(self):
# Test updating profile with empty data
data = {'bio': ''}
response = self.client.put(reverse('authentication:update_profile'), data)
response = self.client.put(reverse('authentication:profile'), data)
self.assertEqual(response.status_code, 200)
self.assertEqual(Profile.objects.get(user=self.user).bio, '') # Ensure bio is empty

def test_update_profile_picture(self):
# Test uploading profile picture
image_path = 'assets/logo.jpg' # Update to the actual path
# Replace image_path with the path to a valid image file
image_path = 'assets\logo.jpg'
with open(image_path, 'rb') as f:
image = SimpleUploadedFile('image.jpg', f.read(), content_type='image/jpeg')
data = {'profile_picture': image}
response = self.client.put(reverse('authentication:update_profile'), data)
response = self.client.put(reverse('authentication:profile'), data)
self.assertEqual(response.status_code, 200)
self.assertTrue(Profile.objects.get(user=self.user).profile_picture) # Ensure profile picture is uploaded

def test_update_profile_with_invalid_data(self):
# Test updating profile with invalid data (uploading an image as bio)
image_path = 'assets/invalid_img.jpg' # Update to the actual invalid image path
image_path = 'assets/invalid_img.jpg' # Path to an invalid image file
with open(image_path, 'rb') as f:
image = SimpleUploadedFile(os.path.basename(image_path), f.read(), content_type='image/jpeg')
data = {'bio': 'New bio', 'profile_picture': image}
response = self.client.put(reverse('authentication:update_profile'), data)
response = self.client.put(reverse('authentication:profile'), data)
self.assertEqual(response.status_code, 400) # Expect a bad request response


def test_update_profile_unauthenticated(self):
# Test updating profile when unauthenticated
self.client.logout()
data = {'bio': 'New bio'}
response = self.client.put(reverse('authentication:update_profile'), data)
response = self.client.put(reverse('authentication:profile'), data)
self.assertEqual(response.status_code, 401) # Expect unauthorized response

def test_update_profile_picture_unauthenticated(self):
# Test uploading profile picture when unauthenticated
self.client.logout()
image_path = 'assets/logo.jpg' # Update to the actual path
# Replace image_path with the path to a valid image file
image_path = 'assets\logo.jpg'
with open(image_path, 'rb') as f:
image = SimpleUploadedFile('image.jpg', f.read(), content_type='image/jpeg')
data = {'profile_picture': image}
response = self.client.put(reverse('authentication:update_profile'), data)
self.assertEqual(response.status_code, 401) # Expect unauthorized response
response = self.client.put(reverse('authentication:profile'), data)
self.assertEqual(response.status_code, 401) # Expect unauthorized response
4 changes: 2 additions & 2 deletions authentication/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.urls import path
from authentication.views import RegisterView, LoginView, SendVerificationEmailView, SendRecoverPasswordEmailView, UpdateProfileView
from authentication.views import RegisterView, LoginView, SendVerificationEmailView, SendRecoverPasswordEmailView, ProfileView
from rest_framework_simplejwt.views import TokenObtainPairView
from rest_framework_simplejwt.views import TokenRefreshView

Expand All @@ -12,5 +12,5 @@
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('verify/', SendVerificationEmailView.as_view(), name='verify_email'),
path('recover/', SendRecoverPasswordEmailView.as_view(), name='recover_password'),
path('profile/update/', UpdateProfileView.as_view(), name='update_profile'),
path('profile/', ProfileView.as_view(), name='profile'),
]
2 changes: 1 addition & 1 deletion authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def put(self, request):
else:
return Response({'msg': "User doesn't exist!"}, status=400)

class UpdateProfileView(APIView):
class ProfileView(APIView):
permission_classes = [IsAuthenticated]
def put(self, request):
user = request.user
Expand Down
Binary file added profile_pictures/image_2GpvAMj.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added profile_pictures/image_AEpLxxV.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added profile_pictures/image_HC2AZta.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added profile_pictures/image_Jejmn3s.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added profile_pictures/image_SUhUVN9.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added profile_pictures/image_TQa5kBn.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added profile_pictures/image_UdhlYrz.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added profile_pictures/image_YrYiAM8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added profile_pictures/image_Zeqilxo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added profile_pictures/image_vBBpLJ1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added profile_pictures/logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added profile_pictures/logo_bVoRuqj.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7444170

Please sign in to comment.