Beispiel #1
0
    def test_username_generated(self):
        self.lendable = Lendable(user=self.user)

        # Test random username is generated when len(username) > 321.
        self.user.username = '******' * 322
        self.lendable._set_username()
        self.assertNotEqual(self.lendable.username, self.user.username)
        self.assertEqual(len(self.lendable.username), 20)
Beispiel #2
0
    def test_username_generated(self):
        self.lendable = Lendable(user=self.user)

        # Test random username is generated when len(username) > 321.
        self.user.username = '******' * 322
        self.lendable._set_username()
        self.assertNotEqual(self.lendable.username, self.user.username)
        self.assertEqual(len(self.lendable.username), 20)
Beispiel #3
0
def lendable_choices():
    """Generate a tuple of lendable choices.

    Returns:
        A tuple containing value/label pairs representing the
        lendable choices for a select widget.
    """
    choices = (('', 'Choose lendable ...'),
               ('all', 'All lendables'))

    choices += tuple((lendable.__name__.lower(), lendable.name)
                     for lendable in Lendable.__subclasses__())

    return choices
Beispiel #4
0
class LibraryTestCase(TestCase):
    """Test library app."""

    def setUp(self):
        """Setup test case."""
        self.c = Client()
        self.factory = RequestFactory()
        self.user = User.objects.create_user(username="******",
                                             email="*****@*****.**",
                                             password="******")

    def test_index(self):
        """Test index view."""
        request = self.factory.get("/index")
        request.user = AnonymousUser()

        view = IndexView.as_view()
        response = view(request)
        self.assertContains(response, 'Log in', status_code=200)
        self.assertContains(response, '<h2>Welcome!</h2>', status_code=200)

    def test_require_login(self):
        """Test login required to perform lendable actions."""
        for view in ['library:checkout', 'library:renew',
                     'library:checkin', 'library:request_extension']:
            response = self.c.get(reverse(view, args=[1]), follow=True)
            self.assertContains(response,
                                'Permission denied: You must log in.',
                                status_code=200)

    def test_lendable_flow(self):
        """Test the flow of lendables."""
        self.c.login(username=self.user.username, password='******')

        # Test check out lendable
        response = self.c.get(reverse('library:checkout',
                                      args=['lendable']),
                              follow=True)

        self.assertContains(response, 'is checked out to you',
                            status_code=200)
        self.assertEqual(Lendable.all_types.count(), 1)

        # Test get lendables for user
        lendables = get_items_checked_out_by(self.user)
        self.assertEqual(len(lendables), 1)

        # Test renew lendable, renew the max amount of times
        max_renewals = lendables[0].max_renewals
        lending_period_in_days = lendables[0].lending_period_in_days

        for i in range(max_renewals):
            self.c.get(reverse('library:renew', args=[lendables[0].id]),
                       follow=True)

        renewed_lendable = Lendable.all_types.get(id=lendables[0].id)

        # Confirm the proper due on date exists and matches
        # the max_due_date for lendable resource.
        d = renewed_lendable.due_on - lendables[0].due_on
        self.assertEqual(d.days, lending_period_in_days * max_renewals)
        self.assertEqual(renewed_lendable.due_on, lendables[0].max_due_date())

        # Test no more renewals available
        response = self.c.get(reverse('library:renew',
                                      args=[lendables[0].id]),
                              follow=True)
        self.assertContains(response, 'No more renewals are available '
                                      'for this item.')

        # Test renew catches generic exception
        with patch.object(Lendable,
                          'renew',
                          side_effect=Exception('Renew Failed!')):
            response = self.c.get(reverse('library:renew',
                                          args=[lendables[0].id]),
                                  follow=True)

        # Confirm error message displayed
        message = list(response.context['messages'])[0].message
        self.assertEqual(message, 'Renew Failed!')

        # Test request extension
        with self.settings(ADMINS=[('John', '*****@*****.**')]):
            response = self.c.post(reverse('library:request_extension',
                                           args=[lendables[0].id]),
                                   {'message': 'Extend me!'},
                                   follow=True)

        self.assertContains(response,
                            'Your request was sent to the openbare Admins'
                            ' and will be evaluated.')
        self.assertEqual(len(mail.outbox), 1)

        # Test request extension catches generic exception
        with patch('library.views._admin_emails',
                   side_effect=Exception('Email Failed!')):
            response = self.c.post(reverse('library:request_extension',
                                           args=[lendables[0].id]),
                                   {'message': 'Extend me!'},
                                   follow=True)

        # Confirm error message displayed
        message = list(response.context['messages'])[0].message
        self.assertEqual(message, 'Email Failed!')

    def test_checkout_exception(self):
        self.c.login(username=self.user.username, password='******')

        # Test check out lendable raises exception
        with patch.object(Lendable,
                          'checkout',
                          side_effect=Exception('Checkout Failed!')):
            response = self.c.get(reverse('library:checkout',
                                          args=['lendable']),
                                  follow=True)

        # Confirm error message displayed
        message = list(response.context['messages'])[0].message
        self.assertEqual(message, 'Checkout Failed!')

        # Confirm lendable not created
        self.assertEqual(Lendable.all_types.count(), 0)

    def test_get_lendable_resources(self):
        """Test get_lendable_resources method."""
        # Anonymous or None user should return an empty set
        resources = get_lendable_resources(AnonymousUser())
        self.assertEqual(resources, [])

        resources = get_lendable_resources(self.user)

        # Confirm AWS resource is available
        aws = list(filter(
            lambda resource: resource['item_subtype'] == 'amazondemoaccount',
            resources))
        self.assertEqual(len(aws), 1)

    def test_get_items_checked_out_by(self):
        """Test get_items_checked_out_by method."""
        lendables = get_items_checked_out_by(None)
        self.assertEqual(lendables, [])

        lendables = get_items_checked_out_by(AnonymousUser())
        self.assertEqual(lendables, [])

    def test_username_generated(self):
        self.lendable = Lendable(user=self.user)

        # Test random username is generated when len(username) > 321.
        self.user.username = '******' * 322
        self.lendable._set_username()
        self.assertNotEqual(self.lendable.username, self.user.username)
        self.assertEqual(len(self.lendable.username), 20)
