app-MAIL-temp/tests/api/test_user_info.py

124 lines
3.0 KiB
Python
Raw Normal View History

2020-01-05 22:48:38 +01:00
from flask import url_for
from app.db import Session
2020-05-09 20:49:38 +02:00
from app.models import User, ApiKey
from tests.utils import login
2020-01-05 22:48:38 +01:00
2020-01-30 07:20:32 +01:00
def test_user_in_trial(flask_client):
2020-01-05 22:48:38 +01:00
user = User.create(
email="a@b.c", password="password", name="Test User", activated=True
)
Session.commit()
2020-01-05 22:48:38 +01:00
# create api_key
api_key = ApiKey.create(user.id, "for test")
Session.commit()
2020-01-05 22:48:38 +01:00
r = flask_client.get(
url_for("api.user_info"), headers={"Authentication": api_key.code}
)
assert r.status_code == 200
2020-03-18 19:08:16 +01:00
assert r.json == {
"is_premium": True,
"name": "Test User",
"email": "a@b.c",
"in_trial": True,
"profile_picture_url": None,
2020-03-18 19:08:16 +01:00
}
2020-01-05 22:48:38 +01:00
def test_wrong_api_key(flask_client):
r = flask_client.get(
url_for("api.user_info"), headers={"Authentication": "Invalid code"}
)
assert r.status_code == 401
assert r.json == {"error": "Wrong api key"}
2020-07-04 11:41:31 +02:00
def test_create_api_key(flask_client):
# create user, user is activated
User.create(email="a@b.c", password="password", name="Test User", activated=True)
Session.commit()
2020-07-04 11:41:31 +02:00
# login user
flask_client.post(
url_for("auth.login"),
data={"email": "a@b.c", "password": "password"},
follow_redirects=True,
)
# create api key
r = flask_client.post(url_for("api.create_api_key"), json={"device": "Test device"})
assert r.status_code == 201
assert r.json["api_key"]
2020-07-04 12:10:04 +02:00
def test_logout(flask_client):
# create user, user is activated
User.create(email="a@b.c", password="password", name="Test User", activated=True)
Session.commit()
2020-07-04 12:10:04 +02:00
# login user
flask_client.post(
url_for("auth.login"),
data={"email": "a@b.c", "password": "password"},
follow_redirects=True,
)
# logout
2020-08-27 10:20:48 +02:00
r = flask_client.get(
url_for("auth.logout"),
follow_redirects=True,
)
2020-07-04 12:10:04 +02:00
assert r.status_code == 200
2020-10-28 17:12:21 +01:00
def test_change_profile_picture(flask_client):
user = login(flask_client)
assert not user.profile_picture_id
# <<< Set the profile picture >>>
img_base64 = """iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="""
r = flask_client.patch(
"/api/user_info",
json={"profile_picture": img_base64},
)
assert r.status_code == 200
assert r.json["profile_picture_url"] is not None
user = User.get(user.id)
assert user.profile_picture_id
# <<< remove the profile picture >>>
r = flask_client.patch(
"/api/user_info",
json={"profile_picture": None},
)
assert r.status_code == 200
assert r.json["profile_picture_url"] is None
user = User.get(user.id)
assert not user.profile_picture_id
def test_change_name(flask_client):
user = login(flask_client)
assert user.name != "new name"
r = flask_client.patch(
"/api/user_info",
json={"name": "new name"},
)
assert r.status_code == 200
assert r.json["name"] == "new name"
assert user.name == "new name"