it('returns associated category', function () { const categoryId = Factory.create('category', {name: "CoolCategory"})._id; const program = Factory.create('program', {categoryId}); expect(program).to.exist; expect(program.category()).to.exist; expect(program.category().name).to.equal("CoolCategory"); });
it('should create season and clean up', () => { const league = Factory.create('league'); const season = Factory.create('season', { leagueId: league._id }); const pool = Factory.create('pool', { leagueId: league._id, latestSeasonId: season._id }); Factory.create('poolTeam', { seasonId: season._id, poolId: pool._id }); const leagueName = league.name; const year = 2050; const startDate = new Date(year, 1, 5); const endDate = new Date(year, 5, 20); const poolTeamCount = PoolTeams.find().count(); const seasonCount = Seasons.find().count(); SeasonCreator.create(leagueName, year, startDate, endDate); // TODO: this is an overly simple test assert.notEqual(PoolTeams.find().count(), poolTeamCount, 'poolTeamCount'); assert.notEqual(Seasons.find().count(), seasonCount, 'seasonCount'); SeasonCreator.remove(leagueName, year); assert.equal(PoolTeams.find().count(), poolTeamCount, 'poolTeamCount'); assert.equal(Seasons.find().count(), seasonCount, 'seasonCount'); });
it("should increase product quantity if anonymous cart items exists in user's cart before merge", function () { sandbox.stub(Reaction, "getShopId", () => shop._id); const anonymousCart = Factory.create("anonymousCart"); let cart = Factory.create("cartOne"); // registered user cart let cartCount = Collections.Cart.find().count(); expect(cartCount).to.equal(2); const initialCartQty = cart.items[0].quantity; Collections.Cart.update({ "_id": anonymousCart._id, "items._id": anonymousCart.items[0]._id }, { $set: { "items.$.variants._id": cart.items[0].variants_id } }); spyOnMethod("mergeCart", cart.userId); const cartRemoveSpy = sandbox.spy(Collections.Cart, "remove"); Collections.Cart.update({}, { $set: { sessionId: sessionId } }); const mergeResult = Meteor.call("cart/mergeCart", cart._id, sessionId); expect(mergeResult).to.be.ok; const anonymousCartAfterMerge = Collections.Cart.findOne(anonymousCart._id); cart = Collections.Cart.findOne(cart._id); cartCount = Collections.Cart.find().count(); expect(cartCount).to.equal(1); expect(cartRemoveSpy).to.have.been.called; expect(anonymousCartAfterMerge).to.be.undefined; expect(cart.items[0].quantity).to.be.above(initialCartQty); });
it('should add up the wins and losses for all completed games', () => { const season = Factory.create('season'); const homeLeagueTeam = Factory.create('leagueTeam', { leagueId: season.leagueId }); const awayLeagueTeam = Factory.create('awayLeagueTeam', { leagueId: season.leagueId }); Games.insert({ leagueId: season.leagueId, seasonId: season._id, gameId: '1', gameDate: new Date(), homeTeamId: homeLeagueTeam._id, homeScore: 17, awayTeamId: awayLeagueTeam._id, awayScore: 10, status: 'completed', period: 'final', }); SeasonLeagueTeamsUpdater.updateTeamStats( season.leagueId, season._id, homeLeagueTeam._id ); const homeSeasonLeagueTeam = SeasonLeagueTeams.findOne({ leagueId: season.leagueId, seasonId: season._id, leagueTeamId: homeLeagueTeam._id, }); assert.equal(homeSeasonLeagueTeam.wins, 1, 'wins'); assert.equal(homeSeasonLeagueTeam.losses, 0, 'losses'); assert.equal(homeSeasonLeagueTeam.closeWins, 0, 'closeWins'); assert.equal(homeSeasonLeagueTeam.closeLosses, 0, 'closeLosses'); });
it('should add up the wins and losses for all completed games', () => { const poolTeamPick = Factory.create('poolTeamPick'); const awayLeagueTeam = Factory.create('awayLeagueTeam'); Games.insert({ leagueId: poolTeamPick.leagueId, seasonId: poolTeamPick.seasonId, gameId: '1', gameDate: new Date(), homeTeamId: poolTeamPick.leagueTeamId, homeScore: 17, awayTeamId: awayLeagueTeam._id, awayScore: 10, status: 'completed', period: 'final', }); // Games.insert will automatically refresh all PoolTeams that are affected const poolTeam = PoolTeams.findOne(poolTeamPick.poolTeamId); log.debug('poolTeam:', poolTeam); assert.equal(poolTeam.totalWins, 1, 'totalWins'); assert.equal(poolTeam.totalGames, 1, 'totalGames'); assert.equal(poolTeam.totalPlusMinus, 7, 'totalPlusMinus'); });
it("should not let non-Admin to edit address of another user", function () { const account = Factory.create("account"); const account2 = Factory.create("account"); sandbox.stub(Meteor, "userId", () => account.userId); const accountUpdateSpy = sandbox.spy(Accounts, "update"); expect(() => Meteor.call("accounts/addressBookUpdate", getAddress(), account2._id)).to.throw; expect(accountUpdateSpy).to.not.have.been.called; });
beforeEach(function () { Websites.remove({}); Journeys.remove({}); Steps.remove({}); websiteId = Factory.create('website', { createdBy: userId })._id; journeyId = Factory.create('journey', { websiteId })._id; stepId = Factory.create('step', { journeyId, number: 1 })._id; });
it("should add a role to the default customer group for a specified shop", () => { const shop = Factory.create("shop"); let group = Factory.create("group", { shopId: shop._id, slug: "customer" }); Reaction.addRolesToGroups({ shops: [shop._id], roles: ["test-role"], groups: ["customer"] }); group = Groups.findOne({ slug: "customer", shopId: shop._id }); expect(group.permissions).to.contain("test-role"); });
beforeEach(function () { Websites.remove({}); Journeys.remove({}); Steps.remove({}); websiteId = Factory.create('website', { createdBy: userId })._id; journeyId = Factory.create('journey', { websiteId })._id; step = Factory.tree('step.forInsert', { journeyId }); });
export function createCart(productId, variantId) { const product = Products.findOne(productId); const variant = Products.findOne(variantId); const user = Factory.create("user"); const account = Factory.create("account", { userId: user._id }); const cartItem = { _id: Random.id(), addedAt: new Date(), createdAt: new Date(), isTaxable: false, optionTitle: variant.optionTitle, priceWhenAdded: { amount: variant.price, currencyCode: "USD" }, productId: product._id, productSlug: product.handle, productType: product.type, shopId: getShop()._id, quantity: _.random(1, variant.inventoryQuantity), title: product.title, updatedAt: new Date(), variantId: variant._id, variantTitle: variant.title }; const cart = { shopId: getShop()._id, accountId: account._id, email: faker.internet.email(), items: [cartItem], currencyCode: "USD", shipping: [ { _id: Random.id(), shopId: getShop()._id, address: getAddress() } ], workflow: { status: "checkoutPayment", workflow: [ "checkoutLogin", "checkoutAddressBook", "coreCheckoutShipping", "checkoutReview", "checkoutPayment" ] }, createdAt: faker.date.past(), updatedAt: new Date() }; const newCartId = Cart.insert(cart); const insertedCart = Cart.findOne({ _id: newCartId }); return insertedCart; }
before(function () { userId = Random.id(); publicList = Factory.create('list'); privateList = Factory.create('list', { userId }); _.times(3, () => { Factory.create('todo', { listId: publicList._id }); // TODO get rid of userId, https://github.com/meteor/todos/pull/49 Factory.create('todo', { listId: privateList._id, userId }); }); });
it('updates the step numbers of all other steps', function () { const step2Id = Factory.create('step', { journeyId, number: 2 })._id; const step3Id = Factory.create('step', { journeyId, number: 3 })._id; const step4Id = Factory.create('step', { journeyId, number: 4 })._id; removeStep._execute({ userId }, { stepId: step2Id }); assert.equal(Steps.findOne({ _id: step3Id }).number, 2, 'step number not updated'); assert.equal(Steps.findOne({ _id: step4Id }).number, 3, 'step number not updated'); });
it("should add multiple roles to the specified group a shop", () => { const shop = Factory.create("shop"); let group = Factory.create("group", { shopId: shop._id, slug: "customer" }); Reaction.addRolesToGroups({ shops: [shop._id], roles: ["test1", "test2"], groups: ["customer"] }); group = Groups.findOne({ slug: "customer", shopId: shop._id }); expect(group.permissions).to.contain("test1"); expect(group.permissions).to.contain("test2"); });
before(function () { Websites.remove({}); Journeys.remove({}); Steps.remove({}); website = Factory.create('website', { createdBy: userId }); journey = Factory.create('journey', { websiteId: website._id }); _.times(1, () => Factory.create('step')); _.times(2, () => Factory.create('step', { journeyId: journey._id })); });
it("should add a role to default customer group for all shops", () => { const shop = Factory.create("shop"); const shop2 = Factory.create("shop"); let group = Factory.create("group", { shopId: shop._id, slug: "customer" }); let group2 = Factory.create("group", { shopId: shop2._id, slug: "customer" }); Reaction.addRolesToGroups({ allShops: true, roles: ["test-all-shops"], groups: ["customer"] }); group = Groups.findOne({ slug: "customer", shopId: shop._id }); group2 = Groups.findOne({ slug: "customer", shopId: shop2._id }); expect(group.permissions).to.contain("test-all-shops"); expect(group2.permissions).to.contain("test-all-shops"); });
it('filters programs by category', function () { const category = Factory.build('category'); const programs = [ Factory.build('program', {title: 'IN', categoryId: category._id}), Factory.build('program', {title: 'OUT'}), ]; const selections = []; const item = mountWrapIntl(<Feed programs={programs} selections={selections} category={category} />); expect(item.text()).to.contain('1 program remains'); expect(item.text()).to.contain('IN'); expect(item.text()).not.to.contain('OUT'); });
describe("Cart", () => { // for this: "should return only one cart in cursor" test we need to avoid // user carts merging. We need registered users for here. const user = Factory.create("registeredUser"); const userId = user._id; const account = Factory.create("account", { userId }); const thisContext = { userId }; beforeEach(() => { Collections.Cart.remove({}); sandbox.stub(Reaction, "getShopId", () => shop._id); sandbox.stub(Reaction, "getPrimaryShopId", () => shop._id); }); afterEach(() => { Collections.Cart.remove({}); }); it("should return a cart cursor", function () { Collections.Cart.insert({ accountId: account._id, shopId: shop._id }); const cartPub = Meteor.server.publish_handlers["Cart"]; const cursor = cartPub.apply(thisContext, []); const data = cursor.fetch()[0]; expect(data.userId).to.equal(userId); }); it("should return only one cart in cursor", function () { const user2 = Factory.create("registeredUser"); const account2 = Factory.create("account", { userId: user2._id }); Collections.Cart.insert({ accountId: account._id, shopId: shop._id }); Collections.Cart.insert({ accountId: account2._id, shopId: shop._id }); expect(Collections.Cart.find().count()).to.equal(2); // ensure we've added 2 carts const cartPub = Meteor.server.publish_handlers["Cart"]; const cursor = cartPub.apply(thisContext, []); const data = cursor.fetch(); expect(data).to.be.an("array"); expect(data.length).to.equal(1); expect(data[0].userId).to.equal(userId); }); });
it("should remove header tag by admin", function (done) { sandbox.stub(Reaction, "hasPermission", function () { return true; }); const tag = Factory.create("tag"); const currentTag = Factory.create("tag"); expect(Tags.find().count()).to.equal(2); Meteor.call("shop/removeHeaderTag", tag._id, currentTag._id); expect(Tags.find().count()).to.equal(1); return done(); });
it("should throw 403 error by non admin", function (done) { const tagUpdateSpy = sandbox.spy(Tags, "update"); const tagRemoveSpy = sandbox.spy(Tags, "remove"); const tag = Factory.create("tag"); const currentTag = Factory.create("tag"); function removeTagFunc() { return Meteor.call("shop/removeHeaderTag", tag._id, currentTag._id); } expect(removeTagFunc).to.throw(Meteor.Error, /Access Denied/); expect(tagUpdateSpy).to.not.have.been.called; expect(tagRemoveSpy).to.not.have.been.called; return done(); });
it("should not let non-Admin to remove address of another user", function () { const account = Factory.create("account"); const account2 = Factory.create("account"); const address2 = account2.profile.addressBook[0]; sandbox.stub(Meteor, "userId", function () { return account.userId; }); const accountUpdateSpy = sandbox.spy(Accounts, "update"); expect(() => Meteor.call( "accounts/addressBookRemove", address2._id, account2.userId )).to.throw; expect(accountUpdateSpy).to.not.have.been.called; });
it("should not add any roles if no shops are specified", () => { const shop = Factory.create("shop"); const shop2 = Factory.create("shop"); let group = Factory.create("group", { shopId: shop._id, slug: "customer" }); let group2 = Factory.create("group", { shopId: shop2._id, slug: "customer" }); Reaction.addRolesToGroups({ shops: [], roles: ["test-no-shop"], groups: ["customer"] }); group = Groups.findOne({ slug: "customer", shopId: shop._id }); group2 = Groups.findOne({ slug: "customer", shopId: shop2._id }); expect(group.permissions).not.to.contain("test-certain-shop"); expect(group2.permissions).not.to.contain("test-certain-shop"); });
it("should not add roles to unspecified role sets", () => { const shop = Factory.create("shop"); let group = Factory.create("group", { shopId: shop._id, slug: "customer" }); let group2 = Factory.create("group", { shopId: shop._id, slug: "guest" }); Reaction.addRolesToGroups({ allShops: true, shops: [shop._id], roles: ["test1", "test2"], groups: ["guest"] }); group = Groups.findOne({ slug: "customer", shopId: shop._id }); group2 = Groups.findOne({ slug: "guest", shopId: shop._id }); expect(group.permissions).not.to.contain("test1"); expect(group.permissions).not.to.contain("test2"); expect(group2.permissions).to.contain("test1"); expect(group2.permissions).to.contain("test2"); });
it("should not include deleted/non-visible products in sitemaps", () => { const { _id: shopId } = primaryShop; Factory.create("tag", { shopId }); Factory.create("product", { shopId }); // Create a deleted/non-visible tag and product Factory.create("tag", { shopId, isVisible: false, isDeleted: true }); Factory.create("product", { shopId, isVisible: false, isDeleted: true }); generateSitemaps({ urlsPerSitemap: 1 }); // 1 URL per sitemap const sitemapsCount = Sitemaps.find({ shopId }).count(); expect(sitemapsCount).to.equal(4); });
it.skip('returns associated program', function () { const selection = Factory.create('selection', {userId, programId}); // TODO: solve timing issues here expect(selection).to.exist; expect(selection.program()).to.exist; expect(selection.program()._id).to.equal(programid); });
it('renders correctly with simple data', function () { const list = Factory.build('list'); const timestamp = new Date(); // Create a local collection in order to get a cursor // Note that we need to pass the transform in so the documents look right when they come out. const todosCollection = new Mongo.Collection(null, { transform: Todos._transform }); _.times(3, (i) => { const todo = Factory.build('todo', { listId: list._id, createdAt: new Date(timestamp - (3 - i)), }); todosCollection.insert(todo); }); const todosCursor = todosCollection.find({}, { sort: { createdAt: -1 } }); const data = { list: () => list, todosReady: true, todos: todosCursor, }; withRenderedTemplate('Lists_show', data, (el) => { const todosText = todosCursor.map(t => t.text); const renderedText = $(el).find('.list-items input[type=text]') .map((i, e) => $(e).val()) .toArray(); chai.assert.deepEqual(renderedText, todosText); }); });
it('does not allow updating other users steps', function () { const updatedStep = Factory.tree('step.forUpdate'); assert.throws(() => { updateStep._execute({ userId: Random.id() }, { stepId, updatedStep }); }, Meteor.Error, /steps.updateStep.accessDenied/); });
it('does not allow updating the step number', function () { const updatedStep = Factory.tree('step.forUpdate', { number: 1 }); assert.throws(() => { updateStep._execute({ userId }, { stepId, updatedStep }); }, Meteor.Error, /validation-error/); });
it('does not allow updating the journey id', function () { const updatedStep = Factory.tree('step.forUpdate', { journeyId: Random.id() }); assert.throws(() => { updateStep._execute({ userId }, { stepId, updatedStep }); }, Meteor.Error, /validation-error/); });
_.times(3, (i) => { const todo = Factory.build('todo', { listId: list._id, createdAt: new Date(timestamp - (3 - i)), }); todosCollection.insert(todo); });
it('does not allow updating steps that do not exist', function () { const updatedStep = Factory.tree('step.forUpdate'); assert.throws(() => { updateStep._execute({ userId }, { stepId: Random.id(), updatedStep }); }, Meteor.Error, /steps.updateStep.invalidStep/); });