Browse Source

[ADD] emca: subscription-request/id_/validate route

pull/116/head
robin.keunen 5 years ago
parent
commit
b0c06835ea
  1. 42
      easy_my_coop/demo/coop.xml
  2. 8
      easy_my_coop_api/demo/demo.xml
  3. 0
      easy_my_coop_api/services/account_invoice_service.py
  4. 8
      easy_my_coop_api/services/schemas.py
  5. 32
      easy_my_coop_api/services/subscription_request_service.py
  6. 30
      easy_my_coop_api/tests/common.py
  7. 38
      easy_my_coop_api/tests/test_subscription_requests.py

42
easy_my_coop/demo/coop.xml

@ -76,8 +76,16 @@
<field name="reconcile" eval="True"/> <field name="reconcile" eval="True"/>
</record> </record>
<record id="account_equity_demo" model="account.account">
<field name="code">100910</field>
<field name="name">Variable Capital</field>
<field eval="True" name="reconcile"/>
<field name="user_type_id" ref="account.data_account_type_equity"/>
</record>
<record id="base.main_company" model="res.company"> <record id="base.main_company" model="res.company">
<field name="property_cooperator_account" ref="account_cooperator_demo"/>
<field name="property_cooperator_account"
ref="account_cooperator_demo"/>
</record> </record>
<record id="base.main_company" model="res.company"> <record id="base.main_company" model="res.company">
@ -127,6 +135,38 @@
<field name="skip_control_ng" eval="True"/> <field name="skip_control_ng" eval="True"/>
</record> </record>
<record id="subscription_request_waiting_demo" model="subscription.request">
<field name="name">Catherine des Champs</field>
<field name="email">catherine@demo.net</field>
<field name="address">Chemin des bois fleuris</field>
<field name="zip_code">1000</field>
<field name="city">Brussels</field>
<field name="country_id" ref="base.be"/>
<field name="date" eval="datetime.now() - timedelta(days=60)"/>
<field name="source">manual</field>
<field name="ordered_parts">4</field>
<field name="share_product_id" model="product.template"
eval="obj(ref('product_template_share_type_1_demo')).product_variant_id.id"/>
<field name="lang">en_US</field>
<field name="skip_control_ng" eval="True"/>
<field name="state">waiting</field>
</record>
<record id="res_partner_cooperator_candidate_1_demo" model="res.partner">
<field name="name">Catherine des Champs</field>
<field name="customer" eval="True"/>
<field name="is_company" eval="False"/>
<field name="email">catherine@demo.net</field>
<field name="street">Chemin des bois fleuris, 2</field>
<field name="city">Brussels</field>
<field name="zip">1000</field>
<field name="country_id" ref="base.be"/>
<field name="member" eval="False"/>
<field name="data_policy_approved" eval="True"/>
<field name="internal_rules_approved" eval="True"/>
</record>
<record id="share_line_1_demo" model="share.line"> <record id="share_line_1_demo" model="share.line">
<field name="share_product_id" model="product.template" <field name="share_product_id" model="product.template"
eval="obj(ref('product_template_share_type_1_demo')).product_variant_id.id"/> eval="obj(ref('product_template_share_type_1_demo')).product_variant_id.id"/>

8
easy_my_coop_api/demo/demo.xml

@ -4,7 +4,15 @@
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
--> -->
<odoo> <odoo>
<record id="auth_api_key_manager_emc_demo" model="auth.api.key">
<field name="user_id" ref="easy_my_coop.res_users_manager_emc_demo"/>
<field name="key">cbd07f57-c903-43b4-b668-436b3bec5f15</field>
</record>
<record id="easy_my_coop.subscription_request_1_demo" model="subscription.request"> <record id="easy_my_coop.subscription_request_1_demo" model="subscription.request">
<field name="external_id">1</field> <field name="external_id">1</field>
</record> </record>
<record id="easy_my_coop.subscription_request_2_demo" model="subscription.request">
<field name="external_id">2</field>
</record>
</odoo> </odoo>

0
easy_my_coop_api/services/account_invoice_service.py

8
easy_my_coop_api/services/schemas.py

