Skip to content

Commit

Permalink
Merge pull request bookieio#473 from sammyshj/apipart2
Browse files Browse the repository at this point in the history
Updated models and corresponding tests
  • Loading branch information
bookiebot committed Jun 12, 2014
2 parents 3a76c0d + 10bfa38 commit 75e5749
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 10 deletions.
8 changes: 7 additions & 1 deletion bookie/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,20 @@ def get_by_hash(hash_id, username=None):

@staticmethod
def find(limit=50, order_by=None, page=0, tags=None, username=None,
with_content=False, with_tags=True):
with_content=False, with_tags=True, requested_by=None):
"""Search for specific sets of bookmarks"""
qry = Bmark.query
# qry = qry.join(Bmark.hashed).\
# options(contains_eager(Bmark.hashed))

offset = limit * page

if requested_by != username:
qry = qry.filter(Bmark.is_private == False) # noqa
# If noqa is not used here the below error occurs with make lint.
# comparison to False should be 'if cond is False:'
# or 'if not cond:'

if with_content:
qry = qry.outerjoin(Bmark.readable).\
options(contains_eager(Bmark.readable))
Expand Down
65 changes: 56 additions & 9 deletions bookie/tests/test_api/test_base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from bookie.tests import BOOKIE_TEST_INI
from bookie.tests import empty_db
from bookie.tests import factory
from bookie.tests import gen_random_word

from datetime import datetime

Expand Down Expand Up @@ -417,27 +418,43 @@ def test_bookmark_remove(self):
"Should have the bmark.us hash: " + res.body)
self._check_cors_headers(res)

def test_bookmark_recent_user(self):
"""Test that we can get list of bookmarks with details"""
self._get_good_request(content=True)
def test_bookmark_recent_same_user(self):
"""Test that we can get list of all bookmarks with details"""
self._get_good_request(content=True, second_bmark=True)
res = self.testapp.get('/api/v1/admin/bmarks?api_key=' + API_KEY,
status=200)

# make sure we can decode the body
bmark = json.loads(res.body)['bmarks'][0]
first_bmark = json.loads(res.body)['bmarks'][0]
second_bmark = json.loads(res.body)['bmarks'][1]
self.assertEqual(
BMARKUS_HASH,
first_bmark[u'hash_id'],
"The hash_id should match: " + str(first_bmark[u'hash_id']))

self.assertTrue(
u'tags' in first_bmark,
"We should have a list of tags in the bmark returned")

self.assertEqual(
u'bookmarks',
first_bmark[u'tags'][0][u'name'],
"Tag should be bookmarks: " +
str(first_bmark[u'tags'][0][u'name']))

self.assertEqual(
GOOGLE_HASH,
bmark[u'hash_id'],
"The hash_id should match: " + str(bmark[u'hash_id']))
second_bmark[u'hash_id'],
"The hash_id should match: " + str(second_bmark[u'hash_id']))

self.assertTrue(
u'tags' in bmark,
u'tags' in second_bmark,
"We should have a list of tags in the bmark returned")

self.assertTrue(
bmark[u'tags'][0][u'name'] in [u'python', u'search'],
second_bmark[u'tags'][0][u'name'] in [u'python', u'search'],
"Tag should be either python or search:" +
str(bmark[u'tags'][0][u'name']))
str(second_bmark[u'tags'][0][u'name']))

res = self.testapp.get(
'/api/v1/admin/bmarks?with_content=true&api_key=' + API_KEY,
Expand All @@ -451,6 +468,36 @@ def test_bookmark_recent_user(self):
# self.assertTrue('here dude' in bmark[u'readable']['content'],
# "There should be content: " + str(bmark))

def test_bookmark_recent_diff_user(self):
"""Test that we can get a list of only public bookmarks with details"""
self._get_good_request(content=True, second_bmark=True)
diff_user_api_key = gen_random_word(6)
res = self.testapp.get('/api/v1/admin/bmarks?api_key=' +
diff_user_api_key,
status=200)

# Make sure we can decode the body.
bmark = json.loads(res.body)['bmarks'][0]
self.assertEqual(
GOOGLE_HASH,
bmark[u'hash_id'],
"The hash_id should match: " + str(bmark[u'hash_id']))

self.assertTrue(
u'tags' in bmark,
"We should have a list of tags in the bmark returned")

self.assertTrue(
bmark[u'tags'][0][u'name'] in [u'python', u'search'],
"Tag should be either python or search: " +
str(bmark[u'tags'][0][u'name']))

res = self.testapp.get(
'/api/v1/admin/bmarks?with_content=true&api_key=' +
diff_user_api_key,
status=200)
self._check_cors_headers(res)

def test_bookmark_recent(self):
"""Test that we can get list of bookmarks with details"""
self._get_good_request(content=True)
Expand Down
75 changes: 75 additions & 0 deletions bookie/tests/test_models/test_bmarkmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,78 @@ def test_delete_all_bookmarks(self):
'There should be no tags left: ' + str(len(tags))
)
DBSession.flush()

