share_test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import { assertEquals } from "@std/assert";
  2. import { find, insert } from "utils/db.ts";
  3. import { getCryptoString } from "utils/server.ts";
  4. import { getSetCookies } from "@std/http";
  5. let testCounter = 0;
  6. function getTestDbPath() {
  7. testCounter++;
  8. return `data/test_api_share_${testCounter}_${Date.now()}.db`;
  9. }
  10. function cleanup(dbPath: string) {
  11. try {
  12. Deno.removeSync(dbPath);
  13. } catch {
  14. // File may not exist
  15. }
  16. }
  17. function makeCtx(method: string, body?: object, cookie?: string) {
  18. const headers: Record<string, string> = {
  19. "Content-Type": "application/json",
  20. };
  21. if (cookie) headers["Cookie"] = cookie;
  22. const req = new Request("http://localhost", {
  23. method,
  24. headers,
  25. body: method === "GET" ? undefined : JSON.stringify(body || {}),
  26. });
  27. return { req };
  28. }
  29. async function seedUserAndToken(email: string, password: string) {
  30. const hashedPw = await getCryptoString(password, "MD5");
  31. const user = insert("User", {
  32. name: email.split("@")[0],
  33. email,
  34. password: hashedPw,
  35. });
  36. const userId = user[0]["id"] as string | number;
  37. const token = await getCryptoString(
  38. "share-token-" + userId + Date.now(),
  39. "MD5",
  40. );
  41. insert("Token", { token, user_id: userId });
  42. return { userId, token };
  43. }
  44. Deno.test("API share - share a post", async () => {
  45. const dbPath = getTestDbPath();
  46. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  47. try {
  48. const { userId, token } = await seedUserAndToken(
  49. "shareshare@example.com",
  50. "password",
  51. );
  52. insert("Post", {
  53. id: "share-post-1",
  54. title: "Share Me",
  55. content: "Content",
  56. user_id: userId,
  57. shared: 0,
  58. });
  59. const { handler } = await import("../../routes/api/share.tsx");
  60. const ctx = makeCtx("POST", {
  61. id: "share-post-1",
  62. shared: true,
  63. }, `pd-user-token=${token}`);
  64. const res = await handler.POST!(ctx as any);
  65. const body = await res.json();
  66. assertEquals(body.success, true);
  67. const post = find("Post", { id: "share-post-1" }, ["shared"]);
  68. assertEquals(post[0]["shared"], 1);
  69. } finally {
  70. Deno.env.delete("POSTDOWN_DB_PATH");
  71. cleanup(dbPath);
  72. }
  73. });
  74. Deno.test("API share - unshare a post", async () => {
  75. const dbPath = getTestDbPath();
  76. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  77. try {
  78. const { userId, token } = await seedUserAndToken(
  79. "shareunshare@example.com",
  80. "password",
  81. );
  82. insert("Post", {
  83. id: "unshare-post-1",
  84. title: "Unshare Me",
  85. content: "Content",
  86. user_id: userId,
  87. shared: 1,
  88. });
  89. const { handler } = await import("../../routes/api/share.tsx");
  90. const ctx = makeCtx("POST", {
  91. id: "unshare-post-1",
  92. shared: false,
  93. }, `pd-user-token=${token}`);
  94. const res = await handler.POST!(ctx as any);
  95. const body = await res.json();
  96. assertEquals(body.success, true);
  97. const post = find("Post", { id: "unshare-post-1" }, ["shared"]);
  98. assertEquals(post[0]["shared"], 0);
  99. } finally {
  100. Deno.env.delete("POSTDOWN_DB_PATH");
  101. cleanup(dbPath);
  102. }
  103. });
  104. Deno.test("API share - without token returns error", async () => {
  105. const dbPath = getTestDbPath();
  106. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  107. try {
  108. const { handler } = await import("../../routes/api/share.tsx");
  109. const ctx = makeCtx("POST", {
  110. id: "some-post",
  111. shared: true,
  112. });
  113. const res = await handler.POST!(ctx as any);
  114. const body = await res.json();
  115. assertEquals(body.success, false);
  116. } finally {
  117. Deno.env.delete("POSTDOWN_DB_PATH");
  118. cleanup(dbPath);
  119. }
  120. });
  121. Deno.test("API share - share another user's post returns error", async () => {
  122. const dbPath = getTestDbPath();
  123. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  124. try {
  125. const { userId: otherUserId } = await seedUserAndToken(
  126. "shareother@example.com",
  127. "password",
  128. );
  129. insert("Post", {
  130. id: "other-post-1",
  131. title: "Other's Post",
  132. content: "Content",
  133. user_id: otherUserId,
  134. shared: 0,
  135. });
  136. const { token: myToken } = await seedUserAndToken(
  137. "shareme@example.com",
  138. "password2",
  139. );
  140. const { handler } = await import("../../routes/api/share.tsx");
  141. const ctx = makeCtx("POST", {
  142. id: "other-post-1",
  143. shared: true,
  144. }, `pd-user-token=${myToken}`);
  145. const res = await handler.POST!(ctx as any);
  146. const body = await res.json();
  147. assertEquals(body.success, false);
  148. } finally {
  149. Deno.env.delete("POSTDOWN_DB_PATH");
  150. cleanup(dbPath);
  151. }
  152. });
  153. Deno.test("API share - share with password stores hashed password", async () => {
  154. const dbPath = getTestDbPath();
  155. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  156. try {
  157. const { userId, token } = await seedUserAndToken(
  158. "sharepw@example.com",
  159. "password",
  160. );
  161. insert("Post", {
  162. id: "pw-post-1",
  163. title: "Password Post",
  164. content: "Secret content",
  165. user_id: userId,
  166. shared: 0,
  167. });
  168. const { handler } = await import("../../routes/api/share.tsx");
  169. const ctx = makeCtx("POST", {
  170. id: "pw-post-1",
  171. shared: true,
  172. password: "mypassword",
  173. }, `pd-user-token=${token}`);
  174. const res = await handler.POST!(ctx as any);
  175. const body = await res.json();
  176. assertEquals(body.success, true);
  177. const post = find("Post", { id: "pw-post-1" }, [
  178. "shared",
  179. "share_password",
  180. ]);
  181. assertEquals(post[0]["shared"], 1);
  182. const expectedHash = await getCryptoString("mypassword", "MD5");
  183. assertEquals(post[0]["share_password"], expectedHash);
  184. } finally {
  185. Deno.env.delete("POSTDOWN_DB_PATH");
  186. cleanup(dbPath);
  187. }
  188. });
  189. Deno.test("API share - unshare clears password", async () => {
  190. const dbPath = getTestDbPath();
  191. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  192. try {
  193. const { userId, token } = await seedUserAndToken(
  194. "unshpw@example.com",
  195. "password",
  196. );
  197. const hashedPw = await getCryptoString("oldpw", "MD5");
  198. insert("Post", {
  199. id: "pw-post-2",
  200. title: "PW Post",
  201. content: "Content",
  202. user_id: userId,
  203. shared: 1,
  204. share_password: hashedPw,
  205. });
  206. const { handler } = await import("../../routes/api/share.tsx");
  207. const ctx = makeCtx("POST", {
  208. id: "pw-post-2",
  209. shared: false,
  210. }, `pd-user-token=${token}`);
  211. const res = await handler.POST!(ctx as any);
  212. const body = await res.json();
  213. assertEquals(body.success, true);
  214. const post = find("Post", { id: "pw-post-2" }, [
  215. "shared",
  216. "share_password",
  217. ]);
  218. assertEquals(post[0]["shared"], 0);
  219. assertEquals(post[0]["share_password"], "");
  220. } finally {
  221. Deno.env.delete("POSTDOWN_DB_PATH");
  222. cleanup(dbPath);
  223. }
  224. });
  225. Deno.test("API share-verify - correct password returns success and sets cookie", async () => {
  226. const dbPath = getTestDbPath();
  227. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  228. try {
  229. const hashedPw = await getCryptoString("secret123", "MD5");
  230. const { userId } = await seedUserAndToken(
  231. "verifypw@example.com",
  232. "password",
  233. );
  234. insert("Post", {
  235. id: "verify-post-1",
  236. title: "Protected",
  237. content: "Secret",
  238. user_id: userId,
  239. shared: 1,
  240. share_password: hashedPw,
  241. });
  242. const { handler } = await import("../../routes/api/share-verify.tsx");
  243. const ctx = makeCtx("POST", {
  244. id: "verify-post-1",
  245. password: "secret123",
  246. });
  247. const res = await handler.POST!(ctx as any);
  248. const body = await res.json();
  249. assertEquals(body.success, true);
  250. const setCookies = getSetCookies(res.headers);
  251. const shareCookie = setCookies.find((c) =>
  252. c.name === "pd-share-verify-post-1"
  253. );
  254. const expectedToken = await getCryptoString(
  255. "verify-post-1" + hashedPw,
  256. "MD5",
  257. );
  258. assertEquals(shareCookie?.value, expectedToken);
  259. } finally {
  260. Deno.env.delete("POSTDOWN_DB_PATH");
  261. cleanup(dbPath);
  262. }
  263. });
  264. Deno.test("API share-verify - wrong password returns error", async () => {
  265. const dbPath = getTestDbPath();
  266. Deno.env.set("POSTDOWN_DB_PATH", dbPath);
  267. try {
  268. const hashedPw = await getCryptoString("secret123", "MD5");
  269. const { userId } = await seedUserAndToken(
  270. "verifywrong@example.com",
  271. "password",
  272. );
  273. insert("Post", {
  274. id: "verify-post-2",
  275. title: "Protected",
  276. content: "Secret",
  277. user_id: userId,
  278. shared: 1,
  279. share_password: hashedPw,
  280. });
  281. const { handler } = await import("../../routes/api/share-verify.tsx");
  282. const ctx = makeCtx("POST", {
  283. id: "verify-post-2",
  284. password: "wrongpassword",
  285. });
  286. const res = await handler.POST!(ctx as any);
  287. const body = await res.json();
  288. assertEquals(body.success, false);
  289. } finally {
  290. Deno.env.delete("POSTDOWN_DB_PATH");
  291. cleanup(dbPath);
  292. }
  293. });