@ -41,6 +41,12 @@ S_SUBSCRIPTION_REQUEST_RETURN_GET = {
}, },
}, },
"lang": {"type": "string", "required": True, "empty": False}, "lang": {"type": "string", "required": True, "empty": False},
"capital_release_request": {
"type": "list",
"schema": {"type": "integer"},
"required": True,
"empty": True,
},
} }
S_SUBSCRIPTION_REQUEST_SEARCH = { S_SUBSCRIPTION_REQUEST_SEARCH = {
@ -93,3 +99,5 @@ S_SUBSCRIPTION_REQUEST_UPDATE = {
"lang": {"type": "string"}, "lang": {"type": "string"},
"share_product": {"type": "integer"}, "share_product": {"type": "integer"},
} }
S_SUBSCRIPTION_REQUEST_VALIDATE = {"_id": {"type": "integer"}}

32
easy_my_coop_api/services/subscription_request_service.py

@ -75,9 +75,34 @@ class SubscriptionRequestService(Component):
sr.write(params) sr.write(params)
return self._to_dict(sr) return self._to_dict(sr)
def validate(self, _id, **params):
sr = self.env["subscription.request"].search(
[("external_id", "=", _id)]
)
if not sr:
raise wrapJsonException(
NotFound(_("No subscription request for id %s") % _id)
)
if sr.state != "draft":
raise wrapJsonException(
BadRequest(
_("Subscription request %s is not in draft state") % _id
)
)
sr.validate_subscription_request()
return self._to_dict(sr)
def _to_dict(self, sr): def _to_dict(self, sr):
sr.ensure_one() sr.ensure_one()
if sr.capital_release_request:
invoice_ids = [
invoice.get_external_id()
for invoice in sr.capital_release_request
]
else:
invoice_ids = []
return { return {
"id": sr.get_external_id(), "id": sr.get_external_id(),
"name": sr.name, "name": sr.name,
@ -96,6 +121,7 @@ class SubscriptionRequestService(Component):
"country": sr.country_id.code, "country": sr.country_id.code,
}, },
"lang": sr.lang, "lang": sr.lang,
"capital_release_request": invoice_ids,
} }
def _get_country(self, code): def _get_country(self, code):
@ -191,3 +217,9 @@ class SubscriptionRequestService(Component):
def _validator_return_update(self): def _validator_return_update(self):
return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET
def _validator_validate(self):
return schemas.S_SUBSCRIPTION_REQUEST_VALIDATE
def _validator_return_validate(self):
return schemas.S_SUBSCRIPTION_REQUEST_RETURN_GET

30
easy_my_coop_api/tests/common.py

@ -17,31 +17,32 @@ HOST = "127.0.0.1"
PORT = odoo.tools.config["http_port"] PORT = odoo.tools.config["http_port"]
def _add_api_key(headers):
key_dict = {"API-KEY": "api-key"}
if headers:
headers.update(key_dict)
else:
headers = key_dict
return headers
class BaseEMCRestCase(BaseRestCase): class BaseEMCRestCase(BaseRestCase):
@classmethod @classmethod
def setUpClass(cls, *args, **kwargs): def setUpClass(cls, *args, **kwargs):
super().setUpClass(*args, **kwargs) super().setUpClass(*args, **kwargs)
cls.AuthApiKey = cls.env["auth.api.key"] cls.AuthApiKey = cls.env["auth.api.key"]
emc_manager = cls.env.ref("easy_my_coop.res_users_manager_emc_demo")
cls.api_key_test = cls.AuthApiKey.create(
{"name": "test-key", "key": "api-key", "user_id": emc_manager.id}
cls.api_key_test = cls.env.ref(
"easy_my_coop_api.auth_api_key_manager_emc_demo"
) )
def _add_api_key(self, headers):
key_dict = {"API-KEY": self.api_key_test.key}
if headers:
headers.update(key_dict)
else:
headers = key_dict
return headers
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.session = requests.Session() self.session = requests.Session()
self.demo_request_1 = self.browse_ref( self.demo_request_1 = self.browse_ref(
"easy_my_coop.subscription_request_1_demo" "easy_my_coop.subscription_request_1_demo"
) )
self.demo_request_2 = self.browse_ref(
"easy_my_coop.subscription_request_2_demo"
)
self.demo_share_product = ( self.demo_share_product = (
self.demo_request_1.share_product_id.product_tmpl_id self.demo_request_1.share_product_id.product_tmpl_id
) )
@ -65,10 +66,11 @@ class BaseEMCRestCase(BaseRestCase):
"country": "BE", "country": "BE",
}, },
"lang": "en_US", "lang": "en_US",
"capital_release_request": [],
} }
def http_get(self, url, headers=None): def http_get(self, url, headers=None):
headers = _add_api_key(headers)
headers = self._add_api_key(headers)
if url.startswith("/"): if url.startswith("/"):
url = "http://{}:{}{}".format(HOST, PORT, url) url = "http://{}:{}{}".format(HOST, PORT, url)
@ -81,7 +83,7 @@ class BaseEMCRestCase(BaseRestCase):
return json.loads(content) return json.loads(content)
def http_post(self, url, data, headers=None): def http_post(self, url, data, headers=None):
headers = _add_api_key(headers)
headers = self._add_api_key(headers)
if url.startswith("/"): if url.startswith("/"):
url = "http://{}:{}{}".format(HOST, PORT, url) url = "http://{}:{}{}".format(HOST, PORT, url)

