trytondo-sale_order/tests/test_api_sale_order.py

292 lines
9.2 KiB
Python

from trytond.tests.test_tryton import RouteTestCase
from trytond.pool import Pool
from http import HTTPStatus
import uuid
import json
from trytond.tests.test_tryton import DB_NAME
from trytond.transaction import Transaction
from trytond.modules.company.tests.tools import create_company
from trytond.tests.tools import activate_modules
class SaleOrderApiRouteTestCase(RouteTestCase):
"""Sale Order API Routes"""
module = "sale_order"
key = uuid.uuid4().hex
@classmethod
def setUpClass(cls):
activate_modules("sale_order", create_company)
def setUp(self):
pool = Pool(DB_NAME)
transaction = Transaction().start(
DB_NAME, 0, _lock_tables=["product_product"])
with transaction:
Party = pool.get("party.party")
ProductTemplate = pool.get("product.template")
Product = pool.get("product.product")
Uom = pool.get("product.uom")
Application = pool.get("res.user.application")
Application(
key=self.key,
user=1,
application="sale_order",
state="validated"
).save()
self.unit = Uom.search([("name", "=", "Unit")])[0].id
(self.productTemplate,) = ProductTemplate.create(
[
{
"name": "Product",
"default_uom": self.unit,
}
]
)
(self.product,) = Product.create([
{"template": self.productTemplate.id}
])
(self.party,) = Party.create(
[
{
"name": "Dunkan",
"contact_mechanisms": [[
"create", [{
"type": "mobile",
"value": "3102223334"}]]
],
}
]
)
def test_post_sale_order(self):
client = self.client()
response = client.post(
f"/{self.db_name}/sale_order/order",
headers={
"Authorization": f"bearer {self.key}",
},
data=json.dumps(
{
"party": self.party.id,
"pickup_location": "on_site",
"lines": [
[
"create",
[
{
"product": self.product.id,
"unit": self.unit,
"quantity": "5",
"unitprice": "10",
}
],
]
],
}
),
)
self.assertEqual(response.status_code, HTTPStatus.OK)
def test_get_sale_orders(self):
client = self.client()
order = json.loads(
client.post(
f"/{self.db_name}/sale_order/order",
headers={
"Authorization": f"bearer {self.key}",
},
data=json.dumps(
{
"party": self.party.id,
"pickup_location": "on_site",
"lines": [
[
"create",
[
{
"product": self.product.id,
"unit": self.unit,
"quantity": "5",
"unitprice": "10",
}
],
]
],
}
),
)
.get_data()
.decode()
)
response = client.get(
f"/{self.db_name}/sale_order/order/{json.loads(order[0])['id']}",
headers={
"Authorization": f"bearer {self.key}",
},
)
self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertEqual(len(json.loads(response.text)), 1)
def test_confirm_sale_order(self):
client = self.client()
order = json.loads(
client.post(
f"/{self.db_name}/sale_order/order",
headers={
"Authorization": f"bearer {self.key}",
},
data=json.dumps(
{
"party": self.party.id,
"pickup_location": "on_site",
"lines": [
[
"create",
[
{
"product": self.product.id,
"unit": self.unit,
"quantity": "5",
"unitprice": "10",
}
],
]
],
}
),
)
.get_data()
.decode()
)
confirm_url = f"/{self.db_name}/sale_order/confirm_order/"
order_id = json.loads(order[0])["id"]
response = client.post(
f"{confirm_url}{order_id}",
headers={
"Authorization": f"bearer {self.key}",
},
)
response_data = json.loads(json.loads(response.text)[0])
self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertEqual(response_data["state"], "confirmed")
def test_post_line_order(self):
client = self.client()
response = client.post(
f"/{self.db_name}/sale_order/order",
headers={"Authorization": f"bearer {self.key}"},
data=json.dumps({
"party": self.party.id,
"pickup_location": "on_site"}),
)
order = json.loads(json.loads(response.text)[0]).get("id")
response = client.post(
f"/{self.db_name}/sale_order/{order}/order_line",
headers={
"Authorization": f"bearer {self.key}",
},
data=json.dumps(
{
"order": order,
"product": self.product.id,
"unit": self.unit,
"quantity": "5",
"unitprice": "10",
}
),
)
self.assertEqual(response.status_code, HTTPStatus.OK)
def test_get_party_asociate_contact_method(self):
"""
Recibe un numbero de telofono y retorna el tercero asociado
a este numero de telofono.
Args:
phone: string
Return:
party: int
"""
client = self.client()
response = client.get(
f"/{self.db_name}/sale_order/associate_party/3102223334",
headers={
"Authorization": f"bearer {self.key}",
},
)
self.assertEqual(response.status_code, HTTPStatus.OK)
self.assertEqual(json.loads(response.text)[1], 200)
self.assertEqual(json.loads(
json.loads(
response.text
)[0])["associate_party"], 2)
def test_create_contact_method(self):
"""
Crea un tercero asociandolo a un metodo de contacto
Args:
email, phone : string
Return:
party: int
"""
def test_delete_sale(self):
client = self.client()
order = json.loads(
client.post(
f"/{self.db_name}/sale_order/order",
headers={
"Authorization": f"bearer {self.key}",
},
data=json.dumps(
{
"party": self.party.id,
"pickup_location": "on_site",
"lines": [
[
"create",
[
{
"product": self.product.id,
"unit": self.unit,
"quantity": "5",
"unitprice": "10",
}
],
]
],
}
),
)
.get_data()
.decode()
)
order_id = json.loads(order[0])["id"] # Get the id of the order
delete_order = client.delete(
f"/{self.db_name}/sale_order/{order_id}",
headers={"Authorization": f"bearer {self.key}"},
)
self.assertEqual(delete_order.status_code, HTTPStatus.OK)
self.assertEqual(json.loads(delete_order.text)[1], 200)