Пример #1
0
def test_get_registered_event_persons(dummy_event, dummy_user, dummy_regform):
    create_registration(dummy_regform, {
        'email': '*****@*****.**',
        'first_name': 'John',
        'last_name': 'Doe',
    },
                        notify_user=False)

    user_person = EventPerson.create_from_user(dummy_user, dummy_event)
    no_user_person = EventPerson(email='*****@*****.**',
                                 first_name='John',
                                 last_name='Doe')

    create_registration(dummy_regform, {
        'email': '*****@*****.**',
        'first_name': 'Jane',
        'last_name': 'Doe',
    },
                        notify_user=False)

    no_user_no_reg = EventPerson(email='*****@*****.**',
                                 first_name='No',
                                 last_name='Show')
    dummy_event.persons.append(user_person)
    dummy_event.persons.append(no_user_person)
    dummy_event.persons.append(no_user_no_reg)
    db.session.flush()

    registered_persons = get_registered_event_persons(dummy_event)
    assert registered_persons == {user_person, no_user_person}
Пример #2
0
def test_import_invitations_duplicate_user(monkeypatch, dummy_regform,
                                           dummy_user):
    monkeypatch.setattr(
        'indico.modules.events.registration.util.notify_invitation',
        lambda *args, **kwargs: None)

    dummy_user.secondary_emails.add('*****@*****.**')
    create_registration(dummy_regform, {
        'email': dummy_user.email,
        'first_name': dummy_user.first_name,
        'last_name': dummy_user.last_name
    },
                        notify_user=False)

    # duplicate user with 'skip_existing=True'
    csv = b'\n'.join([
        b'Big,Boss,ACME Inc.,[email protected]',
        b'Jane,Smith,ACME Inc.,[email protected]'
    ])
    invitations, skipped = import_invitations_from_csv(
        dummy_regform,
        BytesIO(csv),
        email_from='*****@*****.**',
        email_subject='invitation',
        email_body='Invitation to event',
        skip_moderation=True,
        skip_existing=True)
    assert len(invitations) == 1
    assert skipped == 1

    assert invitations[0].first_name == 'Jane'
    assert invitations[0].last_name == 'Smith'
    assert invitations[0].affiliation == 'ACME Inc.'
    assert invitations[0].email == '*****@*****.**'
    assert invitations[0].skip_moderation
Пример #3
0
 def _process(self):
     form = make_registration_form(self.regform, management=True)()
     if form.validate_on_submit():
         data = form.data
         session[
             'registration_notify_user_default'] = notify_user = data.pop(
                 'notify_user', False)
         create_registration(self.regform,
                             data,
                             management=True,
                             notify_user=notify_user)
         flash(_("The registration was created."), 'success')
         return redirect(url_for('.manage_reglist', self.regform))
     elif form.is_submitted():
         # not very pretty but usually this never happens thanks to client-side validation
         for error in form.error_list:
             flash(error, 'error')
     return WPManageRegistration.render_template(
         'display/regform_display.html',
         self._conf,
         event=self.event_new,
         sections=get_event_section_data(self.regform),
         regform=self.regform,
         post_url=url_for('.create_registration', self.regform),
         user_data=self._get_user_data(),
         management=True)
Пример #4
0
def test_import_error(dummy_regform):
    create_registration(dummy_regform, {
        'email': '*****@*****.**',
        'first_name': 'Big',
        'last_name': 'Boss'
    }, notify_user=False)

    # missing column
    csv = b'\n'.join([b'John,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
                      b'Buggy,Entry,ACME Inc.,CEO,'])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'malformed' in e.value.message
    assert 'Row 2' in e.value.message

    # missing e-mail
    csv = b'\n'.join([b'Bill,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
                      b'Buggy,Entry,ACME Inc.,CEO,,'])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'missing e-mail' in e.value.message
    assert 'Row 2' in e.value.message

    # bad e-mail
    csv = b'\n'.join([b'Bill,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
                      b'Buggy,Entry,ACME Inc.,CEO,,not-an-email'])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'invalid e-mail' in e.value.message
    assert 'Row 2' in e.value.message

    # duplicate e-mail (csv)
    csv = b'\n'.join([b'Bill,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
                      b'Bob,Doe,ACME Inc.,Boss,,[email protected]'])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'email address is not unique' in e.value.message
    assert 'Row 2' in e.value.message

    # duplicate e-mail (registration)
    csv = b'\n'.join([b'Big,Boss,ACME Inc.,Supreme Leader,+1-202-555-1337,[email protected]'])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'a registration with this email already exists' in e.value.message
    assert 'Row 1' in e.value.message

    # missing first name
    csv = b'\n'.join([b'Ray,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
                      b',Buggy,ACME Inc.,CEO,,[email protected]'])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'missing first' in e.value.message
    assert 'Row 2' in e.value.message
