| 1 |
""" |
| 2 |
Tests for the products infrastructure. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
import datetime |
| 12 |
import io |
| 13 |
import os |
| 14 |
import tarfile |
| 15 |
|
| 16 |
from gavo.helpers import testhelpers |
| 17 |
|
| 18 |
from gavo import api |
| 19 |
from gavo import base |
| 20 |
from gavo import formats |
| 21 |
from gavo import rscdef |
| 22 |
from gavo import svcs |
| 23 |
from gavo import votable |
| 24 |
from gavo.helpers import testtricks |
| 25 |
from gavo.helpers import trialhelpers |
| 26 |
from gavo.protocols import products |
| 27 |
from gavo.web import producttar |
| 28 |
|
| 29 |
import tresc |
| 30 |
|
| 31 |
|
| 32 |
class StandardPubDIDTest(testhelpers.VerboseTest): |
| 33 |
def testMakeSPD(self): |
| 34 |
self.assertEqual(rscdef.getStandardPubDID("a/b/c"), |
| 35 |
"ivo://x-testing/~?a/b/c") |
| 36 |
|
| 37 |
def testParseSPD(self): |
| 38 |
self.assertEqual( |
| 39 |
rscdef.getAccrefFromStandardPubDID("ivo://x-testing/~?a/b/c"), |
| 40 |
"a/b/c") |
| 41 |
|
| 42 |
def testRejectParseSPD(self): |
| 43 |
self.assertRaisesWithMsg(base.NotFoundError, |
| 44 |
"The authority in the dataset identifier 'ivo://quatsch/batsch'" |
| 45 |
" could not be located in the authorities managed here", |
| 46 |
rscdef.getAccrefFromStandardPubDID, |
| 47 |
("ivo://quatsch/batsch",)) |
| 48 |
|
| 49 |
|
| 50 |
class _TestWithProductsTable(testhelpers.VerboseTest): |
| 51 |
resources = [('conn', tresc.prodtestTable), ('users', tresc.testUsers)] |
| 52 |
|
| 53 |
def setUp(self): |
| 54 |
testhelpers.VerboseTest.setUp(self) |
| 55 |
self.service = api.getRD("//products").getById("p") |
| 56 |
|
| 57 |
|
| 58 |
class TarTest(_TestWithProductsTable): |
| 59 |
def setUp(self): |
| 60 |
_TestWithProductsTable.setUp(self) |
| 61 |
self.tarService = self.service.rd.getById("getTar") |
| 62 |
|
| 63 |
def _getTar(self, inDict, qm=None): |
| 64 |
if qm is None: |
| 65 |
qm = svcs.QueryMeta() |
| 66 |
res = self.tarService.run("form", inDict, qm) |
| 67 |
dest = io.BytesIO() |
| 68 |
producttar.getTarMaker()._productsToTar(res, dest) |
| 69 |
return dest.getvalue() |
| 70 |
|
| 71 |
def _assertIsTar(self, res): |
| 72 |
f = tarfile.open("data.tar", "r:*", io.BytesIO(res)) |
| 73 |
f.close() |
| 74 |
|
| 75 |
def testFreeNoAuth(self): |
| 76 |
res = self._getTar({"pattern": "test.prodtest#data/b.imp"}) |
| 77 |
self._assertIsTar(res) |
| 78 |
self.assertTrue(b"\nobject: michael" in res) |
| 79 |
|
| 80 |
def testAllNoAuth(self): |
| 81 |
res = self._getTar({"pattern": "test.prodtest#%"}) |
| 82 |
self._assertIsTar(res) |
| 83 |
self.assertTrue(b"\nobject: michael" in res) |
| 84 |
self.assertTrue(b"This file is embargoed. Sorry" in res) |
| 85 |
self.assertFalse(b"\nobject: gabriel" in res) |
| 86 |
|
| 87 |
def testAllWithAuth(self): |
| 88 |
qm = svcs.QueryMeta.fromRequest( |
| 89 |
trialhelpers.FakeRequest(user="X_test", password="megapass")) |
| 90 |
res = self._getTar({"pattern": "test.prodtest#%"}, qm) |
| 91 |
self._assertIsTar(res) |
| 92 |
self.assertTrue(b"\nobject: michael" in res) |
| 93 |
self.assertFalse(b"This file is embargoed. Sorry" in res) |
| 94 |
self.assertTrue(b"\nobject: gabriel" in res) |
| 95 |
|
| 96 |
def testAllWithWrongAuth(self): |
| 97 |
qm = svcs.QueryMeta() |
| 98 |
qm["user"], qm["password"] = "Y_test", "megapass" |
| 99 |
res = self._getTar({"pattern": "test.prodtest#%"}, qm) |
| 100 |
self._assertIsTar(res) |
| 101 |
self.assertTrue(b"\nobject: michael" in res) |
| 102 |
self.assertTrue(b"This file is embargoed. Sorry" in res) |
| 103 |
self.assertFalse(b"\nobject: gabriel" in res) |
| 104 |
|
| 105 |
def testAccrefs(self): |
| 106 |
res = self._getTar({"accref": ["data/a.imp", "data/b.imp", |
| 107 |
"data/is-missing.dat"]}) |
| 108 |
tf = tarfile.open("data.tar", "r:*", io.BytesIO(res)) |
| 109 |
# TODO: do we really want to silently ignore missing files? |
| 110 |
self.assertEqual( |
| 111 |
[member.name for member in tf], |
| 112 |
['dc_data/a.imp', 'dc_data/b.imp']) |
| 113 |
|
| 114 |
|
| 115 |
def _FakeRequest(**kwargs): |
| 116 |
return trialhelpers.FakeRequest("/", |
| 117 |
args=trialhelpers.makeRequestArgs(**kwargs)) |
| 118 |
|
| 119 |
|
| 120 |
class RaccrefTest(_TestWithProductsTable): |
| 121 |
|
| 122 |
# tests for dcc: with SDM VOTables are in ssatest.py |
| 123 |
|
| 124 |
def testBadConstructurVals(self): |
| 125 |
self.assertRaisesWithMsg(base.ValidationError, |
| 126 |
"Field accref: Invalid value for constructor argument to RAccref:" |
| 127 |
" scale='klonk'", |
| 128 |
products.RAccref, |
| 129 |
("testing", {"scale": "klonk"})) |
| 130 |
|
| 131 |
def testExtraParamsIgnored(self): |
| 132 |
pk = products.RAccref("name", {"sra": "3", "ignored": True}) |
| 133 |
self.assertEqual(pk.accref, "name") |
| 134 |
self.assertEqual(pk.params, {"sra": 3.}) |
| 135 |
|
| 136 |
def testSerialization(self): |
| 137 |
pk = products.RAccref( |
| 138 |
"extra weird/product+name%something.fits", |
| 139 |
{"scale": "4"}) |
| 140 |
self.assertEqual(str(pk), |
| 141 |
"extra%20weird/product%2Bname%25something.fits?scale=4") |
| 142 |
|
| 143 |
def testFromRequestSimple(self): |
| 144 |
pk = products.RAccref.fromRequest("extra weird+key", |
| 145 |
_FakeRequest()) |
| 146 |
self.assertEqual(pk.accref, "extra weird+key") |
| 147 |
self.assertEqual(pk.params, {}) |
| 148 |
|
| 149 |
def testFromStringWithArgs(self): |
| 150 |
pk = products.RAccref.fromString( |
| 151 |
"extra%20weird&?%2bkey?ra=2&sra=0.5&dec=4&sdec=0.75") |
| 152 |
self.assertEqual(pk.accref, "extra weird&?+key") |
| 153 |
self.assertEqual(pk.params, {"ra": 2, "sra":0.5, "dec":4, "sdec":0.75}) |
| 154 |
|
| 155 |
def testFromStringWithoutArgs(self): |
| 156 |
pk = products.RAccref.fromString("extra%20weird&%2bkey") |
| 157 |
self.assertEqual(pk.accref, "extra weird&+key") |
| 158 |
self.assertEqual(pk.params, {}) |
| 159 |
|
| 160 |
def testBadFromString(self): |
| 161 |
self.assertRaisesWithMsg(base.ValidationError, |
| 162 |
"Field accref: Invalid value for constructor argument to RAccref:" |
| 163 |
" sra='huhu'", |
| 164 |
products.RAccref.fromString, |
| 165 |
("worz?sra=huhu",)) |
| 166 |
|
| 167 |
def testProductsRowRaises(self): |
| 168 |
nonExProd = products.RAccref("junkomatix/@@ridiculosa") |
| 169 |
self.assertRaisesWithMsg(base.NotFoundError, |
| 170 |
"accref 'junkomatix/@@ridiculosa' could not be located in product table", |
| 171 |
lambda: nonExProd.productsRow, |
| 172 |
()) |
| 173 |
|
| 174 |
def testProductsRowReturns(self): |
| 175 |
prod = products.RAccref("data/a.imp") |
| 176 |
self.assertEqual(prod.productsRow, { |
| 177 |
'embargo': datetime.date(2030, 12, 31), |
| 178 |
'accessPath': 'data/a.imp', |
| 179 |
'mime': 'text/plain', |
| 180 |
'owner': 'X_test', |
| 181 |
'accref': 'data/a.imp', |
| 182 |
'datalink': None, |
| 183 |
'preview': 'data/broken.imp', |
| 184 |
'preview_mime': "text/plain", |
| 185 |
'sourceTable': 'test.prodtest'}) |
| 186 |
|
| 187 |
def testPreview(self): |
| 188 |
prod = products.RAccref.fromString("data/a.imp?preview=true") |
| 189 |
self.assertEqual(prod.params, {"preview": True}) |
| 190 |
|
| 191 |
|
| 192 |
class ProductsCoreTest(_TestWithProductsTable): |
| 193 |
def _getProductFor(self, accref, reqArgs={}): |
| 194 |
inData = {"accref": [products.RAccref.fromString(accref)]} |
| 195 |
svc = base.caches.getRD("//products").getById("p") |
| 196 |
rows = trialhelpers.runSvcWith(svc, "get", inData, reqArgs) |
| 197 |
return testhelpers.pickSingle(rows) |
| 198 |
|
| 199 |
def _getOutput(self, prod): |
| 200 |
return b"".join(prod.iterData()) |
| 201 |
|
| 202 |
def testBasic(self): |
| 203 |
res = self._getProductFor("data/b.imp") |
| 204 |
self.assertTrue(isinstance(res, products.FileProduct)) |
| 205 |
self.assertTrue(self._getOutput(res).startswith( |
| 206 |
b"alpha: 03 34 33.45")) |
| 207 |
|
| 208 |
def testNonExistingProduct(self): |
| 209 |
res = self._getProductFor("junk/kotter") |
| 210 |
self.assertTrue(isinstance(res, products.NonExistingProduct)) |
| 211 |
self.assertRaisesWithMsg(IOError, |
| 212 |
"junk/kotter does not exist", |
| 213 |
self._getOutput, |
| 214 |
(res,)) |
| 215 |
self.assertRaisesWithMsg(svcs.UnknownURI, |
| 216 |
"No dataset with accref junk/kotter known here.", |
| 217 |
res.render, |
| 218 |
(None,)) |
| 219 |
|
| 220 |
def testRemovedProduct(self): |
| 221 |
srcPath = os.path.join(base.getConfig("inputsDir"), "data", "b.imp") |
| 222 |
os.rename(srcPath, srcPath+".bak") |
| 223 |
try: |
| 224 |
res = self._getProductFor("data/b.imp") |
| 225 |
self.assertTrue(isinstance(res, products.InvalidProduct)) |
| 226 |
finally: |
| 227 |
os.rename(srcPath+".bak", srcPath) |
| 228 |
|
| 229 |
def testProtectedProductUnauth(self): |
| 230 |
res = self._getProductFor("data/a.imp") |
| 231 |
self.assertTrue(isinstance(res, products.UnauthorizedProduct)) |
| 232 |
|
| 233 |
def testProtectedProductWithMoreArg(self): |
| 234 |
res = self._getProductFor("data/a.imp?scale=2") |
| 235 |
self.assertTrue(isinstance(res, products.UnauthorizedProduct)) |
| 236 |
|
| 237 |
def testProtectedProductBadAuth(self): |
| 238 |
res = self._getProductFor("data/a.imp", |
| 239 |
{"user": "Y_test", "password": "megapass"}) |
| 240 |
self.assertTrue(isinstance(res, products.UnauthorizedProduct)) |
| 241 |
|
| 242 |
def testProtectedAuth(self): |
| 243 |
res = self._getProductFor("data/a.imp", |
| 244 |
{"user": "X_test", "password": "megapass"}) |
| 245 |
self.assertEqual(str(res), '<FileProduct a.imp (text/plain)>') |
| 246 |
|
| 247 |
def testProtectedGavoadminBad(self): |
| 248 |
res = self._getProductFor("data/a.imp", |
| 249 |
{"user": "gavoadmin", "password": "megahey???pass"}) |
| 250 |
self.assertEqual(str(res), '<Protected product a.imp, access denied>') |
| 251 |
|
| 252 |
def testProtectedGavoadminGood(self): |
| 253 |
res = self._getProductFor("data/a.imp", |
| 254 |
{"user": "gavoadmin", "password": base.getConfig("web", "adminpasswd")}) |
| 255 |
self.assertEqual(str(res), '<FileProduct a.imp (text/plain)>') |
| 256 |
|
| 257 |
def testRemoteProduct(self): |
| 258 |
with tresc.prodtestTable.prodtblRow(accessPath="http://foo.bar"): |
| 259 |
res = self._getProductFor("just.testing/nowhere") |
| 260 |
self.assertTrue(isinstance(res, products.RemoteProduct)) |
| 261 |
self.assertRaisesWithMsg(svcs.WebRedirect, |
| 262 |
"This is supposed to redirect to http://foo.bar", |
| 263 |
res.render, |
| 264 |
(trialhelpers.FakeRequest("ignored", isSecure=True),)) |
| 265 |
|
| 266 |
def testRemoteProductHTTPS(self): |
| 267 |
with tresc.prodtestTable.prodtblRow(accessPath="http://foo.bar"): |
| 268 |
res = self._getProductFor("just.testing/nowhere") |
| 269 |
self.assertTrue(isinstance(res, products.RemoteProduct)) |
| 270 |
self.assertRaisesWithMsg(svcs.WebRedirect, |
| 271 |
"This is supposed to redirect to http://foo.bar", |
| 272 |
res.render, |
| 273 |
(trialhelpers.FakeRequest("ignored", isSecure=True),)) |
| 274 |
|
| 275 |
def testInvalidProduct(self): |
| 276 |
with tresc.prodtestTable.prodtblRow(accessPath="/non/existing/file"): |
| 277 |
res = self._getProductFor("just.testing/nowhere") |
| 278 |
self.assertTrue(isinstance(res, products.InvalidProduct)) |
| 279 |
self.assertRaises(svcs.UnknownURI, |
| 280 |
res.render, |
| 281 |
None) |
| 282 |
|
| 283 |
# TODO: have test scaffolding (ex.fits?) to actually make these work |
| 284 |
def testScaledProduct(self): |
| 285 |
prod = self._getProductFor("data/b.imp?scale=3") |
| 286 |
self.assertEqual(str(prod), "<Invalid product data/b.imp?scale=3>") |
| 287 |
|
| 288 |
def testCutoutProduct(self): |
| 289 |
res = self._getProductFor("data/b.imp?ra=3&dec=4&sra=2&sdec=4") |
| 290 |
self.assertEqual(str(res), "<Invalid product data/b.imp?" |
| 291 |
"dec=4.0&ra=3.0&sdec=4.0&sra=2.0>") |
| 292 |
|
| 293 |
|
| 294 |
class _FakeProduct(products.ProductBase): |
| 295 |
def iterData(self): |
| 296 |
yield b"1234" |
| 297 |
yield b"1234" |
| 298 |
yield b" "*10 |
| 299 |
yield b"end" |
| 300 |
|
| 301 |
|
| 302 |
class FileIntfTest(ProductsCoreTest): |
| 303 |
def testFallbackBuffering(self): |
| 304 |
p = _FakeProduct(products.RAccref.fromString("data/a.imp")) |
| 305 |
self.assertEqual(p.read(1), b"1") |
| 306 |
self.assertEqual(p.read(1), b"2") |
| 307 |
self.assertEqual(p.read(7), b"341234 ") |
| 308 |
rest = p.read() |
| 309 |
self.assertEqual(len(rest), 42) |
| 310 |
self.assertEqual(rest[-4:], b" end") |
| 311 |
p.close() |
| 312 |
|
| 313 |
def testNativeRead(self): |
| 314 |
p = self._getProductFor("data/a.imp") |
| 315 |
self.assertEqual(p.read(10), b"alpha: 23 ") |
| 316 |
self.assertTrue(hasattr(p._openedInputFile, "read")) |
| 317 |
p.close() |
| 318 |
self.assertEqual(p._openedInputFile, None) |
| 319 |
|
| 320 |
|
| 321 |
class StaticPreviewTest(testhelpers.VerboseTest): |
| 322 |
|
| 323 |
resources = [('conn', tresc.prodtestTable), ('users', tresc.testUsers)] |
| 324 |
|
| 325 |
def testStaticPreviewLocal(self): |
| 326 |
prod = products.getProductForRAccref("data/a.imp?preview=True") |
| 327 |
self.assertTrue(isinstance(prod, products.StaticPreview)) |
| 328 |
self.assertEqual(prod.read(200), b'kaputt.\n') |
| 329 |
|
| 330 |
def testStaticPreviewRemote(self): |
| 331 |
prod = products.getProductForRAccref("data/b.imp?preview=True") |
| 332 |
self.assertTrue(isinstance(prod, products.RemotePreview)) |
| 333 |
self.assertEqual(str(prod), |
| 334 |
'<Remote image/jpeg at http://example.com/borken.jpg>') |
| 335 |
|
| 336 |
|
| 337 |
class AutoPreviewTest(testhelpers.VerboseTest): |
| 338 |
|
| 339 |
resources = [('fits', tresc.fitsTable)] |
| 340 |
|
| 341 |
def testAutoPreviewMiss(self): |
| 342 |
prod = products.getProductForRAccref("data/ex.fits?preview=True") |
| 343 |
self.assertTrue(isinstance(prod, products.FileProduct)) |
| 344 |
|
| 345 |
def testAutoPreviewHit(self): |
| 346 |
cacheLocation = products.PreviewCacheManager.getCacheName( |
| 347 |
"data/ex.fits") |
| 348 |
with testtricks.testFile(os.path.basename(cacheLocation), |
| 349 |
"Abc, die Katze", inDir=os.path.dirname(cacheLocation)): |
| 350 |
prod = products.getProductForRAccref("data/ex.fits?preview=True") |
| 351 |
self.assertTrue(isinstance(prod, products.StaticPreview)) |
| 352 |
self.assertEqual(prod.read(200), b"Abc, die Katze") |
| 353 |
|
| 354 |
|
| 355 |
class _GlobalFITSLinks(testhelpers.TestResource): |
| 356 |
resources = [("fitsTable", tresc.fitsTable)] |
| 357 |
|
| 358 |
def make(self, deps): |
| 359 |
svc = api.getRD("//products").getById("dl") |
| 360 |
data, metadata = votable.loads(trialhelpers.runSvcWith(svc, "dlmeta", { |
| 361 |
"ID": [rscdef.getStandardPubDID("data/excube.fits")]})[1]) |
| 362 |
res = {} |
| 363 |
for link in metadata.iterDicts(data): |
| 364 |
res.setdefault(link["semantics"], []).append(link) |
| 365 |
return res |
| 366 |
|
| 367 |
|
| 368 |
class GlobalDatalinkTest(testhelpers.VerboseTest): |
| 369 |
resources = [("links", _GlobalFITSLinks())] |
| 370 |
|
| 371 |
def testNumberOfLinks(self): |
| 372 |
self.assertEqual(sum(len(r) for r in list(self.links.values())), 2) |
| 373 |
|
| 374 |
def testDatasetMeta(self): |
| 375 |
r, = self.links["#this"] |
| 376 |
self.assertEqual(tuple(r[s] for s in |
| 377 |
"content_length description error_message content_type ID".split()), |
| 378 |
(5760, "The full dataset.", None, "application/fits", |
| 379 |
"ivo://x-testing/~?data/excube.fits")) |
| 380 |
|
| 381 |
def testDatasetURL(self): |
| 382 |
self.assertEqual(self.links["#this"][0]["access_url"], |
| 383 |
"http://localhost:8080/getproduct/data/excube.fits") |
| 384 |
|
| 385 |
def testPreviewMeta(self): |
| 386 |
r, = self.links["#preview"] |
| 387 |
self.assertEqual(tuple(r[s] for s in |
| 388 |
"content_length description error_message content_type ID".split()), |
| 389 |
(None, "A preview for the dataset.", None, None, |
| 390 |
"ivo://x-testing/~?data/excube.fits")) |
| 391 |
|
| 392 |
def testPreviewURL(self): |
| 393 |
self.assertEqual(self.links["#preview"][0]["access_url"], |
| 394 |
"http://localhost:8080/getproduct/data/excube.fits?preview=True") |
| 395 |
|
| 396 |
|
| 397 |
class MiscTest(testhelpers.VerboseTest): |
| 398 |
def testMediaGuessFITS(self): |
| 399 |
self.assertEqual(formats.guessMediaType("foo/bar/gaz42h+88.old.fits"), |
| 400 |
"application/fits") |
| 401 |
|
| 402 |
def testMediaGuessVOTable(self): |
| 403 |
self.assertEqual(formats.guessMediaType("bal.vot"), |
| 404 |
'application/x-votable+xml') |
| 405 |
|
| 406 |
def testFallback(self): |
| 407 |
self.assertEqual(formats.guessMediaType("voo/bar.d/basz"), |
| 408 |
"application/octet-stream") |
| 409 |
|
| 410 |
|
| 411 |
class ProductLinkTest(testhelpers.VerboseTest): |
| 412 |
def testSimpleAccref(self): |
| 413 |
self.assertEqual( |
| 414 |
products.makeProductLink("foo/bar/baz.fits"), |
| 415 |
"http://localhost:8080/getproduct/foo/bar/baz.fits") |
| 416 |
|
| 417 |
def testWithoutHost(self): |
| 418 |
self.assertEqual( |
| 419 |
products.makeProductLink("foo/bar/baz.fits", withHost=False), |
| 420 |
"/getproduct/foo/bar/baz.fits") |
| 421 |
|
| 422 |
def testUseHost(self): |
| 423 |
self.assertEqual( |
| 424 |
products.makeProductLink("foo/bar/baz.fits", |
| 425 |
useHost="https://dc.g-vo.org"), |
| 426 |
"https://dc.g-vo.org/getproduct/foo/bar/baz.fits") |
| 427 |
|
| 428 |
def testURLIn(self): |
| 429 |
self.assertEqual( |
| 430 |
products.makeProductLink("http://example.org/foo/bar/baz.fits"), |
| 431 |
"http://example.org/foo/bar/baz.fits") |
| 432 |
self.assertEqual( |
| 433 |
products.makeProductLink("https://example.org/foo/bar/baz.fits"), |
| 434 |
"https://example.org/foo/bar/baz.fits") |
| 435 |
|
| 436 |
def testRAccrefIn(self): |
| 437 |
self.assertEqual( |
| 438 |
products.makeProductLink("http://example.org/foo/bar/baz.fits" |
| 439 |
"?ra=10&dec=20&sra=0.1&sdec=0.2&scale=2"), |
| 440 |
"http://example.org/foo/bar/baz.fits?ra=10&dec=20&sra=0.1" |
| 441 |
"&sdec=0.2&scale=2") |
| 442 |
|
| 443 |
|
| 444 |
if __name__=="__main__": |
| 445 |
testhelpers.main(MiscTest) |