def validate_tokens(json): assert "access_token" in json assert "refresh_token" in json access_token = Token.select().where(Token.token_type == "access").get() assert json["access_token"]["token"] == access_token.token assert (json["access_token"]["valid_until"] > datetime.datetime.now().timestamp() + 60 * 60 * 24 * 29) assert (json["access_token"]["valid_until"] < datetime.datetime.now().timestamp() + 60 * 60 * 24 * 31) assert User.get() == access_token.user refresh_token = Token.select().where(Token.token_type == "refresh").get() assert json["refresh_token"]["token"] == refresh_token.token assert (json["refresh_token"]["valid_until"] > datetime.datetime.now().timestamp() + 60 * 60 * 24 * 89) assert (json["refresh_token"]["valid_until"] < datetime.datetime.now().timestamp() + 60 * 60 * 24 * 91) assert User.get() == refresh_token.user assert refresh_token.token != access_token.token
def test_registration_failure(client): rv = client.post("/users/register/", json={}) assert rv.status_code == 400 assert rv.json["success"] == 0 assert "token" not in rv.json assert User.select().count() == 0 assert Token.select().count() == 0
def test_auth_wrong_user(client, user): rv = client.post("/users/login/", json={ "username": "******", "password": "******" }) assert rv.status_code == 401 assert rv.json["success"] == 0 assert "token" not in rv.json assert Token.select().count() == 0
def test_registration_not_all_data(client): rv = client.post("/users/register/", json={ "username": "******", "password": "******" }) assert rv.status_code == 400 assert rv.json["success"] == 0 assert "token" not in rv.json assert rv.json["error"]["code"] == 5 assert User.select().count() == 0 assert Token.select().count() == 0
def migration_v4(db, migrator: SchemaMigrator): from src.model.models import Token with db.atomic(): migrate( migrator.add_column("token", "token_type", CharField(default="")), ) query = Token.select() for t in query: if t.is_refresh_token: t.token_type = "refresh" else: t.token_type = "access" t.save()
def test_registration_email_busy(client, user): rv = client.post( "/users/register/", json={ "username": "******", "password": "******", "name": "name", "email": "asd", }, ) assert rv.status_code == 400 assert rv.json["success"] == 0 assert "token" not in rv.json assert rv.json["error"]["code"] == 12 assert User.select().count() == 1 assert Token.select().count() == 0
def test_auth_failure(client, user): rv = client.post("/users/login/", json={}) assert rv.status_code == 400 assert rv.json["success"] == 0 assert "token" not in rv.json assert Token.select().count() == 0