Skip to content

Commit

Permalink
Add unit test for Coriolis api midddleware auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristi1324 committed Nov 1, 2023
1 parent 64d86bf commit 51988db
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
Empty file added coriolis/tests/api/__init__.py
Empty file.
Empty file.
76 changes: 76 additions & 0 deletions coriolis/tests/api/middleware/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright 2022 Cloudbase Solutions Srl
# All Rights Reserved.

from unittest import mock

from oslo_middleware import request_id
from oslo_serialization import jsonutils

from coriolis.api.middleware.auth import CoriolisKeystoneContext
from coriolis.api import wsgi
from coriolis import context
from coriolis.tests import test_base


class CoriolisKeystoneContextTestCase(test_base.CoriolisBaseTestCase):
"""Test suite for the Coriolis api middleware auth."""

@mock.patch.object(jsonutils, "loads")
@mock.patch.object(context, "RequestContext")
def test__call__(self, mock_request_context, mock_jsonutils_loads):
req_mock = mock.Mock()
headers_mock = mock.Mock()
environ_mock = {}
remote_addr_mock = mock.Mock()

req_mock.headers = headers_mock
req_mock.environ = environ_mock
req_mock.remote_addr = remote_addr_mock

mock_user = 'mock_user'
mock_id = 'mock_id'
mock_project_id = '1'
mock_project_name = 'mock_project_name'
mock_project_domain_name = 'mock_project_domain_name'
mock_user_domain_name = 'mock_user_domain_name'
mock_auth_token = 'mock_token123'
mock_remote_address = 'mock_addr'
mock_service_catalog = 'mock_service_catalog'
mock_req_id = 'mock_req_id'

req_mock.remote_addr = mock_remote_address
mock_jsonutils_loads.return_value = mock_service_catalog
req_mock.environ[request_id.ENV_REQUEST_ID] = mock_req_id

req_mock.headers = {
'X_USER': mock_user,
'X_USER_ID': mock_id,
'X_ROLE': '1,2,3',
'X_TENANT_ID': mock_project_id,
'X_TENANT': 'mock_tenant',
'X_TENANT_NAME': mock_project_name,
'X-Project-Domain-Name': mock_project_domain_name,
'X-User-Domain-Name': mock_user_domain_name,
'X_AUTH_TOKEN': mock_auth_token,
'X_SERVICE_CATALOG': 'mock_catalog',
}

mock_roles = [
r.strip() for r in req_mock.headers.get('X_ROLE', '').split(',')
]

result = CoriolisKeystoneContext(wsgi.Middleware)
result = result(req_mock)

mock_request_context.assert_called_once_with(
mock_id,
mock_project_id,
project_name=mock_project_name,
project_domain_name=mock_project_domain_name,
user_domain_name=mock_user_domain_name,
roles=mock_roles,
auth_token=mock_auth_token,
remote_address=mock_remote_address,
service_catalog=mock_service_catalog,
request_id=mock_req_id
)
Empty file.
Empty file.

0 comments on commit 51988db

Please sign in to comment.