Example #1
0
def test_notice_change(session):
    users = UserCollection(session)

    session.add(GazetteNoticeChange(text='text', channel_id='channel'))
    session.flush()

    change = session.query(GazetteNoticeChange).one()
    assert change.text == 'text'
    assert change.channel_id == 'channel'
    assert change.user is None
    assert change.user_name is None
    assert change._user_name is None
    assert change.notice is None
    assert change.event is None

    # Add user
    change.event = 'event'
    user = users.add('*****@*****.**', 'test', 'editor')
    change.user = user
    session.flush()
    session.refresh(change)

    assert change.text == 'text'
    assert change.channel_id == 'channel'
    assert change.user == user
    assert change.user_name == '*****@*****.**'
    assert change._user_name == '*****@*****.**'
    assert change.notice is None
    assert change.event == 'event'
    assert user.changes == [change]

    # Add to notice
    session.add(GazetteNotice(state='drafted', title='title', name='notice'))
    session.flush()
    notice = session.query(GazetteNotice).one()
    change.notice = notice

    assert notice.changes.one() == change

    # Rename user
    user.realname = 'Peter'
    session.flush()
    session.refresh(change)

    assert change.user == user
    assert change.user_name == 'Peter'
    assert change._user_name == 'Peter'
    assert user.changes == [change]

    # Delete user
    users.delete(user.username)
    session.flush()
    session.refresh(change)

    assert change.user is None
    assert change.user_name == '(Peter)'
    assert change._user_name == 'Peter'
Example #2
0
def delete_user(self, request, form):
    """ Delete a user.

    Publishers may only edit members. Admins can not be deleted.

    """

    layout = Layout(self, request)

    if self.role != 'member' and not request.is_secret(self):
        raise HTTPForbidden()

    if self.official_notices or self.changes:
        request.message(_("There are official notices linked to this user!"),
                        'warning')

    if form.submitted(request):
        collection = UserCollection(request.session)
        user = collection.by_username(self.username)
        if user.role != 'admin':
            self.logout_all_sessions(request)
            collection.delete(self.username)
            request.message(_("User deleted."), 'success')
        return redirect(layout.manage_users_link)

    return {
        'message':
        _('Do you really want to delete "${item}"?',
          mapping={'item': self.title}),
        'layout':
        layout,
        'form':
        form,
        'title':
        self.title,
        'subtitle':
        _("Delete User"),
        'button_text':
        _("Delete User"),
        'button_class':
        'alert',
        'cancel':
        layout.manage_users_link
    }
Example #3
0
def test_notice_user_and_group(session):
    users = UserCollection(session)
    groups = UserGroupCollection(session)

    session.add(GazetteNotice(title='notice'))
    session.flush()
    notice = session.query(GazetteNotice).one()

    assert notice.user is None
    assert notice.user_id is None
    assert notice.user_name is None
    assert notice._user_name is None
    assert notice.group is None
    assert notice.group_id is None
    assert notice.group_name is None
    assert notice._group_name is None

    # add user and group
    user = users.add('[email protected]', 'p', 'editor', realname='user')
    group = groups.add(name='group')
    notice.user = user
    notice.group = group
    session.flush()
    session.refresh(notice)

    assert notice.user == user
    assert notice.user_id == user.id
    assert notice.user_name == 'user'
    assert notice._user_name == 'user'
    assert notice.group == group
    assert notice.group_id == group.id
    assert notice.group_name == 'group'
    assert notice._group_name == 'group'

    # rename user and group
    user.realname = 'xxx'
    group.name = 'yyy'
    session.flush()
    session.refresh(notice)

    assert notice.user == user
    assert notice.user_id == user.id
    assert notice.user_name == 'xxx'
    assert notice._user_name == 'xxx'
    assert notice.group == group
    assert notice.group_id == group.id
    assert notice.group_name == 'yyy'
    assert notice._group_name == 'yyy'

    # delete user and group
    users.delete(user.username)
    groups.delete(group)
    session.flush()
    session.refresh(notice)

    assert notice.user is None
    assert notice.user_id is None
    assert notice.user_name == '(xxx)'
    assert notice._user_name == 'xxx'
    assert notice.group is None
    assert notice.group_id is None
    assert notice.group_name == '(yyy)'
    assert notice._group_name == 'yyy'
Example #4
0
def test_notice_collection_query_deleted_user(session):
    groups = UserGroupCollection(session)
    group_a = groups.add(name="Group A")
    group_b = groups.add(name="Group B")

    users = UserCollection(session)
    user_a = users.add(realname="User A",
                       username='******',
                       password='******',
                       role='admin',
                       group=group_a)
    user_b = users.add(realname="User B",
                       username='******',
                       password='******',
                       role='admin',
                       group=group_b)

    notices = GazetteNoticeCollection(session)
    notice_a = notices.add(title='A',
                           text='Text',
                           organization_id='100',
                           category_id='11',
                           issues=['2017-46'],
                           user=user_a)
    notice_b = notices.add(title='B',
                           text='Text',
                           organization_id='100',
                           category_id='11',
                           issues=['2017-46'],
                           user=user_b)

    assert notices.query().count() == 2

    assert notice_a.user is not None
    assert notice_b.user is not None
    assert notice_a.group is not None
    assert notice_b.group is not None

    assert notices.for_term("User A").query().one() == notice_a
    assert notices.for_term("User B").query().one() == notice_b
    assert notices.for_term("Group A").query().one() == notice_a
    assert notices.for_term("Group B").query().one() == notice_b

    users.delete(user_a.username)
    users.delete(user_b.username)
    groups.delete(group_a)
    groups.delete(group_b)
    commit()

    assert users.query().count() == 0
    assert groups.query().count() == 0

    notice_a = notices.query().filter(GazetteNotice.title == 'A').one()
    notice_b = notices.query().filter(GazetteNotice.title == 'B').one()

    assert notice_a.user is None
    assert notice_b.user is None
    assert notice_a.group is None
    assert notice_b.group is None

    assert notices.query().count() == 2
    assert notices.for_term("User A").query().one() == notice_a
    assert notices.for_term("User B").query().one() == notice_b
    assert notices.for_term("Group A").query().one() == notice_a
    assert notices.for_term("Group B").query().one() == notice_b