Esempio n. 1
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. 2
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. 3
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())