Raw
1 import { SqlEntityManager } from "@medusajs/framework/mikro-orm/postgresql"
2 import { IndexTypes } from "@medusajs/framework/types"
3 import { toMikroORMEntity } from "@medusajs/framework/utils"
4 import { IndexData, IndexRelation } from "@models"
5 import {
6 createIndexTestBed,
7 IndexModuleTestBed,
8 schema,
9 } from "../__fixtures__"
10
11 jest.setTimeout(300000)
12
13 const seedIndexData = async (manager: SqlEntityManager) => {
14 const indexRepository = manager.getRepository(toMikroORMEntity(IndexData))
15
16 await manager.persistAndFlush(
17 [
18 {
19 id: "prod_1",
20 name: "Product",
21 data: {
22 id: "prod_1",
23 title: "Product 1",
24 },
25 },
26 {
27 id: "prod_2",
28 name: "Product",
29 data: {
30 id: "prod_2",
31 title: "Product 2 title",
32 deep: {
33 a: 1,
34 obj: {
35 b: 15,
36 },
37 },
38 },
39 },
40 {
41 id: "var_1",
42 name: "ProductVariant",
43 data: {
44 id: "var_1",
45 sku: "aaa test aaa",
46 },
47 },
48 {
49 id: "var_2",
50 name: "ProductVariant",
51 data: {
52 id: "var_2",
53 sku: "sku 123",
54 },
55 },
56 {
57 id: "link_id_1",
58 name: "LinkProductVariantPriceSet",
59 data: {
60 id: "link_id_1",
61 variant_id: "var_1",
62 price_set_id: "price_set_1",
63 },
64 },
65 {
66 id: "link_id_2",
67 name: "LinkProductVariantPriceSet",
68 data: {
69 id: "link_id_2",
70 variant_id: "var_2",
71 price_set_id: "price_set_2",
72 },
73 },
74 {
75 id: "price_set_1",
76 name: "PriceSet",
77 data: {
78 id: "price_set_1",
79 },
80 },
81 {
82 id: "price_set_2",
83 name: "PriceSet",
84 data: {
85 id: "price_set_2",
86 },
87 },
88 {
89 id: "money_amount_1",
90 name: "Price",
91 data: {
92 id: "money_amount_1",
93 amount: 100,
94 },
95 },
96 {
97 id: "money_amount_2",
98 name: "Price",
99 data: {
100 id: "money_amount_2",
101 amount: 10,
102 },
103 },
104 ].map((data) => indexRepository.create(data))
105 )
106
107 const indexRelationRepository = manager.getRepository(
108 toMikroORMEntity(IndexRelation)
109 )
110
111 await manager.persistAndFlush(
112 [
113 {
114 parent_id: "prod_1",
115 parent_name: "Product",
116 child_id: "var_1",
117 child_name: "ProductVariant",
118 pivot: "Product-ProductVariant",
119 },
120 {
121 parent_id: "prod_1",
122 parent_name: "Product",
123 child_id: "var_2",
124 child_name: "ProductVariant",
125 pivot: "Product-ProductVariant",
126 },
127 {
128 parent_id: "var_1",
129 parent_name: "ProductVariant",
130 child_id: "link_id_1",
131 child_name: "LinkProductVariantPriceSet",
132 pivot: "ProductVariant-LinkProductVariantPriceSet",
133 },
134 {
135 parent_id: "var_2",
136 parent_name: "ProductVariant",
137 child_id: "link_id_2",
138 child_name: "LinkProductVariantPriceSet",
139 pivot: "ProductVariant-LinkProductVariantPriceSet",
140 },
141 {
142 parent_id: "link_id_1",
143 parent_name: "LinkProductVariantPriceSet",
144 child_id: "price_set_1",
145 child_name: "PriceSet",
146 pivot: "LinkProductVariantPriceSet-PriceSet",
147 },
148 {
149 parent_id: "link_id_2",
150 parent_name: "LinkProductVariantPriceSet",
151 child_id: "price_set_2",
152 child_name: "PriceSet",
153 pivot: "LinkProductVariantPriceSet-PriceSet",
154 },
155 {
156 parent_id: "price_set_1",
157 parent_name: "PriceSet",
158 child_id: "money_amount_1",
159 child_name: "Price",
160 pivot: "PriceSet-Price",
161 },
162 {
163 parent_id: "price_set_2",
164 parent_name: "PriceSet",
165 child_id: "money_amount_2",
166 child_name: "Price",
167 pivot: "PriceSet-Price",
168 },
169 ].map((data) => indexRelationRepository.create(data))
170 )
171 }
172
173 describe("IndexModuleService query", function () {
174 let testBed: IndexModuleTestBed
175 let module: IndexTypes.IIndexService
176
177 beforeAll(async () => {
178 testBed = await createIndexTestBed({ schema })
179 module = testBed.module
180 })
181
182 afterAll(async () => {
183 await testBed.teardown()
184 })
185
186 beforeEach(async () => {
187 await testBed.truncateTables()
188 await seedIndexData(testBed.forkManager())
189 })
190
191 it("should query all products where sku not null", async () => {
192 const { data } = await module.query({
193 fields: ["product.*", "product.variants.*", "product.variants.prices.*"],
194 filters: {
195 product: {
196 variants: {
197 sku: { $ne: null },
198 },
199 },
200 },
201 })
202
203 const { data: dataNot } = await module.query({
204 fields: ["product.*", "product.variants.*", "product.variants.prices.*"],
205 filters: {
206 product: {
207 variants: {
208 sku: {
209 $not: {
210 $eq: null,
211 },
212 },
213 },
214 },
215 },
216 })
217
218 expect(data.length).toEqual(1)
219 expect(dataNot.length).toEqual(1)
220 expect(dataNot).toEqual(data)
221
222 const { data: data2 } = await module.query({
223 fields: ["product.*", "product.variants.*", "product.variants.prices.*"],
224 filters: {
225 product: {
226 variants: {
227 sku: { $eq: null },
228 },
229 },
230 },
231 })
232
233 expect(data2.length).toEqual(0)
234 })
235
236 it("should query all products ordered by sku DESC", async () => {
237 const { data } = await module.query({
238 fields: ["product.*", "product.variants.*", "product.variants.prices.*"],
239 pagination: {
240 order: {
241 product: {
242 variants: {
243 sku: "DESC",
244 },
245 },
246 },
247 },
248 })
249
250 expect(data).toEqual([
251 {
252 id: "prod_2",
253 title: "Product 2 title",
254 deep: {
255 a: 1,
256 obj: {
257 b: 15,
258 },
259 },
260 variants: [],
261 },
262 {
263 id: "prod_1",
264 title: "Product 1",
265 variants: [
266 {
267 id: "var_2",
268 sku: "sku 123",
269 prices: [
270 {
271 id: "money_amount_2",
272 amount: 10,
273 },
274 ],
275 },
276
277 {
278 id: "var_1",
279 sku: "aaa test aaa",
280 prices: [
281 {
282 id: "money_amount_1",
283 amount: 100,
284 },
285 ],
286 },
287 ],
288 },
289 ])
290 })
291
292 it("should query all products ordered by sku DESC with specific fields", async () => {
293 const { data } = await module.query({
294 fields: [
295 "product.*",
296 "product.variants.sku",
297 "product.variants.prices.amount",
298 ],
299 pagination: {
300 order: {
301 product: {
302 variants: {
303 sku: "DESC",
304 },
305 },
306 },
307 },
308 })
309
310 expect(data).toEqual([
311 {
312 id: "prod_2",
313 title: "Product 2 title",
314 deep: {
315 a: 1,
316 obj: {
317 b: 15,
318 },
319 },
320 variants: [],
321 },
322 {
323 id: "prod_1",
324 title: "Product 1",
325 variants: [
326 {
327 id: "var_2",
328 sku: "sku 123",
329 prices: [
330 {
331 id: "money_amount_2",
332 amount: 10,
333 },
334 ],
335 },
336
337 {
338 id: "var_1",
339 sku: "aaa test aaa",
340 prices: [
341 {
342 id: "money_amount_1",
343 amount: 100,
344 },
345 ],
346 },
347 ],
348 },
349 ])
350 })
351
352 it("should query all products ordered by price", async () => {
353 const { data } = await module.query({
354 fields: ["product.*", "product.variants.*", "product.variants.prices.*"],
355 pagination: {
356 order: {
357 product: {
358 variants: {
359 prices: {
360 amount: "DESC",
361 },
362 },
363 },
364 },
365 },
366 })
367
368 expect(data).toEqual([
369 {
370 id: "prod_2",
371 title: "Product 2 title",
372 deep: {
373 a: 1,
374 obj: {
375 b: 15,
376 },
377 },
378 variants: [],
379 },
380 {
381 id: "prod_1",
382 title: "Product 1",
383 variants: [
384 {
385 id: "var_1",
386 sku: "aaa test aaa",
387 prices: [
388 {
389 id: "money_amount_1",
390 amount: 100,
391 },
392 ],
393 },
394 {
395 id: "var_2",
396 sku: "sku 123",
397 prices: [
398 {
399 id: "money_amount_2",
400 amount: 10,
401 },
402 ],
403 },
404 ],
405 },
406 ])
407
408 const { data: dataAsc } = await module.query({
409 fields: ["product.*", "product.variants.*", "product.variants.prices.*"],
410 pagination: {
411 order: {
412 product: {
413 variants: {
414 prices: {
415 amount: "ASC",
416 },
417 },
418 },
419 },
420 },
421 })
422
423 expect(dataAsc).toEqual([
424 {
425 id: "prod_1",
426 title: "Product 1",
427 variants: [
428 {
429 id: "var_2",
430 sku: "sku 123",
431 prices: [
432 {
433 id: "money_amount_2",
434 amount: 10,
435 },
436 ],
437 },
438 {
439 id: "var_1",
440 sku: "aaa test aaa",
441 prices: [
442 {
443 id: "money_amount_1",
444 amount: 100,
445 },
446 ],
447 },
448 ],
449 },
450 {
451 id: "prod_2",
452 title: "Product 2 title",
453 deep: {
454 a: 1,
455 obj: {
456 b: 15,
457 },
458 },
459 variants: [],
460 },
461 ])
462 })
463
464 it("should query all products ordered by price returning only ids", async () => {
465 const { data } = await module.query({
466 fields: ["product.*", "product.variants.*"],
467 idsOnly: true,
468 pagination: {
469 order: {
470 product: {
471 variants: {
472 prices: {
473 amount: "DESC",
474 },
475 },
476 },
477 },
478 },
479 })
480
481 expect(data).toEqual([
482 {
483 id: "prod_2",
484 variants: [],
485 },
486 {
487 id: "prod_1",
488 variants: [
489 {
490 id: "var_1",
491 },
492 {
493 id: "var_2",
494 },
495 ],
496 },
497 ])
498 })
499
500 it("should query products filtering by variant sku", async () => {
501 const { data, metadata } = await module.query({
502 fields: ["product.*", "product.variants.*", "product.variants.prices.*"],
503 filters: {
504 product: {
505 variants: {
506 sku: { $like: "aaa%" },
507 },
508 },
509 },
510 pagination: {
511 take: 100,
512 skip: 0,
513 },
514 })
515
516 expect(metadata).toEqual({
517 estimate_count: expect.any(Number),
518 skip: 0,
519 take: 100,
520 })
521
522 expect(data).toEqual([
523 {
524 id: "prod_1",
525 title: "Product 1",
526 variants: [
527 {
528 id: "var_1",
529 sku: "aaa test aaa",
530 prices: [
531 {
532 id: "money_amount_1",
533 amount: 100,
534 },
535 ],
536 },
537 ],
538 },
539 ])
540 })
541
542 it("should query products filtering by variant sku and join filters on prices amount", async () => {
543 const { data, metadata } = await module.query({
544 fields: ["product.*", "product.variants.*", "product.variants.prices.*"],
545 joinFilters: {
546 "product.variants.prices.amount": { $gt: 110 },
547 },
548 filters: {
549 product: {
550 variants: {
551 sku: { $like: "aaa%" },
552 },
553 },
554 },
555 pagination: {
556 take: 100,
557 skip: 0,
558 order: {
559 product: {
560 created_at: "ASC",
561 },
562 },
563 },
564 })
565
566 expect(metadata).toEqual({
567 estimate_count: expect.any(Number),
568 skip: 0,
569 take: 100,
570 })
571
572 expect(data).toEqual([
573 {
574 id: "prod_1",
575 title: "Product 1",
576 variants: [
577 {
578 id: "var_1",
579 sku: "aaa test aaa",
580 prices: [],
581 },
582 ],
583 },
584 ])
585 })
586
587 it("should filter using fields not selected", async () => {
588 const { data } = await module.query({
589 fields: ["product.id", "product.variants.*"],
590 pagination: {
591 order: {
592 product: {
593 variants: {
594 prices: {
595 amount: "ASC",
596 },
597 },
598 },
599 },
600 },
601 })
602
603 expect(data).toEqual([
604 {
605 id: "prod_1",
606 variants: [
607 {
608 id: "var_2",
609 sku: "sku 123",
610 },
611 {
612 id: "var_1",
613 sku: "aaa test aaa",
614 },
615 ],
616 },
617 {
618 id: "prod_2",
619 variants: [],
620 },
621 ])
622 })
623
624 it("should filter using IN operator with array of strings", async () => {
625 const { data } = await module.query({
626 fields: ["product.id", "product.variants.*"],
627 filters: {
628 product: {
629 variants: {
630 sku: { $in: ["sku 123", "aaa test aaa", "does-not-exist"] },
631 },
632 },
633 },
634 pagination: {
635 order: {
636 product: {
637 variants: {
638 prices: {
639 amount: "DESC",
640 },
641 },
642 },
643 },
644 },
645 })
646
647 expect(data).toEqual([
648 {
649 id: "prod_1",
650 variants: [
651 {
652 id: "var_1",
653 sku: "aaa test aaa",
654 },
655 {
656 id: "var_2",
657 sku: "sku 123",
658 },
659 ],
660 },
661 ])
662 })
663
664 it("should query all products", async () => {
665 const { data } = await module.query({
666 fields: ["product.*", "product.variants.*", "product.variants.prices.*"],
667 })
668
669 expect(data).toEqual([
670 {
671 id: "prod_1",
672 title: "Product 1",
673 variants: [
674 {
675 id: "var_1",
676 sku: "aaa test aaa",
677 prices: [
678 {
679 id: "money_amount_1",
680 amount: 100,
681 },
682 ],
683 },
684 {
685 id: "var_2",
686 sku: "sku 123",
687 prices: [
688 {
689 id: "money_amount_2",
690 amount: 10,
691 },
692 ],
693 },
694 ],
695 },
696 {
697 id: "prod_2",
698 title: "Product 2 title",
699 deep: {
700 a: 1,
701 obj: {
702 b: 15,
703 },
704 },
705 variants: [],
706 },
707 ])
708 })
709
710 it("should paginate products", async () => {
711 const { data, metadata } = await module.query({
712 fields: ["product.*", "product.variants.*", "product.variants.prices.*"],
713 pagination: {
714 take: 1,
715 skip: 1,
716 order: {
717 product: {
718 id: "ASC",
719 },
720 },
721 },
722 })
723
724 expect(metadata).toEqual({
725 estimate_count: expect.any(Number),
726 skip: 1,
727 take: 1,
728 })
729 expect(data).toEqual([
730 {
731 id: "prod_2",
732 title: "Product 2 title",
733 deep: {
734 a: 1,
735 obj: {
736 b: 15,
737 },
738 },
739 variants: [],
740 },
741 ])
742 })
743
744 it("should query products filtering by deep nested levels", async () => {
745 const { data, metadata } = await module.query({
746 fields: ["product.*"],
747 filters: {
748 product: {
749 deep: {
750 obj: {
751 b: 15,
752 },
753 },
754 },
755 },
756 pagination: {
757 take: 1,
758 skip: 0,
759 },
760 })
761
762 expect(metadata).toEqual({
763 estimate_count: expect.any(Number),
764 skip: 0,
765 take: 1,
766 })
767 expect(data).toEqual([
768 {
769 id: "prod_2",
770 title: "Product 2 title",
771 deep: {
772 a: 1,
773 obj: {
774 b: 15,
775 },
776 },
777 },
778 ])
779 })
780
781 it("should query products filtering by prices bigger than 20", async () => {
782 const { data, metadata } = await module.query({
783 fields: ["product.*", "product.variants.*", "product.variants.prices.*"],
784 filters: {
785 product: {
786 variants: {
787 prices: {
788 amount: { $gt: 20 },
789 },
790 },
791 },
792 },
793 pagination: {
794 take: 100,
795 skip: 0,
796 order: {
797 product: {
798 created_at: "ASC",
799 },
800 },
801 },
802 })
803
804 expect(metadata).toEqual({
805 estimate_count: expect.any(Number),
806 skip: 0,
807 take: 100,
808 })
809
810 expect(data).toEqual([
811 {
812 id: "prod_1",
813 title: "Product 1",
814 variants: [
815 {
816 id: "var_1",
817 sku: "aaa test aaa",
818 prices: [
819 {
820 id: "money_amount_1",
821 amount: 100,
822 },
823 ],
824 },
825 ],
826 },
827 ])
828 })
829
830 it("should query products filtering product not in [X]", async () => {
831 const expected = [
832 {
833 id: "prod_2",
834 title: "Product 2 title",
835 deep: {
836 a: 1,
837 obj: {
838 b: 15,
839 },
840 },
841 },
842 ]
843
844 const { data } = await module.query({
845 fields: ["product.*"],
846 filters: {
847 product: {
848 $not: [
849 {
850 id: {
851 $in: ["prod_1"],
852 },
853 },
854 ],
855 },
856 },
857 })
858 expect(data).toEqual(expected)
859 })
860
861 it("should query products filtering product not in [X] using $nin", async () => {
862 const expected = [
863 {
864 id: "prod_2",
865 title: "Product 2 title",
866 deep: {
867 a: 1,
868 obj: {
869 b: 15,
870 },
871 },
872 },
873 ]
874
875 const { data } = await module.query({
876 fields: ["product.*"],
877 filters: {
878 product: {
879 id: {
880 $nin: ["prod_1"],
881 },
882 },
883 },
884 })
885 expect(data).toEqual(expected)
886 })
887
888 it("should query products with variants.sku not in [X] and title eq", async () => {
889 const expected = [
890 {
891 id: "prod_2",
892 title: "Product 2 title",
893 deep: {
894 a: 1,
895 obj: {
896 b: 15,
897 },
898 },
899 },
900 ]
901
902 const { data } = await module.query({
903 fields: ["product.*", "variants.*"],
904 filters: {
905 product: {
906 variants: {
907 sku: {
908 $nin: ["sku 123"],
909 },
910 },
911 title: {
912 $eq: "Product 2 title",
913 },
914 },
915 },
916 })
917 expect(data).toEqual(expected)
918 })
919
920 it("should query products filtering title like and not equal specific value", async () => {
921 const expected = [
922 {
923 id: "prod_2",
924 title: "Product 2 title",
925 deep: {
926 a: 1,
927 obj: {
928 b: 15,
929 },
930 },
931 },
932 ]
933
934 const { data } = await module.query({
935 fields: ["product.*"],
936 filters: {
937 product: {
938 $and: [
939 {
940 title: {
941 $like: "Product%",
942 },
943 },
944 {
945 $not: {
946 title: {
947 $eq: "Product 1",
948 },
949 },
950 },
951 ],
952 },
953 },
954 })
955 expect(data).toEqual(expected)
956 })
957
958 it("should query products filtering title using $ilike", async () => {
959 const expected = [
960 {
961 id: "prod_2",
962 title: "Product 2 title",
963 },
964 ]
965
966 const { data } = await module.query({
967 fields: ["product.id", "product.title"],
968 filters: {
969 product: {
970 title: {
971 $ilike: "PROdUCt 2%",
972 },
973 },
974 },
975 })
976 expect(data).toEqual(expected)
977
978 const { data: sensitive } = await module.query({
979 fields: ["product.id", "product.title"],
980 filters: {
981 product: {
982 title: {
983 $like: "PROdUCt 2%",
984 },
985 },
986 },
987 })
988 expect(sensitive).toEqual([])
989 })
990
991 describe("special characters in @> containment filter values", () => {
992 const injectionPayloads = [
993 "'",
994 "'; SELECT pg_sleep(0)--",
995 "' OR 1=1--",
996 `\\' OR 1=1--`,
997 ]
998
999 it.each(injectionPayloads)(
1000 "does not error and returns no matches for direct equality with payload: %s",
1001 async (payload) => {
1002 const { data } = await module.query({
1003 fields: ["product.id", "product.title"],
1004 filters: {
1005 product: {
1006 title: payload,
1007 },
1008 },
1009 })
1010
1011 expect(data).toEqual([])
1012 }
1013 )
1014
1015 it.each(injectionPayloads)(
1016 "does not error and returns no matches for $eq operator with payload: %s",
1017 async (payload) => {
1018 const { data } = await module.query({
1019 fields: ["product.id", "product.title"],
1020 filters: {
1021 product: {
1022 title: { $eq: payload },
1023 },
1024 },
1025 })
1026
1027 expect(data).toEqual([])
1028 }
1029 )
1030
1031 it("matches a literal title containing a single quote", async () => {
1032 const manager = testBed.forkManager()
1033
1034 const indexRepository = manager.getRepository(toMikroORMEntity(IndexData))
1035 await manager.persistAndFlush(
1036 indexRepository.create({
1037 id: "prod_quote",
1038 name: "Product",
1039 data: {
1040 id: "prod_quote",
1041 title: "O'Reilly's Book",
1042 },
1043 })
1044 )
1045
1046 const { data } = await module.query({
1047 fields: ["product.id", "product.title"],
1048 filters: {
1049 product: {
1050 title: "O'Reilly's Book",
1051 },
1052 },
1053 })
1054
1055 expect(data).toEqual([{ id: "prod_quote", title: "O'Reilly's Book" }])
1056 })
1057 })
1058 })