67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
"""
|
|
This file (test_telegram.py) contains the functional tests for the `telegram` blueprint.
|
|
"""
|
|
import json
|
|
|
|
|
|
def test_add_telegram_not_logged_in(test_client, init_database):
|
|
"""
|
|
Test POST '/api/telegram'
|
|
|
|
User is not logged in
|
|
"""
|
|
response = test_client.post('/api/telegram')
|
|
assert response.status_code == 401
|
|
assert b'Unauthorized' in response.data
|
|
|
|
|
|
def test_add_telegram_user1_logged_in(test_client, init_database):
|
|
"""
|
|
Test POST '/api/telegram'
|
|
|
|
User1 is logged in
|
|
"""
|
|
response = test_client.post('/api/telegram', data=json.dumps(dict(telegram_user_id="12345678")),
|
|
headers={"Authorization": "Bearer {}".format(get_token(test_client, "user1@example.com", "password"))},
|
|
content_type='application/json')
|
|
assert response.status_code == 200
|
|
assert b'Successfully connected telegram user' in response.data
|
|
|
|
|
|
def test_add_telegram_user1_logged_in_user_data_missing(test_client, init_database):
|
|
"""
|
|
Test POST '/api/telegram'
|
|
|
|
User1 is logged in
|
|
telegram_user_id is missing
|
|
"""
|
|
response = test_client.post('/api/telegram', 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_telegram_user1_logged_in_user_data_empty(test_client, init_database):
|
|
"""
|
|
Test POST '/api/telegram'
|
|
|
|
User1 is logged in
|
|
telegram_user_id is empty
|
|
"""
|
|
response = test_client.post('/api/telegram', data=json.dumps(dict(telegram_user_id="")),
|
|
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 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 ""
|