def test_editors_cannot_change_responsible(self): """ Asserts that editors cannot change the responsible of a course through POST-hacking. Regression test for #504. """ course = mommy.make(Course) user1 = mommy.make(UserProfile) user2 = mommy.make(UserProfile) questionnaire = mommy.make(Questionnaire, is_for_contributors=True) contribution1 = mommy.make(Contribution, course=course, contributor=user1, responsible=True, can_edit=True, comment_visibility=Contribution.ALL_COMMENTS, questionnaires=[questionnaire]) InlineContributionFormset = inlineformset_factory(Course, Contribution, formset=ContributionFormSet, form=EditorContributionForm, extra=0) data = to_querydict({ 'contributions-TOTAL_FORMS': 1, 'contributions-INITIAL_FORMS': 1, 'contributions-MAX_NUM_FORMS': 5, 'contributions-0-id': contribution1.pk, 'contributions-0-course': course.pk, 'contributions-0-questionnaires': questionnaire.pk, 'contributions-0-order': 1, 'contributions-0-responsibility': "RESPONSIBLE", 'contributions-0-comment_visibility': "ALL", 'contributions-0-contributor': user1.pk, }) formset = InlineContributionFormset(instance=course, data=data.copy()) self.assertTrue(formset.is_valid()) self.assertTrue(course.contributions.get(responsible=True).contributor == user1) data["contributions-0-contributor"] = user2.pk formset = InlineContributionFormset(instance=course, data=data.copy()) self.assertTrue(formset.is_valid()) formset.save() self.assertTrue(course.contributions.get(responsible=True).contributor == user1)
def test_dont_validate_deleted_contributions(self): """ Tests whether contributions marked for deletion are validated. Regression test for #415 and #244 """ evaluation = mommy.make(Evaluation) user1 = mommy.make(UserProfile) user2 = mommy.make(UserProfile) mommy.make(UserProfile) questionnaire = mommy.make(Questionnaire, type=Questionnaire.CONTRIBUTOR) contribution_formset = inlineformset_factory(Evaluation, Contribution, formset=ContributionFormSet, form=ContributionForm, extra=0) # Here we have two editors (one of them deleted with no questionnaires), and a deleted contributor with no questionnaires. data = to_querydict({ 'contributions-TOTAL_FORMS': 3, 'contributions-INITIAL_FORMS': 0, 'contributions-MAX_NUM_FORMS': 5, 'contributions-0-evaluation': evaluation.pk, 'contributions-0-questionnaires': "", 'contributions-0-order': 0, 'contributions-0-responsibility': Contribution.IS_EDITOR, 'contributions-0-textanswer_visibility': Contribution.GENERAL_TEXTANSWERS, 'contributions-0-contributor': user1.pk, 'contributions-1-evaluation': evaluation.pk, 'contributions-1-questionnaires': questionnaire.pk, 'contributions-1-order': 0, 'contributions-1-responsibility': Contribution.IS_EDITOR, 'contributions-1-textanswer_visibility': Contribution.GENERAL_TEXTANSWERS, 'contributions-1-contributor': user2.pk, 'contributions-2-evaluation': evaluation.pk, 'contributions-2-questionnaires': "", 'contributions-2-order': 1, 'contributions-2-responsibility': "CONTRIBUTOR", 'contributions-2-textanswer_visibility': Contribution.OWN_TEXTANSWERS, 'contributions-2-contributor': user2.pk, }) # Without deletion, this form should be invalid formset = contribution_formset(instance=evaluation, form_kwargs={'evaluation': evaluation}, data=data) self.assertFalse(formset.is_valid()) data['contributions-0-DELETE'] = 'on' data['contributions-2-DELETE'] = 'on' # With deletion, it should be valid formset = contribution_formset(instance=evaluation, form_kwargs={'evaluation': evaluation}, data=data) self.assertTrue(formset.is_valid())
def test_dont_validate_deleted_contributions(self): """ Tests whether contributions marked for deletion are validated. Regression test for #415 and #244 """ course = mommy.make(Course) user1 = mommy.make(UserProfile) user2 = mommy.make(UserProfile) mommy.make(UserProfile) questionnaire = mommy.make(Questionnaire, is_for_contributors=True) contribution_formset = inlineformset_factory(Course, Contribution, formset=ContributionFormSet, form=ContributionForm, extra=0) # Here we have two responsibles (one of them deleted), and a deleted contributor with no questionnaires. data = to_querydict({ 'contributions-TOTAL_FORMS': 3, 'contributions-INITIAL_FORMS': 0, 'contributions-MAX_NUM_FORMS': 5, 'contributions-0-course': course.pk, 'contributions-0-questionnaires': questionnaire.pk, 'contributions-0-order': 0, 'contributions-0-responsibility': "RESPONSIBLE", 'contributions-0-comment_visibility': "ALL", 'contributions-0-contributor': user1.pk, 'contributions-0-DELETE': 'on', 'contributions-1-course': course.pk, 'contributions-1-questionnaires': questionnaire.pk, 'contributions-1-order': 0, 'contributions-1-responsibility': "RESPONSIBLE", 'contributions-1-comment_visibility': "ALL", 'contributions-1-contributor': user2.pk, 'contributions-2-course': course.pk, 'contributions-2-questionnaires': "", 'contributions-2-order': 1, 'contributions-2-responsibility': "CONTRIBUTOR", 'contributions-2-comment_visibility': "OWN", 'contributions-2-contributor': user2.pk, 'contributions-2-DELETE': 'on', }) formset = contribution_formset(instance=course, form_kwargs={'course': course}, data=data) self.assertTrue(formset.is_valid())
def setUp(self): self.data = to_querydict({ 'contributions-TOTAL_FORMS': 2, 'contributions-INITIAL_FORMS': 2, 'contributions-MAX_NUM_FORMS': 5, 'contributions-0-id': str(self.contribution1.pk), # browsers send strings so we should too 'contributions-0-course': self.course.pk, 'contributions-0-questionnaires': self.questionnaire.pk, 'contributions-0-order': 0, 'contributions-0-responsibility': "RESPONSIBLE", 'contributions-0-comment_visibility': "ALL", 'contributions-0-contributor': self.user1.pk, 'contributions-1-id': str(self.contribution2.pk), 'contributions-1-course': self.course.pk, 'contributions-1-questionnaires': self.questionnaire.pk, 'contributions-1-order': 0, 'contributions-1-responsibility': "CONTRIBUTOR", 'contributions-1-comment_visibility': "OWN", 'contributions-1-contributor': self.user2.pk, })
def setUp(self): self.data = to_querydict({ 'contributions-TOTAL_FORMS': 2, 'contributions-INITIAL_FORMS': 2, 'contributions-MAX_NUM_FORMS': 5, 'contributions-0-id': str(self.contribution1.pk), # browsers send strings so we should too 'contributions-0-evaluation': self.evaluation.pk, 'contributions-0-questionnaires': self.questionnaire.pk, 'contributions-0-order': 0, 'contributions-0-responsibility': Contribution.IS_EDITOR, 'contributions-0-textanswer_visibility': Contribution.GENERAL_TEXTANSWERS, 'contributions-0-contributor': self.user1.pk, 'contributions-1-id': str(self.contribution2.pk), 'contributions-1-evaluation': self.evaluation.pk, 'contributions-1-questionnaires': self.questionnaire.pk, 'contributions-1-order': 0, 'contributions-1-responsibility': "CONTRIBUTOR", 'contributions-1-textanswer_visibility': Contribution.OWN_TEXTANSWERS, 'contributions-1-contributor': self.user2.pk, })
def test_deleted_empty_contribution_does_not_crash(self): """ When removing the empty extra contribution formset, validating the form should not crash. Similarly, when removing the contribution formset of an existing contributor, and entering some data in the extra formset, it should not crash. Regression test for #1057 """ evaluation = mommy.make(Evaluation) user1 = mommy.make(UserProfile) questionnaire = mommy.make(Questionnaire, type=Questionnaire.CONTRIBUTOR) contribution_formset = inlineformset_factory(Evaluation, Contribution, formset=ContributionFormSet, form=ContributionForm, extra=0) data = to_querydict({ 'contributions-TOTAL_FORMS': 2, 'contributions-INITIAL_FORMS': 0, 'contributions-MAX_NUM_FORMS': 5, 'contributions-0-evaluation': evaluation.pk, 'contributions-0-questionnaires': questionnaire.pk, 'contributions-0-order': 0, 'contributions-0-responsibility': Contribution.IS_EDITOR, 'contributions-0-textanswer_visibility': Contribution.GENERAL_TEXTANSWERS, 'contributions-0-contributor': user1.pk, 'contributions-1-evaluation': evaluation.pk, 'contributions-1-questionnaires': "", 'contributions-1-order': -1, 'contributions-1-responsibility': "CONTRIBUTOR", 'contributions-1-textanswer_visibility': Contribution.OWN_TEXTANSWERS, 'contributions-1-contributor': "", }) # delete extra formset data['contributions-1-DELETE'] = 'on' formset = contribution_formset(instance=evaluation, form_kwargs={'evaluation': evaluation}, data=data) formset.is_valid() data['contributions-1-DELETE'] = '' # delete first, change data in extra formset data['contributions-0-DELETE'] = 'on' data['contributions-1-responsibility'] = Contribution.IS_EDITOR formset = contribution_formset(instance=evaluation, form_kwargs={'evaluation': evaluation}, data=data) formset.is_valid()
def test_contribution_form_set(self): """ Tests the ContributionFormset with various input data sets. """ evaluation = mommy.make(Evaluation) user1 = mommy.make(UserProfile) user2 = mommy.make(UserProfile) mommy.make(UserProfile) questionnaire = mommy.make(Questionnaire, type=Questionnaire.CONTRIBUTOR) ContributionFormset = inlineformset_factory(Evaluation, Contribution, formset=ContributionFormSet, form=ContributionForm, extra=0) data = to_querydict({ 'contributions-TOTAL_FORMS': 1, 'contributions-INITIAL_FORMS': 0, 'contributions-MAX_NUM_FORMS': 5, 'contributions-0-evaluation': evaluation.pk, 'contributions-0-questionnaires': questionnaire.pk, 'contributions-0-order': 0, 'contributions-0-responsibility': Contribution.IS_EDITOR, 'contributions-0-textanswer_visibility': Contribution.GENERAL_TEXTANSWERS, }) # no contributor self.assertFalse(ContributionFormset(instance=evaluation, form_kwargs={'evaluation': evaluation}, data=data.copy()).is_valid()) # valid data['contributions-0-contributor'] = user1.pk self.assertTrue(ContributionFormset(instance=evaluation, form_kwargs={'evaluation': evaluation}, data=data.copy()).is_valid()) # duplicate contributor data['contributions-TOTAL_FORMS'] = 2 data['contributions-1-contributor'] = user1.pk data['contributions-1-evaluation'] = evaluation.pk data['contributions-1-questionnaires'] = questionnaire.pk data['contributions-1-order'] = 1 data['contributions-1-textanswer_visibility'] = Contribution.GENERAL_TEXTANSWERS self.assertFalse(ContributionFormset(instance=evaluation, form_kwargs={'evaluation': evaluation}, data=data).is_valid()) # two contributors data['contributions-1-contributor'] = user2.pk data['contributions-1-responsibility'] = Contribution.IS_EDITOR self.assertTrue(ContributionFormset(instance=evaluation, form_kwargs={'evaluation': evaluation}, data=data).is_valid())
def test_contribution_form_set(self): """ Tests the ContributionFormset with various input data sets. """ course = mommy.make(Course) user1 = mommy.make(UserProfile) user2 = mommy.make(UserProfile) mommy.make(UserProfile) questionnaire = mommy.make(Questionnaire, is_for_contributors=True) ContributionFormset = inlineformset_factory(Course, Contribution, formset=ContributionFormSet, form=ContributionForm, extra=0) data = to_querydict({ 'contributions-TOTAL_FORMS': 1, 'contributions-INITIAL_FORMS': 0, 'contributions-MAX_NUM_FORMS': 5, 'contributions-0-course': course.pk, 'contributions-0-questionnaires': questionnaire.pk, 'contributions-0-order': 0, 'contributions-0-responsibility': "RESPONSIBLE", 'contributions-0-comment_visibility': "ALL", }) # no contributor and no responsible self.assertFalse(ContributionFormset(instance=course, form_kwargs={'course': course}, data=data.copy()).is_valid()) # valid data['contributions-0-contributor'] = user1.pk self.assertTrue(ContributionFormset(instance=course, form_kwargs={'course': course}, data=data.copy()).is_valid()) # duplicate contributor data['contributions-TOTAL_FORMS'] = 2 data['contributions-1-contributor'] = user1.pk data['contributions-1-course'] = course.pk data['contributions-1-questionnaires'] = questionnaire.pk data['contributions-1-order'] = 1 self.assertFalse(ContributionFormset(instance=course, form_kwargs={'course': course}, data=data).is_valid()) # two responsibles data['contributions-1-contributor'] = user2.pk data['contributions-1-responsibility'] = "RESPONSIBLE" self.assertFalse(ContributionFormset(instance=course, form_kwargs={'course': course}, data=data).is_valid())
def test_take_deleted_contributions_into_account(self): """ Tests whether contributions marked for deletion are properly taken into account when the same contributor got added again in the same formset. Regression test for #415 """ course = mommy.make(Course) user1 = mommy.make(UserProfile) questionnaire = mommy.make(Questionnaire, is_for_contributors=True) contribution1 = mommy.make(Contribution, course=course, contributor=user1, responsible=True, can_edit=True, comment_visibility=Contribution.ALL_COMMENTS, questionnaires=[questionnaire]) contribution_formset = inlineformset_factory(Course, Contribution, formset=ContributionFormSet, form=ContributionForm, extra=0) data = to_querydict({ 'contributions-TOTAL_FORMS': 2, 'contributions-INITIAL_FORMS': 1, 'contributions-MAX_NUM_FORMS': 5, 'contributions-0-id': contribution1.pk, 'contributions-0-course': course.pk, 'contributions-0-questionnaires': questionnaire.pk, 'contributions-0-order': 0, 'contributions-0-responsibility': "RESPONSIBLE", 'contributions-0-comment_visibility': "ALL", 'contributions-0-contributor': user1.pk, 'contributions-0-DELETE': 'on', 'contributions-1-course': course.pk, 'contributions-1-questionnaires': questionnaire.pk, 'contributions-1-order': 0, 'contributions-1-id': '', 'contributions-1-responsibility': "RESPONSIBLE", 'contributions-1-comment_visibility': "ALL", 'contributions-1-contributor': user1.pk, }) formset = contribution_formset(instance=course, form_kwargs={'course': course}, data=data) self.assertTrue(formset.is_valid())
def test_take_deleted_contributions_into_account(self): """ Tests whether contributions marked for deletion are properly taken into account when the same contributor got added again in the same formset. Regression test for #415 """ evaluation = mommy.make(Evaluation) user1 = mommy.make(UserProfile) questionnaire = mommy.make(Questionnaire, type=Questionnaire.CONTRIBUTOR) contribution1 = mommy.make(Contribution, evaluation=evaluation, contributor=user1, can_edit=True, textanswer_visibility=Contribution.GENERAL_TEXTANSWERS, questionnaires=[questionnaire]) contribution_formset = inlineformset_factory(Evaluation, Contribution, formset=ContributionFormSet, form=ContributionForm, extra=0) data = to_querydict({ 'contributions-TOTAL_FORMS': 2, 'contributions-INITIAL_FORMS': 1, 'contributions-MAX_NUM_FORMS': 5, 'contributions-0-id': contribution1.pk, 'contributions-0-evaluation': evaluation.pk, 'contributions-0-questionnaires': questionnaire.pk, 'contributions-0-order': 0, 'contributions-0-responsibility': Contribution.IS_EDITOR, 'contributions-0-textanswer_visibility': Contribution.GENERAL_TEXTANSWERS, 'contributions-0-contributor': user1.pk, 'contributions-0-DELETE': 'on', 'contributions-1-evaluation': evaluation.pk, 'contributions-1-questionnaires': questionnaire.pk, 'contributions-1-order': 0, 'contributions-1-id': '', 'contributions-1-responsibility': Contribution.IS_EDITOR, 'contributions-1-textanswer_visibility': Contribution.GENERAL_TEXTANSWERS, 'contributions-1-contributor': user1.pk, }) formset = contribution_formset(instance=evaluation, form_kwargs={'evaluation': evaluation}, data=data) self.assertTrue(formset.is_valid())
def test_contribution_form_set(self): """ Tests the ContributionFormset with various input data sets. """ evaluation = baker.make(Evaluation) user1 = baker.make(UserProfile, _fill_optional=["first_name", "last_name"]) user2 = baker.make(UserProfile) baker.make(UserProfile) questionnaire = baker.make(Questionnaire, type=Questionnaire.Type.CONTRIBUTOR) ContributionFormset = inlineformset_factory( Evaluation, Contribution, formset=ContributionFormSet, form=ContributionForm, extra=0) data = to_querydict({ "contributions-TOTAL_FORMS": 1, "contributions-INITIAL_FORMS": 0, "contributions-MAX_NUM_FORMS": 5, "contributions-0-evaluation": evaluation.pk, "contributions-0-questionnaires": questionnaire.pk, "contributions-0-order": 0, "contributions-0-role": Contribution.Role.EDITOR, "contributions-0-textanswer_visibility": Contribution.TextAnswerVisibility.GENERAL_TEXTANSWERS, }) # no contributor self.assertFalse( ContributionFormset(instance=evaluation, form_kwargs={ "evaluation": evaluation }, data=data.copy()).is_valid()) # valid data["contributions-0-contributor"] = user1.pk self.assertTrue( ContributionFormset(instance=evaluation, form_kwargs={ "evaluation": evaluation }, data=data.copy()).is_valid()) # duplicate contributor data["contributions-TOTAL_FORMS"] = 2 data["contributions-1-contributor"] = user1.pk data["contributions-1-evaluation"] = evaluation.pk data["contributions-1-order"] = 1 data[ "contributions-1-textanswer_visibility"] = Contribution.TextAnswerVisibility.GENERAL_TEXTANSWERS data["contributions-1-role"] = Contribution.Role.EDITOR formset = ContributionFormset(instance=evaluation, form_kwargs={"evaluation": evaluation}, data=data) self.assertFalse(formset.is_valid()) # regression for https://github.com/e-valuation/EvaP/issues/1082 # assert same error message with and without questionnaire self.assertEqual( formset.non_form_errors(), [("Duplicate contributor ({}) found. Each contributor should only be used once." ).format(user1.full_name)], ) data["contributions-1-questionnaires"] = questionnaire.pk formset = ContributionFormset(instance=evaluation, form_kwargs={"evaluation": evaluation}, data=data) self.assertFalse(formset.is_valid()) self.assertEqual( formset.non_form_errors(), [("Duplicate contributor ({}) found. Each contributor should only be used once." ).format(user1.full_name)], ) # two contributors data["contributions-1-contributor"] = user2.pk self.assertTrue( ContributionFormset(instance=evaluation, form_kwargs={ "evaluation": evaluation }, data=data).is_valid())