Esempio n. 1
0
File: views.py Progetto: kfarr2/ITS
def adminaction(request, item_num):

    """
    Administrative action page
    Allows user to change status of items.
    """

    chosen_item = get_object_or_404(Item, pk=item_num)
    status_list = Status.objects.filter(item=item_num)

    # Perform action on item
    if request.method == 'POST':

        form = AdminActionForm(request.POST, current_user=request.user)

        if form.is_valid():
            messages.success(request, "Item successfully changed")
            form.save(item_pk=item_num, current_user=request.user)
            return HttpResponseRedirect(request.get_full_path())

    else:
        form = AdminActionForm(current_user=request.user)

    context = {'item': chosen_item,
               'form': form,
               'status_list': status_list}

    return render(request, 'items/admin-action.html', context)
Esempio n. 2
0
File: tests.py Progetto: kfarr2/ITS
    def test_clean_with_errors(self):

        """
        Test 1 - Check that errors appear when returning item with bad data.
        Test 2 - Check that errors appear when performing other action with bad data.
        Test 3 - Check that errors appear when action_choice is of type None.
        """

        # Test 1 - Check for errors when returning item.
        user = create_staff()

        new_action = Action.objects.get(machine_name=Action.RETURNED)

        data = {'action_choice': new_action,
                'note': "",
                'first_name': "",
                'last_name': "",
                'email': "", }

        with patch('its.items.forms.AdminActionForm.clean', return_value=data):
                with patch("its.items.forms.AdminActionForm.add_error") as add_error:
                    form = AdminActionForm(data, current_user=user)
                    form.clean()

                    add_error.assert_any_call_with("first_name", "First name is required when returning item.")
                    add_error.assert_any_call_with("last_name", "Last name is required when returning item.")
                    add_error.assert_any_call_with("email", "Email is required when returning item.")

        # Test 2 - Check for errors when selecting other action.

        new_action = Action.objects.get(machine_name=Action.OTHER)

        data = {'action_choice': new_action,
                'note': "",
                'first_name': "",
                'last_name': "",
                'email': "", }

        with patch('its.items.forms.AdminActionForm.clean', return_value=data):
                with patch("its.items.forms.AdminActionForm.add_error") as add_error:
                    form = AdminActionForm(data, current_user=user)
                    form.clean()

                    add_error.assert_any_call_with("note", "Note required when choosing action of type Other.")

        # Test 3 - Check for errors when action_choice not in dictionary.
        # Failed as NoneType AttributeError for machine_name in clean on old code.
        user = create_user()

        data = {'action_choice': None,
                'note': "",
                'first_name': "",
                'last_name': "",
                'email': "", }

        form = AdminActionForm(data, current_user=user)
        self.assertFalse(form.is_valid())
Esempio n. 3
0
File: tests.py Progetto: kfarr2/ITS
    def test_checkout_email(self):

        """
        Check that an email is sent to the staff when a valuable
        item is returned.
        """
        user = create_staff()

        new_item = make(Item, is_valuable=True)
        new_action = Action.objects.get(machine_name=Action.RETURNED)

        data = {'action_choice': new_action,
                'note': "",
                'first_name': 'abcd',
                'last_name': '1234',
                'email': '*****@*****.**', }

        with patch('its.items.forms.AdminActionForm.is_valid', return_value=True):
            form = AdminActionForm(data, current_user=user)
            form.cleaned_data = data
            form.save(item_pk=new_item.pk, current_user=user)

            self.assertEquals(len(mail.outbox), 1)
            self.assertEquals(mail.outbox[0].subject, 'Valuable item checked out')
Esempio n. 4
0
File: tests.py Progetto: kfarr2/ITS
    def test_clean_no_errors(self):

        """
        Test 1 - Check that no errors appear when returning an item with correct data.
        Test 2 - Check that no errors appear when performing other action with correct data.
        """

        # Test 1 - Check for no errors when returning item.
        user = create_staff()

        new_action = Action.objects.get(machine_name=Action.RETURNED)

        data = {'action_choice': new_action,
                'note': "",
                'first_name': "abcd",
                'last_name': "1234",
                'email': "*****@*****.**", }

        with patch('its.items.forms.AdminActionForm.clean', return_value=data):
            form = AdminActionForm(data, current_user=user)
            form.cleaned_data = form.clean()

            self.assertTrue(form.cleaned_data['first_name'] == data['first_name'])
            self.assertTrue(form.cleaned_data['last_name'] == data['last_name'])
            self.assertTrue(form.cleaned_data['email'] == data['email'])

        # Test 2 - Check for no errors when performing other action.
        new_action = Action.objects.get(machine_name=Action.OTHER)

        data = {'action_choice': new_action,
                'note': "test",
                'first_name': "",
                'last_name': "",
                'email': "", }

        with patch('its.items.forms.AdminActionForm.clean', return_value=data):
            form = AdminActionForm(data, current_user=user)
            form.cleaned_data = form.clean()

            self.assertTrue(form.cleaned_data['note'] == data['note'])
