Tests
- Improved directory structure - Added functional and unit tests
This commit is contained in:
191
api/tests/functional/test_keyword.py
Normal file
191
api/tests/functional/test_keyword.py
Normal file
@@ -0,0 +1,191 @@
|
||||
"""
|
||||
This file (test_keyword.py) contains the functional tests for the `keyword` blueprint.
|
||||
"""
|
||||
import json
|
||||
|
||||
|
||||
def test_add_keyword_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/keyword'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.post('/api/keyword')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_add_keyword_user1_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
"""
|
||||
response = test_client.post('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully added keyword' in response.data
|
||||
|
||||
|
||||
def test_add_keyword_user1_logged_in_but_keyword_exist(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Add keyword two times
|
||||
"""
|
||||
test_client.post('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
response = test_client.post('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 500
|
||||
assert b'Keyword already exist for this user' in response.data
|
||||
|
||||
|
||||
def test_add_keyword_user1_logged_in_but_keyword_missing(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Keyword is missing in post data
|
||||
"""
|
||||
response = test_client.post('/api/keyword', data=json.dumps(dict()),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_add_keyword_user1_logged_in_but_keyword_empty(test_client, init_database):
|
||||
"""
|
||||
Test POST '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Keyword is empty in post data
|
||||
"""
|
||||
response = test_client.post('/api/keyword', data=json.dumps(dict(keyword="")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_get_keyword_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/keyword'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.get('/api/keywords')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_get_keyword_user1_logged_in_empty_response(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Empty response
|
||||
"""
|
||||
response = test_client.get('/api/keywords', headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))})
|
||||
assert response.status_code == 200
|
||||
assert b'"data":[' in response.data
|
||||
|
||||
|
||||
def test_get_keyword_user1_logged_in_response_data(test_client, init_database):
|
||||
"""
|
||||
Test GET '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Create some keywords for user1
|
||||
"""
|
||||
test_client.post('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
response = test_client.get('/api/keywords', headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))})
|
||||
assert response.status_code == 200
|
||||
assert b'"data":[' in response.data
|
||||
|
||||
|
||||
def test_delete_keyword_not_logged_in(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/keyword'
|
||||
|
||||
User is not logged in
|
||||
"""
|
||||
response = test_client.delete('/api/keyword')
|
||||
assert response.status_code == 401
|
||||
assert b'Unauthorized' in response.data
|
||||
|
||||
|
||||
def test_delete_keyword_user1_logged_in_but_empty_keyword(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Keyword empty in in delete data
|
||||
"""
|
||||
response = test_client.delete('/api/keyword', data=json.dumps(dict(keyword="")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_delete_keyword_user1_logged_in_but_missing_keyword(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Keyword missing in in delete data
|
||||
"""
|
||||
response = test_client.delete('/api/keyword', data=json.dumps(dict()),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 400
|
||||
assert b'missing' in response.data
|
||||
|
||||
|
||||
def test_delete_keyword_user1_logged_in_keyword_exists(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Keyword exists
|
||||
"""
|
||||
test_client.post('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
response = test_client.delete('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 200
|
||||
assert b'Successfully removed keyword' in response.data
|
||||
|
||||
|
||||
def test_delete_keyword_user1_logged_in_keyword_not_exists(test_client, init_database):
|
||||
"""
|
||||
Test DELETE '/api/keyword'
|
||||
|
||||
User1 is logged in
|
||||
Keyword doesn't exists
|
||||
"""
|
||||
response = test_client.delete('/api/keyword', data=json.dumps(dict(keyword="DTEGY")),
|
||||
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
||||
content_type='application/json')
|
||||
assert response.status_code == 500
|
||||
assert b'Keyword doesn\'t exist for this user' in response.data
|
||||
|
||||
|
||||
def get_token(test_client, email, password):
|
||||
response = test_client.post('/api/user/login', data=json.dumps(dict(email=email, password=password)), content_type='application/json')
|
||||
|
||||
if "data" in json.loads(response.data):
|
||||
if "token" in json.loads(response.data)["data"]:
|
||||
return json.loads(response.data)["data"]["token"]
|
||||
|
||||
return ""
|
Reference in New Issue
Block a user