def test_per_user(self): """We should only get a pair of results for this single user""" ct = 5 common = 'testing.com' user = User() user.username = gen_random_word(10) DBSession.add(user) usercommon = User() usercommon.username = common DBSession.add(usercommon) for i in range(ct - 2): b = Bmark(url=gen_random_word(12), username=user.username) DBSession.add(b) # add in our dupes c = Bmark( url=gen_random_word(10), username=usercommon.username, ) DBSession.add(c) DBSession.flush() d = Bmark( url=gen_random_word(10), username=usercommon.username, ) DBSession.add(d) DBSession.flush() ct = BmarkMgr.count(username=usercommon.username) eq_(2, ct, 'We should have a total of 2: ' + str(ct))
def test_one_import(self): """You should be able to only get one import running at a time""" self._login_admin() # Prep the db with 2 other imports ahead of this user's. # We have to commit these since the request takes place in a new # session/transaction. DBSession.add(ImportQueue(username=u'testing', file_path=u'testing.txt')) DBSession.add(ImportQueue(username=u'testing2', file_path=u'testing2.txt')) DBSession.flush() transaction.commit() res = self._upload() res.follow() # now let's hit the import page, we shouldn't get a form, but instead a # message about our import res = self.app.get('/admin/import') self.assertTrue('<form' not in res.body, "We shouldn't have a form") self.assertTrue( 'waiting in the queue' in res.body, "We want to display a waiting message.") self.assertTrue( '2 other imports' in res.body, "We want to display a count message." + res.body)
def upgrade(migrate_engine): """By default start all installs out with a bookmark to bmark.us It's cheesy, but helps with testing to have some base data and is good for pubbing the links """ from datetime import datetime import transaction from bookie.models import initialize_sql initialize_sql(migrate_engine) from bookie.models import DBSession from bookie.models import Bmark bmark_us = Bmark('http://bmark.us', desc="Bookie Website", ext="Bookie Documentation Home", tags="bookmarks") bmark_us.stored = datetime.now() bmark_us.updated = datetime.now() DBSession.add(bmark_us) DBSession.flush() transaction.commit()
def db_init_bookmark(): """install the initial bookmark in a new install""" require('hosts', provided_by=[sample]) require('ini', provided_by=[sample]) parse_ini(env["ini_file"]) from datetime import datetime import transaction from bookie.models import initialize_sql from sqlalchemy import create_engine engine = create_engine(env.ini.get('app:bookie', 'sqlalchemy.url')) initialize_sql(engine) from bookie.models import DBSession from bookie.models import Bmark bmark_us = Bmark(u'http://bmark.us', u'admin', desc=u"Bookie Website", ext= u"Bookie Documentation Home", tags = u"bookmarks") bmark_us.stored = datetime.now() bmark_us.updated = datetime.now() DBSession.add(bmark_us) DBSession.flush() transaction.commit()
def new_user(username, email): """Add new user function, pass username, email :param username: string of new user :param email: string of new email """ require('hosts', provided_by=[sample]) require('ini', provided_by=[sample]) parse_ini(env["ini_file"]) import transaction from bookie.models import initialize_sql initialize_sql(dict(env.ini.items('app:main'))) from bookie.models import DBSession from bookie.models.auth import get_random_word, User sess = DBSession() u = User() u.username = unicode(username) passwd = get_random_word(8) u.password = passwd u.email = unicode(email) u.activated = True u.is_admin = False u.api_key = User.gen_api_key() print dict(u) print passwd sess.add(u) sess.flush() transaction.commit()
def upgrade(migrate_engine): """By default start all installs out with a bookmark to bmark.us It's cheesy, but helps with testing to have some base data and is good for pubbing the links """ from datetime import datetime import transaction from bookie.models import initialize_sql initialize_sql(migrate_engine) from bookie.models import DBSession from bookie.models import Bmark bmark_us = Bmark('http://bmark.us', desc="Bookie Website", ext= "Bookie Documentation Home", tags = "bookmarks") bmark_us.stored = datetime.now() bmark_us.updated = datetime.now() DBSession.add(bmark_us) DBSession.flush() transaction.commit()
def _new_user(args): """Handle adding a new user to the system. If you don't include the required info, it will prompt you for it """ if not args.username: args.username = raw_input('username? ') if not args.email: args.email = raw_input('email address? ') if not args.username or not args.email: raise Exception('Must supply a username and email address') import transaction _init_sql(args) from bookie.models import DBSession sess = DBSession() u = User() u.username = unicode(args.username) passwd = get_random_word(8) u.password = passwd u.email = unicode(args.email) u.activated = True u.is_admin = False u.api_key = User.gen_api_key() print dict(u) print passwd sess.add(u) sess.flush() transaction.commit()
def test_one_import(self): """You should be able to only get one import running at a time""" self._login_admin() # Prep the db with 2 other imports ahead of this user's. # We have to commit these since the request takes place in a new # session/transaction. DBSession.add( ImportQueue(username=u'testing', file_path=u'testing.txt')) DBSession.add( ImportQueue(username=u'testing2', file_path=u'testing2.txt')) DBSession.flush() transaction.commit() res = self._upload() res.follow() # now let's hit the import page, we shouldn't get a form, but instead a # message about our import res = self.app.get('/admin/import') self.assertTrue('<form' not in res.body, "We shouldn't have a form") self.assertTrue('waiting in the queue' in res.body, "We want to display a waiting message.") self.assertTrue('2 other imports' in res.body, "We want to display a count message." + res.body)
def new_user(request): """Add a new user to the system manually.""" rdict = request.params u = User() u.username = unicode(rdict.get('username')) u.email = unicode(rdict.get('email')) passwd = get_random_word(8) u.password = passwd u.activated = True u.is_admin = False u.api_key = User.gen_api_key() try: DBSession.add(u) DBSession.flush() # We need to return the password since the admin added the user # manually. This is only time we should have/give the original # password. ret = dict(u) ret['random_pass'] = passwd return _api_response(request, ret) except IntegrityError, exc: # We might try to add a user that already exists. LOG.error(exc) request.response.status_int = 400 return _api_response(request, { 'error': 'Bad Request: User exists.', })
def new_user(request): """Add a new user to the system manually.""" rdict = request.params u = User() u.username = unicode(rdict.get('username')) u.email = unicode(rdict.get('email')) passwd = get_random_word(8) u.password = passwd u.activated = True u.is_admin = False u.api_key = User.gen_api_key() try: DBSession.add(u) DBSession.flush() # We need to return the password since the admin added the user # manually. This is only time we should have/give the original # password. ret = dict(u) ret['random_pass'] = passwd return ret except IntegrityError, exc: # We might try to add a user that already exists. LOG.error(exc) request.response.status_int = 400 return { 'error': 'Bad Request: User exists.', }
def _add_demo_import(self): """DB Needs some imports to be able to query.""" # add out completed one q = ImportQueue(username='******', file_path='testing.txt') DBSession.add(q) transaction.commit() return
def import_bmarks(self): """Allow users to upload a bookmark export file for processing""" username = self.matchdict.get('username') # if auth fails, it'll raise an HTTPForbidden exception with ReqAuthorize(self.request): data = {} post = self.POST # We can't let them submit multiple times, check if this user has # an import in process. if ImportQueueMgr.get(username=username, status=NEW): # They have an import, get the information about it and shoot # to the template. return { 'existing': True, 'import_stats': ImportQueueMgr.get_details( username=username) } if post: # we have some posted values files = post.get('import_file', None) if files is not None: storage_dir_tpl = self.settings.get('import_files', '/tmp/bookie') storage_dir = storage_dir_tpl.format( here=self.settings.get('app_root')) out_fname = store_import_file(storage_dir, username, files) # Mark the system that there's a pending import that needs # to be completed q = ImportQueue(username, unicode(out_fname)) DBSession.add(q) DBSession.flush() # Schedule a task to start this import job. tasks.importer_process.delay(q.id) return HTTPFound( location=self.request.route_url('user_import', username=username)) else: msg = self.request.session.pop_flash() if msg: data['error'] = msg else: data['error'] = None return data else: # we need to see if they've got # just display the form return { 'existing': False }
def test_total_ct(self): """Verify that our total count method is working""" ct = 5 for i in range(ct): t = Tag(gen_random_word(10)) DBSession.add(t) ct = TagMgr.count() eq_(5, ct, 'We should have a total of 5: ' + str(ct))
def test_total_ct(self): """Verify that our total count method is working""" ct = 5 for i in range(ct): t = Tag(gen_random_word(10)) DBSession.add(t) ct = TagMgr.count() eq_(5, ct, 'We should have a total of 5: ' + str(ct))
def _add_demo_import(self): """DB Needs some imports to be able to query.""" # add out completed one q = ImportQueue( username=u'admin', file_path=u'testing.txt' ) DBSession.add(q) transaction.commit() return
def make_bookmark(user=None): """Generate a fake bookmark for testing use.""" bmark = Bmark(random_url(), username="******", desc=random_string(), ext=random_string(), tags=u"bookmarks") if user: bmark.username = user.username bmark.user = user DBSession.add(bmark) DBSession.flush() return bmark
def make_user(username=None): """Generate a fake user to test against.""" user = User() if not username: username = random_string(10) user.username = username DBSession.add(user) DBSession.flush() return user
def _add_bmark_w_desc(self): # setup the default bookie bookmark bmark_us = Bmark(u'http://bmark.us', username=u"admin", desc=u"Bookie Website", ext=u"Bookie Documentation Home", tags=u"bookmarks") bmark_us.stored = datetime.now() bmark_us.updated = datetime.now() DBSession.add(bmark_us) transaction.commit()
def _add_bmark_wt_desc(self): #setup the default google bookmark bmark_us = Bmark(u'http://google.com', username=u"admin", desc=u"", ext=u"Google Search Engine", tags=u"bookmarks") bmark_us.stored = datetime.now() bmark_us.updated = datetime.now() DBSession.add(bmark_us) transaction.commit()
def _add_bmark_w_desc(self): # setup the default bookie bookmark bmark_us = Bmark(u'http://bmark.us', username=u"admin", desc=u"Bookie Website", ext=u"Bookie Documentation Home", tags=u"bookmarks") bmark_us.stored = datetime.now() bmark_us.updated = datetime.now() DBSession.add(bmark_us) transaction.commit()
def testInitialUserInactivated(self): """A new user signup should be a deactivated user""" u = User() u.email = gen_random_word(10) DBSession.add(u) eq_(False, u.activated, 'A new signup should start out deactivated by default') ok_(u.activation.code is not None, 'A new signup should start out as deactivated') eq_('signup', u.activation.created_by, 'This is a new signup, so mark is as thus')
def _add_bmark_wt_desc(self): #setup the default google bookmark bmark_us = Bmark(u'http://google.com', username=u"admin", desc=u"", ext=u"Google Search Engine", tags=u"bookmarks") bmark_us.stored = datetime.now() bmark_us.updated = datetime.now() DBSession.add(bmark_us) transaction.commit()
def make_user(username=None): """Generate a fake user to test against.""" user = User() if not username: username = random_string(10) user.username = username DBSession.add(user) DBSession.flush() return user
def test_total_ct(self): """Verify that our total count method is working""" ct = 5 user = User() user.username = gen_random_word(10) DBSession.add(user) for i in range(ct): b = Bmark(url=gen_random_word(12), username=user.username) b.hash_id = gen_random_word(3) DBSession.add(b) ct = BmarkMgr.count() eq_(5, ct, 'We should have a total of 5: ' + str(ct))
def invite(self, email): """Invite a user""" if not self.has_invites(): return False if not email: raise ValueError('You must supply an email address to invite') else: # get this invite party started, create a new useracct new_user = UserMgr.signup_user(email, self.username) # decrement the invite counter self.invite_ct = self.invite_ct - 1 DBSession.add(new_user) return new_user
def _add_bmark(self): # setup the default bookie bookmark import logging log = logging.getLogger(__name__) log.error('called to add bmark') bmark_us = Bmark('http://bmark.us', desc="Bookie Website", ext="Bookie Documentation Home", tags="bookmarks") bmark_us.stored = datetime.now() bmark_us.updated = datetime.now() DBSession.add(bmark_us) transaction.commit()
def invite(self, email): """Invite a user""" if not self.has_invites(): return False if not email: raise ValueError('You must supply an email address to invite') else: # get this invite party started, create a new useracct new_user = UserMgr.signup_user(email, self.username) # decrement the invite counter self.invite_ct = self.invite_ct - 1 DBSession.add(new_user) return new_user
def signup_user(email, signup_method): # Get this invite party started, create a new user acct. new_user = User() new_user.email = email new_user.username = email new_user.invited_by = signup_method new_user.api_key = User.gen_api_key() # they need to be deactivated new_user.reactivate('invite') # decrement the invite counter DBSession.add(new_user) return new_user
def _add_bmark(self): # setup the default bookie bookmark import logging log = logging.getLogger(__name__) log.error('called to add bmark') bmark_us = Bmark('http://bmark.us', desc="Bookie Website", ext= "Bookie Documentation Home", tags = "bookmarks") bmark_us.stored = datetime.now() bmark_us.updated = datetime.now() DBSession.add(bmark_us) transaction.commit()
def signup_user(email, signup_method): # Get this invite party started, create a new user acct. new_user = User() new_user.email = email new_user.username = email new_user.invited_by = signup_method new_user.api_key = User.gen_api_key() # they need to be deactivated new_user.reactivate('invite') # decrement the invite counter DBSession.add(new_user) return new_user
def test_unique_ct(self): """Verify that our unique count method is working""" ct = 5 common = 'testing.com' users = [] for i in range(ct): user = User() user.username = gen_random_word(10) DBSession.add(user) users.append(user) for i in range(ct - 2): b = Bmark( url=gen_random_word(12), username=users[i].username ) DBSession.add(b) # Add in our dupes c = Bmark( url=common, username=users[3].username ) DBSession.add(c) DBSession.flush() d = Bmark( url=common, username=users[4].username ) DBSession.add(d) DBSession.flush() ct = BmarkMgr.count(distinct=True) eq_(4, ct, 'We should have a total of 4: ' + str(ct))
def test_unique_ct(self): """Verify that our unique count method is working""" ct = 5 common = 'testing.com' users = [] for i in range(ct): user = User() user.username = gen_random_word(10) DBSession.add(user) users.append(user) for i in range(ct - 2): b = Bmark(url=gen_random_word(12), username=users[i].username) DBSession.add(b) # Add in our dupes c = Bmark(url=common, username=users[3].username) DBSession.add(c) DBSession.flush() d = Bmark(url=common, username=users[4].username) DBSession.add(d) DBSession.flush() ct = BmarkMgr.count(distinct=True) eq_(4, ct, 'We should have a total of 4: ' + str(ct))
def make_bookmark(user=None): """Generate a fake bookmark for testing use.""" bmark = Bmark(random_url(), username="******", desc=random_string(), ext=random_string(), tags=u"bookmarks") if user: bmark.username = user.username bmark.user = user DBSession.add(bmark) DBSession.flush() return bmark
def test_activation_delete(self): """Make sure removing an activation does not remove a user.""" tst = User() tst.username = gen_random_word(10) tst.activation = Activation('signup') DBSession.add(tst) DBSession.flush() DBSession.delete(tst.activation) users = UserMgr.get_list() # We still have the admin user as well so the count is two. eq_(2, len(users), 'We should have a total of 2 users still: ' + str(len(users)))
def test_completed_dont_count(self): """Once completed, we should get the form again""" self._login_admin() # add out completed one q = ImportQueue(username=u'admin', file_path=u'testing.txt') q.completed = datetime.now() q.status = 2 DBSession.add(q) transaction.commit() # now let's hit the import page, we shouldn't get a form, but instead a # message about our import res = self.app.get('/admin/import') self.assertTrue('<form' in res.body, "We should have a form")
def test_total_ct(self): """Verify that our total count method is working""" ct = 5 user = User() user.username = gen_random_word(10) DBSession.add(user) for i in range(ct): b = Bmark( url=gen_random_word(12), username=user.username ) b.hash_id = gen_random_word(3) DBSession.add(b) ct = BmarkMgr.count() eq_(5, ct, 'We should have a total of 5: ' + str(ct))
def test_activation_delete(self): """Make sure removing an activation does not remove a user.""" tst = User() tst.username = gen_random_word(10) tst.activation = Activation(u'signup') DBSession.add(tst) DBSession.flush() DBSession.delete(tst.activation) users = UserMgr.get_list() # We still have the admin user as well so the count is two. self.assertEqual( 2, len(users), 'We should have a total of 2 users still: ' + str(len(users)))
def test_activation_cascade(self): """Removing a user cascades the activations as well.""" tst = User() tst.username = gen_random_word(10) tst.activation = Activation('signup') DBSession.add(tst) DBSession.flush() DBSession.delete(tst) users = UserMgr.get_list() # We still have the admin user as well so the count is one. eq_(1, len(users), 'We should have a total of 1 user still: ' + str(len(users))) activations = DBSession.query(Activation).all() eq_(0, len(activations), 'There should be no activations left')
def test_case_insensitive(self): """Suggestion does not care about case of the prefix.""" # Generate demo tag into the system tags = [make_tag() for i in range(5)] [DBSession.add(t) for t in tags] test_str = tags[0].name[0:4].upper() suggestions = TagMgr.complete(test_str) ok_(tags[0] in suggestions, "The sample tag was found in the completion set")
def test_case_insensitive(self): """Suggestion does not care about case of the prefix.""" # Generate demo tag into the system tags = [make_tag() for i in range(5)] [DBSession.add(t) for t in tags] test_str = tags[0].name[0:4].upper() suggestions = TagMgr.complete(test_str) ok_(tags[0] in suggestions, "The sample tag was found in the completion set")
def test_completed_dont_count(self): """Once completed, we should get the form again""" self._login_admin() # add out completed one q = ImportQueue( username=u'admin', file_path=u'testing.txt' ) q.completed = datetime.now() q.status = 2 DBSession.add(q) transaction.commit() # now let's hit the import page, we shouldn't get a form, but instead a # message about our import res = self.app.get('/admin/import') self.assertTrue('<form' in res.body, "We should have a form")
def test_basic_complete(self): """Tags should provide completion options.""" # Generate demo tag into the system tags = [make_tag() for i in range(5)] [DBSession.add(t) for t in tags] test_str = tags[0].name[0:2] suggestions = TagMgr.complete(test_str) ok_(tags[0] in suggestions, "The sample tag was found in the completion set")
def test_basic_complete(self): """Tags should provide completion options.""" # Generate demo tag into the system tags = [make_tag() for i in range(5)] [DBSession.add(t) for t in tags] test_str = tags[0].name[0:2] suggestions = TagMgr.complete(test_str) ok_(tags[0] in suggestions, "The sample tag was found in the completion set")
def test_activation_cascade(self): """Removing a user cascades the activations as well.""" tst = User() tst.username = gen_random_word(10) tst.activation = Activation('signup') DBSession.add(tst) DBSession.flush() DBSession.delete(tst) users = UserMgr.get_list() # We still have the admin user as well so the count is one. eq_( 1, len(users), 'We should have a total of 1 user still: ' + str(len(users))) activations = DBSession.query(Activation).all() eq_(0, len(activations), 'There should be no activations left')
def test_per_user(self): """We should only get a pair of results for this single user""" ct = 5 common = 'testing.com' user = User() user.username = gen_random_word(10) DBSession.add(user) usercommon = User() usercommon.username = common DBSession.add(usercommon) for i in range(ct - 2): b = Bmark( url=gen_random_word(12), username=user.username ) DBSession.add(b) # add in our dupes c = Bmark( url=gen_random_word(10), username=usercommon.username, ) DBSession.add(c) DBSession.flush() d = Bmark( url=gen_random_word(10), username=usercommon.username, ) DBSession.add(d) DBSession.flush() ct = BmarkMgr.count(username=usercommon.username) eq_(2, ct, 'We should have a total of 2: ' + str(ct))
def setUp(self): """Populate the DB with a couple of testing records""" trans = transaction.begin() user = User() user.username = gen_random_word(10) DBSession.add(user) for i in range(3): url = gen_random_word(12) b = self.__create_bookmark(url, user.username) DBSession.add(b) # add bookmark with duplicate url b = self.__create_bookmark(url, gen_random_word(10)) DBSession.add(b) trans.commit()
def setUp(self): """Populate the DB with a couple of testing records""" trans = transaction.begin() user = User() user.username = gen_random_word(10) DBSession.add(user) for i in range(3): url = gen_random_word(12) b = self.__create_bookmark(url, user.username) DBSession.add(b) # add bookmark with duplicate url b = self.__create_bookmark(url, gen_random_word(10)) DBSession.add(b) trans.commit()
def import_bmarks(request): """Allow users to upload a delicious bookmark export""" rdict = request.matchdict username = rdict.get('username') # if auth fails, it'll raise an HTTPForbidden exception with ReqAuthorize(request): data = {} post = request.POST # we can't let them submit multiple times, check if this user has an # import in process if ImportQueueMgr.get(username=username, status=NEW): # they have an import, get the information about it and shoot to # the template return { 'existing': True, 'import_stats': ImportQueueMgr.get_details(username=username) } if post: # we have some posted values files = post.get('import_file', None) if files is not None: # save the file off to the temp storage out_dir = "{storage_dir}/{randdir}".format( storage_dir=request.registry.settings.get( 'import_files', '/tmp/bookie').format( here=request.registry.settings.get('app_root')), randdir=random.choice(string.letters), ) # make sure the directory exists # we create it with parents as well just in case if not os.path.isdir(out_dir): os.makedirs(out_dir) out_fname = "{0}/{1}.{2}".format( out_dir, username, files.filename) out = open(out_fname, 'w') out.write(files.file.read()) out.close() # mark the system that there's a pending import that needs to # be completed q = ImportQueue(username, out_fname) DBSession.add(q) DBSession.flush() # Schedule a task to start this import job. tasks.importer_process.delay(q.id) return HTTPFound(location=request.route_url('user_import', username=username)) else: msg = request.session.pop_flash() if msg: data['error'] = msg else: data['error'] = None return data else: # we need to see if they've got # just display the form return { 'existing': False }
def count_importer_depth(): """Mark how deep the importer queue is at the moment""" total = ImportQueueMgr.size() stat = StatBookmark(attrib=IMPORTER_CT, data=total) DBSession.add(stat)
def count_total_tags(): """Count the total number of tags in the system""" total = TagMgr.count() stat = StatBookmark(attrib=TAG_CT, data=total) DBSession.add(stat)
def count_total_bookmarks(): """Count the total number of bookmarks in the system""" total = BmarkMgr.count() stat = StatBookmark(attrib=TOTAL_CT, data=total) DBSession.add(stat)
def count_unique_bookmarks(): """Count the unique number of bookmarks in the system""" total = BmarkMgr.count(distinct=True) stat = StatBookmark(attrib=UNIQUE_CT, data=total) DBSession.add(stat)
from datetime import datetime from ConfigParser import ConfigParser from os import path from bookie.models import initialize_sql if __name__ == "__main__": ini = ConfigParser() ini_path = path.join(path.dirname(path.dirname(path.dirname(__file__))), 'bookie.ini') ini.readfp(open(ini_path)) initialize_sql(dict(ini.items("app:bookie"))) from bookie.models import DBSession from bookie.models import Bmark bmark_us = Bmark(u'http://bmark.us', u'admin', desc=u'Bookie Website', ext= u'Bookie Documentation Home', tags = u'bookmarks') bmark_us.stored = datetime.now() bmark_us.updated = datetime.now() DBSession.add(bmark_us) DBSession.flush() transaction.commit()