Skip to content

Commit 9952165

Browse files
committed
Add a test case for the /api/resources endpoint.
1 parent a647886 commit 9952165

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

src/pyff/test/test_md_api.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import tempfile
33
import unittest
4+
from datetime import datetime, timezone
45

56
import requests
67
from mako.lookup import TemplateLookup
@@ -126,3 +127,78 @@ def test_load_and_query(self):
126127
assert type(info) == dict
127128
assert info['title'] == 'NORDUnet'
128129
assert 'nordu.net' in info['scope']
130+
131+
132+
class PyFFAPITestResources(PipeLineTest):
133+
"""
134+
Runs twill tests using the wsgi-intercept
135+
"""
136+
137+
mdx = None
138+
mdx_template = None
139+
app = None
140+
141+
@classmethod
142+
def setUpClass(cls):
143+
SignerTestCase.setUpClass()
144+
cls.templates = TemplateLookup(directories=[os.path.join(cls.datadir, 'mdx')])
145+
cls.mdx = tempfile.NamedTemporaryFile('w').name
146+
# cls.mdx_template = cls.templates.get_template('mdx.fd')
147+
cls.test01 = os.path.join(cls.datadir, 'metadata', 'test01.xml')
148+
with open(cls.mdx, "w") as fd:
149+
fd.write(
150+
f"""
151+
- when update:
152+
- load:
153+
- {cls.test01}
154+
"""
155+
)
156+
with open(cls.mdx, 'r') as r:
157+
print("".join(r.readlines()))
158+
cls._app = mkapp(cls.mdx)
159+
cls.app = lambda *args, **kwargs: cls._app
160+
161+
@classmethod
162+
def tearDownClass(cls):
163+
SignerTestCase.tearDownClass()
164+
if os.path.exists(cls.mdx):
165+
os.unlink(cls.mdx)
166+
167+
def test_api_resources(self):
168+
""""""
169+
with RequestsInterceptor(self.app, host='127.0.0.1', port=80) as url:
170+
r1 = requests.post(f'{url}/api/call/update')
171+
assert r1.status_code == 200
172+
173+
r2 = requests.get(f'{url}/api/resources')
174+
assert 'application/json' in r2.headers['content-type']
175+
# assert "version" in r.text
176+
assert r2.status_code == 200
177+
data = r2.json()
178+
179+
expected = [
180+
{
181+
'Resource': f'file://{self.test01}',
182+
'HTTP Response Headers': {'Content-Length': 3633},
183+
'Status Code': '200',
184+
'Reason': None,
185+
'Entities': ['https://idp.example.com/saml2/idp/metadata.php'],
186+
'Validation Errors': {},
187+
'Expiration Time': data[0]['Expiration Time'], # '2021-04-14 15:21:33.150742',
188+
'Expired': False,
189+
'Valid': True,
190+
'Parser': 'SAML',
191+
'Last Seen': data[0]['Last Seen'], # '2021-04-14 14:21:33.150781',
192+
}
193+
]
194+
assert data == expected
195+
196+
# Now check the timestamps
197+
now = datetime.now(tz=timezone.utc)
198+
199+
exp = datetime.fromisoformat(data[0]['Expiration Time'])
200+
assert (exp - now).total_seconds() > 3590
201+
assert (exp - now).total_seconds() < 3610
202+
203+
last_seen = datetime.fromisoformat(data[0]['Last Seen'])
204+
assert (last_seen - now).total_seconds() < 60

0 commit comments

Comments
 (0)