Beispiel #5
0
class LibraryTestCase(TestCase):
    """Test library app."""

    def setUp(self):
        """Setup test case."""
        self.c = Client()
        self.factory = RequestFactory()
        self.user = User.objects.create_user(username="******",
                                             email="*****@*****.**",
                                             password="******")

    def test_index(self):
        """Test index view."""
        request = self.factory.get("/index")
        request.user = AnonymousUser()

        view = IndexView.as_view()
        response = view(request)
        self.assertContains(response, 'Log in', status_code=200)
        self.assertContains(response, '<h2>Welcome!</h2>', status_code=200)

    def test_require_login(self):
        """Test login required to perform lendable actions."""
        for view in ['library:checkout', 'library:renew',
                     'library:checkin', 'library:request_extension']:
            response = self.c.get(reverse(view, args=[1]), follow=True)
            self.assertContains(response,
                                'Permission denied: You must log in.',
                                status_code=200)

    def test_lendable_flow(self):
        """Test the flow of lendables."""
        self.c.login(username=self.user.username, password='******')

        # Test check out lendable
        response = self.c.get(reverse('library:checkout',
                                      args=['lendable']),
                              follow=True)

        self.assertContains(response, 'is checked out to you',
                            status_code=200)
        self.assertEqual(Lendable.all_types.count(), 1)

        # Test get lendables for user
        lendables = get_items_checked_out_by(self.user)
        self.assertEqual(len(lendables), 1)

        # Test renew lendable, renew the max amount of times
        max_renewals = lendables[0].max_renewals
        lending_period_in_days = lendables[0].lending_period_in_days

        for i in range(max_renewals):
            self.c.get(reverse('library:renew', args=[lendables[0].id]),
                       follow=True)

        renewed_lendable = Lendable.all_types.get(id=lendables[0].id)

        # Confirm the proper due on date exists and matches
        # the max_due_date for lendable resource.
        d = renewed_lendable.due_on - lendables[0].due_on
        self.assertEqual(d.days, lending_period_in_days * max_renewals)
        self.assertEqual(renewed_lendable.due_on, lendables[0].max_due_date())

        # Test no more renewals available
        response = self.c.get(reverse('library:renew',
                                      args=[lendables[0].id]),
                              follow=True)
        self.assertContains(response, 'No more renewals are available '
                                      'for this item.')

        # Test renew catches generic exception
        with patch.object(Lendable,
                          'renew',
                          side_effect=Exception('Renew Failed!')):
            response = self.c.get(reverse('library:renew',
                                          args=[lendables[0].id]),
                                  follow=True)

        # Confirm error message displayed
        message = list(response.context['messages'])[0].message
        self.assertEqual(message, 'Renew Failed!')

        # Test request extension
        with self.settings(ADMINS=[('John', '*****@*****.**')]):
            response = self.c.post(reverse('library:request_extension',
                                           args=[lendables[0].id]),
                                   {'message': 'Extend me!'},
                                   follow=True)

        self.assertContains(response,
                            'Your request was sent to the openbare Admins'
                            ' and will be evaluated.')
        self.assertEqual(len(mail.outbox), 1)

        # Test request extension catches generic exception
        with patch('library.views._admin_emails',
                   side_effect=Exception('Email Failed!')):
            response = self.c.post(reverse('library:request_extension',
                                           args=[lendables[0].id]),
                                   {'message': 'Extend me!'},
                                   follow=True)

        # Confirm error message displayed
        message = list(response.context['messages'])[0].message
        self.assertEqual(message, 'Email Failed!')

    def test_checkout_exception(self):
        self.c.login(username=self.user.username, password='******')

        # Test check out lendable raises exception
        with patch.object(Lendable,
                          'checkout',
                          side_effect=Exception('Checkout Failed!')):
            response = self.c.get(reverse('library:checkout',
                                          args=['lendable']),
                                  follow=True)

        # Confirm error message displayed
        message = list(response.context['messages'])[0].message
        self.assertEqual(message, 'Checkout Failed!')

        # Confirm lendable not created
        self.assertEqual(Lendable.all_types.count(), 0)

    def test_get_lendable_resources(self):
        """Test get_lendable_resources method."""
        # Anonymous or None user should return an empty set
        resources = get_lendable_resources(AnonymousUser())
        self.assertEqual(resources, [])

        resources = get_lendable_resources(self.user)

        # Confirm AWS resource is available
        aws = list(filter(
            lambda resource: resource['item_subtype'] == 'amazondemoaccount',
            resources))
        self.assertEqual(len(aws), 1)

    def test_get_items_checked_out_by(self):
        """Test get_items_checked_out_by method."""
        lendables = get_items_checked_out_by(None)
        self.assertEqual(lendables, [])

        lendables = get_items_checked_out_by(AnonymousUser())
        self.assertEqual(lendables, [])

    def test_username_generated(self):
        self.lendable = Lendable(user=self.user)

        # Test random username is generated when len(username) > 321.
        self.user.username = '******' * 322
        self.lendable._set_username()
        self.assertNotEqual(self.lendable.username, self.user.username)
        self.assertEqual(len(self.lendable.username), 20)