Skip to content

Commit

Permalink
added tests for submitting articles
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamYuxinXu committed May 6, 2024
1 parent 3b08a48 commit 7d8569d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
4 changes: 3 additions & 1 deletion server/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,10 @@ def post(self):
Submit an article for review.
Must submit either a link or a body of text, or both.
"""
print("checking user id")
if 'user_id' not in session:
return {DATA: 'No user currently logged in'}, HTTPStatus.UNAUTHORIZED
print("Parsing Arguements")
parser = reqparse.RequestParser(bundle_errors=True)
parser.add_argument(articles.ARTICLE_LINK, type=str, required=False,
help='Article link cannot be blank if article body is also blank.')
Expand All @@ -478,7 +480,7 @@ def post(self):
article_title = args[articles.ARTICLE_TITLE]
# print(args[articles.PRIVATE])
private_article = args[articles.PRIVATE] or False

print("Checking arguements")
if not article_link and not article_body:
return {DATA:
f"Either {articles.ARTICLE_LINK} or {articles.ARTICLE_BODY} must be provided"}, HTTPStatus.BAD_REQUEST
Expand Down
81 changes: 81 additions & 0 deletions server/tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,87 @@ def test_fetch_users_articles_not_found(self, mock_filter, mock_get):
with TEST_CLIENT.session_transaction() as test_session:
test_session.pop('user_id')

#tests for fetching all submitted articles by a given user
@patch('userdata.articles.store_article_submission', return_value=(True, usrs._gen_id()), autospec=True)
@patch('userdata.users.get_user_if_logged_in', return_value=None, autospec=True)
def test_submit_articles_success(self, mock_filter, mock_get):
with TEST_CLIENT.session_transaction() as test_session:
test_session['user_id'] = usrs._gen_id()
route = ep.ARTICLES_EP + ep.SUBMIT_EP
resp = TEST_CLIENT.post(route, json = { articles.ARTICLE_LINK: 'link.com',
articles.ARTICLE_BODY: 'body', articles.ARTICLE_TITLE: 'title', articles.PRIVATE: 'True'})
assert resp.status_code == OK
with TEST_CLIENT.session_transaction() as test_session:
test_session.pop('user_id')

@patch('userdata.articles.store_article_submission', return_value=(True, usrs._gen_id()), autospec=True)
@patch('userdata.users.get_user_if_logged_in', return_value=None, autospec=True)
def test_submit_articles_not_login(self, mock_filter, mock_get):
route = ep.ARTICLES_EP + ep.SUBMIT_EP
resp = TEST_CLIENT.post(route, json = { articles.ARTICLE_LINK: 'link.com',
articles.ARTICLE_BODY: 'body', articles.ARTICLE_TITLE: 'title', articles.PRIVATE: 'True'})
assert resp.status_code == UNAUTHORIZED

@patch('userdata.articles.store_article_submission', return_value=(False, usrs._gen_id()), autospec=True)
@patch('userdata.users.get_user_if_logged_in', return_value=None, autospec=True)
def test_submit_articles_submission_failure(self, mock_filter, mock_get):
with TEST_CLIENT.session_transaction() as test_session:
test_session['user_id'] = usrs._gen_id()
route = ep.ARTICLES_EP + ep.SUBMIT_EP
resp = TEST_CLIENT.post(route, json = { articles.ARTICLE_LINK: 'link.com',
articles.ARTICLE_BODY: 'body', articles.ARTICLE_TITLE: 'title', articles.PRIVATE: 'True'})
assert resp.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
with TEST_CLIENT.session_transaction() as test_session:
test_session.pop('user_id')

@patch('userdata.articles.store_article_submission', return_value=(True, usrs._gen_id()), autospec=True)
@patch('userdata.users.get_user_if_logged_in', return_value=None, autospec=True)
def test_submit_articles_body_too_short(self, mock_filter, mock_get):
with TEST_CLIENT.session_transaction() as test_session:
test_session['user_id'] = usrs._gen_id()
route = ep.ARTICLES_EP + ep.SUBMIT_EP
resp = TEST_CLIENT.post(route, json = { articles.ARTICLE_LINK: '',
articles.ARTICLE_BODY: 'body', articles.ARTICLE_TITLE: 'title', articles.PRIVATE: 'True'})
assert resp.status_code == BAD_REQUEST
with TEST_CLIENT.session_transaction() as test_session:
test_session.pop('user_id')

@patch('userdata.articles.store_article_submission', return_value=(True, usrs._gen_id()), autospec=True)
@patch('userdata.users.get_user_if_logged_in', return_value=None, autospec=True)
def test_submit_articles_blank_submission(self, mock_filter, mock_get):
with TEST_CLIENT.session_transaction() as test_session:
test_session['user_id'] = usrs._gen_id()
route = ep.ARTICLES_EP + ep.SUBMIT_EP
resp = TEST_CLIENT.post(route, json = { articles.ARTICLE_LINK: '',
articles.ARTICLE_BODY: '', articles.ARTICLE_TITLE: 'title', articles.PRIVATE: 'True'})
assert resp.status_code == BAD_REQUEST
with TEST_CLIENT.session_transaction() as test_session:
test_session.pop('user_id')


@patch('userdata.articles.store_article_submission', return_value=(True, usrs._gen_id()), autospec=True)
@patch('userdata.users.get_user_if_logged_in', return_value=None, autospec=True)
def test_submit_articles_success_only_body(self, mock_filter, mock_get):
with TEST_CLIENT.session_transaction() as test_session:
test_session['user_id'] = usrs._gen_id()
route = ep.ARTICLES_EP + ep.SUBMIT_EP
resp = TEST_CLIENT.post(route, json = { articles.ARTICLE_LINK: '',
articles.ARTICLE_BODY: '1' * 1000, articles.ARTICLE_TITLE: 'title', articles.PRIVATE: 'True'})
assert resp.status_code == OK
with TEST_CLIENT.session_transaction() as test_session:
test_session.pop('user_id')

@patch('userdata.articles.store_article_submission', return_value=(True, usrs._gen_id()), autospec=True)
@patch('userdata.users.get_user_if_logged_in', return_value=None, autospec=True)
def test_submit_articles_success_body_too_long(self, mock_filter, mock_get):
with TEST_CLIENT.session_transaction() as test_session:
test_session['user_id'] = usrs._gen_id()
route = ep.ARTICLES_EP + ep.SUBMIT_EP
resp = TEST_CLIENT.post(route, json = { articles.ARTICLE_LINK: '',
articles.ARTICLE_BODY: '1' * 6000, articles.ARTICLE_TITLE: 'title', articles.PRIVATE: 'True'})
assert resp.status_code == BAD_REQUEST
with TEST_CLIENT.session_transaction() as test_session:
test_session.pop('user_id')
# @patch('userdata.users.add_user', side_effect=ValueError(), autospec=True)
# def test_users_bad_add(mock_add):
# """
Expand Down

0 comments on commit 7d8569d

Please sign in to comment.