def populate(db): """ Create some data for tests to use. Args: db (sqlalchemy.orm.session.Session): The database session. """ user = User(name='guest') db.add(user) anonymous = User(name='anonymous') db.add(anonymous) provenpackager = Group(name='provenpackager') db.add(provenpackager) packager = Group(name='packager') db.add(packager) user.groups.append(packager) release = Release(name='F17', long_name='Fedora 17', id_prefix='FEDORA', version='17', dist_tag='f17', stable_tag='f17-updates', testing_tag='f17-updates-testing', candidate_tag='f17-updates-candidate', pending_signing_tag='f17-updates-signing-pending', pending_testing_tag='f17-updates-testing-pending', pending_stable_tag='f17-updates-pending', override_tag='f17-override', branch='f17', state=ReleaseState.current, create_automatic_updates=True, package_manager=PackageManager.unspecified, testing_repository=None) db.add(release) db.flush() # This mock will help us generate a consistent update alias. with mock.patch(target='uuid.uuid4', return_value='wat'): update = create_update(db, ['bodhi-2.0-1.fc17']) update.type = UpdateType.bugfix update.severity = UpdateSeverity.medium bug = Bug(bug_id=12345) db.add(bug) update.bugs.append(bug) comment = Comment(karma=1, text="wow. amaze.") db.add(comment) comment.user = user update.comments.append(comment) comment = Comment(karma=0, text="srsly. pretty good.") comment.user = anonymous db.add(comment) update.comments.append(comment) db.add(update) db.commit()
def populate(db): """ Create some data for tests to use. Args: db (sqlalchemy.orm.session.Session): The database session. """ user = User(name=u'guest') db.add(user) anonymous = User(name=u'anonymous') db.add(anonymous) provenpackager = Group(name=u'provenpackager') db.add(provenpackager) packager = Group(name=u'packager') db.add(packager) user.groups.append(packager) release = Release(name=u'F17', long_name=u'Fedora 17', id_prefix=u'FEDORA', version=u'17', dist_tag=u'f17', stable_tag=u'f17-updates', testing_tag=u'f17-updates-testing', candidate_tag=u'f17-updates-candidate', pending_signing_tag=u'f17-updates-testing-signing', pending_testing_tag=u'f17-updates-testing-pending', pending_stable_tag=u'f17-updates-pending', override_tag=u'f17-override', branch=u'f17', state=ReleaseState.current) db.add(release) db.flush() update = create_update(db, [u'bodhi-2.0-1.fc17']) update.type = UpdateType.bugfix update.severity = UpdateSeverity.medium bug = Bug(bug_id=12345) db.add(bug) update.bugs.append(bug) cve = CVE(cve_id=u"CVE-1985-0110") db.add(cve) update.cves.append(cve) comment = Comment(karma=1, text=u"wow. amaze.") db.add(comment) comment.user = user update.comments.append(comment) comment = Comment(karma=0, text=u"srsly. pretty good.", anonymous=True) comment.user = anonymous db.add(comment) update.comments.append(comment) with mock.patch(target='uuid.uuid4', return_value='wat'): update.assign_alias() db.add(update) db.commit()
def test_remember_me(self): """Test the post-login hook""" req = DummyRequest( params={ 'openid.op_endpoint': self.app_settings['openid.provider'], }) req.db = self.db req.session = {'came_from': '/'} info = { 'identity_url': 'http://lmacken.id.fedoraproject.org', 'groups': [u'releng'], 'sreg': { 'email': u'[email protected]' }, } req.registry.settings = self.app_settings # Ensure the user doesn't exist yet self.assertIsNone(User.get(u'lmacken', self.db)) self.assertIsNone(Group.get(u'releng', self.db)) remember_me(None, req, info) # The user should now exist, and be a member of the releng group user = User.get(u'lmacken', self.db) self.assertEquals(user.name, u'lmacken') self.assertEquals(user.email, u'[email protected]') self.assertEquals(len(user.groups), 1) self.assertEquals(user.groups[0].name, u'releng') # Pretend the user has been removed from the releng group info['groups'] = [] req.session = {'came_from': '/'} remember_me(None, req, info) user = User.get(u'lmacken', self.db) self.assertEquals(len(user.groups), 0) self.assertEquals(len(Group.get(u'releng', self.db).users), 0)
def test_edit_stack_with_no_group_privs(self, *args): self.stack.users = [] group = Group(name=u'gnome-team') self.db.add(RpmPackage(name=u'gnome-music')) self.db.add(group) self.stack.groups.append(group) self.db.commit() attrs = {'name': 'GNOME', 'packages': 'gnome-music gnome-shell', 'csrf_token': self.get_csrf_token()} res = self.app.post("/stacks/", attrs, status=403) body = res.json_body self.assertEquals(body['status'], 'error') self.assertEquals(body['errors'][0]['description'], 'guest does not have privileges to modify the GNOME stack')
def test_edit_stack_with_group_privs(self, *args): self.stack.users = [] user = self.db.query(User).filter_by(name=u'guest').one() self.db.add(RpmPackage(name=u'gnome-music')) group = Group(name=u'gnome-team') self.db.add(group) self.stack.groups.append(group) user.groups.append(group) self.db.commit() attrs = {'name': 'GNOME', 'packages': 'gnome-music gnome-shell', 'csrf_token': self.get_csrf_token()} res = self.app.post("/stacks/", attrs, status=200) body = res.json_body['stack'] self.assertEquals(body['name'], 'GNOME') self.assertEquals(len(body['packages']), 2) self.assertEquals(body['packages'][-1]['name'], 'gnome-music')
def create_or_update_user(db, username, email, groups): """Create or update a user in the database. Args: db (sqlalchemy.orm.session.Session): The database session. username (str): The username to create or update email (str): The user's email address groups (list(str)): A list of group names the user belongs to, that will be synced. Returns: bodhi.server.models.User: The user instance. """ # Find the user in our database. Create it if it doesn't exist. user = db.query(User).filter_by(name=username).first() if not user: user = User(name=username, email=email) db.add(user) db.flush() else: # Update email address if the address changed if user.email != email: user.email = email db.flush() # Keep track of what groups the user is a member of for group_name in groups: # Drop empty group names https://github.com/fedora-infra/bodhi/issues/306 if not group_name.strip(): continue group = db.query(Group).filter_by(name=group_name).first() if not group: group = Group(name=group_name) db.add(group) db.flush() if group not in user.groups: log.info('Adding %s to %s group', user.name, group.name) user.groups.append(group) # See if the user was removed from any groups for group in user.groups: if group.name not in groups: log.info('Removing %s from %s group', user.name, group.name) user.groups.remove(group) return user