Esempio n. 5
0
File: tests.py Progetto: kfarr2/ITS
    def test_save(self):

        """
        Test 1 - Check that the returned to field is set to None when an item
        has it's status set back to checked in

        Test 2 - Check that the returned to field is correctly set when an item
        is returned to it's owner.

        Test 3 Check that an email is sent to the staff mailing list
        when a valuable item is returned to it's owner.

        Test 4 check that a new user is made when an item is returned and that
        person did not exist in the system previously.

        Test 5 Check that an existing user does not have an account created when
        an item is returned to them.
        """

        # Test 1 Check that the returned to field is set to None when an item
        # has it's status set back to checked in

        user = create_staff()

        new_item = make(Item, is_valuable=False)
        new_action = Action.objects.get(machine_name=Action.CHECKED_IN)

        data = {'action_choice': new_action,
                'note': "",
                'first_name': "",
                'last_name': "",
                'email': "", }

        with patch('its.items.forms.AdminActionForm.is_valid', return_value=True):
            with patch('its.items.forms.AdminActionForm.clean', return_value=data):

                form = AdminActionForm(data, current_user=user)
                form.cleaned_data = form.clean()
                form.save(item_pk=new_item.pk, current_user=user)
                new_item = Item.objects.get(pk=new_item.pk)
                self.assertIsNone(new_item.returned_to)

        # Test 2 Check that the returned to field is correctly set when an item
        # is returned to it's owner.
        new_action = Action.objects.get(machine_name=Action.RETURNED)

        data = {'action_choice': new_action,
                'note': "",
                'first_name': "abcd",
                'last_name': "1234",
                'email': "*****@*****.**", }

        with patch('its.items.forms.AdminActionForm.is_valid', return_value=True):
            with patch('its.items.forms.AdminActionForm.clean', return_value=data):

                form = AdminActionForm(data, current_user=user)
                form.cleaned_data = form.clean()
                form.save(item_pk=new_item.pk, current_user=user)
                new_item = Item.objects.get(pk=new_item.pk)
                self.assertIsNotNone(new_item.returned_to)

        # Test 3 Check that an email is sent to the staff mailing list
        # when a valuable item is returned to it's owner.

        new_item = make(Item, is_valuable=True)

        data = {'action_choice': new_action,
                'note': "",
                'first_name': "abcd",
                'last_name': "1234",
                'email': "*****@*****.**", }

        with patch('its.items.forms.AdminActionForm.is_valid', return_value=True):
            with patch('its.items.forms.AdminActionForm.clean', return_value=data):

                form = AdminActionForm(data, current_user=user)
                form.cleaned_data = form.clean()
                form.save(item_pk=new_item.pk, current_user=user)

                self.assertEquals(len(mail.outbox), 1)
                self.assertEquals(mail.outbox[0].subject, 'Valuable item checked out')

        # Test 4 check that a new user is made when an item is returned and that
        # person did not exist in the system previously.

        data = {'action_choice': new_action,
                'note': "",
                'first_name': "test",
                'last_name': "1234",
                'email': "*****@*****.**", }

        try:
            user_search = User.objects.get(first_name=data["first_name"])
        except User.DoesNotExist:
            user_search = None

        self.assertIsNone(user_search)

        with patch('its.items.forms.AdminActionForm.is_valid', return_value=True):
            with patch('its.items.forms.AdminActionForm.clean', return_value=data):

                form = AdminActionForm(data, current_user=user)
                form.cleaned_data = form.clean()
                form.save(item_pk=new_item.pk, current_user=user)

                try:
                    user_search = User.objects.get(first_name=data["first_name"])
                except User.DoesNotExist:
                    user_search = None

                self.assertIsNotNone(user_search)

        # Test 5 Check that an existing user does not have an account created when
        # an item is returned to them.

        num_users = User.objects.all().count()

        with patch('its.items.forms.AdminActionForm.is_valid', return_value=True):
            with patch('its.items.forms.AdminActionForm.clean', return_value=data):

                form = AdminActionForm(data, current_user=user)
                form.cleaned_data = form.clean()
                form.save(item_pk=new_item.pk, current_user=user)

                self.assertEqual(num_users, User.objects.all().count())