def create_sample_data( num_films: int, num_reviews: int, num_users: int, ) -> None: """ Populates the database with random data. """ for _ in range(num_users): user = UserFactory() logger.info('User created: %s' % user) for _ in range(num_films): film = FilmFactory() logger.info('Film created: %s' % film) users = User.objects.filter(is_superuser=False) films = Film.objects.all() for _ in range(num_reviews): user_not_reviewed = False user, film = None, None while not user_not_reviewed: user = users[random.randint(0, users.count()-1)] film = films[random.randint(0, films.count()-1)] user_not_reviewed = not bool( Review.objects.filter(user=user, film=film).count() ) review = ReviewFactory(user=user, film=film) logger.info('Review created: %s' % review)
def test_films_delete_as_unauthenticated_user(client): film = FilmFactory(poster=None) response = client.delete( reverse('api:film-detail', args=[film.pk]) ) assert response.status_code == 403
def test_films_get_as_unauthenticated_user(client): film = FilmFactory(poster=None) response = client.get(reverse('api:film-list')) assert response.status_code == 200 response = client.get(reverse('api:film-detail', args=[film.pk])) assert response.status_code == 200
def test_review_delete_as_staff_user(client, staff): client.force_login(staff) film = FilmFactory(poster=None) review = ReviewFactory(film=film) response = client.delete(reverse('api:review-detail', args=[review.pk])) assert response.status_code == 204
def test_films_delete_as_staff_user(client, staff): client.force_login(staff) film = FilmFactory(poster=None) response = client.delete( reverse('api:film-detail', args=[film.pk]) ) assert response.status_code == 204
def test_review_get_as_unauthenticated_user(client): film = FilmFactory(poster=None) review = ReviewFactory(film=film) response = client.get(reverse('api:review-list')) assert response.status_code == 200 response = client.get(reverse('api:review-detail', args=[review.pk])) assert response.status_code == 200
def test_review_patch_as_unauthenticated_user(client): film = FilmFactory(poster=None) review = ReviewFactory(film=film) review_edit = { 'title': 'TestReviewEdit', } response = client.patch(reverse('api:review-detail', args=[review.pk]), data=review_edit, content_type='application/json') assert response.status_code == 403
def test_films_patch_as_unauthenticated_user(client): film = FilmFactory(poster=None) film_edit = { 'title': 'TestFilmEdit', } response = client.patch( reverse('api:film-detail', args=[film.pk]), data=film_edit, content_type='application/json' ) assert response.status_code == 403
def test_review_post_as_unauthenticated_user(client): user = UserFactory() film = FilmFactory(poster=None) review = { 'user': user.pk, 'film': film.pk, 'rating': 10, 'review': 'Thumbs up.' } response = client.post(reverse('api:review-list'), data=review) assert response.status_code == 403
def test_review_patch_as_reviewer(client, user): client.force_login(user) film = FilmFactory(poster=None) review = ReviewFactory(film=film, user=user) review_edit = { 'title': 'TestReviewEdit', } response = client.patch(reverse('api:review-detail', args=[review.pk]), data=review_edit, content_type='application/json') assert response.status_code == 200
def test_films_patch_as_staff_user(client, staff): client.force_login(staff) film = FilmFactory(poster=None) film_edit = { 'title': 'TestFilmEdit', } response = client.patch( reverse('api:film-detail', args=[film.pk]), data=film_edit, content_type='application/json' ) assert response.status_code == 200
def test_review_post_for_newly_reviewed_film_as_authenticated_user( client, user): client.force_login(user) film = FilmFactory(poster=None) review = { 'user': user.pk, 'film': film.pk, 'rating': 10, 'review': 'Thumbs up.' } response = client.post(reverse('api:review-list'), data=review) assert response.status_code == 201