|
1 | 1 | import os |
2 | 2 | import tempfile |
3 | 3 | import unittest |
| 4 | +from datetime import datetime, timezone |
4 | 5 |
|
5 | 6 | import requests |
6 | 7 | from mako.lookup import TemplateLookup |
@@ -126,3 +127,78 @@ def test_load_and_query(self): |
126 | 127 | assert type(info) == dict |
127 | 128 | assert info['title'] == 'NORDUnet' |
128 | 129 | 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