post_test.ts 7.5 KB

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