Ejemplo n.º 1
0
    def test_books_related_properties(self):
        sub_with_no_book = erp_factories.SubscriberFactory()
        self.assertEqual(sub_with_no_book.current_rentals.count(), 0)
        self.assertEqual(list(sub_with_no_book.current_bookings), [])
        self.assertTrue(sub_with_no_book.can_rent)

        sub_with_books = erp_factories.SubscriberFactory()
        # create_batch() returns a list of objects, not queryset just like create() does with an object
        books = erp_factories.RentBookFactory.create_batch(
            settings.MAX_RENT_BOOKS - 1)
        rentals = [
            erp_models.Rental.objects.create(
                user=sub_with_books.user,
                book=book,
            ) for book in books
        ]
        self.assertEqual(sub_with_books.current_rentals.count(), 2)
        self.assertEqual(list(sub_with_books.current_rentals), rentals)
        self.assertEqual(list(sub_with_books.current_bookings), [])
        self.assertTrue(sub_with_books.can_rent)

        another_book = erp_factories.RentBookFactory()
        another_rental = erp_models.Rental.objects.create(
            user=sub_with_books.user,
            book=another_book,
        )
        rentals.append(another_rental)
        self.assertEqual(sub_with_books.current_rentals.count(), 3)
        self.assertEqual(list(sub_with_books.current_rentals), rentals)
        self.assertEqual(list(sub_with_books.current_bookings), [])
        self.assertFalse(sub_with_books.can_rent)
    def setUpTestData(cls):
        cls.client = APIClient()

        # all share the password 'fakepwdd'
        cls.sub = erp_factories.SubscriberFactory()
        cls.lib = erp_factories.StandardLibrarianFactory()
        cls.mgr = erp_factories.ManagerLibrarianFactory()
Ejemplo n.º 3
0
    def test_subscription_validity(self):
        day_before_end_sub = date.today() - timedelta(
            days=settings.SUBSCRIPTION_DAYS_LENGTH - 1)
        day_end_sub = date.today() - timedelta(
            days=settings.SUBSCRIPTION_DAYS_LENGTH)

        new_sub = erp_factories.SubscriberFactory(
            subscription_date=date.today())
        self.assertTrue(new_sub.valid_subscription)

        with freeze_time(day_before_end_sub):
            tight_sub = erp_factories.SubscriberFactory()
        self.assertTrue(tight_sub.valid_subscription)

        with freeze_time(day_end_sub):
            inactive_sub = erp_factories.SubscriberFactory()
        self.assertFalse(inactive_sub.valid_subscription)
Ejemplo n.º 4
0
    def test_issues_with_subscriber(self):
        sub = erp_factories.SubscriberFactory()
        self.assertFalse(sub.has_issue)
        self.assertFalse(sub.has_received_warning)
        self.assertTrue(sub.can_rent)

        sub.has_received_warning = True
        self.assertFalse(sub.has_issue)
        self.assertTrue(sub.can_rent)

        sub.has_issue = True
        self.assertFalse(sub.can_rent)
Ejemplo n.º 5
0
    def test_book_rental_property(self):
        available_book = erp_factories.AvailableBookFactory()
        self.assertIsNone(available_book.current_rental)

        # opti: would be better to use one rental factory for that
        sub = erp_factories.SubscriberFactory()
        rent_book = erp_factories.RentBookFactory()
        rental = erp_models.Rental.objects.create(
            user=sub.user,
            book=rent_book,
        )
        self.assertEqual(rent_book.current_rental, rental)
Ejemplo n.º 6
0
    def test_subscriber_detail_get_response(self):
        sub = erp_factories.SubscriberFactory()
        erp_factories.AvailableBookFactory()

        res = self.client.get(
            path='/api/subscribers/%s/' % sub.id,
            format='json',
            HTTP_AUTHORIZATION='Token %s' % self.lib_token,
        )

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertTrue('current_rentals' in res.data)
        self.assertTrue('current_bookings' in res.data)
Ejemplo n.º 7
0
    def test_reserve_cant_book(self):
        sub = erp_factories.SubscriberFactory(has_issue=True)

        res = self.client.post(
            path='/api/reserve/%s/' % sub.id,
            data={'genericbook_id': self.gbook.id},
            format='json',
            HTTP_AUTHORIZATION='Token %s' % self.lib_token,
        )
        self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertContains(
            response=res,
            text="Sorry, you can't reserve books. Check your status to find out why.",
            status_code=status.HTTP_400_BAD_REQUEST,
        )
    def test_invalid_update_subscribers(self):
        """Read_only fields don't raise an error when trying to update them.
        """
        sub = erp_factories.SubscriberFactory(
            user__username='******'
        )

        sub_update = {
            "user": {"username": "******"}
        }

        serializer = erp_serializers.SubscriberSerializer(instance=sub, data=sub_update, partial=True)
        serializer.is_valid(raise_exception=True)
        new_sub = serializer.save()

        self.assertEqual(new_sub.user.username, '*****@*****.**')
Ejemplo n.º 9
0
    def test_return_book_from_someone_else(self):
        sub1 = self.sub
        sub2 = erp_factories.SubscriberFactory()
        book = self.books[0]

        rental = erp_models.Rental.objects.create(
            user=sub1.user,
            book=book,
        )
        book.status = 'RENT'
        book.save()

        res = self.client.post(
            path='/api/return/%s/' % sub2.pk,
            data={"book_id": book.id},
            format='json',
            HTTP_AUTHORIZATION='Token %s' % self.lib_token,
        )

        self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(
            res.data['detail'],
            "You can't return a book that you didn't rent yourself."
        )
 def setUp(self):
     self.sub = erp_factories.SubscriberFactory()
Ejemplo n.º 11
0
 def setUp(self):
     self.sub = erp_factories.SubscriberFactory()
     self.gbook = erp_factories.GenericBookFactory()
Ejemplo n.º 12
0
 def setUp(self):
     self.sub = erp_factories.SubscriberFactory()
     self.books = erp_factories.AvailableBookFactory.create_batch(5)