Пример #5
0
 def _register_user(self, user, notify):
     # Fill only the personal data fields, custom fields are left empty.
     data = {
         pdt.name: getattr(user, pdt.name, None)
         for pdt in PersonalDataType
     }
     data['title'] = get_title_uuid(self.regform, data['title'])
     with db.session.no_autoflush:
         create_registration(self.regform,
                             data,
                             management=True,
                             notify_user=notify)
Пример #6
0
 def _process_POST(self):
     if self.regform.is_purged:
         raise Forbidden(
             _('Registration is disabled due to an expired retention period'
               ))
     schema = make_registration_schema(self.regform, management=True)()
     form = parser.parse(schema)
     session['registration_notify_user_default'] = notify_user = form.pop(
         'notify_user', False)
     create_registration(self.regform,
                         form,
                         management=True,
                         notify_user=notify_user)
     flash(_('The registration was created.'), 'success')
     return jsonify({'redirect': url_for('.manage_reglist', self.regform)})
Пример #7
0
 def _process(self):
     form = make_registration_form(self.regform)()
     if form.validate_on_submit():
         create_registration(self.regform, form.data, management=True)
         flash(_("The registration was created."), 'success')
         return redirect(url_for('.manage_reglist', self.regform))
     elif form.is_submitted():
         # not very pretty but usually this never happens thanks to client-side validation
         for error in form.error_list:
             flash(error, 'error')
     user_data = {t.name: getattr(self.user, t.name, None) if self.user else '' for t in PersonalDataType}
     return WPManageRegistration.render_template('display/regform_display.html', self.event, event=self.event,
                                                 sections=get_event_section_data(self.regform), regform=self.regform,
                                                 post_url=url_for('.create_registration', self.regform),
                                                 user_data=user_data, management=True)
Пример #8
0
def test_during_registration_negative(dummy_regform):
    """Data can be provided during registration (it is not)."""
    form = make_registration_form(dummy_regform)(csrf_enabled=False)
    assert form.validate()

    registration = create_registration(dummy_regform, form.data)
    assert registration.cern_access_request is None
Пример #9
0
    def _process(self):
        form = make_registration_form(self.regform)()
        if form.validate_on_submit():
            registration = create_registration(self.regform, form.data, self.invitation)
            return redirect(url_for('.display_regform', registration.locator.registrant))
        elif form.is_submitted():
            # not very pretty but usually this never happens thanks to client-side validation
            for error in form.error_list:
                flash(error, 'error')

        user_data = {t.name: getattr(session.user, t.name, None) if session.user else '' for t in PersonalDataType}
        if self.invitation:
            user_data.update((attr, getattr(self.invitation, attr)) for attr in ('first_name', 'last_name', 'email'))
        user_data['title'] = get_title_uuid(self.regform, user_data['title'])
        return self.view_class.render_template('display/regform_display.html', self._conf, event=self.event_new,
                                               regform=self.regform,
                                               sections=get_event_section_data(self.regform),
                                               payment_conditions=payment_event_settings.get(self.event_new,
                                                                                             'conditions'),
                                               payment_enabled=self.event_new.has_feature('payment'),
                                               user_data=user_data,
                                               invitation=self.invitation,
                                               registration=self.registration,
                                               management=False,
                                               login_required=self.regform.require_login and not session.user)
Пример #10
0
 def _process(self):
     form = make_registration_form(self.regform, management=True)()
     if form.validate_on_submit():
         data = form.data
         session['registration_notify_user_default'] = notify_user = data.pop('notify_user', False)
         create_registration(self.regform, data, management=True, notify_user=notify_user)
         flash(_("The registration was created."), 'success')
         return redirect(url_for('.manage_reglist', self.regform))
     elif form.is_submitted():
         # not very pretty but usually this never happens thanks to client-side validation
         for error in form.error_list:
             flash(error, 'error')
     return WPManageRegistration.render_template('display/regform_display.html', self.event,
                                                 sections=get_event_section_data(self.regform), regform=self.regform,
                                                 post_url=url_for('.create_registration', self.regform),
                                                 user_data=self._get_user_data(), management=True)
