def test_set_email_notifications_no_products_in_order(self): order = models.Order.query.first() user_buyer = models.User(email=testing_constants.NEW_USER_EMAIL, name=testing_constants.NEW_USER_NAME) order.user = user_buyer user_shop_owner = models.User() models.Customer(user=user_shop_owner) shop = models.Shop(name=testing_constants.NEW_SHOP_NAME) shop.owner = user_shop_owner order.shop = shop order.shipment_timestamp = date_parser.parse(testing_constants.ORDER_SHIPPED_AT).astimezone(pytz.utc).replace(tzinfo=None) # The results from the asynchronous tasks are executed immediately with self.assertRaises(DbException): order.set_notifications()
def test_set_email_notifications_no_shop(self): order = models.Order.query.first() product = models.Product(name=testing_constants.NEW_PRODUCT_NAME, platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID) user_buyer = models.User(email=testing_constants.NEW_USER_EMAIL, name=testing_constants.NEW_USER_NAME) order.user = user_buyer user_shop_owner = models.User() models.Customer(user=user_shop_owner) shop = None product.shop = shop order.shop = shop order.products.append(product) order.shipment_timestamp = date_parser.parse(testing_constants.ORDER_SHIPPED_AT).astimezone(pytz.utc).replace(tzinfo=None) # The results from the asynchronous tasks are executed immediately with self.assertRaises(DbException): order.set_notifications()
def test_create_review_request_repeated_products(self): order = models.Order.query.first() user_shop_owner = models.User() models.Customer(user=user_shop_owner) shop = models.Shop(name=testing_constants.NEW_SHOP_NAME) shop.owner = user_shop_owner order.shop = shop product1 = models.Product(platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID) product2 = models.Product(platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID_2) # add the first product twice order.products.append(product1) order.products.append(product1) # not a typo!. << Good to know ;) -- T. order.products.append(product2) order.create_review_requests(order.id) self.assertEquals(len(order.review_requests), 2)
def create_customer_account(user_id, plan_name): # 1. Flushing only doesn't work because in production, flush doesn't end the transation. Though it works in test. # 2. Instantiating a session with session maker doesn't work, because # you can't add the same object to different sessions (Customer already exists in session 2, this is 3) # 3. Original code doesn't work in test, because this task executes immediately and a commit to db clears # the session. Therefore when we return to models.post_registration_handler which returns to flask_security # registrable:40, the use is no longer in the session. user = models.User.query.filter_by(id=user_id).first() plan_name = plan_name or Constants.PLAN_NAME_SHOPIFY_BASIC plan = models.Plan.query.filter_by(name=plan_name).first() customer = models.Customer(user=user).create() subscription = models.Subscription(customer=customer, plan=plan).create() db.session.add(customer) db.session.add(subscription) db.session.commit()
def test_get_order_by_id(self): order = models.Order.query.first() product = models.Product(name=testing_constants.NEW_PRODUCT_NAME, platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID) user_buyer = models.User(email=testing_constants.NEW_USER_EMAIL, name=testing_constants.NEW_USER_NAME) order.user = user_buyer user_shop_owner = models.User() models.Customer(user=user_shop_owner) shop = models.Shop(name=testing_constants.NEW_SHOP_NAME) shop.owner = user_shop_owner product.shop = shop order.shop = shop order.products.append(product) order.shipment_timestamp = date_parser.parse(testing_constants.ORDER_SHIPPED_AT).astimezone(pytz.utc).replace(tzinfo=None) order_id = order.id db.session.add(order) db.session.commit() retrieved_order = models.Order.get_by_id(order_id=order_id) self.assertTrue(retrieved_order is not None) self.assertTrue(isinstance(retrieved_order, models.Order))
def test_create_review_request(self): user_buyer = models.User(email=testing_constants.NEW_USER_EMAIL, name=testing_constants.NEW_USER_NAME) user_shop_owner = models.User(is_shop_owner=True) order = models.Order() product = models.Product(name=testing_constants.NEW_PRODUCT_NAME, platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID) order.user = user_buyer customer = models.Customer(user=user_shop_owner) shop = models.Shop(name=testing_constants.NEW_SHOP_NAME) shop.owner = user_shop_owner product.shop = shop order.shop = shop order.products.append(product) #creates a review request and returns a token associated with it review_request_token = models.ReviewRequest.create(to_user=user_buyer, from_customer=customer, for_product=product, for_shop=shop, for_order=order) review_request = models.ReviewRequest.query.filter_by(token=review_request_token).first() self.assertEqual(review_request.token, review_request_token) self.assertEqual(review_request.for_shop.name, shop.name) self.assertEqual(review_request.from_customer.user.is_shop_owner, user_shop_owner.is_shop_owner)
def test_set_email_notifications(self): # setup an order order = models.Order.query.first() product = models.Product(name=testing_constants.NEW_PRODUCT_NAME, platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID) user_buyer = models.User(email=testing_constants.NEW_USER_EMAIL, name=testing_constants.NEW_USER_NAME) order.user = user_buyer user_shop_owner = models.User() models.Customer(user=user_shop_owner) shop = models.Shop(name=testing_constants.NEW_SHOP_NAME) shop.owner = user_shop_owner product.shop = shop order.shop = shop order.products.append(product) order.shipment_timestamp = date_parser.parse(testing_constants.ORDER_SHIPPED_AT).astimezone(pytz.utc).replace(tzinfo=None) # The results from the asynchronous tasks are executed immediately order.set_notifications() self.assertEquals(len(self.outbox), 1) self.assertEquals(len(self.outbox[0].send_to), 1) self.assertEquals(self.outbox[0].send_to.pop(), testing_constants.NEW_USER_EMAIL) self.assertEquals(self.outbox[0].subject, Constants.DEFAULT_REVIEW_SUBJECT %(testing_constants.NEW_USER_NAME.split()[0], testing_constants.NEW_SHOP_NAME)) self.assertTrue(testing_constants.NEW_USER_NAME.split()[0] in self.outbox[0].body) self.assertTrue(testing_constants.NEW_PRODUCT_NAME in self.outbox[0].body) self.assertTrue(testing_constants.NEW_SHOP_NAME in self.outbox[0].body) order = models.Order.query.first() expected_notification_ts = date_parser.parse(testing_constants.ORDER_SHIPPED_AT).astimezone(pytz.utc).replace(tzinfo=None) + \ datetime.timedelta(days=Constants.DIFF_SHIPMENT_NOTIFY) self.assertEquals(order.status, Constants.ORDER_STATUS_NOTIFIED) self.assertEquals(order.to_notify_timestamp,expected_notification_ts) self.assertEquals(len(order.tasks), 2) for task in order.tasks: self.assertEquals(task.status, 'SUCCESS') self.assertEquals(task.eta, date_parser.parse('2015-12-16 18:56:26'))
c3 = models.Customer.query.filter_by(id=27).first() c3.stripe_customer_id = "cus_7zxDyPLgVmd09B" db.session.add(c3) c4 = models.Customer.query.filter_by(id=34).first() c4.stripe_customer_id = "cus_7zxFZlJeoLHyE8" db.session.add(c4) c5 = models.Customer.query.filter_by(id=35).first() c5.stripe_customer_id = "cus_7zxGaoutiZyLUX" db.session.add(c5) # Create 3 users: madeline_u = models.User(email="*****@*****.**", is_shop_owner=True) madeline_c = models.Customer(user=madeline_u, stripe_customer_id="cus_8EApEJLklCJFkw") madeline_s = models.Subscription(customer=madeline_c) db.session.add(madeline_s) test_u = models.User(email="*****@*****.**", is_shop_owner=True) test_c = models.Customer(user=test_u, stripe_customer_id="cus_8EAt4P3noce59l") test_s = models.Subscription(customer=test_c) db.session.add(test_s) now = datetime.datetime.utcnow() started = datetime.datetime(2016, 3, 22, 10, 32) osg_u = models.User(email="*****@*****.**", is_shop_owner=True, confirmed_at=started) osg_shop = models.Shop(