From 00619cbd05dd7c3f519b4a04ea48a6bf73fd41ab Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Tue, 22 Jun 2021 17:35:27 +0200 Subject: [PATCH] fix db error on second login --- ki/auth.py | 2 +- ki/models.py | 2 +- ki/test/test_login_endpoint.py | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 ki/test/test_login_endpoint.py diff --git a/ki/auth.py b/ki/auth.py index b5e346f..569ba27 100644 --- a/ki/auth.py +++ b/ki/auth.py @@ -24,7 +24,7 @@ def auth(username, password): if user is None: user = User(auth_id=username) db.session.add(user) - + token = Token(token=str(uuid.uuid4()), user=user) db.session.add(token) db.session.commit() diff --git a/ki/models.py b/ki/models.py index 356aa79..77014f6 100644 --- a/ki/models.py +++ b/ki/models.py @@ -13,7 +13,7 @@ class User(db.Model): auth_id = Column(String(50), nullable=False, unique=True) profile_id = Column(Integer, ForeignKey("profile.id"), nullable=True) - tokens = relationship("Token", uselist=False, back_populates="user") + tokens = relationship("Token", back_populates="user") profile = relationship("Profile", back_populates="user") def to_dict(self): diff --git a/ki/test/test_login_endpoint.py b/ki/test/test_login_endpoint.py new file mode 100644 index 0000000..86c48c2 --- /dev/null +++ b/ki/test/test_login_endpoint.py @@ -0,0 +1,39 @@ +from alembic import command +import json +import unittest + +from app import app, migrate +from ki.commands import seed + + +class TestLoginEndpoint(unittest.TestCase): + def setUp(self): + app.debug = True + app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:" + self.client = app.test_client() + + with app.app_context(): + config = migrate.get_config() + command.upgrade(config, "head") + + seed() + + def test_login(self): + response1_data = self.login() + response2_data = self.login() + self.assertNotEqual(response1_data["token"], response2_data["token"]) + + def login(self): + response = self.client.post("/users/login", + data=json.dumps({ + "username": "peter", + "password": "geheim" + }), + content_type="application/json") + self.assertEqual(response.status_code, 200) + self.assertIn("token", response.json) + return response.json + + +if __name__ == "main": + unittest.main()