def test_find_bookmarks_same_user(self):
"""A user requesting their bookmarks includes private ones"""
bookmark_count_private = 5
bookmark_count_public = 5
user = User()
user.username = gen_random_word(19)
DBSession.add(user)

for i in range(bookmark_count_private):
b = Bmark(
url=gen_random_word(12),
username=user.username,
)
DBSession.add(b)

for i in range(bookmark_count_public):
b = Bmark(
url=gen_random_word(12),
username=user.username,
is_private=False,
)
DBSession.add(b)

DBSession.flush()
res = BmarkMgr.find(username=user.username, requested_by=user.username)
self.assertEqual(
bookmark_count_private + bookmark_count_public,
len(res),
'There should be ' + str(bookmark_count_private +
bookmark_count_public) +
' bookmarks present: ' + str(len(res))
)

def test_find_bookmarks_diff_user(self):
"""A user requesting another user's bookmarks get public only"""
bookmark_count_private = 5
bookmark_count_public = 5
user = User()
user.username = gen_random_word(19)
DBSession.add(user)

for i in range(bookmark_count_private):
b = Bmark(
url=gen_random_word(12),
username=user.username,
)
DBSession.add(b)

for i in range(bookmark_count_public):
b = Bmark(
url=gen_random_word(12),
username=user.username,
is_private=False,
)
DBSession.add(b)

DBSession.flush()
res = BmarkMgr.find(username=user.username,
requested_by=gen_random_word(19))
self.assertEqual(
bookmark_count_public,
len(res),
'There should be ' + str(bookmark_count_public) +
' bookmarks present: ' + str(len(res))
)

# Also check if requested_by is None.
res = BmarkMgr.find(username=user.username, requested_by=None)
self.assertEqual(
bookmark_count_public,
len(res),
'There should be ' + str(bookmark_count_public) +
' bookmarks present: ' + str(len(res))
)
7 changes: 7 additions & 0 deletions bookie/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ def bmark_recent(request, with_content=False):
if username:
username = username.lower()

# We need to check who has requested for the bookmarks.
if request.user:
requested_by = request.user.username
else:
requested_by = None

# We need to check if we have an ordering crtieria specified.
order_by = params.get('sort', None)
if order_by == "popular":
Expand Down Expand Up @@ -405,6 +411,7 @@ def bmark_recent(request, with_content=False):
tags=tags,
username=username,
with_tags=True,
requested_by=requested_by,
)

result_set = []
Expand Down
3 changes: 3 additions & 0 deletions docs/api/user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Example
"bid": 1,
"stored": "2011-08-06 20:35:54",
"inserted_by": "unknown_api",
"is_private": true,
"tag_str": "bookmarks",
"clicks": 0,
"hash_id": "c5c21717c99797"
Expand Down Expand Up @@ -238,6 +239,7 @@ Example
"bid": 2,
"stored": "2011-06-21 13:20:26",
"inserted_by": null,
"is_private": true,
"tag_str": "test bookmarks",
"clicks": 1,
"hash_id": "c605a21cf19560",
Expand All @@ -258,6 +260,7 @@ Example
"bid": 1,
"stored": "2011-06-20 11:42:47",
"inserted_by": null,
"is_private": true,
"tag_str": "bookmarks",
"clicks": 1,
"hash_id": "c5c21717c99797",
Expand Down

0 comments on commit 75e5749

Please sign in to comment.