Skip to content

Commit

Permalink
Merge pull request #34 from reactioncommerce/fix-mohan-33-query-with-…
Browse files Browse the repository at this point in the history
…cartid-and-accesstoken

Query with cartId and accessToken. Unit tests.
  • Loading branch information
brent-hoover authored Jan 5, 2022
2 parents 9ed7b7f + bd98272 commit 87ed544
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/queries/anonymousCartByCartId.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
}
37 changes: 37 additions & 0 deletions src/queries/anonymousCartByCartId.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 87ed544

Please sign in to comment.