diff --git a/package.json b/package.json index 8957a17..8c3f54f 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,9 @@ "jest": "^25.1.0", "semantic-release": "^17.4.7" }, + "peerDependencies": { + "@reactioncommerce/api-plugin-address-validation": "^1.0.0" + }, "scripts": { "lint": "npm run lint:eslint", "lint:eslint": "eslint .", diff --git a/src/mutations/transformAndValidateCart.js b/src/mutations/transformAndValidateCart.js index d61f4d2..544ea4c 100644 --- a/src/mutations/transformAndValidateCart.js +++ b/src/mutations/transformAndValidateCart.js @@ -14,7 +14,7 @@ const logCtx = { name: "cart", file: "transformAndValidateCart" }; * @returns {undefined} */ export default async function transformAndValidateCart(context, cart) { - const cartSchema = context.simpleSchemas.Cart; + const { simpleSchemas: { Cart: cartSchema } } = context; updateCartFulfillmentGroups(context, cart); let commonOrders; diff --git a/src/mutations/transformAndValidateCart.test.js b/src/mutations/transformAndValidateCart.test.js new file mode 100644 index 0000000..aeb1592 --- /dev/null +++ b/src/mutations/transformAndValidateCart.test.js @@ -0,0 +1,79 @@ +import mockContext from "@reactioncommerce/api-utils/tests/mockContext.js"; +import { Cart } from "../simpleSchemas.js"; +import transformAndValidateCart from "./transformAndValidateCart.js"; + +mockContext.simpleSchemas = {}; +mockContext.simpleSchemas.Cart = Cart; + +const accountCart = { + _id: "cartId", + accountId: "accountId", + shopId: "shopId", + currencyCode: "currencyCode", + createdAt: new Date() +}; +const expectedResult = { ...accountCart, shipping: [] }; +test("valid account cart", async () => { + await transformAndValidateCart(mockContext, accountCart); + expect(accountCart).toEqual(expectedResult); +}); + + +const anonymousCart = { + _id: "cartId", + anonymousAccessToken: "anonymousAccessToken", + shopId: "shopId", + currencyCode: "currencyCode", + createdAt: new Date() +}; +const expectedAnonymousResult = { ...anonymousCart, shipping: [] }; +test("valid anonymous cart", async () => { + await transformAndValidateCart(mockContext, anonymousCart); + expect(anonymousCart).toEqual(expectedAnonymousResult); +}); + + +const accountCartNoShopId = { + _id: "cartId", + accountId: "accountId", + currencyCode: "currencyCode", + createdAt: new Date() +}; +test("invalid account cart - no shopId", async () => { + try { + await transformAndValidateCart(mockContext, accountCartNoShopId); + } catch (error) { + expect(error.details[0].message).toEqual("Cart ShopId is required"); + } +}); + + +const accountCartNoCurrencyCode = { + _id: "cartId", + accountId: "accountId", + shopId: "shopId", + createdAt: new Date() +}; +test("invalid account cart - no currency code", async () => { + try { + await transformAndValidateCart(mockContext, accountCartNoCurrencyCode); + } catch (error) { + expect(error.details[0].message).toEqual("Currency code is required"); + } +}); + + +const accountCartInvalidDate = { + _id: "cartId", + accountId: "accountId", + shopId: "shopId", + currencyCode: "currencyCode", + createdAt: "date" +}; +test("invalid account cart - invalid date format", async () => { + try { + await transformAndValidateCart(mockContext, accountCartInvalidDate); + } catch (error) { + expect(error.details[0].message).toEqual("Created at must be of type Date"); + } +}); diff --git a/src/queries/anonymousCartByCartId.js b/src/queries/anonymousCartByCartId.js index 67d9042..4720c5b 100644 --- a/src/queries/anonymousCartByCartId.js +++ b/src/queries/anonymousCartByCartId.js @@ -20,5 +20,8 @@ export default async function anonymousCartByCartId(context, { cartId, cartToken throw new ReactionError("invalid-param", "You must provide a cartId"); } - return Cart.findOne({ $or: [{ _id: cartId }, { anonymousAccessToken: hashToken(cartToken) }] }); + return Cart.findOne({ + _id: cartId, + anonymousAccessToken: hashToken(cartToken) + }); } diff --git a/src/queries/anonymousCartByCartId.test.js b/src/queries/anonymousCartByCartId.test.js new file mode 100644 index 0000000..c5c3d83 --- /dev/null +++ b/src/queries/anonymousCartByCartId.test.js @@ -0,0 +1,37 @@ +import mockContext from "@reactioncommerce/api-utils/tests/mockContext.js"; +import hashToken from "@reactioncommerce/api-utils/hashToken.js"; +import anonymousCartByCartId from "./anonymousCartByCartId.js"; + +test("query anonymous cart", async () => { + const cartId = "123"; + const cartToken = "xyz"; + const cart = { + _id: cartId, + anonymousAccessToken: cartToken + }; + const callingParams = { + _id: cartId, + anonymousAccessToken: hashToken(cartToken) + }; + + mockContext.collections.Cart.findOne.mockReturnValueOnce(Promise.resolve(cart)); + + const result = await anonymousCartByCartId(mockContext, { cartId, cartToken }); + expect(result).toEqual(cart); + expect(mockContext.collections.Cart.findOne).toHaveBeenCalledWith(callingParams); +}); + +test("query without hashed access token", async () => { + const cartId = "123"; + const cartToken = "xyz"; + const callingParams = { + _id: cartId, + anonymousAccessToken: cartToken + }; + + mockContext.collections.Cart.findOne.mockReturnValueOnce(Promise.resolve(null)); + + const result = await anonymousCartByCartId(mockContext, { cartId, cartToken }); + expect(result).toEqual(null); + expect(mockContext.collections.Cart.findOne).not.toHaveBeenCalledWith(callingParams); +});