crypto.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const { eq, ok, notOk } = require('../../helpers/assert');
  2. const { makeNetwork, makePeer } = require('../../helpers/setup');
  3. describe('forum: crypto', (t) => {
  4. t('public forum is plaintext and any peer can read', async () => {
  5. const net = makeNetwork();
  6. const A = makePeer(net); A.setActor();
  7. const r = await A.use('forum').createForum('GENERAL', 'Hello world', 'open thread', false);
  8. ok(r && r.key);
  9. const f = await A.use('forum').getForumById(r.key);
  10. eq(f.title, 'Hello world');
  11. notOk(f.isPrivate);
  12. });
  13. t('private forum encrypts title/text; author can decrypt', async () => {
  14. const net = makeNetwork();
  15. const A = makePeer(net); A.setActor();
  16. const r = await A.use('forum').createForum('GENERAL', 'Secret thread', 'sensitive', true);
  17. const f = await A.use('forum').getForumById(r.key);
  18. eq(f.title, 'Secret thread');
  19. eq(f.isPrivate, true);
  20. });
  21. t('outsider without key cannot decrypt private forum', async () => {
  22. const net = makeNetwork();
  23. const A = makePeer(net); const B = makePeer(net);
  24. A.setActor();
  25. const r = await A.use('forum').createForum('GENERAL', 'Members only', 'private text', true);
  26. B.setActor();
  27. let threw = false;
  28. try { await B.use('forum').getForumById(r.key); } catch (_) { threw = true; }
  29. ok(threw);
  30. });
  31. t('outsider redeems forum invite and can read', async () => {
  32. const net = makeNetwork();
  33. const A = makePeer(net); const B = makePeer(net);
  34. A.setActor();
  35. const r = await A.use('forum').createForum('GENERAL', 'Invite-only', 'private', true);
  36. const { code } = await A.use('forum').generateInvite(r.key);
  37. eq(typeof code, 'string');
  38. eq(code.length, 32);
  39. B.setActor();
  40. const result = await B.use('forum').joinByInvite(code);
  41. eq(result.ok, true);
  42. const f = await B.use('forum').getForumById(r.key);
  43. eq(f.title, 'Invite-only');
  44. eq(f.isPrivate, true);
  45. });
  46. t('private forum rejects invite generation by non-author', async () => {
  47. const net = makeNetwork();
  48. const A = makePeer(net); const B = makePeer(net);
  49. A.setActor();
  50. const r = await A.use('forum').createForum('GENERAL', 'A', 'a', true);
  51. B.setActor();
  52. let threw = false;
  53. try { await B.use('forum').generateInvite(r.key); } catch (_) { threw = true; }
  54. ok(threw);
  55. });
  56. t('public forum invite generation is rejected', async () => {
  57. const net = makeNetwork();
  58. const A = makePeer(net); A.setActor();
  59. const r = await A.use('forum').createForum('GENERAL', 'A', 'a', false);
  60. let threw = false;
  61. try { await A.use('forum').generateInvite(r.key); } catch (_) { threw = true; }
  62. ok(threw);
  63. });
  64. t('private forum reply encrypts content with the same key', async () => {
  65. const net = makeNetwork();
  66. const A = makePeer(net); A.setActor();
  67. const r = await A.use('forum').createForum('GENERAL', 'Discussion', 'topic', true);
  68. const reply = await A.use('forum').addMessageToForum(r.key, { text: 'a reply' });
  69. ok(reply);
  70. const data = await A.use('forum').getMessagesByForumId(r.key);
  71. ok(data);
  72. const found = (data.messages || []).find(rp => rp.text === 'a reply');
  73. ok(found, 'reply text should decrypt for the author');
  74. eq(data.total, 1);
  75. });
  76. });