示例#1
0
    def test_update_book_for_admin(self):
        url = f'/api/library/books/{self.book.uuid}/'

        self.client.force_login(self.admin, None)

        data = {
            'author': self.author.uuid,
            'language': self.language.name,
            'genre': [
                self.genre.name,
            ],
            'title': 'Евгений Онегин',
            'publication_year': timezone.now()
        }

        serializer = serializers.BookListSerializer(data=data)
        serializer.is_valid(raise_exception=True)

        response = self.client.put(path=url, data=data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        book = models.Book.objects.get(uuid=self.book.uuid)

        self.assertEqual(data.get('author'), book.author.uuid)
        self.assertEqual(data.get('language'), book.language.name)
        self.assertEqual(data.get('genre')[0], book.genre.all()[0].name)
        self.assertEqual(data.get('title'), book.title)
        self.assertEqual(data.get('publication_year'), book.publication_year)
示例#2
0
    def test_get_list_books_for_admin(self):
        url = '/api/library/books/'
        self.client.force_login(self.admin, None)

        response = self.client.get(path=url, format='json')

        books = models.Book.objects.all()
        serializer = serializers.BookListSerializer(instance=books, many=True)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, serializer.data)
示例#3
0
    def test_search_books_for_user(self):
        query_param = str(self.book.title)
        url = f'/api/library/books/'

        self.client.force_login(self.user, None)

        response = self.client.get(path=url, data={'title': query_param})

        book = models.Book.objects.filter(title=query_param)
        serializer = serializers.BookListSerializer(instance=book, many=True)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, serializer.data)
示例#4
0
    def test_get_special_book_for_admin(self):
        self.book.publication_year = timezone.now() + timezone.timedelta(
            days=10)
        self.book.save()

        url = '/api/library/books/'

        self.client.force_login(self.admin, None)

        response = self.client.get(path=url, format='json')

        books = models.Book.objects.all()
        serializer = serializers.BookListSerializer(instance=books, many=True)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, serializer.data)
示例#5
0
    def test_update_book_for_user(self):
        url = f'/api/library/books/{self.book.uuid}/'

        self.client.force_login(self.user, None)

        data = {
            'author': self.author.uuid,
            'language': self.language.name,
            'genre': [
                self.genre.name,
            ],
            'title': 'Евгений Онегин',
            'publication_year': timezone.now()
        }

        serializer = serializers.BookListSerializer(data=data)
        serializer.is_valid(raise_exception=True)

        response = self.client.put(path=url, data=data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
示例#6
0
    def test_create_book_for_admin(self):
        url = f'/api/library/books/'

        self.client.force_login(self.admin, None)

        data = {
            'author': self.author.uuid,
            'language': self.language.name,
            'genre': [
                self.genre.name,
            ],
            'title': 'Евгений Онегин',
            'publication_year': timezone.now()
        }

        serializer = serializers.BookListSerializer(data=data)
        serializer.is_valid(raise_exception=True)

        response = self.client.post(path=url, data=data, format='json')

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(models.Book.objects.count(), 2)