forked from keylime/keylime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_messages.py
More file actions
157 lines (116 loc) · 5.02 KB
/
Copy pathtest_api_messages.py
File metadata and controls
157 lines (116 loc) · 5.02 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
Unit tests for keylime.web.base.api_messages module
"""
import unittest
# Import directly from submodules to avoid circular import
from keylime.web.base.api_messages.api_error import APIError
from keylime.web.base.api_messages.api_info import APIInfo
from keylime.web.base.api_messages.api_links import APILink
from keylime.web.base.api_messages.api_message_body import APIMessageBody
from keylime.web.base.api_messages.api_meta import APIMeta
from keylime.web.base.api_messages.api_resource import APIResource
class TestAPIMessageBody(unittest.TestCase):
"""Test cases for APIMessageBody"""
def test_api_message_body_initialization(self):
"""Test that APIMessageBody initializes correctly"""
body = APIMessageBody()
self.assertIsNotNone(body)
def test_api_message_body_with_resource(self):
"""Test APIMessageBody with a resource"""
resource = APIResource("agents", "test-123")
body = APIMessageBody(resource)
rendered = body.render()
self.assertIn("data", rendered)
def test_api_message_body_add_resource(self):
"""Test adding a resource to message body"""
resource = APIResource("agents", "test-456")
body = APIMessageBody()
body.add_resource(resource)
rendered = body.render()
self.assertIn("data", rendered)
class TestAPIError(unittest.TestCase):
"""Test cases for APIError"""
def test_api_error_initialization(self):
"""Test that APIError initializes correctly"""
error = APIError("invalid_request")
self.assertIsNotNone(error)
def test_api_error_with_detail(self):
"""Test APIError with detail"""
error = APIError("not_found", "Resource not found")
rendered = error.render()
self.assertIn("code", rendered)
self.assertEqual(rendered["code"], "not_found")
def test_api_error_with_http_code(self):
"""Test APIError with HTTP code and detail"""
error = APIError("validation_error", 422, "Invalid input")
rendered = error.render()
self.assertIn("code", rendered)
self.assertIn("status", rendered)
self.assertEqual(rendered["status"], "422")
class TestAPIResource(unittest.TestCase):
"""Test cases for APIResource"""
def test_api_resource_initialization(self):
"""Test that APIResource initializes correctly"""
resource = APIResource("agents", "test-agent-123")
self.assertIsNotNone(resource)
self.assertEqual(resource.type, "agents")
self.assertEqual(resource.id, "test-agent-123")
def test_api_resource_render(self):
"""Test APIResource rendering"""
resource = APIResource("agents", "test-123")
rendered = resource.render()
self.assertEqual(rendered["type"], "agents")
self.assertEqual(rendered["id"], "test-123")
class TestAPILink(unittest.TestCase):
"""Test cases for APILink"""
def test_api_link_initialization(self):
"""Test that APILink initializes correctly"""
link = APILink("self", "/v3.0/agents")
self.assertIsNotNone(link)
def test_api_link_render(self):
"""Test APILink rendering"""
link = APILink("related", "/v3.0/agents/123/relationships/office")
rendered = link.render()
self.assertIsInstance(rendered, str)
class TestAPIMeta(unittest.TestCase):
"""Test cases for APIMeta"""
def test_api_meta_initialization(self):
"""Test that APIMeta initializes correctly"""
meta = APIMeta("version", "3.0")
self.assertIsNotNone(meta)
def test_api_meta_render(self):
"""Test APIMeta rendering"""
meta = APIMeta("timestamp", "2025-01-15T12:00:00Z")
rendered = meta.render()
self.assertIsInstance(rendered, str)
class TestAPIInfo(unittest.TestCase):
"""Test cases for APIInfo"""
def test_api_info_initialization(self):
"""Test that APIInfo initializes correctly"""
info = APIInfo()
self.assertIsNotNone(info)
def test_api_info_render(self):
"""Test APIInfo rendering"""
info = APIInfo()
rendered = info.render()
self.assertIsInstance(rendered, dict)
class TestAPIMessageIntegration(unittest.TestCase):
"""Integration tests for API message components"""
def test_complete_success_response(self):
"""Test building a complete success response"""
# Create resource
resource = APIResource("agents", "test-agent-123")
# Create message body with resource
body = APIMessageBody(resource)
rendered = body.render()
self.assertIn("data", rendered)
self.assertEqual(rendered["data"]["type"], "agents")
self.assertEqual(rendered["data"]["id"], "test-agent-123")
def test_message_body_with_error(self):
"""Test building an error response"""
error = APIError("not_found", 404, "Resource not found")
body = APIMessageBody(error)
rendered = body.render()
self.assertIn("errors", rendered)
if __name__ == "__main__":
unittest.main()