|
1 | 1 | from django.test import TestCase |
| 2 | +from django.contrib.auth.models import User |
| 3 | +from django.urls import reverse |
| 4 | +from rest_framework import status |
| 5 | +from rest_framework.test import APIClient |
| 6 | +from .models import UserProfile, WatchlistItem, Note |
| 7 | +from .serializers import UserSerializer, WatchlistItemSerializer, NoteSerializer |
2 | 8 |
|
3 | | -# Create your tests here. |
| 9 | +class APITestCase(TestCase): |
| 10 | + def setUp(self): |
| 11 | + self.client = APIClient() |
| 12 | + self.user = User.objects.create_user( |
| 13 | + username='testuser', |
| 14 | + email='test@example.com', |
| 15 | + password='testpass123' |
| 16 | + ) |
| 17 | + self.client.force_authenticate(user=self.user) |
| 18 | + |
| 19 | + self.watchlist_item = WatchlistItem.objects.create( |
| 20 | + user=self.user, |
| 21 | + imdb_id='tt0111161', |
| 22 | + title='The Shawshank Redemption', |
| 23 | + year='1994', |
| 24 | + poster='https://example.com/poster.jpg', |
| 25 | + imdb_rating='9.3' |
| 26 | + ) |
| 27 | + |
| 28 | + self.note = Note.objects.create( |
| 29 | + title='Test Note', |
| 30 | + content='Test Content', |
| 31 | + author=self.user |
| 32 | + ) |
| 33 | + |
| 34 | + def test_user_profile(self): |
| 35 | + """Test user profile endpoint""" |
| 36 | + response = self.client.get(reverse('user-profile')) |
| 37 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 38 | + self.assertEqual(response.data['username'], 'testuser') |
| 39 | + self.assertEqual(response.data['email'], 'test@example.com') |
| 40 | + |
| 41 | + def test_watchlist_crud(self): |
| 42 | + """Test watchlist CRUD operations""" |
| 43 | + response = self.client.get(reverse('watchlist')) |
| 44 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 45 | + self.assertEqual(len(response.data), 1) |
| 46 | + |
| 47 | + new_item = { |
| 48 | + 'imdb_id': 'tt0068646', |
| 49 | + 'title': 'The Godfather', |
| 50 | + 'year': '1972', |
| 51 | + 'poster': 'https://example.com/godfather.jpg', |
| 52 | + 'imdb_rating': '9.2' |
| 53 | + } |
| 54 | + response = self.client.post(reverse('watchlist'), new_item, format='json') |
| 55 | + self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
| 56 | + |
| 57 | + update_data = {'watched': True, 'rating': 5} |
| 58 | + response = self.client.put( |
| 59 | + reverse('watchlist-detail', args=[self.watchlist_item.id]), |
| 60 | + update_data, |
| 61 | + format='json' |
| 62 | + ) |
| 63 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 64 | + self.assertEqual(response.data['watched'], True) |
| 65 | + self.assertEqual(response.data['rating'], 5) |
| 66 | + |
| 67 | + response = self.client.delete(reverse('watchlist-detail', args=[self.watchlist_item.id])) |
| 68 | + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) |
| 69 | + |
| 70 | + def test_notes_crud(self): |
| 71 | + """Test notes CRUD operations""" |
| 72 | + response = self.client.get(reverse('note-list')) |
| 73 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 74 | + self.assertEqual(len(response.data), 1) |
| 75 | + |
| 76 | + new_note = { |
| 77 | + 'title': 'New Note', |
| 78 | + 'content': 'New Content', |
| 79 | + 'author': self.user.id |
| 80 | + } |
| 81 | + response = self.client.post(reverse('note-list'), new_note, format='json') |
| 82 | + self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
| 83 | + |
| 84 | + update_data = { |
| 85 | + 'title': 'Updated Note', |
| 86 | + 'content': 'Updated Content', |
| 87 | + 'author': self.user.id |
| 88 | + } |
| 89 | + response = self.client.put( |
| 90 | + reverse('note-detail', args=[self.note.id]), |
| 91 | + update_data, |
| 92 | + format='json' |
| 93 | + ) |
| 94 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 95 | + self.assertEqual(response.data['content'], 'Updated Content') |
| 96 | + self.assertEqual(response.data['title'], 'Updated Note') |
| 97 | + |
| 98 | + response = self.client.delete(reverse('note-detail', args=[self.note.id])) |
| 99 | + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) |
| 100 | + |
| 101 | + def test_search_movies(self): |
| 102 | + """Test movie search endpoint""" |
| 103 | + response = self.client.get(reverse('search-movies'), {'q': 'Inception'}) |
| 104 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 105 | + self.assertIn('Search', response.data) |
| 106 | + |
| 107 | + def test_unauthorized_access(self): |
| 108 | + """Test unauthorized access to protected endpoints""" |
| 109 | + unauthorized_client = APIClient() |
| 110 | + |
| 111 | + response = unauthorized_client.get(reverse('watchlist')) |
| 112 | + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
| 113 | + |
| 114 | + response = unauthorized_client.get(reverse('note-list')) |
| 115 | + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
| 116 | + |
| 117 | + response = unauthorized_client.get(reverse('user-profile')) |
| 118 | + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) |
0 commit comments