-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtest_app_user_profile.py
More file actions
112 lines (84 loc) · 4.59 KB
/
Copy pathtest_app_user_profile.py
File metadata and controls
112 lines (84 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# The Okta software accompanied by this notice is provided pursuant to the following terms:
# Copyright © 2026-Present, Okta, Inc.
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
"""Tests for app-user profile tools (per-assignment attribute values)."""
from __future__ import annotations
import json
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from okta_mcp_server.tools.applications.applications import get_app_user, update_app_user_profile
APP_ID = "0oaHRAPP00000001"
USER_ID = "00uJANE000000001"
def _make_ctx():
from tests.conftest import FakeLifespanContext, FakeOktaAuthManager
request_context = MagicMock()
request_context.lifespan_context = FakeLifespanContext(
okta_auth_manager=FakeOktaAuthManager()
)
ctx = MagicMock()
ctx.request_context = request_context
return ctx
def _client_returning(body, execute_error=None):
executor = MagicMock()
executor.create_request = AsyncMock(return_value=({"method": "X"}, None))
if execute_error is not None:
executor.execute = AsyncMock(return_value=(None, None, execute_error))
else:
executor.execute = AsyncMock(return_value=(MagicMock(), body, None))
client = MagicMock()
client.get_request_executor = MagicMock(return_value=executor)
return client
class TestGetAppUser:
@pytest.mark.asyncio
@patch("okta_mcp_server.tools.applications.applications.get_okta_client")
async def test_returns_app_user_json(self, mock_get_client):
app_user = {"id": USER_ID, "scope": "USER", "status": "ACTIVE",
"profile": {"employeeNumber": "E-12345"}}
client = _client_returning(json.dumps(app_user))
mock_get_client.return_value = client
result = await get_app_user(ctx=_make_ctx(), app_id=APP_ID, user_id=USER_ID)
assert result["id"] == USER_ID
assert result["profile"]["employeeNumber"] == "E-12345"
kwargs = client.get_request_executor.return_value.create_request.call_args.kwargs
assert kwargs["method"] == "GET"
assert kwargs["url"].endswith(f"/api/v1/apps/{APP_ID}/users/{USER_ID}")
@pytest.mark.asyncio
@patch("okta_mcp_server.tools.applications.applications.get_okta_client")
async def test_api_error_is_returned(self, mock_get_client):
mock_get_client.return_value = _client_returning(None, execute_error="404 not found")
result = await get_app_user(ctx=_make_ctx(), app_id=APP_ID, user_id=USER_ID)
assert result == {"error": "404 not found"}
class TestUpdateAppUserProfile:
@pytest.mark.asyncio
@patch("okta_mcp_server.tools.applications.applications.get_okta_client")
async def test_posts_profile_to_app_user_endpoint(self, mock_get_client):
returned = {"id": USER_ID, "profile": {"employeeNumber": "E-12345"}}
client = _client_returning(json.dumps(returned))
mock_get_client.return_value = client
profile = {"employeeNumber": "E-12345", "employmentStartDate": "2026-01-01"}
result = await update_app_user_profile(
ctx=_make_ctx(), app_id=APP_ID, user_id=USER_ID, profile=profile
)
assert result == returned
kwargs = client.get_request_executor.return_value.create_request.call_args.kwargs
assert kwargs["method"] == "POST"
assert kwargs["url"].endswith(f"/api/v1/apps/{APP_ID}/users/{USER_ID}")
assert kwargs["body"] == {"profile": profile}
@pytest.mark.asyncio
@patch("okta_mcp_server.tools.applications.applications.get_okta_client")
async def test_api_error_is_returned(self, mock_get_client):
mock_get_client.return_value = _client_returning(None, execute_error="400 bad profile")
result = await update_app_user_profile(
ctx=_make_ctx(), app_id=APP_ID, user_id=USER_ID, profile={"x": 1}
)
assert result == {"error": "400 bad profile"}
@pytest.mark.asyncio
async def test_invalid_id_rejected_before_api_call(self):
result = await update_app_user_profile(
ctx=_make_ctx(), app_id=APP_ID, user_id="../../etc", profile={"x": 1}
)
assert isinstance(result, dict)
assert "error" in result