def test_results_cache_refreshed(self): contributor = baker.make(UserProfile, first_name="Peter") evaluation = baker.make(Evaluation, state="published") baker.make(Contribution, contributor=contributor, evaluation=evaluation) cache_results(evaluation) results_before = get_results(evaluation) form_data = get_form_data_from_instance(UserForm, contributor) form_data["first_name"] = "Patrick" form = UserForm(form_data, instance=contributor) form.save() results_after = get_results(evaluation) self.assertCountEqual( (result.contributor.first_name for result in results_before.contribution_results if result.contributor), ("Peter", ), ) self.assertCountEqual( (result.contributor.first_name for result in results_after.contribution_results if result.contributor), ("Patrick", ), )
def user_create(request): form = UserForm(request.POST or None, instance=UserProfile()) if form.is_valid(): form.save() messages.success(request, _("Successfully created user.")) return redirect('staff:user_index') else: return render(request, "staff_user_form.html", dict(form=form))
def test_user_cannot_be_removed_from_evaluation_already_voted_for(self): student = baker.make(UserProfile) baker.make(Evaluation, participants=[student], voters=[student], course__semester__is_active=True) form_data = get_form_data_from_instance(UserForm, student) form_data["evaluations_participating_in"] = [] form = UserForm(form_data, instance=student) self.assertFalse(form.is_valid()) self.assertIn('evaluations_participating_in', form.errors) self.assertIn("Evaluations for which the user already voted can't be removed", form.errors['evaluations_participating_in'][0])
def test_user_cannot_be_removed_from_evaluation_already_voted_for(self): student = mommy.make(UserProfile) mommy.make(Evaluation, participants=[student], voters=[student]) form_data = get_form_data_from_instance(UserForm, student) form_data["evaluations_participating_in"] = [] form = UserForm(form_data, instance=student) self.assertFalse(form.is_valid()) self.assertIn('evaluations_participating_in', form.errors) self.assertIn("Evaluations for which the user already voted can't be removed", form.errors['evaluations_participating_in'][0])
def test_user_cannot_be_removed_from_course_already_voted_for(self): student = mommy.make(UserProfile) mommy.make(Course, participants=[student], voters=[student]) form_data = get_form_data_from_instance(UserForm, student) form_data["courses_participating_in"] = [] form = UserForm(form_data, instance=student) self.assertFalse(form.is_valid()) self.assertIn('courses_participating_in', form.errors) self.assertIn("Courses for which the user already voted can't be removed", form.errors['courses_participating_in'][0])
def user_edit(request, user_id): user = get_object_or_404(UserProfile, id=user_id) form = UserForm(request.POST or None, request.FILES or None, instance=user) courses_contributing_to = Course.objects.filter(semester=Semester.active_semester(), contributions__contributor=user) if form.is_valid(): form.save() messages.success(request, _("Successfully updated user.")) return redirect('staff:user_index') else: return render(request, "staff_user_form.html", dict(form=form, object=user, courses_contributing_to=courses_contributing_to))
def test_user_form(self): """ Tests the UserForm with one valid and one invalid input dataset. """ user = mommy.make(UserProfile) another_user = mommy.make(UserProfile) data = {"username": "******", "email": "*****@*****.**"} form = UserForm(instance=user, data=data) self.assertTrue(form.is_valid()) data = {"username": another_user.username, "email": "[email protected]"} form = UserForm(instance=user, data=data) self.assertFalse(form.is_valid())
def user_edit(request, user_id): user = get_object_or_404(UserProfile, id=user_id) form = UserForm(request.POST or None, request.FILES or None, instance=user) courses_contributing_to = Course.objects.filter( semester=Semester.active_semester(), contributions__contributor=user) if form.is_valid(): form.save() messages.success(request, _("Successfully updated user.")) return redirect('staff:user_index') else: return render( request, "staff_user_form.html", dict(form=form, object=user, courses_contributing_to=courses_contributing_to))
def test_user_with_same_username(self): """ Tests whether the user form correctly handles usernames that already exist in the database """ user = mommy.make(UserProfile) data = {"username": user.username} form = UserForm(data=data) self.assertFalse(form.is_valid()) data = {"username": user.username.upper()} form = UserForm(data=data) self.assertFalse(form.is_valid()) data = {"username": user.username.upper()} form = UserForm(instance=user, data=data) self.assertTrue(form.is_valid())
def test_user_with_same_email(self): """ Tests whether the user form correctly handles email adresses that already exist in the database Regression test for #590 """ user = mommy.make(UserProfile, email="*****@*****.**") data = {"username": "******", "email": user.email} form = UserForm(data=data) self.assertFalse(form.is_valid()) data = {"username": "******", "email": user.email.upper()} form = UserForm(data=data) self.assertFalse(form.is_valid()) data = {"username": "******", "email": user.email.upper()} form = UserForm(instance=user, data=data) self.assertTrue(form.is_valid())
def test_user_form(self): """ Tests the UserForm with one valid and one invalid input dataset. """ user = baker.make(UserProfile) another_user = baker.make(UserProfile, email="*****@*****.**") data = {"email": "*****@*****.**"} form = UserForm(instance=user, data=data) self.assertTrue(form.is_valid()) data = {"email": another_user.email} form = UserForm(instance=user, data=data) self.assertFalse(form.is_valid())