Browse Source

[ADD] emc_api: api_key authentification

pull/71/head
robin.keunen 5 years ago
parent
commit
10bffeb8c9
  1. 5
      easy_my_coop_api/__manifest__.py
  2. 2
      easy_my_coop_api/controllers/controllers.py
  3. 23
      easy_my_coop_api/tests/common.py
  4. 10
      easy_my_coop_api/tests/test_ping.py
  5. 10
      easy_my_coop_api/tests/test_subscription_requests.py

5
easy_my_coop_api/__manifest__.py

@ -5,7 +5,10 @@
{
"name": "Easy My Coop API",
"version": "12.0.0.0.1",
"depends": ["base_rest", "easy_my_coop"],
"depends": [
"base_rest",
"easy_my_coop",
], # auth_api_key + running_env = dev
"author": "Coop IT Easy SCRLfs",
"category": "Cooperative management",
"website": "www.coopiteasy.be",

2
easy_my_coop_api/controllers/controllers.py

@ -10,7 +10,7 @@ from odoo.http import route
class UserController(main.RestController):
_root_path = "/api/"
_collection_name = "emc.services"
_default_auth = "user"
_default_auth = "api_key"
@route(
_root_path + "<string:_service_name>/test",

23
easy_my_coop_api/tests/common.py

@ -15,6 +15,15 @@ PORT = odoo.tools.config["http_port"]
class BaseEMCRestCase(BaseRestCase):
@classmethod
def setUpClass(cls, *args, **kwargs):
super().setUpClass(*args, **kwargs)
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}
)
def setUp(self):
super().setUp()
self.session = requests.Session()
@ -40,13 +49,19 @@ class BaseEMCRestCase(BaseRestCase):
"lang": "en_US",
}
def http_get(self, url):
def http_get(self, url, headers=None):
key_dict = {"API-KEY": "api-key"}
if headers:
headers.update(key_dict)
else:
headers = key_dict
if url.startswith("/"):
url = "http://%s:%s%s" % (HOST, PORT, url)
return self.session.get(url)
return self.session.get(url, headers=headers)
def http_get_content(self, route):
response = self.http_get(route)
def http_get_content(self, route, headers=None):
response = self.http_get(route, headers=headers)
self.assertEquals(response.status_code, 200)
return json.loads(response.content)

10
easy_my_coop_api/tests/test_ping.py

@ -4,11 +4,12 @@
import json
import requests
from odoo.addons.base_rest.controllers.main import _PseudoCollection
from odoo.addons.component.core import WorkContext
from .common import BaseEMCRestCase
from .common import BaseEMCRestCase, HOST, PORT
class TestPing(BaseEMCRestCase):
@ -24,14 +25,15 @@ class TestPing(BaseEMCRestCase):
self.assertTrue("message" in result)
def test_ping_route(self):
response = self.http_get("/api/ping/test")
# public route
path = "/api/ping/test"
url = "http://%s:%s%s" % (HOST, PORT, path)
response = requests.get(url)
self.assertEquals(response.status_code, 200)
content = json.loads(response.content)
self.assertTrue("message" in content)
def test_search_route(self):
self.login("manager-emc", "demo")
response = self.http_get("/api/ping")
self.assertEquals(response.status_code, 200)

10
easy_my_coop_api/tests/test_subscription_requests.py

@ -3,8 +3,6 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import json
from datetime import date, timedelta
import odoo
from odoo.fields import Date
@ -42,7 +40,6 @@ class TestSRController(BaseEMCRestCase):
self.assertTrue(date_sr)
def test_route_get(self):
self.login("manager-emc", "demo")
id_ = self.demo_request_1.id
route = "/api/subscription_request/%s" % id_
content = self.http_get_content(route)
@ -50,25 +47,21 @@ class TestSRController(BaseEMCRestCase):
@odoo.tools.mute_logger("odoo.addons.base_rest.http")
def test_route_get_returns_not_found(self):
self.login("manager-emc", "demo")
route = "/api/subscription_request/%s" % "99999"
response = self.http_get(route)
self.assertEquals(response.status_code, 404)
def test_route_get_string_returns_method_not_allowed(self):
self.login("manager-emc", "demo")
route = "/api/subscription_request/%s" % "abc"
response = self.http_get(route)
self.assertEquals(response.status_code, 405)
def test_route_search_all(self):
self.login("manager-emc", "demo")
route = "/api/subscription_request"
content = self.http_get_content(route)
self.assertIn(self.demo_request_1_dict, content["rows"])
def test_route_search_by_date(self):
self.login("manager-emc", "demo")
sr_date = self.demo_request_1.date
date_from = Date.to_string(sr_date - timedelta(days=1))
date_to = Date.to_string(sr_date + timedelta(days=1))
@ -98,7 +91,8 @@ class TestSRController(BaseEMCRestCase):
@odoo.tools.mute_logger("odoo.addons.base_rest.http")
def test_route_search_acd_date_returns_bad_request(self):
self.login("manager-emc", "demo")
route = "/api/subscription_request?date_from=%s" % "20200101"
response = self.http_get(route)
self.assertEquals(response.status_code, 400)
# def test_route_create(self):
Loading…
Cancel
Save