-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
135 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,70 @@ | ||
# Copyright 2021 UW-IT, University of Washington | ||
# Copyright 2022 UW-IT, University of Washington | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import json | ||
import logging | ||
import os | ||
from os.path import abspath, dirname | ||
from restclients_core.dao import DAO | ||
from restclients_core.exceptions import DataFailureException | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class MyPlan_Auth_DAO(DAO): | ||
def service_name(self): | ||
return "myplan_auth" | ||
|
||
def _is_cacheable(self, method, url, headers, body=None): | ||
return True | ||
|
||
def get_auth_token(self, secret): | ||
url = "/oauth2/token" | ||
headers = {"Authorization": "Basic {}".format(secret), | ||
"Content-type": "application/x-www-form-urlencoded"} | ||
|
||
response = self.postURL(url, headers, "grant_type=client_credentials") | ||
logger.debug( | ||
{'url': url, | ||
'status': response.status, | ||
'data': response.data}) | ||
if response.status != 200: | ||
raise DataFailureException(url, response.status, response.data) | ||
|
||
data = json.loads(response.data) | ||
return data.get("access_token", "") | ||
|
||
def service_mock_paths(self): | ||
return [abspath(os.path.join(dirname(__file__), "resources"))] | ||
|
||
def _edit_mock_response(self, method, url, headers, body, response): | ||
if response.status == 404 and method != "GET": | ||
alternative_url = "{0}.{1}".format(url, method) | ||
backend = self.get_implementation() | ||
new_resp = backend.load(method, alternative_url, headers, body) | ||
response.status = new_resp.status | ||
response.data = new_resp.data | ||
logger.debug( | ||
{'url': alternative_url, | ||
'status': response.status, | ||
'data': response.data}) | ||
|
||
|
||
class MyPlan_DAO(DAO): | ||
|
||
def __init__(self): | ||
self.auth_dao = MyPlan_Auth_DAO() | ||
return super(MyPlan_DAO, self).__init__() | ||
|
||
def service_name(self): | ||
return 'myplan' | ||
|
||
def service_mock_paths(self): | ||
return [abspath(os.path.join(dirname(__file__), "resources"))] | ||
|
||
def _custom_headers(self, method, url, headers, body): | ||
headers = {} | ||
secret = self.get_service_setting("AUTH_SECRET", "") | ||
if secret: | ||
headers["Authorization"] = self.auth_dao.get_auth_token(secret) | ||
return headers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"access_token":"eyJraWQiOmYmEwwvOQ", | ||
"expires_in":3600, | ||
"token_type":"Bearer" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright 2022 UW-IT, University of Washington | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from unittest import TestCase | ||
import mock | ||
from commonconf import override_settings | ||
from restclients_core.exceptions import DataFailureException | ||
from uw_myplan.dao import MyPlan_Auth_DAO, MyPlan_DAO | ||
from uw_myplan.utils import ( | ||
fdao_myplan_override, fdao_myplan_auth_override) | ||
|
||
|
||
@fdao_myplan_auth_override | ||
@fdao_myplan_override | ||
class TestMyPlanAuth(TestCase): | ||
|
||
def test_is_cacheable(self): | ||
auth = MyPlan_Auth_DAO() | ||
self.assertTrue(auth._is_cacheable("POST", "/", {}, "")) | ||
|
||
def test_get_auth_token(self): | ||
self.assertIsNotNone( | ||
MyPlan_Auth_DAO().get_auth_token("test1")) | ||
|
||
def test_no_auth_header(self): | ||
headers = MyPlan_DAO()._custom_headers("GET", "/", {}, "") | ||
self.assertFalse("Authorization" in headers) | ||
|
||
@override_settings(RESTCLIENTS_MYPLAN_AUTH_SECRET="test1") | ||
@mock.patch.object(MyPlan_Auth_DAO, "get_auth_token") | ||
def test_auth_header(self, mock_get_auth_token): | ||
mock_get_auth_token.return_value = "abcdef" | ||
headers = MyPlan_DAO()._custom_headers("GET", "/", {}, "") | ||
self.assertTrue("Authorization" in headers) | ||
self.assertEqual(headers["Authorization"], "abcdef") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Copyright 2022 UW-IT, University of Washington | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from commonconf import override_settings | ||
|
||
fdao_myplan_override = override_settings( | ||
RESTCLIENTS_MYPLAN_DAO_CLASS='Mock') | ||
fdao_myplan_auth_override = override_settings( | ||
RESTCLIENTS_MYPLAN_AUTH_DAO_CLASS='Mock') |