Пример #11
0
    def _process(self):
        form = make_registration_form(self.regform)()
        if self._can_register() and form.validate_on_submit():
            registration = create_registration(self.regform, form.data,
                                               self.invitation)
            return redirect(
                url_for('.display_regform', registration.locator.registrant))
        elif form.is_submitted():
            # not very pretty but usually this never happens thanks to client-side validation
            for error in form.error_list:
                flash(error, 'error')

        user_data = {
            t.name: getattr(session.user, t.name, None) if session.user else ''
            for t in PersonalDataType
        }
        if self.invitation:
            user_data.update((attr, getattr(self.invitation, attr))
                             for attr in ('first_name', 'last_name', 'email'))
        user_data['title'] = get_title_uuid(self.regform, user_data['title'])
        return self.view_class.render_template(
            'display/regform_display.html',
            self.event,
            regform=self.regform,
            sections=get_event_section_data(self.regform),
            payment_conditions=payment_event_settings.get(
                self.event, 'conditions'),
            payment_enabled=self.event.has_feature('payment'),
            user_data=user_data,
            invitation=self.invitation,
            registration=self.registration,
            management=False,
            login_required=self.regform.require_login and not session.user,
            is_restricted_access=self.is_restricted_access)
Пример #12
0
    def _process(self):
        form = make_registration_form(self.regform)()
        if form.validate_on_submit():
            registration = create_registration(self.regform, form.data, self.invitation)
            return redirect(url_for(".display_regform", registration.locator.registrant))
        elif form.is_submitted():
            # not very pretty but usually this never happens thanks to client-side validation
            for error in form.error_list:
                flash(error, "error")

        user_data = {t.name: getattr(session.user, t.name, None) if session.user else "" for t in PersonalDataType}
        if self.invitation:
            user_data.update((attr, getattr(self.invitation, attr)) for attr in ("first_name", "last_name", "email"))
        return self.view_class.render_template(
            "display/regform_display.html",
            self.event,
            event=self.event,
            sections=get_event_section_data(self.regform),
            regform=self.regform,
            payment_conditions=payment_event_settings.get(self.event, "conditions"),
            payment_enabled=self.event.has_feature("payment"),
            currency=payment_event_settings.get(self.event, "currency"),
            user_data=user_data,
            invitation=self.invitation,
            registration=self.registration,
            login_required=self.regform.require_login and not session.user,
        )
Пример #13
0
    def _process_POST(self):
        if not self._can_register():
            raise ExpectedError(_('You cannot register for this event'))

        schema = make_registration_schema(self.regform)()
        form_data = parser.parse(schema)
        registration = create_registration(self.regform, form_data, self.invitation)
        return jsonify({'redirect': url_for('.display_regform', registration.locator.registrant)})
Пример #14
0
 def _process(self):
     form = make_registration_form(self.regform, management=True)()
     if form.validate_on_submit():
         data = form.data
         notify_user = data.pop('notify_user', False)
         create_registration(self.regform, data, management=True, notify_user=notify_user)
         flash(_("The registration was created."), 'success')
         return redirect(url_for('.manage_reglist', self.regform))
     elif form.is_submitted():
         # not very pretty but usually this never happens thanks to client-side validation
         for error in form.error_list:
             flash(error, 'error')
     user_data = {t.name: getattr(self.user, t.name, None) if self.user else '' for t in PersonalDataType}
     return WPManageRegistration.render_template('display/regform_display.html', self.event, event=self.event,
                                                 sections=get_event_section_data(self.regform), regform=self.regform,
                                                 post_url=url_for('.create_registration', self.regform),
                                                 user_data=user_data, management=True)
Пример #15
0
def test_during_registration_required_positive(dummy_regform):
    """Data must be provided during registration (it is)."""
    form = make_registration_form(dummy_regform)(csrf_enabled=False)
    assert form.validate()

    registration = create_registration(dummy_regform, form.data)
    assert registration.cern_access_request is not None
    assert registration.cern_access_request.nationality == 'PT'
    assert registration.cern_access_request.birth_date == date(2000, 3, 2)
    assert registration.cern_access_request.birth_place == 'Freixo de Espada as Costas'
