post_test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import { assertEquals } from "@std/assert";
  2. import { find, insert } from "utils/db.ts";
  3. import { getCryptoString } from "utils/server.ts";
  4. let testCounter = 0;
  5. function getTestDbPath() {
  6. testCounter++;
  7. return `data/test_api_post_${testCounter}_${Date.now()}.db`;
  8. }
  9. function cleanup(dbPath: string) {
  10. try {
  11. Deno.removeSync(dbPath);
  12. } catch {
  13. // File may not exist
  14. }
  15. }
  16. function makeCtx(method: string, body?: object, cookie?: string) {
  17. const headers: Record<string, string> = {
  18. "Content-Type": "application/json",
  19. };
  20. if (cookie) headers["Cookie"] = cookie;
  21. const initMethod = method === "GET" && body ? "POST" : method;
  22. const req = new Request("http://localhost", {
  23. method: initMethod,
  24. headers,
  25. body: body ? JSON.stringify(body) : undefined,
  26. });
  27. if (method === "GET" && body) {
  28. Object.defineProperty(req, "method", { value: "GET" });
  29. }
  30. return { req };
  31. }
  32. async function seedUserAndToken(email: string, password: string) {
  33. const hashedPw = await getCryptoString(password, "MD5");
  34. const user = insert("User", {
  35. name: email.split("@")[0],
  36. email,
  37. password: hashedPw,
  38. });
  39. const userId = user[0]["id"] as string | number;
  40. const token = await getCryptoString(
  41. "post-token-" + userId + Date.now(),
  42. "MD5",
  43. );
  44. insert("Token", { token, user_id: userId });
  45. return { userId, token };
  46. }
  47. Deno.test("API post - create a new post", async () => {
  48. const dbPath = getTestDbPath();
  49. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  50. try {
  51. const { token } = await seedUserAndToken(
  52. "postcreate@example.com",
  53. "password",
  54. );
  55. const { handler } = await import("../../routes/api/post.tsx");
  56. const ctx = makeCtx("POST", {
  57. title: "My First Post",
  58. content: "# Hello World",
  59. }, `pd-user-token=${token}`);
  60. const res = await handler.POST!(ctx as any);
  61. const body = await res.json();
  62. assertEquals(body.success, true);
  63. assertEquals(typeof body.data, "string");
  64. } finally {
  65. Deno.env.delete("POSTDOWN_DB_PATH");
  66. cleanup(dbPath);
  67. }
  68. });
  69. Deno.test("API post - create without token returns error", async () => {
  70. const dbPath = getTestDbPath();
  71. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  72. try {
  73. const { handler } = await import("../../routes/api/post.tsx");
  74. const ctx = makeCtx("POST", {
  75. title: "Unauthorized Post",
  76. content: "Should fail",
  77. });
  78. const res = await handler.POST!(ctx as any);
  79. const body = await res.json();
  80. assertEquals(body.success, false);
  81. } finally {
  82. Deno.env.delete("POSTDOWN_DB_PATH");
  83. cleanup(dbPath);
  84. }
  85. });
  86. Deno.test("API post - get own post", async () => {
  87. const dbPath = getTestDbPath();
  88. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  89. try {
  90. const { userId, token } = await seedUserAndToken(
  91. "postget@example.com",
  92. "password",
  93. );
  94. insert("Post", {
  95. id: "test-post-1",
  96. title: "Test Post",
  97. content: "Some content",
  98. user_id: userId,
  99. shared: 0,
  100. });
  101. const { handler } = await import("../../routes/api/post.tsx");
  102. const ctx = makeCtx("GET", { id: "test-post-1" }, `pd-user-token=${token}`);
  103. const res = await handler.GET!(ctx as any);
  104. const body = await res.json();
  105. assertEquals(body.success, true);
  106. assertEquals(body.data.title, "Test Post");
  107. assertEquals(body.data.content, "Some content");
  108. } finally {
  109. Deno.env.delete("POSTDOWN_DB_PATH");
  110. cleanup(dbPath);
  111. }
  112. });
  113. Deno.test("API post - get shared post without token", async () => {
  114. const dbPath = getTestDbPath();
  115. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  116. try {
  117. const { userId } = await seedUserAndToken(
  118. "postshared@example.com",
  119. "password",
  120. );
  121. insert("Post", {
  122. id: "shared-post-1",
  123. title: "Shared Post",
  124. content: "Public content",
  125. user_id: userId,
  126. shared: 1,
  127. });
  128. const { handler } = await import("../../routes/api/post.tsx");
  129. const ctx = makeCtx("GET", { id: "shared-post-1" });
  130. const res = await handler.GET!(ctx as any);
  131. const body = await res.json();
  132. assertEquals(body.success, true);
  133. assertEquals(body.data.title, "Shared Post");
  134. } finally {
  135. Deno.env.delete("POSTDOWN_DB_PATH");
  136. cleanup(dbPath);
  137. }
  138. });
  139. Deno.test("API post - get non-shared post without token returns error", async () => {
  140. const dbPath = getTestDbPath();
  141. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  142. try {
  143. const { userId } = await seedUserAndToken(
  144. "postprivate@example.com",
  145. "password",
  146. );
  147. insert("Post", {
  148. id: "private-post-1",
  149. title: "Private Post",
  150. content: "Secret content",
  151. user_id: userId,
  152. shared: 0,
  153. });
  154. const { handler } = await import("../../routes/api/post.tsx");
  155. const ctx = makeCtx("GET", { id: "private-post-1" });
  156. const res = await handler.GET!(ctx as any);
  157. const body = await res.json();
  158. assertEquals(body.success, false);
  159. } finally {
  160. Deno.env.delete("POSTDOWN_DB_PATH");
  161. cleanup(dbPath);
  162. }
  163. });
  164. Deno.test("API post - update post title", async () => {
  165. const dbPath = getTestDbPath();
  166. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  167. try {
  168. const { userId, token } = await seedUserAndToken(
  169. "postupdate@example.com",
  170. "password",
  171. );
  172. insert("Post", {
  173. id: "update-post-1",
  174. title: "Old Title",
  175. content: "Content",
  176. user_id: userId,
  177. shared: 0,
  178. });
  179. const { handler } = await import("../../routes/api/post.tsx");
  180. const ctx = makeCtx("PUT", {
  181. id: "update-post-1",
  182. title: "New Title",
  183. }, `pd-user-token=${token}`);
  184. const res = await handler.PUT!(ctx as any);
  185. const body = await res.json();
  186. assertEquals(body.success, true);
  187. const post = find("Post", { id: "update-post-1" }, ["title"]);
  188. assertEquals(post[0]["title"], "New Title");
  189. } finally {
  190. Deno.env.delete("POSTDOWN_DB_PATH");
  191. cleanup(dbPath);
  192. }
  193. });
  194. Deno.test("API post - update without token returns error", async () => {
  195. const dbPath = getTestDbPath();
  196. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  197. try {
  198. const { handler } = await import("../../routes/api/post.tsx");
  199. const ctx = makeCtx("PUT", {
  200. id: "some-post",
  201. title: "Hacked Title",
  202. });
  203. const res = await handler.PUT!(ctx as any);
  204. const body = await res.json();
  205. assertEquals(body.success, false);
  206. } finally {
  207. Deno.env.delete("POSTDOWN_DB_PATH");
  208. cleanup(dbPath);
  209. }
  210. });
  211. Deno.test("API post - delete own post", async () => {
  212. const dbPath = getTestDbPath();
  213. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  214. try {
  215. const { userId, token } = await seedUserAndToken(
  216. "postdelete@example.com",
  217. "password",
  218. );
  219. insert("Post", {
  220. id: "delete-post-1",
  221. title: "Delete Me",
  222. content: "Bye",
  223. user_id: userId,
  224. shared: 0,
  225. });
  226. const { handler } = await import("../../routes/api/post.tsx");
  227. const ctx = makeCtx("DELETE", {
  228. id: "delete-post-1",
  229. }, `pd-user-token=${token}`);
  230. const res = await handler.DELETE!(ctx as any);
  231. const body = await res.json();
  232. assertEquals(body.success, true);
  233. const post = find("Post", { id: "delete-post-1" }, ["id"]);
  234. assertEquals(post.length, 0);
  235. } finally {
  236. Deno.env.delete("POSTDOWN_DB_PATH");
  237. cleanup(dbPath);
  238. }
  239. });
  240. Deno.test("API post - delete without token returns error", async () => {
  241. const dbPath = getTestDbPath();
  242. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  243. try {
  244. const { handler } = await import("../../routes/api/post.tsx");
  245. const ctx = makeCtx("DELETE", { id: "some-post" });
  246. const res = await handler.DELETE!(ctx as any);
  247. const body = await res.json();
  248. assertEquals(body.success, false);
  249. } finally {
  250. Deno.env.delete("POSTDOWN_DB_PATH");
  251. cleanup(dbPath);
  252. }
  253. });