def test_publish(self): Tariff.create_tariff(self.localized_name("tariff_for_balance"), "tariff!!!", "rub", None) customer_id = Customer.new_customer("*****@*****.**", "123qwe", self.admin_user.user_id).customer_id db.session.commit() customer = Customer.get_by_id(customer_id) news_id = self.admin_client.news.create(**self.test_news)['news_id'] # check publish news = self.admin_client.news.publish(news_id, publish=True) self.assertTrue(news['published']) self.assertEqual(outbox[0].to, [customer.email]) self.assertIn(self.test_news['subject'], outbox[0].subject) news_id = self.admin_client.news.create(subject='$test_subject', body='test_body')['news_id'] self.admin_client.news.publish(news_id, publish=True) self.assertEqual(len(outbox), 1) # check unpublish news = self.admin_client.news.publish(news_id, publish=False) self.assertFalse(news['published']) self.assertEqual(len(outbox), 1) # check publish deleted news self.admin_client.news.delete(news_id) with self.expect_error(errors.RemovedNews): self.admin_client.news.publish(news_id, publish=True)
def test_publish(self): Tariff.create_tariff(self.localized_name("tariff_for_balance"), "tariff!!!", "rub", None) customer_id = Customer.new_customer( "*****@*****.**", "123qwe", self.admin_user.user_id).customer_id db.session.commit() customer = Customer.get_by_id(customer_id) news_id = self.admin_client.news.create(**self.test_news)['news_id'] # check publish news = self.admin_client.news.publish(news_id, publish=True) self.assertTrue(news['published']) self.assertEqual(outbox[0].to, [customer.email]) self.assertIn(self.test_news['subject'], outbox[0].subject) news_id = self.admin_client.news.create(subject='$test_subject', body='test_body')['news_id'] self.admin_client.news.publish(news_id, publish=True) self.assertEqual(len(outbox), 1) # check unpublish news = self.admin_client.news.publish(news_id, publish=False) self.assertFalse(news['published']) self.assertEqual(len(outbox), 1) # check publish deleted news self.admin_client.news.delete(news_id) with self.expect_error(errors.RemovedNews): self.admin_client.news.publish(news_id, publish=True)
def setUp(self): super().setUp() self.collector = Collector() self.image_size_price = "12.34" self.volume_size_price = "12.37" self.root_disk_price = "2.91" self.nano_price = "3.47" services = [{ "service_id": "storage.image", "price": self.image_size_price }, { "service_id": "storage.volume", "price": self.volume_size_price }, { "service_id": "storage.disk", "price": self.root_disk_price }, { "service_id": self.service_nano_id, "price": self.nano_price }] self.tariff = Tariff.create_tariff( self.localized_name("Tariff for customers"), "tariff!!!", "rub", services=services) self.tariff.mark_immutable() self.tariff.make_default() self.loop = asyncio.get_event_loop()
def test_get(self): with self.expect_error(errors.Unauthorized): self.cabinet_client.get("/lk_api/0/news/", auth_required=False) self.create_news() news_list = self.admin_client.news.list() self.assertEqual(len(news_list['items']), 2) self.assertEqual(news_list['items'][0]['news_id'], 2) news_list = self.admin_client.news.list(visible=False) self.assertEqual(len(news_list['items']), 4) news_list = self.admin_client.news.list(visible=False, published=True) self.assertEqual(len(news_list['items']), 2) # check filtering by creation date hour_ago = utcnow().replace(hours=-1).datetime in_hour = utcnow().replace(hours=+1).datetime filtered_news = self.admin_client.news.list(visible=False, deleted_before=in_hour) self.assertEqual(len(filtered_news['items']), 2) filtered_news = self.admin_client.news.list(visible=False, deleted_before=hour_ago) self.assertEqual(len(filtered_news['items']), 0) filtered_news = self.admin_client.news.list(visible=False, deleted_after=in_hour) self.assertEqual(len(filtered_news['items']), 0) filtered_news = self.admin_client.news.list(visible=False, deleted_after=hour_ago) self.assertEqual(len(filtered_news['items']), 2) filtered_news = self.admin_client.news.list(visible=False, published_before=in_hour) self.assertEqual(len(filtered_news['items']), 2) filtered_news = self.admin_client.news.list(visible=False, published_before=hour_ago) self.assertEqual(len(filtered_news['items']), 0) filtered_news = self.admin_client.news.list(visible=False, published_after=in_hour) self.assertEqual(len(filtered_news['items']), 0) filtered_news = self.admin_client.news.list(visible=False, published_after=hour_ago) self.assertEqual(len(filtered_news['items']), 2) with self.expect_error(errors.BadRequest): self.admin_client.news.list(sort='+published') self.tariff = Tariff.create_tariff(self.localized_name("Tariff for customers"), "tariff!!!", "RUB", None) db.session.commit() password = "******" email = "*****@*****.**" self.cabinet_client.customer.create(email=email, password=password) self.cabinet_client.auth(email, password) self.cabinet_client.get("/lk_api/0/news/")
def test_tariff_history(self): t = Tariff.create_tariff(self.localized_name("tariff_and_services"), "Test tariff with services", "RUB", None) user = User.query.first() TariffHistory.create(TariffHistory.EVENT_CREATE, user.user_id, t) t.update(services=[{"service_id": "m1.small", "price": "12.3456"}]) TariffHistory.create(TariffHistory.EVENT_UPDATE, user.user_id, t) db.session.commit() self.assertEqual(TariffHistory.query.count(), 2) for x in range(10): t.update(services=[{"service_id": "m1.small", "price": "12.3456" + str(x)}]) TariffHistory.create(TariffHistory.EVENT_UPDATE, user.user_id, t) db.session.commit() self.assertEqual(TariffHistory.query.count(), 2)
def test_tariff_update(self): lname = self.localized_name("tariff_update") tariff = Tariff.create_tariff(lname, "Tariff Update", "RUB", None) db.session.commit() tariff.update({"tr": "Tariffo"}) db.session.commit() tariff.update({"en": "Super Tariff"}) tariff.update(description="new description") db.session.commit() t = Tariff.get_by_id(tariff.tariff_id) localizations = {l.language: l for l in t.localized_name} self.assertEqual(localizations["en"].localized_name, "Super Tariff") self.assertEqual(localizations["tr"].localized_name, "Tariffo") self.assertEqual(t.description, "new description")
def test_tariff_add_service(self): t = Tariff.create_tariff(self.localized_name("tariff_and_services"), "Test tariff with services", "RUB", None) t.update(services=[{"service_id": "m1.small", "price": "12.3456"}]) db.session.commit() tt = Tariff.get_by_id(t.tariff_id) self.assertEqual(len(tt.services), 1) t.update(services=[{"service_id": "m1.small", "price": "23.1"}]) db.session.commit() tt = Tariff.get_by_id(t.tariff_id) self.assertEqual(tt.services_as_dict()["m1.small"].price, Decimal("23.1")) t.update(services=[{"service_id": "m1.micro", "price": "231.333333"}]) db.session.commit() tt = Tariff.get_by_id(t.tariff_id) self.assertEqual(tt.services_as_dict()["m1.micro"].price, Decimal("231.333333")) self.assertEqual(len(tt.services), 1) # m1.small was removed
def test_tariff_history(self): t = Tariff.create_tariff(self.localized_name("tariff_and_services"), "Test tariff with services", "RUB", None) user = User.query.first() TariffHistory.create(TariffHistory.EVENT_CREATE, user.user_id, t) t.update(services=[{"service_id": "m1.small", "price": "12.3456"}]) TariffHistory.create(TariffHistory.EVENT_UPDATE, user.user_id, t) db.session.commit() self.assertEqual(TariffHistory.query.count(), 2) for x in range(10): t.update(services=[{ "service_id": "m1.small", "price": "12.3456" + str(x) }]) TariffHistory.create(TariffHistory.EVENT_UPDATE, user.user_id, t) db.session.commit() self.assertEqual(TariffHistory.query.count(), 2)
def setUp(self): super().setUp() self.collector = Collector() self.image_size_price = "12.34" self.volume_size_price = "12.37" self.root_disk_price = "2.91" self.nano_price = "3.47" services = [{"service_id": "storage.image", "price": self.image_size_price}, {"service_id": "storage.volume", "price": self.volume_size_price}, {"service_id": "storage.disk", "price": self.root_disk_price}, {"service_id": self.service_nano_id, "price": self.nano_price} ] self.tariff = Tariff.create_tariff(self.localized_name("Tariff for customers"), "tariff!!!", "rub", services=services) self.tariff.mark_immutable() self.tariff.make_default() self.loop = asyncio.get_event_loop()
def new_tariff(self, token, localized_name, description, currency, services=None, parent_id=None, all_parameters=None): """ Create new tariff. Parameters must be sent as json object. :param LocalizedName localized_name: Dict with name localization. en is mandatory key ``{"en": "Name", "ru": "\u0418\u043c\u044f"}`` :param str description: Tariff description :param str currency: Currency code :param TariffId parent_id: Parent tariff id :param list services: List services and its prices **Example of list**:: services: [ { "service_id": "m1.small", "price": "12.23" }, { "service_id": "m1.medium", "price": "21.32" } ] } :return dict tariff_info: Dict as returned by :obj:`GET /0/tariff/\<tariff\>/ <view.GET /0/tariff/\<tariff\>>` """ if parent_id: parent = Tariff.get_by_id(parent_id) if not parent: raise errors.TariffNotFound() if parent.currency != currency: raise errors.ParentTariffCurrency() created = Tariff.create_tariff(**all_parameters) TariffHistory.create(TariffHistory.EVENT_CREATE, token.user_id, created) return {"tariff_info": display(created)}
def setUp(self): super().setUp() self.tariff = Tariff.create_tariff(self.localized_name("Tariff for customers"), "tariff!!!", "RUB", None) db.session.commit()
def setUp(self): super().setUp() self.tariff = Tariff.create_tariff( self.localized_name("Tariff for customers"), "tariff!!!", "RUB", None) db.session.commit()
def test_tariff_create(self): lname = self.localized_name("tariff1") Tariff.create_tariff(lname, "tariff!!!", "RUB", None) db.session.commit() with self.assertRaises(errors.TariffAlreadyExists): Tariff.create_tariff(lname, "tariff!!!", "RUB", None) db.session.rollback() Tariff.create_tariff({ "en": "tt", "ru": "tt" }, "tariff!!!", "RUB", None) name = lname.copy() name["en"] = "xxxx" with self.assertRaises(errors.TariffAlreadyExists): Tariff.create_tariff(name, "tariff!!!", "RUB", None) db.session.rollback() name = lname.copy() name["ru"] = "xxxx" with self.assertRaises(errors.TariffAlreadyExists): Tariff.create_tariff(name, "tariff!!!", "RUB", None) db.session.rollback() name["ru"] = lname["en"] name["en"] = lname["ru"] Tariff.create_tariff(name, "tariff!!!", "RUB", None)
def test_get(self): with self.expect_error(errors.Unauthorized): self.cabinet_client.get("/lk_api/0/news/", auth_required=False) self.create_news() news_list = self.admin_client.news.list() self.assertEqual(len(news_list['items']), 2) self.assertEqual(news_list['items'][0]['news_id'], 2) news_list = self.admin_client.news.list(visible=False) self.assertEqual(len(news_list['items']), 4) news_list = self.admin_client.news.list(visible=False, published=True) self.assertEqual(len(news_list['items']), 2) # check filtering by creation date hour_ago = utcnow().replace(hours=-1).datetime in_hour = utcnow().replace(hours=+1).datetime filtered_news = self.admin_client.news.list(visible=False, deleted_before=in_hour) self.assertEqual(len(filtered_news['items']), 2) filtered_news = self.admin_client.news.list(visible=False, deleted_before=hour_ago) self.assertEqual(len(filtered_news['items']), 0) filtered_news = self.admin_client.news.list(visible=False, deleted_after=in_hour) self.assertEqual(len(filtered_news['items']), 0) filtered_news = self.admin_client.news.list(visible=False, deleted_after=hour_ago) self.assertEqual(len(filtered_news['items']), 2) filtered_news = self.admin_client.news.list(visible=False, published_before=in_hour) self.assertEqual(len(filtered_news['items']), 2) filtered_news = self.admin_client.news.list(visible=False, published_before=hour_ago) self.assertEqual(len(filtered_news['items']), 0) filtered_news = self.admin_client.news.list(visible=False, published_after=in_hour) self.assertEqual(len(filtered_news['items']), 0) filtered_news = self.admin_client.news.list(visible=False, published_after=hour_ago) self.assertEqual(len(filtered_news['items']), 2) with self.expect_error(errors.BadRequest): self.admin_client.news.list(sort='+published') self.tariff = Tariff.create_tariff( self.localized_name("Tariff for customers"), "tariff!!!", "RUB", None) db.session.commit() password = "******" email = "*****@*****.**" self.cabinet_client.customer.create(email=email, password=password) self.cabinet_client.auth(email, password) self.cabinet_client.get("/lk_api/0/news/")
def test_tariff_create(self): lname = self.localized_name("tariff1") Tariff.create_tariff(lname, "tariff!!!", "RUB", None) db.session.commit() with self.assertRaises(errors.TariffAlreadyExists): Tariff.create_tariff(lname, "tariff!!!", "RUB", None) db.session.rollback() Tariff.create_tariff({"en": "tt", "ru": "tt"}, "tariff!!!", "RUB", None) name = lname.copy() name["en"] = "xxxx" with self.assertRaises(errors.TariffAlreadyExists): Tariff.create_tariff(name, "tariff!!!", "RUB", None) db.session.rollback() name = lname.copy() name["ru"] = "xxxx" with self.assertRaises(errors.TariffAlreadyExists): Tariff.create_tariff(name, "tariff!!!", "RUB", None) db.session.rollback() name["ru"] = lname["en"] name["en"] = lname["ru"] Tariff.create_tariff(name, "tariff!!!", "RUB", None)