Пример #16
0
def dummy_registration(dummy_regform, dummy_user, db):
    registration = create_registration(dummy_regform, {
        'email': dummy_user.email,
        'first_name': dummy_user.first_name,
        'last_name': dummy_user.last_name,
        'affiliation': dummy_user.affiliation,
        'phone': dummy_user.phone,
        'position': 'Business Relationship Manager',
        'user': dummy_user
    })
    db.session.flush()
    return registration
Пример #17
0
def test_import_registrations_error(dummy_regform, dummy_user):
    dummy_user.secondary_emails.add('*****@*****.**')

    create_registration(dummy_regform, {
        'email': dummy_user.email,
        'first_name': dummy_user.first_name,
        'last_name': dummy_user.last_name
    },
                        notify_user=False)

    create_registration(dummy_regform, {
        'email': '*****@*****.**',
        'first_name': 'Big',
        'last_name': 'Boss'
    },
                        notify_user=False)

    # duplicate e-mail
    csv = b'\n'.join([
        b'Big,Boss,ACME Inc.,Supreme Leader,+1-202-555-1337,[email protected]'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'a registration with this email already exists' in str(e.value)
    assert 'Row 1' in str(e.value)

    # duplicate user
    csv = b'\n'.join([
        b'Big,Boss,ACME Inc.,Supreme Leader,+1-202-555-1337,[email protected]'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'a registration for this user already exists' in str(e.value)
    assert 'Row 1' in str(e.value)
Пример #18
0
def test_import_error(dummy_regform):
    create_registration(dummy_regform, {
        'email': '*****@*****.**',
        'first_name': 'Big',
        'last_name': 'Boss'
    },
                        notify_user=False)

    # missing column
    csv = b'\n'.join([
        b'John,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
        b'Buggy,Entry,ACME Inc.,CEO,'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'malformed' in e.value.message
    assert 'Row 2' in e.value.message

    # missing e-mail
    csv = b'\n'.join([
        b'Bill,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
        b'Buggy,Entry,ACME Inc.,CEO,,'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'missing e-mail' in e.value.message
    assert 'Row 2' in e.value.message

    # bad e-mail
    csv = b'\n'.join([
        b'Bill,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
        b'Buggy,Entry,ACME Inc.,CEO,,not-an-email'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'invalid e-mail' in e.value.message
    assert 'Row 2' in e.value.message

    # duplicate e-mail (csv)
    csv = b'\n'.join([
        b'Bill,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
        b'Bob,Doe,ACME Inc.,Boss,,[email protected]'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'email address is not unique' in e.value.message
    assert 'Row 2' in e.value.message

    # duplicate e-mail (registration)
    csv = b'\n'.join([
        b'Big,Boss,ACME Inc.,Supreme Leader,+1-202-555-1337,[email protected]'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'a registration with this email already exists' in e.value.message
    assert 'Row 1' in e.value.message

    # missing first name
    csv = b'\n'.join([
        b'Ray,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
        b',Buggy,ACME Inc.,CEO,,[email protected]'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'missing first' in e.value.message
    assert 'Row 2' in e.value.message
Пример #19
0
def test_create_registration(monkeypatch, dummy_event, dummy_user,
                             dummy_regform):
    monkeypatch.setattr('indico.modules.users.util.get_user_by_email',
                        lambda *args, **kwargs: dummy_user)

    # Extend the dummy_regform with more sections and fields
    section = RegistrationFormSection(registration_form=dummy_regform,
                                      title='dummy_section',
                                      is_manager_only=False)
    db.session.add(section)
    db.session.flush()

    boolean_field = RegistrationFormField(parent_id=section.id,
                                          registration_form=dummy_regform)
    _fill_form_field_with_data(boolean_field, {
        'input_type': 'bool',
        'default_value': False,
        'title': 'Yes/No'
    })
    db.session.add(boolean_field)

    multi_choice_field = RegistrationFormField(parent_id=section.id,
                                               registration_form=dummy_regform)
    _fill_form_field_with_data(
        multi_choice_field, {
            'input_type':
            'multi_choice',
            'with_extra_slots':
            False,
            'title':
            'Multi Choice',
            'choices': [
                {
                    'caption': 'A',
                    'id': 'new:test1',
                    'is_enabled': True
                },
                {
                    'caption': 'B',
                    'id': 'new:test2',
                    'is_enabled': True
                },
            ]
        })
    db.session.add(multi_choice_field)
    db.session.flush()

    data = {
        boolean_field.html_field_name: True,
        multi_choice_field.html_field_name: {
            'test1': 2
        },
        'email': dummy_user.email,
        'first_name': dummy_user.first_name,
        'last_name': dummy_user.last_name
    }
    reg = create_registration(dummy_regform,
                              data,
                              invitation=None,
                              management=False,
                              notify_user=False)

    assert reg.data_by_field[boolean_field.id].data
    assert reg.data_by_field[multi_choice_field.id].data == {'test1': 2}
    db.session.delete(reg)
    db.session.flush()

    # Make sure that missing data gets default values:
    data = {
        'email': dummy_user.email,
        'first_name': dummy_user.first_name,
        'last_name': dummy_user.last_name
    }
    reg = create_registration(dummy_regform,
                              data,
                              invitation=None,
                              management=False,
                              notify_user=False)

    assert not reg.data_by_field[boolean_field.id].data
    assert reg.data_by_field[multi_choice_field.id].data == {}
    db.session.delete(reg)
    db.session.flush()

    # Add a manager only section
    section = RegistrationFormSection(registration_form=dummy_regform,
                                      title='manager_section',
                                      is_manager_only=True)
    db.session.add(section)
    db.session.flush()

    checkbox_field = RegistrationFormField(parent_id=section.id,
                                           registration_form=dummy_regform)
    _fill_form_field_with_data(checkbox_field, {
        'input_type': 'checkbox',
        'title': 'Checkbox'
    })
    db.session.add(checkbox_field)
    db.session.flush()

    data = {
        checkbox_field.html_field_name: True,
        'email': dummy_user.email,
        'first_name': dummy_user.first_name,
        'last_name': dummy_user.last_name
    }
    reg = create_registration(dummy_regform,
                              data,
                              invitation=None,
                              management=False,
                              notify_user=False)

    assert not reg.data_by_field[boolean_field.id].data
    assert reg.data_by_field[multi_choice_field.id].data == {}
    # Assert that the manager field gets the default value, not the value sent
    assert not reg.data_by_field[checkbox_field.id].data
    db.session.delete(reg)
    db.session.flush()

    # Try again with management=True
    data = {
        checkbox_field.html_field_name: True,
        'email': dummy_user.email,
        'first_name': dummy_user.first_name,
        'last_name': dummy_user.last_name
    }
    reg = create_registration(dummy_regform,
                              data,
                              invitation=None,
                              management=True,
                              notify_user=False)

    assert not reg.data_by_field[boolean_field.id].data
    assert reg.data_by_field[multi_choice_field.id].data == {}
    # Assert that the manager field gets properly set with management=True
    assert reg.data_by_field[checkbox_field.id].data
Пример #20
0
def test_import_invitations_error(dummy_regform, dummy_user):
    dummy_user.secondary_emails.add('*****@*****.**')

    create_registration(dummy_regform, {
        'email': dummy_user.email,
        'first_name': dummy_user.first_name,
        'last_name': dummy_user.last_name
    },
                        notify_user=False)

    create_registration(dummy_regform, {
        'email': '*****@*****.**',
        'first_name': 'Big',
        'last_name': 'Boss'
    },
                        notify_user=False)

    invitation = RegistrationInvitation(skip_moderation=True,
                                        email='*****@*****.**',
                                        first_name='Bill',
                                        last_name='Doe',
                                        affiliation='ACME Inc.')
    dummy_regform.invitations.append(invitation)

    # duplicate e-mail (registration)
    csv = b'\n'.join([b'Big,Boss,ACME Inc.,[email protected]'])

    with pytest.raises(UserValueError) as e:
        import_invitations_from_csv(dummy_regform,
                                    BytesIO(csv),
                                    email_from='*****@*****.**',
                                    email_subject='invitation',
                                    email_body='Invitation to event',
                                    skip_moderation=False,
                                    skip_existing=False)
    assert 'a registration with this email already exists' in str(e.value)
    assert 'Row 1' in str(e.value)

    # duplicate user
    csv = b'\n'.join([b'Big,Boss,ACME Inc.,[email protected]'])

    with pytest.raises(UserValueError) as e:
        import_invitations_from_csv(dummy_regform,
                                    BytesIO(csv),
                                    email_from='*****@*****.**',
                                    email_subject='invitation',
                                    email_body='Invitation to event',
                                    skip_moderation=False,
                                    skip_existing=False)
    assert 'a registration for this user already exists' in str(e.value)
    assert 'Row 1' in str(e.value)

    # duplicate email (invitation)
    csv = b'\n'.join([b'Bill,Doe,ACME Inc.,[email protected]'])
    with pytest.raises(UserValueError) as e:
        import_invitations_from_csv(dummy_regform,
                                    BytesIO(csv),
                                    email_from='*****@*****.**',
                                    email_subject='invitation',
                                    email_body='Invitation to event',
                                    skip_moderation=False,
                                    skip_existing=False)
    assert 'an invitation for this user already exists' in str(e.value)
    assert 'Row 1' in str(e.value)
Пример #21
0
def test_modify_registration(monkeypatch, dummy_event, dummy_user,
                             dummy_regform):
    session.set_session_user(dummy_user)
    monkeypatch.setattr('indico.modules.users.util.get_user_by_email',
                        lambda *args, **kwargs: dummy_user)

    # Extend the dummy_regform with more sections and fields
    user_section = RegistrationFormSection(registration_form=dummy_regform,
                                           title='dummy_section',
                                           is_manager_only=False)
    db.session.add(user_section)
    db.session.flush()

    boolean_field = RegistrationFormField(parent_id=user_section.id,
                                          registration_form=dummy_regform)
    _fill_form_field_with_data(boolean_field, {
        'input_type': 'bool',
        'default_value': False,
        'title': 'Yes/No'
    })
    db.session.add(boolean_field)

    multi_choice_field = RegistrationFormField(parent_id=user_section.id,
                                               registration_form=dummy_regform)
    _fill_form_field_with_data(
        multi_choice_field, {
            'input_type':
            'multi_choice',
            'with_extra_slots':
            False,
            'title':
            'Multi Choice',
            'choices': [
                {
                    'caption': 'A',
                    'id': 'new:test1',
                    'is_enabled': True
                },
                {
                    'caption': 'B',
                    'id': 'new:test2',
                    'is_enabled': True
                },
            ]
        })
    choice_uuid = next(k
                       for k, v in multi_choice_field.data['captions'].items()
                       if v == 'A')
    db.session.add(multi_choice_field)
    db.session.flush()

    # Add a manager-only section
    management_section = RegistrationFormSection(
        registration_form=dummy_regform,
        title='manager_section',
        is_manager_only=True)
    db.session.add(management_section)
    db.session.flush()

    checkbox_field = RegistrationFormField(parent_id=management_section.id,
                                           registration_form=dummy_regform)
    _fill_form_field_with_data(checkbox_field, {
        'input_type': 'checkbox',
        'is_required': True,
        'title': 'Checkbox'
    })
    db.session.add(checkbox_field)
    db.session.flush()

    # Create a registration
    data = {
        boolean_field.html_field_name: True,
        multi_choice_field.html_field_name: {
            choice_uuid: 2
        },
        checkbox_field.html_field_name: True,
        'email': dummy_user.email,
        'first_name': dummy_user.first_name,
        'last_name': dummy_user.last_name
    }
    reg = create_registration(dummy_regform,
                              data,
                              invitation=None,
                              management=True,
                              notify_user=False)

    assert reg.data_by_field[boolean_field.id].data
    assert reg.data_by_field[multi_choice_field.id].data == {choice_uuid: 2}
    assert reg.data_by_field[checkbox_field.id].data

    # Modify the registration
    data = {
        boolean_field.html_field_name: True,
        multi_choice_field.html_field_name: {
            choice_uuid: 1
        },
        checkbox_field.html_field_name: False,
    }
    modify_registration(reg, data, management=False, notify_user=False)

    assert reg.data_by_field[boolean_field.id].data
    assert reg.data_by_field[multi_choice_field.id].data == {choice_uuid: 1}
    # Assert that the manager field is not changed
    assert reg.data_by_field[checkbox_field.id].data

    # Modify as a manager
    data = {
        multi_choice_field.html_field_name: {
            choice_uuid: 3
        },
        checkbox_field.html_field_name: False,
    }
    modify_registration(reg, data, management=True, notify_user=False)

    assert reg.data_by_field[boolean_field.id].data
    assert reg.data_by_field[multi_choice_field.id].data == {choice_uuid: 3}
    assert not reg.data_by_field[checkbox_field.id].data

    # Add a new field after registering
    new_multi_choice_field = RegistrationFormField(
        parent_id=user_section.id, registration_form=dummy_regform)
    _fill_form_field_with_data(
        new_multi_choice_field, {
            'input_type': 'multi_choice',
            'with_extra_slots': False,
            'title': 'Multi Choice',
            'choices': [
                {
                    'caption': 'A',
                    'id': 'new:test3',
                    'is_enabled': True
                },
            ]
        })
    db.session.add(new_multi_choice_field)
    db.session.flush()

    modify_registration(reg, {}, management=False, notify_user=False)

    assert reg.data_by_field[boolean_field.id].data
    assert reg.data_by_field[multi_choice_field.id].data == {choice_uuid: 3}
    assert not reg.data_by_field[checkbox_field.id].data
    # Assert that the new field got a default value
    assert reg.data_by_field[new_multi_choice_field.id].data == {}

    # Remove a field after registering
    multi_choice_field.is_deleted = True
    db.session.flush()

    data = {
        multi_choice_field.html_field_name: {
            choice_uuid: 7
        },
    }
    modify_registration(reg, data, management=True, notify_user=False)
    assert reg.data_by_field[boolean_field.id].data
    # Assert that the removed field keeps its old value
    assert reg.data_by_field[multi_choice_field.id].data == {choice_uuid: 3}
    assert not reg.data_by_field[checkbox_field.id].data
    assert reg.data_by_field[new_multi_choice_field.id].data == {}
Пример #22
0
def test_import_error(dummy_regform, dummy_user, create_user):
    dummy_user.secondary_emails.add('*****@*****.**')

    create_registration(dummy_regform, {
        'email': '*****@*****.**',
        'first_name': 'Big',
        'last_name': 'Boss'
    },
                        notify_user=False)

    create_registration(dummy_regform, {
        'email': '*****@*****.**',
        'first_name': 'Guinea',
        'last_name': 'Pig'
    },
                        notify_user=False)

    user = create_user(123, email='*****@*****.**')
    user.secondary_emails.add('*****@*****.**')

    # missing column
    csv = b'\n'.join([
        b'John,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
        b'Buggy,Entry,ACME Inc.,CEO,'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'malformed' in str(e.value)
    assert 'Row 2' in str(e.value)

    # missing e-mail
    csv = b'\n'.join([
        b'Bill,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
        b'Buggy,Entry,ACME Inc.,CEO,,'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'missing e-mail' in str(e.value)
    assert 'Row 2' in str(e.value)

    # bad e-mail
    csv = b'\n'.join([
        b'Bill,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
        b'Buggy,Entry,ACME Inc.,CEO,,not-an-email'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'invalid e-mail' in str(e.value)
    assert 'Row 2' in str(e.value)

    # duplicate e-mail (csv)
    csv = b'\n'.join([
        b'Bill,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
        b'Bob,Doe,ACME Inc.,Boss,,[email protected]'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'email address is not unique' in str(e.value)
    assert 'Row 2' in str(e.value)

    # duplicate e-mail (registration)
    csv = b'\n'.join([
        b'Big,Boss,ACME Inc.,Supreme Leader,+1-202-555-1337,[email protected]'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'a registration with this email already exists' in str(e.value)
    assert 'Row 1' in str(e.value)

    # duplicate user (csv)
    csv = b'\n'.join([
        b'Big,Boss,ACME Inc.,Supreme Leader,+1-202-555-1337,[email protected]',
        b'Little,Boss,ACME Inc.,Wannabe Leader,+1-202-555-1338,[email protected]'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'Row 2: email address belongs to the same user as in row 1' in str(
        e.value)

    # duplicate user (registration)
    csv = b'\n'.join([
        b'Big,Boss,ACME Inc.,Supreme Leader,+1-202-555-1337,[email protected]'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'a registration for this user already exists' in str(e.value)
    assert 'Row 1' in str(e.value)

    # missing first name
    csv = b'\n'.join([
        b'Ray,Doe,ACME Inc.,Regional Manager,+1-202-555-0140,[email protected]',
        b',Buggy,ACME Inc.,CEO,,[email protected]'
    ])

    with pytest.raises(UserValueError) as e:
        import_registrations_from_csv(dummy_regform, BytesIO(csv))
    assert 'missing first' in str(e.value)
    assert 'Row 2' in str(e.value)
Пример #23
0
    def _process(self):
        form = make_registration_form(self.regform)()

        verif_field, reg_field = ('', '')
        for k in form._fields:
            if form._fields[k].label.text.lower() == 'registration option':
                reg_field = form._fields[k].label.field_id
            if form._fields[k].label.text.lower() == 'verification code':
                verif_field = form._fields[k].label.field_id

        verif_code = ''
        if verif_field and form._fields[verif_field].data:
            verif_code = form._fields[verif_field].data
        elif request.args.get('vc'):
            verif_code = request.args.get('vc')

        reg_opt = ''
        if reg_field and form._fields[reg_field].data:
            reg_opt = form._fields[reg_field].data

        opt_text = ''
        if reg_opt:
            for i in self.regform.active_fields:
                if i.title.lower() == 'registration option':
                    for k in i.data['captions']:
                        if k == next(iter(reg_opt)):
                            opt_text = i.data['captions'][k]
                else:
                    continue

        members_choice = False
        if opt_text and re.search(r' members', opt_text, flags=re.IGNORECASE):
            members_choice = True

        user_data = {
            t.name: getattr(session.user, t.name, None) if session.user else ''
            for t in PersonalDataType
        }

        reg_allowed = False
        if members_choice:
            str_list = [
                user_data['first_name'] + user_data['last_name'] + '/' +
                str(self.event.id) + '/' +
                datetime.today().strftime('%Y-%m-%d'),
                user_data['first_name'] + user_data['last_name'] + '/' +
                str(self.event.id) + '/' +
                (datetime.now() + timedelta(days=-1)).strftime('%Y-%m-%d'),
                user_data['first_name'] + user_data['last_name'] + '/' +
                str(self.event.id) + '/' +
                (datetime.now() + timedelta(days=-2)).strftime('%Y-%m-%d')
            ]
            for strn in str_list:
                sha = hmac.new(u''.encode(),
                               strn.lower().encode('utf-8'),
                               hashlib.sha256).hexdigest()
                md = hmac.new(''.encode(), sha.encode(),
                              hashlib.md5).hexdigest()
                enc = codecs.encode(codecs.decode(
                    md, 'hex'), 'base64').decode().replace("\n", "").replace(
                        "=", "").replace("/", "9").replace("+", "8")
                if enc == verif_code:
                    reg_allowed = True

        setattr(self.regform, 'member_attempt', False)
        if (members_choice and reg_allowed) or not members_choice:
            if form.validate_on_submit() and not self.regform.limit_reached:
                registration = create_registration(self.regform, form.data,
                                                   self.invitation)
                return redirect(
                    url_for('.display_regform',
                            registration.locator.registrant))
            elif form.is_submitted():
                # not very pretty but usually this never happens thanks to client-side validation
                for error in form.error_list:
                    flash(error, 'error')
        else:
            setattr(self.regform, 'member_attempt', True)

        if self.invitation:
            user_data.update((attr, getattr(self.invitation, attr))
                             for attr in ('first_name', 'last_name', 'email'))
        user_data['title'] = get_title_uuid(self.regform, user_data['title'])

        if verif_field:
            user_data[verif_field] = request.args.get('vc')

        return self.view_class.render_template(
            'display/regform_display.html',
            self.event,
            regform=self.regform,
            sections=get_event_section_data(self.regform),
            payment_conditions=payment_event_settings.get(
                self.event, 'conditions'),
            payment_enabled=self.event.has_feature('payment'),
            vc=request.args.get('vc'),
            user_data=user_data,
            invitation=self.invitation,
            registration=self.registration,
            management=False,
            login_required=self.regform.require_login and not session.user)
Пример #24
0
 def _register_user(self, user, notify):
     # Fill only the personal data fields, custom fields are left empty.
     data = {pdt.name: getattr(user, pdt.name, None) for pdt in PersonalDataType}
     data['title'] = get_title_uuid(self.regform, data['title'])
     with db.session.no_autoflush:
         create_registration(self.regform, data, management=True, notify_user=notify)
Пример #25
0
def dummy_access_request(dummy_regform):
    """Create a registration and corresponding request."""
    form = make_registration_form(dummy_regform)(csrf_enabled=False)
    form.validate()
    return create_registration(dummy_regform, form.data).cern_access_request