routes.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import os
  2. import json
  3. os.environ["LC_DB_PATH"] = ":memory:"
  4. os.environ["LC_SECRET_KEY"] = "TEST_KEY"
  5. os.environ["LC_APP_PATH"] = "localhost"
  6. import lc.config as c
  7. import lc.model as m
  8. import lc.request as r
  9. import lc.app as a
  10. class TestRoutes:
  11. def setup_method(self, _):
  12. c.app.in_memory_db()
  13. m.create_tables()
  14. self.app = a.app.test_client()
  15. def teardown_method(self, _):
  16. c.app.close_db()
  17. def mk_user(self, username="gdritter", password="foo") -> m.User:
  18. return m.User.from_request(
  19. r.User(
  20. name=username,
  21. password=password,
  22. )
  23. )
  24. def test_index(self):
  25. result = self.app.get("/")
  26. assert result.status == "200 OK"
  27. def test_successful_api_login(self):
  28. username = "gdritter"
  29. password = "bar"
  30. u = self.mk_user(username=username, password=password)
  31. result = self.app.post("/auth", json={"name": username, "password": password})
  32. assert result.status == "200 OK"
  33. decoded_token = c.app.load_token(result.json["token"])
  34. assert decoded_token["name"] == username
  35. def test_failed_api_login(self):
  36. username = "gdritter"
  37. password = "bar"
  38. u = self.mk_user(username=username, password=password)
  39. result = self.app.post("/auth", json={"name": username, "password": "foo"})
  40. assert result.status == "403 FORBIDDEN"
  41. def test_successful_web_login(self):
  42. username = "gdritter"
  43. password = "bar"
  44. u = self.mk_user(username=username, password=password)
  45. result = self.app.post(
  46. "/auth",
  47. data={"username": username, "password": password},
  48. follow_redirects=True,
  49. )
  50. assert result.status == "200 OK"
  51. def test_failed_web_login(self):
  52. username = "gdritter"
  53. password = "bar"
  54. u = self.mk_user(username=username, password=password)
  55. result = self.app.post("/auth", data={"username": username, "password": "foo"})
  56. assert result.status == "403 FORBIDDEN"
  57. def test_successful_api_add_link(self):
  58. password = "foo"
  59. u = self.mk_user(password=password)
  60. result = self.app.post("/auth", json={"name": u.name, "password": password})
  61. assert result.status == "200 OK"
  62. token = result.json["token"]
  63. result = self.app.post(
  64. f"/u/{u.name}/l",
  65. json={
  66. "url": "http://example.com/",
  67. "name": "Example Dot Com",
  68. "description": "Some Description",
  69. "private": False,
  70. "tags": ["website"],
  71. },
  72. headers={"Authorization": f"Bearer {token}"},
  73. )
  74. assert result.status == "200 OK"
  75. assert result.json["url"] == "http://example.com/"
  76. def test_no_permissions_api_add_link(self):
  77. # create a user who owns a link collection
  78. owner = self.mk_user()
  79. password = "foo"
  80. # and another user who should not be able to post to it
  81. interloper = self.mk_user(username="interloper", password=password)
  82. # authenticate as interloper
  83. result = self.app.post(
  84. "/auth", json={"name": interloper.name, "password": password}
  85. )
  86. assert result.status == "200 OK"
  87. token = result.json["token"]
  88. # try to add a link to owner's collection
  89. result = self.app.post(
  90. f"/u/{owner.name}/l",
  91. json={
  92. "url": "http://example.com/",
  93. "name": "Example Dot Com",
  94. "description": "Some Description",
  95. "private": False,
  96. "tags": ["website"],
  97. },
  98. headers={"Authorization": f"Bearer {token}"},
  99. )
  100. assert result.status == "403 FORBIDDEN"
  101. def test_successful_api_delete_link(self):
  102. password = "foo"
  103. u = self.mk_user(password=password)
  104. result = self.app.post("/auth", json={"name": u.name, "password": password})
  105. assert result.status == "200 OK"
  106. token = result.json["token"]
  107. sample_url = "http://example.com/"
  108. result = self.app.post(
  109. f"/u/{u.name}/l",
  110. json={
  111. "url": sample_url,
  112. "name": "Example Dot Com",
  113. "description": "Some Description",
  114. "private": False,
  115. "tags": ["website"],
  116. },
  117. )
  118. link_id = result.json["id"]
  119. # this should be fine
  120. check_link = self.app.get(
  121. f"/u/{u.name}/l/{link_id}",
  122. headers={"Content-Type": "application/json"},
  123. )
  124. assert check_link.status == "200 OK"
  125. assert check_link.json["url"] == sample_url
  126. # delete the link
  127. delete_link = self.app.delete(
  128. f"/u/{u.name}/l/{link_id}",
  129. headers={"Authorization": f"Bearer {token}"},
  130. )
  131. assert delete_link.status == "200 OK"
  132. # make sure it is gone
  133. bad_result = self.app.get(
  134. f"/u/{u.name}/l/{link_id}",
  135. headers={"Content-Type": "application/json"},
  136. )
  137. assert bad_result.status == "404 NOT FOUND"