routes.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import json
  2. import lc.config as c
  3. import lc.model as m
  4. import lc.request as r
  5. import lc.app as a
  6. class TestRoutes:
  7. def setup_method(self, _):
  8. c.db.init(":memory:")
  9. c.db.create_tables(m.MODELS)
  10. self.app = a.app.test_client()
  11. def teardown_method(self, _):
  12. c.db.close()
  13. def mk_user(self, name="gdritter", password="foo") -> m.User:
  14. return m.User.from_request(r.User(name=name, password=password,))
  15. def test_index(self):
  16. result = self.app.get("/")
  17. assert result.status == "200 OK"
  18. def test_successful_api_login(self):
  19. username = "gdritter"
  20. password = "bar"
  21. u = self.mk_user(password=password)
  22. result = self.app.post("/auth", json={"name": username, "password": password,})
  23. assert result.status == "200 OK"
  24. decoded_token = c.serializer.loads(result.json["token"])
  25. assert decoded_token["name"] == username
  26. assert decoded_token["password"] == password
  27. def test_failed_api_login(self):
  28. username = "gdritter"
  29. password = "bar"
  30. u = self.mk_user(password=password)
  31. result = self.app.post("/auth", json={"name": username, "password": "foo",})
  32. assert result.status == "403 FORBIDDEN"