38
easy_my_coop_api/tests/test_subscription_requests.py

@ -5,6 +5,8 @@
import json import json
from datetime import timedelta from datetime import timedelta
from werkzeug.exceptions import BadRequest
import odoo import odoo
from odoo.fields import Date from odoo.fields import Date
@ -22,27 +24,29 @@ class TestSRController(BaseEMCRestCase):
model_name="rest.service.registration", collection=collection model_name="rest.service.registration", collection=collection
) )
self.service = emc_services_env.component(usage="subscription-request")
self.sr_service = emc_services_env.component(
usage="subscription-request"
)
def test_service(self): def test_service(self):
# kept as example # kept as example
# useful if you need to change data in database and check db type # useful if you need to change data in database and check db type
result = self.service.get(self.demo_request_1.id)
result = self.sr_service.get(self.demo_request_1.external_id)
self.assertEquals(self.demo_request_1_dict, result) self.assertEquals(self.demo_request_1_dict, result)
all_sr = self.service.search()
all_sr = self.sr_service.search()
self.assertTrue(all_sr) self.assertTrue(all_sr)
sr_date = self.demo_request_1.date sr_date = self.demo_request_1.date
date_from = Date.to_string(sr_date - timedelta(days=1)) date_from = Date.to_string(sr_date - timedelta(days=1))
date_to = Date.to_string(sr_date + timedelta(days=1)) date_to = Date.to_string(sr_date + timedelta(days=1))
date_sr = self.service.search(date_from=date_from, date_to=date_to)
date_sr = self.sr_service.search(date_from=date_from, date_to=date_to)
self.assertTrue(date_sr) self.assertTrue(date_sr)
def test_route_get(self): def test_route_get(self):
id_ = self.demo_request_1.id
id_ = self.demo_request_1.external_id
route = "/api/subscription-request/%s" % id_ route = "/api/subscription-request/%s" % id_
content = self.http_get_content(route) content = self.http_get_content(route)
self.assertEquals(self.demo_request_1_dict, content) self.assertEquals(self.demo_request_1_dict, content)
@ -126,12 +130,13 @@ class TestSRController(BaseEMCRestCase):
"id": self.demo_share_product.id, "id": self.demo_share_product.id,
"name": self.demo_share_product.name, "name": self.demo_share_product.name,
}, },
"capital_release_request": [],
}, },
} }
self.assertEquals(expected, content) self.assertEquals(expected, content)
def test_route_update(self): def test_route_update(self):
url = "/api/subscription-request/%s" % self.demo_request_1.id
url = "/api/subscription-request/%s" % self.demo_request_1.external_id
data = {"state": "done"} data = {"state": "done"}
response = self.http_post(url, data=data) response = self.http_post(url, data=data)
@ -141,3 +146,24 @@ class TestSRController(BaseEMCRestCase):
expected = self.demo_request_1_dict expected = self.demo_request_1_dict
expected["state"] = "done" expected["state"] = "done"
self.assertEquals(expected, content) self.assertEquals(expected, content)
def test_route_validate(self):
url = (
"/api/subscription-request/%s/validate"
% self.demo_request_1.external_id
)
response = self.http_post(url, data={})
self.assertEquals(response.status_code, 200)
content = json.loads(response.content.decode("utf-8"))
state = content.get("state")
self.assertEquals(state, "done")
def test_service_validate_draft_request(self):
self.sr_service.validate(self.demo_request_1.external_id)
self.assertEquals(self.demo_request_1.state, "done")
self.assertTrue(len(self.demo_request_1.capital_release_request) > 0)
def test_service_validate_done_request(self):
with self.assertRaises(BadRequest):
self.sr_service.validate(self.demo_request_2.external_id)
Loading…
Cancel
Save