def test_google_import(self): """Test that we can upload our google file""" session = DBSession() loc = os.path.dirname(__file__) del_file = open(os.path.join(loc, 'googlebookmarks.html')) post = { 'api_key': 'testapi', } res = self.testapp.post( '/import', params=post, upload_files=[('import_file', 'googlebookmarks.html', del_file.read())], ) eq_(res.status, "302 Found", msg='Import status is 302 redirect by home, ' + res.status) session.flush() # test all the data we want to test after the import _google_data_test()
def test_tag_with_space(self): """Test that we strip out spaces from tags and don't get empty tags """ self._get_good_request() # now build a new version of the request we can check session = DBSession() prms = { 'url': u'http://google.com', 'description': u'This is my updated google desc', 'extended': 'updated extended notes about it in full form', 'tags': u'python search updated ', 'api_key': u'testapi', } req_params = urllib.urlencode(prms) self.testapp.get('/delapi/posts/add?' + req_params) session.flush() res = Bmark.query.filter(Bmark.url == u'http://google.com').one() ok_( len(res.tags) == 3, 'Should only have 3 tags: ' + str([str(t) for t in res.tags])) for tag in res.tags: ok_(tag[0] != " ", "Tag should not start with a space") ok_(tag[-1] != " ", "Tag should not end with a space")
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_update_post(self): """Updates allowed over the last one If you /post/add to an existing bookmark, the new data overrides the old data """ self._get_good_request() # now build a new version of the request we can check session = DBSession() prms = { 'url': u'http://google.com', 'description': u'This is my updated google desc', 'extended': 'updated extended notes about it in full form', 'tags': u'python search updated', 'api_key': u'testapi', } req_params = urllib.urlencode(prms) self.testapp.get('/delapi/posts/add?' + req_params) session.flush() res = Bmark.query.filter(Bmark.url == u'http://google.com').one() ok_('updated' in res.description, 'Updated description took: ' + res.description) ok_('updated' in res.extended, 'Updated extended took: ' + res.extended) ok_('python' in res.tags, 'Found the python tag in the bmark') ok_('search' in res.tags, 'Found the search tag in the bmark') ok_('updated' in res.tags, 'Found the updated tag in the bmark')
def tearDown(self): """Regular tear down method""" session = DBSession() Bmark.query.delete() Tag.query.delete() session.execute(bmarks_tags.delete()) session.flush() transaction.commit()
def tearDown(self): """Tear down each test""" testing.tearDown() session = DBSession() Bmark.query.delete() Tag.query.delete() session.execute(bmarks_tags.delete()) session.flush() transaction.commit()
def tearDown(self): """We need to empty the bmarks table on each run""" testing.tearDown() session = DBSession() Bmark.query.delete() Tag.query.delete() session.execute(bmarks_tags.delete()) session.flush() transaction.commit()
def _get_good_request(self, content=False, second_bmark=False): """Return the basics for a good add bookmark request""" session = DBSession() # the main bookmark, added second to prove popular will sort correctly prms = { 'url': u'http://google.com', 'description': u'This is my google desc', 'extended': u'And some extended notes about it in full form', 'tags': u'python search', 'api_key': API_KEY, 'username': '******', 'inserted_by': 'chrome_ext', } # if we want to test the readable fulltext side we want to make sure we # pass content into the new bookmark if content: prms['content'] = "<p>There's some content in here dude</p>" # req_params = urllib.urlencode(prms) res = self.testapp.post( '/api/v1/admin/bmark?', content_type='application/json', params=json.dumps(prms), ) if second_bmark: prms = { 'url': u'http://bmark.us', 'description': u'Bookie', 'extended': u'Exteded notes', 'tags': u'bookmarks', 'api_key': API_KEY, 'username': '******', 'inserted_by': 'chrome_ext', } # if we want to test the readable fulltext side we want to make # sure we pass content into the new bookmark prms['content'] = "<h1>Second bookmark man</h1>" # req_params = urllib.urlencode(prms) res = self.testapp.post('/api/v1/admin/bmark?', content_type='application/json', params=json.dumps(prms)) session.flush() transaction.commit() # Run the celery task for indexing this bookmark. tasks.reindex_fulltext_allbookmarks(sync=True) return res
def _get_good_request(self): """Return the basics for a good add bookmark request""" session = DBSession() prms = { 'url': u'http://google.com', 'description': u'This is my google desc', 'extended': u'And some extended notes about it in full form', 'tags': u'python search', 'api_key': u'testapi', } req_params = urllib.urlencode(prms) res = self.testapp.get('/delapi/posts/add?' + req_params) session.flush() transaction.commit() return res
def _get_good_request(self): """Return the basics for a good add bookmark request""" session = DBSession() prms = { 'url': u'http://google.com', 'description': u'This is my google desc', 'extended': u'And some extended notes about it in full form', 'tags': u'python search', 'api_key': API_KEY, 'content': 'bmark content is the best kind of content man', } req_params = urllib.urlencode(prms) res = self.testapp.post('/api/v1/admin/bmark', params=req_params) session.flush() transaction.commit() return res
def posts_delete(request): """Remove a bmark from the system""" params = request.params request.response.content_type = 'text/xml' if 'url' in params and params['url']: try: bmark = BmarkMgr.get_by_url(params['url'], username=request.user.username) session = DBSession() session.delete(bmark) return '<result code="done" />' except NoResultFound: # if it's not found, then there's not a bookmark to delete return '<result code="Bad Request: bookmark not found" />'
def posts_delete(request): """Remove a bmark from the system""" params = request.GET request.response_content_type = 'text/xml' with Authorize(request.registry.settings.get('api_key', ''), params.get('api_key', None)): if 'url' in params and params['url']: try: bmark = BmarkMgr.get_by_url(params['url']) session = DBSession() session.delete(bmark) return '<result code="done" />' except NoResultFound: # if it's not found, then there's not a bookmark to delete return '<result code="Bad Request: bookmark not found" />'
def test_datestimes_set(self): """Test that we get the new datetime fields as we work""" now = datetime.now() self._get_good_request() res = Bmark.query.filter(Bmark.url == u'http://google.com').one() ok_( res.stored >= now, "Stored time is now or close to now {0}:{1}".format( res.stored, now)) res.url = u"Somethingnew.com" session = DBSession() session.flush() # now hopefully have an updated value ok_( res.updated >= now, "Stored time is now or close to now {0}:{1}".format( res.updated, now))
def _get_good_request(self, content=False): """Return the basics for a good add bookmark request""" session = DBSession() prms = { 'url': u'http://googles.com', 'description': u'This is my google desc', 'extended': u'And some extended notes about it in full form', 'tags': u'python search', 'api_key': API_KEY, } # if we want to test the readable fulltext side we want to make sure we # pass content into the new bookmark if content: prms['content'] = "<h1>There's some content in here dude</h1>" req_params = urllib.urlencode(prms) res = self.testapp.get('/admin/delapi/posts/add?' + req_params) session.flush() transaction.commit() return res
def _reset_password(args): """Reset a user's password""" if not args.username: args.username = raw_input('username? ') if not args.password: args.password = raw_input('password? ') if not args.username or not args.password: raise Exception('Must supply a username and password') import transaction _init_sql(args) from bookie.models import DBSession sess = DBSession() u = UserMgr.get(username=unicode(args.username)) u.password = args.password sess.flush() transaction.commit()
def _get_good_request(self, new_tags=None): """Return the basics for a good add bookmark request""" session = DBSession() prms = { 'url': u'http://google.com', 'description': u'This is my google desc SEE', 'extended': u'And some extended notes about it in full form', 'tags': u'python search', 'api_key': API_KEY, } if new_tags: prms['tags'] = new_tags req_params = urllib.urlencode(prms) res = self.testapp.post('/api/v1/admin/bmark', params=req_params) session.flush() transaction.commit() tasks.reindex_fulltext_allbookmarks(sync=True) return res
def test_post_add_with_dt(self): """Make sure if we provide a date it works Success response: <result code="done" /> Not supporting optional params right now replace, shared """ success = '<result code="done" />' session = DBSession() # pick a date that is tomorrow today = datetime.now() yesterday = today - timedelta(days=1) dt = yesterday.strftime("%Y-%m-%dT%H:%M:%SZ") prms = { 'url': u'http://google.com', 'description': u'This is my google desc', 'extended': u'And some extended notes about it in full form', 'tags': u'python search', 'dt': dt, 'api_key': u'testapi', } req_params = urllib.urlencode(prms) res = self.testapp.get('/delapi/posts/add?' + req_params) session.flush() eq_(res.status, "200 OK", msg='Post Add status is 200, ' + res.status) eq_(res.body, success, msg="Request should return done msg") # now pull up the bmark and check the date is yesterday res = Bmark.query.filter(Bmark.url == u'http://google.com').one() eq_( res.stored.strftime('%Y-%m-%d'), yesterday.strftime('%Y-%m-%d'), "The stored date {0} is the same as the requested {1}".format( res.stored, yesterday))