shops.test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const { eq, ok } = require('../../helpers/assert');
  2. const { makeNetwork, makePeer } = require('../../helpers/setup');
  3. describe('shops: create + list + product + buy', (t) => {
  4. t('A creates a shop', async () => {
  5. const net = makeNetwork(); const A = makePeer(net); A.setActor();
  6. const r = await A.use('shops').createShop('Alice Shop', 'short desc', 'long', null, 'https://shop.example', 'Madrid', ['retail'], 'OPEN', '');
  7. ok(r);
  8. const list = await A.use('shops').listAll({ filter: 'all', viewerId: A.keypair.id });
  9. ok(list.length >= 1);
  10. eq(list[0].title, 'Alice Shop');
  11. });
  12. t('A creates a product in own shop', async () => {
  13. const net = makeNetwork(); const A = makePeer(net); A.setActor();
  14. const shop = await A.use('shops').createShop('S', '', '', null, '', '', [], 'OPEN', '');
  15. const prod = await A.use('shops').createProduct(shop.key, 'Widget', 'desc', null, 10, 5, false);
  16. ok(prod);
  17. const list = await A.use('shops').listProducts(shop.key);
  18. ok(Array.isArray(list));
  19. ok(list.length >= 1);
  20. });
  21. t('A updates own shop', async () => {
  22. const net = makeNetwork(); const A = makePeer(net); A.setActor();
  23. const shop = await A.use('shops').createShop('Shop', '', '', null, '', '', [], 'OPEN', '');
  24. await A.use('shops').updateShopById(shop.key, { title: 'Shop V2' });
  25. const list = await A.use('shops').listAll({ filter: 'all', viewerId: A.keypair.id });
  26. const updated = list.find(s => s.title === 'Shop V2');
  27. ok(updated);
  28. });
  29. t('A deletes own shop', async () => {
  30. const net = makeNetwork(); const A = makePeer(net); A.setActor();
  31. const shop = await A.use('shops').createShop('Tmp', '', '', null, '', '', [], 'OPEN', '');
  32. await A.use('shops').deleteShopById(shop.key);
  33. const list = await A.use('shops').listAll({ filter: 'all', viewerId: A.keypair.id });
  34. const found = list.find(s => s.title === 'Tmp');
  35. ok(!found);
  36. });
  37. });
  38. describe('shops: encrypted purchase orders', (t) => {
  39. const setupShopWithProduct = async (net, A) => {
  40. A.setActor();
  41. const shop = await A.use('shops').createShop('Alice Shop', '', '', null, '', '', [], 'OPEN', '');
  42. const prod = await A.use('shops').createProduct(shop.key, 'Widget', 'd', null, 10, 5, false);
  43. return { shop, prod };
  44. };
  45. t('B creates encrypted order; A (owner) can decrypt and read delivery details', async () => {
  46. const net = makeNetwork(); const A = makePeer(net); const B = makePeer(net);
  47. const { shop, prod } = await setupShopWithProduct(net, A);
  48. B.setActor();
  49. await B.use('shops').createPurchaseOrder(prod.key, {
  50. deliveryAddress: '123 Main St',
  51. contact: 'b@example.com',
  52. notes: 'leave at door'
  53. });
  54. A.setActor();
  55. const orders = await A.use('shops').listShopOrders(shop.key);
  56. eq(orders.length, 1);
  57. eq(orders[0].deliveryAddress, '123 Main St');
  58. eq(orders[0].contact, 'b@example.com');
  59. eq(orders[0].notes, 'leave at door');
  60. eq(orders[0].buyer, B.keypair.id);
  61. });
  62. t('B (buyer) sees own order in listMyPurchases', async () => {
  63. const net = makeNetwork(); const A = makePeer(net); const B = makePeer(net);
  64. const { prod } = await setupShopWithProduct(net, A);
  65. B.setActor();
  66. await B.use('shops').createPurchaseOrder(prod.key, { deliveryAddress: '999 Side Rd' });
  67. const mine = await B.use('shops').listMyPurchases();
  68. eq(mine.length, 1);
  69. eq(mine[0].deliveryAddress, '999 Side Rd');
  70. });
  71. t('C (third party) cannot decrypt the order', async () => {
  72. const net = makeNetwork(); const A = makePeer(net); const B = makePeer(net); const C = makePeer(net);
  73. const { shop, prod } = await setupShopWithProduct(net, A);
  74. B.setActor();
  75. await B.use('shops').createPurchaseOrder(prod.key, { deliveryAddress: 'SECRET ADDR' });
  76. C.setActor();
  77. let threw = false;
  78. try { await C.use('shops').listShopOrders(shop.key); } catch (_) { threw = true; }
  79. ok(threw, 'non-owner is rejected by listShopOrders');
  80. const cMine = await C.use('shops').listMyPurchases();
  81. eq(cMine.length, 0, 'C sees no purchases of their own');
  82. });
  83. t('seller cannot create order on own product', async () => {
  84. const net = makeNetwork(); const A = makePeer(net);
  85. const { prod } = await setupShopWithProduct(net, A);
  86. let threw = false;
  87. try { await A.use('shops').createPurchaseOrder(prod.key, { deliveryAddress: 'X' }); } catch (_) { threw = true; }
  88. ok(threw);
  89. });
  90. t('listShopOrders filters by shopId (orders for other shop excluded)', async () => {
  91. const net = makeNetwork(); const A = makePeer(net); const B = makePeer(net);
  92. A.setActor();
  93. const shop1 = await A.use('shops').createShop('Shop1', '', '', null, '', '', [], 'OPEN', '');
  94. const shop2 = await A.use('shops').createShop('Shop2', '', '', null, '', '', [], 'OPEN', '');
  95. const prod1 = await A.use('shops').createProduct(shop1.key, 'P1', '', null, 10, 5, false);
  96. const prod2 = await A.use('shops').createProduct(shop2.key, 'P2', '', null, 10, 5, false);
  97. B.setActor();
  98. await B.use('shops').createPurchaseOrder(prod1.key, { deliveryAddress: 'addr1' });
  99. await B.use('shops').createPurchaseOrder(prod2.key, { deliveryAddress: 'addr2' });
  100. A.setActor();
  101. const ord1 = await A.use('shops').listShopOrders(shop1.key);
  102. eq(ord1.length, 1);
  103. eq(ord1[0].deliveryAddress, 'addr1');
  104. const ord2 = await A.use('shops').listShopOrders(shop2.key);
  105. eq(ord2.length, 1);
  106. eq(ord2[0].deliveryAddress, 'addr2');
  107. });
  108. });