def test_save_no_email(self): """ Checks that an email is not sent when a non-valuable item is checked in. """ new_item = make(Item, is_valuable=False) new_action = Action.objects.get(machine_name=Action.CHECKED_IN) make(Status, action_taken=new_action, item=new_item) new_category = make(Category) new_location = make(Location) data = {'location': new_location, 'category': new_category, 'description': new_item.description, 'is_valuable': new_item.is_valuable, 'username': "", 'possible_owner_found': False, 'first_name': "", 'last_name': "", 'email': "", } user = create_user() with patch('its.items.forms.CheckInForm.clean', return_value=data): form = CheckInForm(data) form.cleaned_data = data with patch("its.items.forms.ModelForm.save", return_value=new_item): form.save(current_user=user) self.assertEquals(len(mail.outbox), 0)
def test_save_staff_and_user_email(self): """ Checks that an email is sent to the user as well as another to staff when a valuable item is checked in. """ new_item = make(Item, is_valuable=True) new_action = Action.objects.get(machine_name=Action.CHECKED_IN) make(Status, action_taken=new_action, item=new_item) new_category = make(Category) new_location = make(Location) data = {'location': new_location, 'category': new_category, 'description': new_item.description, 'is_valuable': new_item.is_valuable, 'username': "", 'possible_owner_found': True, 'first_name': "test", 'last_name': "test", 'email': "*****@*****.**", } user = create_user() with patch('its.items.forms.CheckInForm.clean', return_value=data): form = CheckInForm(data) form.cleaned_data = data with patch("its.items.forms.ModelForm.save", return_value=new_item): form.save(current_user=user) self.assertEquals(len(mail.outbox), 2) self.assertEquals(mail.outbox[0].subject, 'An item belonging to you was found') self.assertEquals(mail.outbox[1].subject, 'Valuable item checked in')
def test_save_new_user(self): """ Tests to make sure that the save method creates a new user when it does not already exist in the database. """ new_item = make(Item) new_action = Action.objects.get(machine_name=Action.CHECKED_IN) make(Status, action_taken=new_action, item=new_item) new_category = make(Category) new_location = make(Location) data = {'location': new_location, 'category': new_category, 'description': new_item.description, 'is_valuable': True, 'username': "", 'possible_owner_found': True, 'first_name': "test", 'last_name': "test", 'email': "*****@*****.**", } user = create_user() with patch('its.items.forms.CheckInForm.clean', return_value=data): form = CheckInForm(data) form.cleaned_data = data with patch("its.items.forms.ModelForm.save", return_value=new_item): form.save(current_user=user) new_user = User.objects.get(first_name=data['first_name'], last_name=data['last_name'], email=data['email']) self.assertTrue(data['first_name'] == new_user.first_name) self.assertTrue(data['last_name'] == new_user.last_name) self.assertTrue(data['email'] == new_user.email)
def checkin(request): """ Item check in form Allows lab attendant to check an item into inventory. """ if request.method == 'POST': form = CheckInForm(request.POST) if form.is_valid(): new_item = form.save(current_user=request.user) return HttpResponseRedirect(reverse("printoff", args=[new_item.pk])) else: form = CheckInForm() return render(request, 'items/checkin.html', {'form': form})