| 1 |
""" |
| 2 |
Tests for various parts of the server infrastructure, using trial. |
| 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 contextlib |
| 12 |
import os |
| 13 |
import re |
| 14 |
import tempfile |
| 15 |
import time |
| 16 |
|
| 17 |
from twisted.internet import reactor |
| 18 |
|
| 19 |
from gavo.helpers import trialhelpers |
| 20 |
|
| 21 |
from gavo import api |
| 22 |
from gavo import base |
| 23 |
from gavo import utils |
| 24 |
from gavo.web import ifpages |
| 25 |
from gavo.web import root |
| 26 |
|
| 27 |
base.DEBUG = True |
| 28 |
|
| 29 |
|
| 30 |
class ErrorReportingTest(trialhelpers.ArchiveTest): |
| 31 |
def testBotchedErrorGeneration(self): |
| 32 |
def assertStatus(res): |
| 33 |
self.assertEqual(res[1].code, 500) |
| 34 |
return res |
| 35 |
return self.assertGETHasStrings("test/blowuperror", {}, [ |
| 36 |
"NameError: name 'ddt' is not defined", |
| 37 |
'a href="mailto:[email protected]">' |
| 38 |
], |
| 39 |
).addCallback(assertStatus) |
| 40 |
|
| 41 |
|
| 42 |
class AdminTest(trialhelpers.ArchiveTest): |
| 43 |
def _makeAdmin(self, req): |
| 44 |
req.user = "gavoadmin" |
| 45 |
req.password = api.getConfig("web", "adminpasswd") |
| 46 |
|
| 47 |
def testDefaultDeny(self): |
| 48 |
return self.assertStatus("/seffe/__system__/adql", 401) |
| 49 |
|
| 50 |
def testFormsRendered(self): |
| 51 |
return self.assertGETHasStrings("/seffe/__system__/adql", {}, [ |
| 52 |
"ADQL Query</a>", |
| 53 |
"Schedule downtime for", |
| 54 |
'action="seffe/__system__/adql'], |
| 55 |
rm=self._makeAdmin) |
| 56 |
|
| 57 |
def testDowntimeScheduled(self): |
| 58 |
def checkDowntimeCanceled(ignored): |
| 59 |
self.assertRaises(api.NoMetaKey, |
| 60 |
api.getRD("__system__/adql").getMeta, "_scheduledDowntime", |
| 61 |
raiseOnFail=True) |
| 62 |
|
| 63 |
def cancelDowntime(ignored): |
| 64 |
return self.assertPOSTHasStrings("/seffe/__system__/adql", |
| 65 |
{"__nevow_form__": "setDowntime"}, [], |
| 66 |
rm=self._makeAdmin) |
| 67 |
|
| 68 |
def checkDowntime(ignored): |
| 69 |
self.assertEqual( |
| 70 |
str(api.getRD("__system__/adql").getMeta("_scheduledDowntime")), |
| 71 |
'2009-10-13') |
| 72 |
return self.assertGETHasStrings("/__system__/adql/query/availability", |
| 73 |
{}, ["<avl:downAt>2009-10-13<"]) |
| 74 |
|
| 75 |
return trialhelpers.runQuery(self.renderer, |
| 76 |
"POST", "/seffe/__system__/adql", |
| 77 |
{"__nevow_form__": "setDowntime", "scheduled": "2009-10-13"}, |
| 78 |
requestMogrifier=self._makeAdmin |
| 79 |
).addCallback(checkDowntime |
| 80 |
).addCallback(cancelDowntime |
| 81 |
).addCallback(checkDowntimeCanceled) |
| 82 |
|
| 83 |
def testBlockAndReload(self): |
| 84 |
def checkUnBlocked(ignored): |
| 85 |
return self.assertGETHasStrings("/seffe/__system__/adql", {}, |
| 86 |
["currently is not blocked"], |
| 87 |
rm=self._makeAdmin) |
| 88 |
|
| 89 |
def reload(ignored): |
| 90 |
return trialhelpers.runQuery(self.renderer, |
| 91 |
"POST", "/seffe/__system__/adql", |
| 92 |
{"__nevow_form__": "adminOps", "submit": "Reload RD"}, |
| 93 |
requestMogrifier=self._makeAdmin |
| 94 |
).addCallback(checkUnBlocked) |
| 95 |
|
| 96 |
def checkBlocked(ignored): |
| 97 |
return self.assertGETHasStrings("/seffe/__system__/adql", {}, |
| 98 |
["currently is blocked", "[email protected]"], |
| 99 |
rm=self._makeAdmin |
| 100 |
).addCallback(reload) |
| 101 |
|
| 102 |
return trialhelpers.runQuery(self.renderer, |
| 103 |
"POST", "/seffe/__system__/adql", |
| 104 |
{"__nevow_form__": "adminOps", "block": "Block"}, |
| 105 |
requestMogrifier=self._makeAdmin |
| 106 |
).addCallback(checkBlocked) |
| 107 |
|
| 108 |
|
| 109 |
class CustomizationTest(trialhelpers.ArchiveTest): |
| 110 |
def testSidebarRendered(self): |
| 111 |
return self.assertGETHasStrings("/data/test/basicprod/form", {}, [ |
| 112 |
'<a href="mailto:[email protected]">site operators</a>', |
| 113 |
'<div class="exploBody"><span class="plainmeta">ivo://' |
| 114 |
'x-testing/data/test/basicprod</span>']) |
| 115 |
|
| 116 |
def testMacrosExpanded(self): |
| 117 |
return self.assertGETHasStrings("/__system__/dc_tables/list/info", {}, [ |
| 118 |
"Information on Service 'Unittest Suite Public Tables'", |
| 119 |
"tables available for ADQL querying within the\nUnittest Suite", |
| 120 |
"Unittest Suite Table Infos</a>",]) |
| 121 |
|
| 122 |
def testExpandedInXML(self): |
| 123 |
return self.assertGETHasStrings("/oai.xml", { |
| 124 |
"verb": "GetRecord", |
| 125 |
"metadataPrefix": "ivo_vor", |
| 126 |
"identifier": "ivo://x-testing/__system__/services/registry" |
| 127 |
}, [ |
| 128 |
"<title>Unittest Suite Registry</title>", |
| 129 |
"<managedAuthority>x-testing</managedAuthority>"]) |
| 130 |
|
| 131 |
|
| 132 |
class StaticTest(trialhelpers.ArchiveTest): |
| 133 |
def testSimple(self): |
| 134 |
return self.assertGETHasStrings("/data/cores/rds/static/", {}, [ |
| 135 |
'<td><a href="test-gavorc">test-gavorc</a>', |
| 136 |
'<title>Directory listing for data/cores/rds/static/</title>']) |
| 137 |
|
| 138 |
def testRendererInferred(self): |
| 139 |
def assertRedirected(result): |
| 140 |
self.assertEqual(result[1].code, 301) |
| 141 |
self.assertTrue(result[1].getLocationValue(), |
| 142 |
"http://localhost:8080/data/cores/rds/static/") |
| 143 |
|
| 144 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 145 |
"/data/cores/rds", {} |
| 146 |
).addCallback(assertRedirected) |
| 147 |
|
| 148 |
def testSlashAdded(self): |
| 149 |
def assertRedirected(result): |
| 150 |
self.assertEqual(result[1].code, 301) |
| 151 |
self.assertTrue(result[1].getLocationValue(), |
| 152 |
"http://localhost:8080/data/cores/rds/static/") |
| 153 |
|
| 154 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 155 |
"/data/cores/rds/static", {} |
| 156 |
).addCallback(assertRedirected) |
| 157 |
|
| 158 |
def testStaticFile(self): |
| 159 |
return self.assertGETHasStrings("/data/cores/rds/static/ex.fits", {}, [ |
| 160 |
"BITPIX = 16"]) |
| 161 |
|
| 162 |
def test404(self): |
| 163 |
return self.assertStatus("/data/cores/rds/static/ex.fit", 404) |
| 164 |
|
| 165 |
def testWithIndexFile(self): |
| 166 |
return self.assertGETHasStrings("/data/test/basicprod/static/", {}, [ |
| 167 |
"alpha: 23 34 33.45"]) |
| 168 |
|
| 169 |
def testSubDir(self): |
| 170 |
return self.assertGETHasStrings("/data/cores/rds/static/bin/", {}, |
| 171 |
["Directory listing for data/cores/rds/static/bin/"]) |
| 172 |
|
| 173 |
def testAutoMinification(self): |
| 174 |
return self.assertGETHasStrings("/static/js/fancyroot.js", {}, |
| 175 |
["ResourceHeader);fetchSubject"]) |
| 176 |
|
| 177 |
def testNoTraversalGlobal(self): |
| 178 |
return self.assertStatus("/static/../src/boosterskel.h", 403) |
| 179 |
|
| 180 |
def test403WithoutStaticPath(self): |
| 181 |
def assertMessage(res): |
| 182 |
self.assertTrue(b">No static data on this service" in res[0]) |
| 183 |
|
| 184 |
return self.assertStatus("data/cores/coltest/static/bla.dat", 403 |
| 185 |
).addCallback(assertMessage) |
| 186 |
|
| 187 |
def testRedirectForDir(self): |
| 188 |
def assertDestination(res): |
| 189 |
self.assertEqual(res[1].getLocationValue(), |
| 190 |
'http://localhost:8080/data/cores/rds/static/bin/') |
| 191 |
|
| 192 |
return self.assertStatus("data/cores/rds/static/bin", 301 |
| 193 |
).addCallback(assertDestination) |
| 194 |
|
| 195 |
def testVolatileDoesNotAcceptFixedTemplate(self): |
| 196 |
return self.assertGETHasStrings("data/cores/coltest/volatile", {}, |
| 197 |
['class="errmsg">volatile renderer needs a \'volatile\' template']) |
| 198 |
|
| 199 |
def testMissingIndexFile(self): |
| 200 |
return self.assertStatus("data/cores/cc/static/", 404) |
| 201 |
|
| 202 |
|
| 203 |
class PathResolutionTest(trialhelpers.ArchiveTest): |
| 204 |
def testExternalVanityRedirect(self): |
| 205 |
def checkDestination(res): |
| 206 |
self.assertEqual(res[1].getLocationValue(), "http://sofo-hd.de") |
| 207 |
return res |
| 208 |
|
| 209 |
return self.assertStatus("/sofo", 301 |
| 210 |
).addCallback(checkDestination) |
| 211 |
|
| 212 |
def testNoRD404(self): |
| 213 |
return self.assertGETHasStrings("/data/nord", {}, |
| 214 |
["No matching RD"]).addCallback( |
| 215 |
lambda res: self.assertEqual(res[1].code, 404)) |
| 216 |
|
| 217 |
def testNoSvc404(self): |
| 218 |
return self.assertGETHasStrings("/data/cores/hell", {}, |
| 219 |
["No such service"]).addCallback( |
| 220 |
lambda res: self.assertEqual(res[1].code, 404)) |
| 221 |
|
| 222 |
def testDifferentRootPage(self): |
| 223 |
with trialhelpers.tempConfig( |
| 224 |
("web", "root", "__system__/adql/query/form")): |
| 225 |
return self.assertGETHasStrings("", {}, |
| 226 |
["<title>Unittest Suite ADQL Query</title>"]) |
| 227 |
|
| 228 |
|
| 229 |
class MaintResponsesTest(trialhelpers.ArchiveTest): |
| 230 |
def testMaintFile(self): |
| 231 |
with open(self.renderer.maintFile, "w", encoding="utf-8") as f: |
| 232 |
f.write("Blöde Welt.\n") |
| 233 |
|
| 234 |
return self.assertGETHasStrings("/data/cores/rds/static/ex.fits", {}, [ |
| 235 |
"mainterror-to-html.xsl", 'value="ERROR">Blöde Welt.\n</INFO>'] |
| 236 |
).addCallback( |
| 237 |
lambda res: self.assertEqual(res[1].code, 503) |
| 238 |
).addBoth( |
| 239 |
lambda res: os.unlink(self.renderer.maintFile) or res) |
| 240 |
|
| 241 |
def testRDBlocking(self): |
| 242 |
theRD = base.caches.getRD("data/cores") |
| 243 |
theRD.currently_blocked = True |
| 244 |
return self.assertGETHasStrings("/data/cores/rds/static/ex.fits", {}, [ |
| 245 |
"<title>Service temporarily taken down</title>", |
| 246 |
"from a few minutes to a day", |
| 247 |
"[email protected]"]).addBoth( |
| 248 |
lambda res: delattr(theRD, "currently_blocked") or res) |
| 249 |
|
| 250 |
|
| 251 |
class StreamingTest(trialhelpers.ArchiveTest): |
| 252 |
def testStreamingWorks(self): |
| 253 |
return self.assertGETHasStrings("/test/stream", {"size": "30"}, [ |
| 254 |
"123456789012345678901234567890"]) |
| 255 |
|
| 256 |
def testChunksWork(self): |
| 257 |
return self.assertGETHasStrings("/test/stream", {"chunksize": "10", |
| 258 |
"size": "35"}, [ |
| 259 |
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12345"]) |
| 260 |
|
| 261 |
def testCrashStreamDoesNotHang(self): |
| 262 |
return self.assertGETHasStrings("/test/streamcrash", {}, [ |
| 263 |
"Here is some data.", |
| 264 |
"XXX Internal", |
| 265 |
"raise Exception"]) |
| 266 |
|
| 267 |
def testStopProducing(self): |
| 268 |
|
| 269 |
class StoppingRequest(trialhelpers.FakeRequest): |
| 270 |
def write(self, data): |
| 271 |
trialhelpers.FakeRequest.write(self, data) |
| 272 |
if self.producer: |
| 273 |
self.producer.stopProducing() |
| 274 |
|
| 275 |
def assertResult(result): |
| 276 |
# at least one chunk must have been delivered |
| 277 |
self.assertTrue(result[0].startswith(b"xxxxxxxxxx"), |
| 278 |
"No data delivered at all") |
| 279 |
# ...but the kill must become active before the whole mess |
| 280 |
# has been written; there are some ways in which this can |
| 281 |
# fail because of concurreny. If all else fails, add sync |
| 282 |
# between the test and streaming, but that'd suck. |
| 283 |
self.assertFalse(len(result[0])>utils.StreamBuffer.chunkSize*4, |
| 284 |
"Kill hasn't happened") |
| 285 |
|
| 286 |
return trialhelpers.runQuery(self.renderer, "GET", "/test/stream", { |
| 287 |
"chunksize": str(utils.StreamBuffer.chunkSize), |
| 288 |
"size": str(utils.StreamBuffer.chunkSize*5)}, |
| 289 |
requestClass=StoppingRequest |
| 290 |
).addCallback(assertResult) |
| 291 |
|
| 292 |
def testClosingConnection(self): |
| 293 |
|
| 294 |
class ClosingRequest(trialhelpers.FakeRequest): |
| 295 |
def write(self, data): |
| 296 |
raise IOError("Connection closed") |
| 297 |
|
| 298 |
def assertResult(result): |
| 299 |
self.assertEqual(result[0], b"") |
| 300 |
|
| 301 |
return trialhelpers.runQuery(self.renderer, "GET", "/test/stream", |
| 302 |
{"chunksize": "10", "size": "3500"}, requestClass=ClosingRequest |
| 303 |
).addCallback(assertResult) |
| 304 |
|
| 305 |
|
| 306 |
_TEMPLATE_TEMPLATE = """ |
| 307 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
| 308 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 309 |
<html xmlns:n="http://nevow.com/ns/nevow/0.1" |
| 310 |
xmlns="http://www.w3.org/1999/xhtml"> |
| 311 |
<head> |
| 312 |
<title>Template test</title> |
| 313 |
<n:invisible n:render="commonhead"/> |
| 314 |
</head> |
| 315 |
<body> |
| 316 |
%s |
| 317 |
</body> |
| 318 |
</html> |
| 319 |
""" |
| 320 |
|
| 321 |
class TemplatingTest(trialhelpers.ArchiveTest): |
| 322 |
|
| 323 |
def _assertTemplateRendersTo(self, templateBody, args, strings, |
| 324 |
render="volatile"): |
| 325 |
hdl, tplpath = tempfile.mkstemp( |
| 326 |
suffix=".html", dir=api.getConfig("tempDir")) |
| 327 |
|
| 328 |
f = os.fdopen(hdl, "w") |
| 329 |
f.write(_TEMPLATE_TEMPLATE%templateBody) |
| 330 |
f.close() |
| 331 |
|
| 332 |
svc = api.getRD("//tests").getById("dyntemplate") |
| 333 |
svc._loadedTemplates = {} |
| 334 |
svc.templates[render] = tplpath |
| 335 |
|
| 336 |
def cleanup(res): |
| 337 |
os.unlink(tplpath) |
| 338 |
return res |
| 339 |
|
| 340 |
return self.assertGETHasStrings("//tests/dyntemplate/"+render, |
| 341 |
args, strings).addBoth(cleanup) |
| 342 |
|
| 343 |
def testContentDelivered(self): |
| 344 |
return self._assertTemplateRendersTo( |
| 345 |
'<p>stuff: <n:invisible n:render="string" n:data="parameter foo"/></p>', |
| 346 |
{"foo": "content delivered test"}, |
| 347 |
["<p>stuff: content delivered test", 'href="/static/css/gavo_dc.css']) |
| 348 |
|
| 349 |
def testNoParOk(self): |
| 350 |
return self._assertTemplateRendersTo( |
| 351 |
'<p>stuff: <n:invisible n:render="string" n:data="parameter foo"/></p>', |
| 352 |
{}, |
| 353 |
["stuff: </p>"]) |
| 354 |
|
| 355 |
def testEightBitClean(self): |
| 356 |
return self._assertTemplateRendersTo( |
| 357 |
'<p>stuff: <n:invisible n:render="string" n:data="parameter foo"/></p>', |
| 358 |
{"foo": [b'Kn\xC3\xB6llchen']}, |
| 359 |
["stuff: Knöllchen</p>"]) |
| 360 |
|
| 361 |
def testMessEscaped(self): |
| 362 |
return self._assertTemplateRendersTo( |
| 363 |
'<p>stuff: <n:invisible n:render="string" n:data="parameter foo"/></p>', |
| 364 |
{"foo": '<script language="nasty&"/>'}, |
| 365 |
['<script language="nasty&"/>']) |
| 366 |
|
| 367 |
def testRDData(self): |
| 368 |
return self._assertTemplateRendersTo( |
| 369 |
'<p>rd data</p><p n:data="rd //tap">' |
| 370 |
'<n:invisible n:render="string"/></p>', |
| 371 |
{}, |
| 372 |
['<p><resource descriptor for __system__/tap']) |
| 373 |
|
| 374 |
def testNoRDData(self): |
| 375 |
return self._assertTemplateRendersTo( |
| 376 |
'<div n:data="rd /junky/path/that/leads/nowhere">' |
| 377 |
'<p n:render="ifnodata">No junky weirdness here</p></div>', |
| 378 |
{}, |
| 379 |
['<div><p>No junky weirdness']) |
| 380 |
|
| 381 |
def testParamRender(self): |
| 382 |
return self._assertTemplateRendersTo( |
| 383 |
'<div n:data="result">' |
| 384 |
'<p n:render="param a float is equal to %5.2f">aFloat</p>' |
| 385 |
'</div>', |
| 386 |
{"__nevow_form__": "genForm",}, [ |
| 387 |
"<p>a float is equal to 1.25</p>"], render="form") |
| 388 |
|
| 389 |
def testNoParamRender(self): |
| 390 |
return self._assertTemplateRendersTo( |
| 391 |
'<div n:data="result">' |
| 392 |
'<p n:render="param %5.2f">foo</p>' |
| 393 |
'</div>', |
| 394 |
{"__nevow_form__": "genForm",}, [ |
| 395 |
"<p>N/A</p>"], render="form") |
| 396 |
|
| 397 |
def testIfslot(self): |
| 398 |
def assertNoslot(result): |
| 399 |
self.assertTrue(b"dernetsei" not in result[0]) |
| 400 |
self.assertTrue(b'class="world"' not in result[0]) |
| 401 |
return result |
| 402 |
|
| 403 |
return self._assertTemplateRendersTo( |
| 404 |
'<ul n:render="sequence" n:data="titleList">' |
| 405 |
'<li n:pattern="item" n:render="mapping">' |
| 406 |
'<p n:render="ifslot gibtsnicht">derfnetsei</p>' |
| 407 |
'<p n:render="ifnoslot gibtsnicht">desscho</p>' |
| 408 |
'<p n:render="ifslot title">' |
| 409 |
'<n:attr name="class"><n:slot name="other"/></n:attr>' |
| 410 |
'Title: <n:slot name="title"/></p>' |
| 411 |
'</li></ul>', |
| 412 |
{}, |
| 413 |
["<p>desscho</p>", '<p class="ing">Title: on title</p>', |
| 414 |
"desscho</p></li>"]).addCallback( |
| 415 |
assertNoslot) |
| 416 |
|
| 417 |
def testFlattenFailureIs500(self): |
| 418 |
def assert500(result): |
| 419 |
self.assertEqual(result[1].code, 500) |
| 420 |
return self._assertTemplateRendersTo( |
| 421 |
'<div><p>This is there</p><p n:render="borken">This is not</p></div>', |
| 422 |
{}, [ |
| 423 |
"Exception while flattening", |
| 424 |
"Missing render method", |
| 425 |
": borken" |
| 426 |
]).addCallback(assert500) |
| 427 |
|
| 428 |
def testDefaultConfigSection(self): |
| 429 |
return self._assertTemplateRendersTo( |
| 430 |
'<p n:render="getconfig">group</p>', |
| 431 |
{}, |
| 432 |
['<p>gavo</p>']) |
| 433 |
|
| 434 |
def testURLEscape(self): |
| 435 |
return self._assertTemplateRendersTo( |
| 436 |
'<a n:data="parameter foo" n:render="string">' |
| 437 |
'<n:attr name="href">http://a.b/c?<n:invisible n:render="urlescape"/>' |
| 438 |
'</n:attr></a>', |
| 439 |
{'foo':'Kröss&http://nourl'}, |
| 440 |
['<a href="http://a.b/c?Kr%C3%B6ss%26http%3A//nourl">Kröss&' |
| 441 |
'http://nourl</a>']) |
| 442 |
|
| 443 |
def testWithFixedQueryCore(self): |
| 444 |
with open( |
| 445 |
os.path.join(base.getConfig("inputsDir"), "fixtest.html"), |
| 446 |
"w") as f: |
| 447 |
f.write("""<html xmlns="http://www.w3.org/1999/xhtml" |
| 448 |
xmlns:n="http://nevow.com/ns/nevow/0.1"><head n:render="commonhead"> |
| 449 |
</head><body n:render="withsidebar"> |
| 450 |
<div class="result" n:render="ifdata" n:data="result"> |
| 451 |
<n:invisible n:render="resulttable"/></div></body></html>""") |
| 452 |
|
| 453 |
return self.assertGETHasStrings( |
| 454 |
"/data/cores/wfq/fixed", {}, [ |
| 455 |
'<div class="result">', |
| 456 |
'title="Num1"', |
| 457 |
'<td>2</td><td>3</td></tr>']) |
| 458 |
|
| 459 |
|
| 460 |
class PathResoutionTest(trialhelpers.ArchiveTest): |
| 461 |
def testDefaultRenderer(self): |
| 462 |
return self.assertGETHasStrings("/data/cores/impgrouptest", {}, |
| 463 |
['id="genForm-rV"']) #form rendered |
| 464 |
|
| 465 |
def testNoDefaultRenderer(self): |
| 466 |
def assertInfoMessage(result): |
| 467 |
self.assertTrue(b"No renderer given and service has no default" |
| 468 |
in result[0]) |
| 469 |
|
| 470 |
self.assertStatus("/data/cores/grouptest", 404 |
| 471 |
).addCallback(assertInfoMessage) |
| 472 |
|
| 473 |
def testSlashAtEndRedirects(self): |
| 474 |
def checkRedirect(result): |
| 475 |
self.assertEqual(result[1].code, 301) |
| 476 |
# the destination of the redirect currently is wrong due to trialhelper |
| 477 |
# restrictions. |
| 478 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 479 |
"/data/cores/convcat/form/", {} |
| 480 |
).addCallback(checkRedirect) |
| 481 |
|
| 482 |
|
| 483 |
def _makeOriginAdder(origin): |
| 484 |
def _(req): |
| 485 |
req.requestHeaders.setRawHeaders("origin", [origin]) |
| 486 |
return _ |
| 487 |
|
| 488 |
|
| 489 |
class CORSTest(trialhelpers.ArchiveTest): |
| 490 |
def testAuthorizedCORS(self): |
| 491 |
def assertCORSHeader(res): |
| 492 |
self.assertEqual( |
| 493 |
res[1].responseHeaders.getRawHeaders("access-control-allow-origin"), |
| 494 |
["https://example.com/corsusing/abc/d"]) |
| 495 |
|
| 496 |
return trialhelpers.runQuery(self.renderer, "GET", "/robots.txt", {}, |
| 497 |
requestMogrifier=_makeOriginAdder("https://example.com/corsusing/abc/d") |
| 498 |
).addCallback(assertCORSHeader) |
| 499 |
|
| 500 |
def testUnauthorizedCORS(self): |
| 501 |
def assertNoCORSHeader(res): |
| 502 |
self.assertFalse(res[1].responseHeaders.hasHeader( |
| 503 |
"access-control-allow-origin")) |
| 504 |
|
| 505 |
return trialhelpers.runQuery(self.renderer, "GET", "/robots.txt", {}, |
| 506 |
requestMogrifier=_makeOriginAdder("https://examplexcom/corsusing/abc/d") |
| 507 |
).addCallback(assertNoCORSHeader) |
| 508 |
|
| 509 |
|
| 510 |
class IfPagesTest(trialhelpers.ArchiveTest): |
| 511 |
def testRobotsTxt(self): |
| 512 |
return self.assertGETHasStrings("/robots.txt", {}, |
| 513 |
['Disallow: /login']) |
| 514 |
|
| 515 |
def testRobotsChild(self): |
| 516 |
return self.assertStatus("/robots.txt/foork", 404) |
| 517 |
|
| 518 |
def testScaledLogo(self): |
| 519 |
return self.assertGETHasStrings("/favicon.png", {}, |
| 520 |
["PNG", "IEND"]) |
| 521 |
|
| 522 |
def testCurReaders(self): |
| 523 |
def assertContent(res): |
| 524 |
self.assertTrue(re.match(rb"\d+$", res[0])) |
| 525 |
|
| 526 |
return self.assertStatus("clientcount", 200 |
| 527 |
).addCallback(assertContent) |
| 528 |
|
| 529 |
def testACMEChallengeNoDir(self): |
| 530 |
return self.assertStatus(".well-known/acme-challenge", 403) |
| 531 |
|
| 532 |
def testNoOddWellKnownChildren(self): |
| 533 |
return self.assertGETHasStrings(".well-known/unknown", {}, |
| 534 |
[">b'unknown' not supported in .well-known"]) |
| 535 |
|
| 536 |
def testChallenge(self): |
| 537 |
challengeName = utils.getRandomString(20) |
| 538 |
if not os.path.isdir(ifpages.ACMEChallenge.acmeChallengeDir): |
| 539 |
os.mkdir(ifpages.ACMEChallenge.acmeChallengeDir) |
| 540 |
|
| 541 |
cpath = os.path.join( |
| 542 |
ifpages.ACMEChallenge.acmeChallengeDir, challengeName) |
| 543 |
def cleanup(res): |
| 544 |
os.unlink(cpath) |
| 545 |
return res |
| 546 |
|
| 547 |
with open(cpath, "w") as f: |
| 548 |
f.write("Challenge!") |
| 549 |
return self.assertGETHasStrings(".well-known/acme-challenge/"+challengeName, |
| 550 |
{}, ["Challenge!"]).addBoth(cleanup) |
| 551 |
|
| 552 |
def testMissingChallenge404(self): |
| 553 |
return self.assertGETHasStrings(".well-known/acme-challenge/not-here", |
| 554 |
{}, ["No such challenge known here"]) |
| 555 |
|
| 556 |
def testMissingStatic(self): |
| 557 |
return self.assertGETHasStrings("static/not-here", {}, |
| 558 |
[">No matching file, neither built-in"]) |
| 559 |
|
| 560 |
def testTestUserpathOverrides(self): |
| 561 |
destPath = os.path.join( |
| 562 |
ifpages.StaticServer().userPath, "help_vizier.shtml") |
| 563 |
with open(destPath, "w") as f: |
| 564 |
f.write("<html>++Overridden++.</html>\n") |
| 565 |
|
| 566 |
def cleanup(res): |
| 567 |
os.unlink(destPath) |
| 568 |
return res |
| 569 |
|
| 570 |
return self.assertGETHasStrings("static/help_vizier.shtml", {}, |
| 571 |
["++Overridden++.</html>"]).addBoth(cleanup) |
| 572 |
|
| 573 |
def testUserpathNoSymlink(self): |
| 574 |
destPath = os.path.join( |
| 575 |
ifpages.StaticServer().userPath, "null") |
| 576 |
os.symlink("/dev/null", destPath) |
| 577 |
|
| 578 |
def cleanup(res): |
| 579 |
os.unlink(destPath) |
| 580 |
return res |
| 581 |
|
| 582 |
return self.assertGETHasStrings("static/null", {}, |
| 583 |
[">/dev/null is not located "]).addBoth(cleanup) |
| 584 |
|
| 585 |
def testDebianFallback(self): |
| 586 |
jqDist = os.path.join( |
| 587 |
ifpages.StaticServer().systemPath, "js", "jquery-gavo.js") |
| 588 |
if not os.path.exists(jqDist): |
| 589 |
# we're not running on a checkout in the first place |
| 590 |
return |
| 591 |
|
| 592 |
os.rename(jqDist, jqDist+".bak") |
| 593 |
|
| 594 |
def cleanup(res): |
| 595 |
os.rename(jqDist+".bak", jqDist) |
| 596 |
return res |
| 597 |
|
| 598 |
return self.assertGETHasStrings("static/js/jquery-gavo.js", {}, |
| 599 |
['function(global,factory){"use strict";if(typeof module'], |
| 600 |
).addBoth(cleanup) |
| 601 |
|
| 602 |
def testShtmlProcessng(self): |
| 603 |
return self.assertGETHasStrings("static/help.shtml", {}, |
| 604 |
["<title>Unittest Suite Site Help</title>"]) |
| 605 |
|
| 606 |
def testSecurityTxt(self): |
| 607 |
self.assertGETHasStrings(".well-known/security.txt", {}, [ |
| 608 |
"PGP SIGNATURE", |
| 609 |
"Canonical: https://dc.g-vo.org/.well-known/security.txt"]) |
| 610 |
|
| 611 |
|
| 612 |
class MetaRenderTest(trialhelpers.ArchiveTest): |
| 613 |
def testMacroExpanded(self): |
| 614 |
return self.assertGETHasStrings("/browse/__system__/tap", {}, |
| 615 |
['<div class="rddesc"><span class="plainmeta"> Unittest' |
| 616 |
" Suite's Table Access"]) |
| 617 |
|
| 618 |
def testTableinfoSortOrder(self): |
| 619 |
def checkOrder(res): |
| 620 |
indices = [res[0].find(s) for s in [ |
| 621 |
b"<td>description</td>", |
| 622 |
b"<td>schema_name</td>", |
| 623 |
b"<td>sourceRD</td>", |
| 624 |
b"<td>table_index</td>", |
| 625 |
b"<td>table_name</td>"]] |
| 626 |
sorted_indices = list(sorted(indices)) |
| 627 |
self.assertEqual(sorted_indices, indices) |
| 628 |
return res |
| 629 |
|
| 630 |
return self.assertGETHasStrings("/tableinfo/tap_schema.tables", |
| 631 |
{"alphaOrder": ["True"]}, |
| 632 |
["<p>Sorted alphabetically. <a href="] |
| 633 |
).addCallback(checkOrder) |
| 634 |
|
| 635 |
def testTableinfoNotable(self): |
| 636 |
return self.assertGETHasStrings("/tableinfo", {}, |
| 637 |
["You must provide a table name to this renderer."]) |
| 638 |
|
| 639 |
def testTableinfoDirlike(self): |
| 640 |
return self.assertGETHasStrings("/tableinfo/", {}, [ |
| 641 |
'<div class="errmsg">table \'\' could not be located in' |
| 642 |
' DaCHS\' table listing.</div>']) |
| 643 |
|
| 644 |
def testWithTapinfo(self): |
| 645 |
return self.assertGETHasStrings("/tableinfo/tap_schema.tables", |
| 646 |
{"tapinfo": ["t"]}, [ |
| 647 |
"available through this site's TAP service", |
| 648 |
"url: <strong>http://localhost:8080/tap"]) |
| 649 |
|
| 650 |
def testRelationships(self): |
| 651 |
return self.assertGETHasStrings("/data/cores/pc/info", {}, [ |
| 652 |
"is derived from</span>", # relationship names translated |
| 653 |
'href="http://localhost:8080/tableinfo/test.abcd"', # ref URL fetch |
| 654 |
'">Some Service', # titles rendered |
| 655 |
'<a href="http://just.any.old.url">DataCite be damned', #_related support |
| 656 |
'<a href="https://dc.g-vo.org/LP/ivo://org', # Landing page URLs |
| 657 |
]) |
| 658 |
|
| 659 |
def testRelatedMacroExpanded(self): |
| 660 |
return self.assertGETHasStrings("/data/cores/pc/info", {}, [ |
| 661 |
"http://localhost:8080/foo/bar/baz"]) |
| 662 |
|
| 663 |
|
| 664 |
class MetaPagesTest(trialhelpers.ArchiveTest): |
| 665 |
def testGetRR404(self): |
| 666 |
return self.assertGETHasStrings("/getRR/non/existing", {}, |
| 667 |
['The resource non#existing is unknown at this site.']) |
| 668 |
|
| 669 |
def testEmptyGetRR404(self): |
| 670 |
return self.assertGETHasStrings("/getRR", {}, |
| 671 |
['What resource record do you want?']) |
| 672 |
|
| 673 |
|
| 674 |
def testGetRRForService(self): |
| 675 |
return self.assertGETHasStrings("/getRR/data/pubtest/moribund", {}, |
| 676 |
['<identifier>ivo://x-testing/data/pubtest/moribund</identifier>']) |
| 677 |
|
| 678 |
|
| 679 |
class FormRenderTest(trialhelpers.ArchiveTest): |
| 680 |
def testTableWidget(self): |
| 681 |
return self.assertGETHasStrings("/data/test/basicprod/form", |
| 682 |
{}, [ |
| 683 |
'<select name="_DBOPTIONS_DIR" id="genForm-_DBOPTIONS_DIR">' |
| 684 |
'<option value="ASC">ASC</option>', |
| 685 |
'<select name="MAXREC" id="genForm-MAXREC">' |
| 686 |
'<option value="100" selected="selected">100<', |
| 687 |
'id="genForm-_FORMAT"', |
| 688 |
'Sort by <select name="_DBOPTIONS_ORDER" id="genForm-_DBOPTIONS_ORDER">' |
| 689 |
'<option value="" selected="selected">',]) |
| 690 |
|
| 691 |
def testDefaultsRendered(self): |
| 692 |
return self.assertGETHasStrings("/data/cores/pc/form", |
| 693 |
{}, |
| 694 |
['value="1.0"', 'value="1 2 3"']) |
| 695 |
|
| 696 |
def testValuesPassedRendered(self): |
| 697 |
return self.assertGETHasStrings("/data/cores/pc/form", |
| 698 |
{"opre": "3"}, |
| 699 |
['value="1.0"', 'value="1 2 3"', 'value="3.0"']) |
| 700 |
|
| 701 |
def testDefaultForFormRendered(self): |
| 702 |
return self.assertGETHasStrings("/data/cores/impgrouptest/form", |
| 703 |
{}, |
| 704 |
['value="-30"']) |
| 705 |
|
| 706 |
def testOverflowIndication(self): |
| 707 |
return self.assertGETHasStrings("/data/test/basicprod/form", |
| 708 |
{"__nevow_form__": "genForm", "MAXREC": "1"}, [ |
| 709 |
"The query limit was reached."]) |
| 710 |
|
| 711 |
|
| 712 |
class ExternalRenderTest(trialhelpers.ArchiveTest): |
| 713 |
def testBasic(self): |
| 714 |
return self.assertStatus("data/cores/singlearg/external", 301 |
| 715 |
).addCallback(lambda res: |
| 716 |
self.assertEqual( |
| 717 |
res[1].getLocationValue(), |
| 718 |
"http://supersmart.edu/singlearg")) |
| 719 |
|
| 720 |
def testNoExternal(self): |
| 721 |
return self.assertStatus("data/cores/coltest/external", 404 |
| 722 |
).addCallback(lambda res: |
| 723 |
self.assertTrue( |
| 724 |
b"No publication for an external service here.</div>" in res[0], |
| 725 |
"Unexpected: %s"%res[0])) |
| 726 |
|
| 727 |
|
| 728 |
class CoverageTest(trialhelpers.ArchiveTest): |
| 729 |
def test404WithoutCoverage(self): |
| 730 |
def assertHasMessage(res): |
| 731 |
self.assertTrue( |
| 732 |
b"No spatial coverage available for this service." in res[0], |
| 733 |
"Improper error message when no coverage info is available.") |
| 734 |
|
| 735 |
return self.assertStatus("/data/test/basicprod/coverage", 404 |
| 736 |
).addCallback(assertHasMessage) |
| 737 |
|
| 738 |
def testFITSReturned(self): |
| 739 |
return self.assertGETHasStrings("/data/cores/scs/coverage", {}, [ |
| 740 |
"SIMPLE =", |
| 741 |
"XTENSION= 'BINTABLE'", |
| 742 |
"NAXIS = 2", |
| 743 |
"\0\0\0\x1a"]) |
| 744 |
|
| 745 |
|
| 746 |
class TestExamples(trialhelpers.ArchiveTest): |
| 747 |
def testBasic(self): |
| 748 |
def assertNoContinuation(res): |
| 749 |
self.assertTrue(b"continuations" not in res[0]) |
| 750 |
return res |
| 751 |
|
| 752 |
return self.assertGETHasStrings("/data/cores/dl/examples", |
| 753 |
{}, [ |
| 754 |
'<title>Examples for Hollow Datalink</title', |
| 755 |
'<h2 property="name">Example 1', |
| 756 |
'property="dl-id"', |
| 757 |
'ivo://org.gavo.dc/~?bla/foo/qua</em>', |
| 758 |
'resource="#Example2"', |
| 759 |
'<p>This is another example for examples.</p>']).addCallback( |
| 760 |
assertNoContinuation) |
| 761 |
|
| 762 |
def testPublicationSelection(self): |
| 763 |
def checkRenderers(result): |
| 764 |
tree = trialhelpers.getXMLTree(result[0]) |
| 765 |
rends = set(e.get("standardID").split("#")[-1] |
| 766 |
for e in tree.xpath("//capability[@standardID]")) |
| 767 |
self.assertEqual(rends, set( |
| 768 |
['availability', 'capabilities', 'examples', |
| 769 |
'links-1.1', 'tables'])) |
| 770 |
|
| 771 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 772 |
"/getRR/data/cores/dl", {} |
| 773 |
).addCallback(checkRenderers) |
| 774 |
|
| 775 |
def testNoExamples(self): |
| 776 |
def assert404(res): |
| 777 |
self.assertEqual(res[1].code, 404) |
| 778 |
return res |
| 779 |
|
| 780 |
return self.assertGETHasStrings("/__system__/adql/query/examples", {}, [ |
| 781 |
"This service does not define examples of its own."] |
| 782 |
).addCallback(assert404) |
| 783 |
|
| 784 |
def testInternalContinuation(self): |
| 785 |
return self.assertGETHasStrings("/data/cores/basiccat/examples", {}, [ |
| 786 |
'href="http://localhost:8080/data/cores/convcat/examples">More from conv</a>', |
| 787 |
'href="http://localhost:8080/data/ssatest/s/examples">More<', |
| 788 |
'href="http://ivoa.net/doc/obscore/tap-examples.xhtml">More external</', |
| 789 |
'property="continuation"', |
| 790 |
]) |
| 791 |
|
| 792 |
|
| 793 |
class QPTest(trialhelpers.ArchiveTest): |
| 794 |
def testBasic(self): |
| 795 |
return self.assertGETHasStrings("data/cores/singlearg/qp/abc", {}, [ |
| 796 |
'<span class="plainmeta">ivo://x-testing/data/cores/singlearg', |
| 797 |
'title="I\'m not value"', |
| 798 |
'<td>3</td><td>abcabcabc</td>', |
| 799 |
'</html>']) |
| 800 |
|
| 801 |
def testAdditionalParam(self): |
| 802 |
return self.assertGETHasStrings("data/cores/singlearg/qp/x", |
| 803 |
{"otherarg": ["4"]}, [ |
| 804 |
'<td>4</td><td>xxxx</td>']) |
| 805 |
|
| 806 |
def testVOTable(self): |
| 807 |
return self.assertGETHasStrings("data/cores/singlearg/qp/x", |
| 808 |
{"_FORMAT": ["VOTable"]}, [ |
| 809 |
'<VOTABLE', "AAAAAQAAA"]) |
| 810 |
|
| 811 |
def testEmptyMatch(self): |
| 812 |
return self.assertGETHasStrings("data/cores/singlearg/qp/x", |
| 813 |
{"otherarg": ["0"]}, [ |
| 814 |
'<div class="errmsg">No record matching x.</div>']) |
| 815 |
|
| 816 |
def testRaisingCore(self): |
| 817 |
return self.assertGETHasStrings("data/cores/singlearg/qp/fail hard", |
| 818 |
{}, [ |
| 819 |
"The query initiated by your URL failed, yielding a message" |
| 820 |
" 'name 'ddt' is not defined'."] |
| 821 |
).addCallback(lambda res: self.assertEqual(res[1].code, 404)) |
| 822 |
|
| 823 |
def testMissingArg(self): |
| 824 |
return self.assertGETHasStrings("data/cores/singlearg/qp/", |
| 825 |
{}, [ |
| 826 |
'<div class="errmsg">This page is a root page for a query-based' |
| 827 |
' service. You have to give a valid value in the path.</div>'] |
| 828 |
).addCallback(lambda res: self.assertEqual(res[1].code, 404)) |
| 829 |
|
| 830 |
|
| 831 |
class VOSITest(trialhelpers.ArchiveTest): |
| 832 |
def testBrokenCapDoesNotKillCapResponse(self): |
| 833 |
def checkNoSIAP(res): |
| 834 |
self.assertTrue(b"std/SIA" not in res[0]) |
| 835 |
return res |
| 836 |
return self.assertGETHasStrings("data/uwstest/borken/capabilities", {}, [ |
| 837 |
'<interface xsi:type="vr:WebBrowser">', |
| 838 |
'</cap:capabilities>']).addCallback(checkNoSIAP) |
| 839 |
|
| 840 |
def testBadAvailability(self): |
| 841 |
return self.assertGETHasStrings("data/cores/uploadtest/availability", {}, [ |
| 842 |
"<h1>Unittest Suite -- Error (500)</h1>", |
| 843 |
'class="errmsg">Invalid constructor func in (\'crash_this\'']) |
| 844 |
|
| 845 |
def _nukeHostPart(uri): |
| 846 |
return "/"+uri.split("/", 3)[-1] |
| 847 |
|
| 848 |
|
| 849 |
class TestUserUWS(trialhelpers.ArchiveTest): |
| 850 |
def testWorkingLifecycle(self): |
| 851 |
def assertDeleted(result, jobURL): |
| 852 |
self.assertEqual(result[1].code, 303) |
| 853 |
next = _nukeHostPart(result[1].getLocationValue()) |
| 854 |
jobId = next.split("/")[-1] |
| 855 |
return self.assertGETLacksStrings(next, {}, ['jobref id="%s"'%jobId] |
| 856 |
).addCallback(lambda res: reactor.disconnectAll()) |
| 857 |
|
| 858 |
def delete(jobURL): |
| 859 |
return trialhelpers.runQuery(self.renderer, "DELETE", jobURL, {} |
| 860 |
).addCallback(assertDeleted, jobURL) |
| 861 |
|
| 862 |
def checkOtherResult(result, jobURL): |
| 863 |
self.assertEqual( |
| 864 |
result[1].responseHeaders.getRawHeaders("content-type"), |
| 865 |
["text/plain"]) |
| 866 |
self.assertEqual(result[0], b"Hello World.\n") |
| 867 |
return delete(jobURL) |
| 868 |
|
| 869 |
def checkResult(result, jobURL): |
| 870 |
self.assertTrue(b"<TR><TD>1.0</TD><TD>3.0</TD><TD>1.151292" in result[0]) |
| 871 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 872 |
jobURL+"/results/aux.txt", {} |
| 873 |
).addCallback(checkOtherResult, jobURL) |
| 874 |
|
| 875 |
def checkFinished(result, jobURL): |
| 876 |
self.assertTrue(b"phase>COMPLETED" in result[0]) |
| 877 |
self.assertTrue(b'xlink:href="http://' in result[0]) |
| 878 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 879 |
jobURL+"/results/result", {} |
| 880 |
).addCallback(checkResult, jobURL) |
| 881 |
|
| 882 |
def waitForResult(result, jobURL, ct=0): |
| 883 |
if ct>20: |
| 884 |
raise AssertionError("user UWS job doesn't COMPLETE or ERROR") |
| 885 |
time.sleep(0.5) |
| 886 |
if b"phase>COMPLETED" in result[0] or b"phase>ERROR" in result[0]: |
| 887 |
if b"phase>ERROR" in result[0]: |
| 888 |
raise AssertionError("UWS user test job failed with %s"%result[0]) |
| 889 |
return checkFinished(result, jobURL) |
| 890 |
else: |
| 891 |
return trialhelpers.runQuery(self.renderer, "GET", jobURL, {} |
| 892 |
).addCallback(waitForResult, jobURL, ct+1) |
| 893 |
|
| 894 |
def checkParametersImmutable(result, jobURL): |
| 895 |
self.assertStringsIn(result, ['<INFO name="QUERY_STATUS" value="ERROR">', |
| 896 |
'Field phase: Parameters cannot be changed in phase']) |
| 897 |
self.assertEqual(result[1].code, 200) |
| 898 |
return trialhelpers.runQuery(self.renderer, "GET", jobURL, {} |
| 899 |
).addCallback(waitForResult, jobURL) |
| 900 |
|
| 901 |
def checkPhaseIsExecuting(result, jobURL): |
| 902 |
self.assertEqual(result[0], b"EXECUTING") |
| 903 |
return trialhelpers.runQuery(self.renderer, "POST", |
| 904 |
jobURL+"/parameters", {"opim": ["4"]} |
| 905 |
).addCallback(checkParametersImmutable, jobURL) |
| 906 |
|
| 907 |
def assertStarted(lastRes, jobURL): |
| 908 |
req = lastRes[1] |
| 909 |
self.assertEqual(req.code, 303) |
| 910 |
self.assertTrue( |
| 911 |
req.getLocationValue().endswith(jobURL)) |
| 912 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 913 |
jobURL+"/phase", {} |
| 914 |
).addCallback(checkPhaseIsExecuting, jobURL) |
| 915 |
|
| 916 |
def checkPosted(result): |
| 917 |
request = result[1] |
| 918 |
self.assertEqual(request.code, 303) |
| 919 |
jobURL = _nukeHostPart( |
| 920 |
request.getLocationValue()) |
| 921 |
self.assertTrue(jobURL.startswith("/data/cores/pc/async/")) |
| 922 |
return trialhelpers.runQuery(self.renderer, "POST", |
| 923 |
jobURL+"/phase", {"PHASE": "RUN"} |
| 924 |
).addCallback(assertStarted, jobURL) |
| 925 |
|
| 926 |
# See the same thing in test_tap. What can I do? |
| 927 |
return trialhelpers.runQuery(self.renderer, "POST", |
| 928 |
"/data/cores/pc/uws.xml", { |
| 929 |
"opre": ["1"], "opim": ["3"], "powers": ["1 2 3"], |
| 930 |
"responseformat": "application/x-votable+xml;serialization=TABLEDATA", |
| 931 |
} |
| 932 |
).addCallback(checkPosted) |
| 933 |
|
| 934 |
def testParameterSetting(self): |
| 935 |
def deleteJob(jobURL): |
| 936 |
return trialhelpers.runQuery(self.renderer, "DELETE", jobURL, {} |
| 937 |
).addCallback(lambda res: reactor.disconnectAll()) |
| 938 |
|
| 939 |
def assertParams(result, jobURL): |
| 940 |
self.assertTrue( |
| 941 |
b'<uws:parameter id="opim">3.0</uws:parameter>' in result[0], "opim") |
| 942 |
self.assertTrue( |
| 943 |
b'<uws:parameter id="powers">1 2 3</uws:parameter>' in result[0], |
| 944 |
"powers") |
| 945 |
self.assertTrue( |
| 946 |
b'<uws:parameter id="responseformat">application/x-votable+xml' |
| 947 |
b'</uws:parameter>' in result[0], |
| 948 |
"responseformat") |
| 949 |
self.assertTrue( |
| 950 |
b'<uws:parameter id="opre">1.0</uws:parameter>' in result[0], |
| 951 |
"opre") |
| 952 |
self.assertTrue(re.search(b'<uws:parameter byReference="True" id="stuff">' |
| 953 |
b'http://localhost:8080/data/cores/pc/async/[^/]*/results/stuff', |
| 954 |
result[0]), "stuff from upload") |
| 955 |
|
| 956 |
self.assertTrue(b'<uws:quote xsi:nil="true"></uws:quote>' |
| 957 |
in result[0]) |
| 958 |
|
| 959 |
return deleteJob(jobURL) |
| 960 |
|
| 961 |
def checkParameters(result): |
| 962 |
jobURL = _nukeHostPart( |
| 963 |
result[1].getLocationValue()) |
| 964 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 965 |
jobURL, {} |
| 966 |
).addCallback(assertParams, jobURL) |
| 967 |
|
| 968 |
return trialhelpers.runQuery(self.renderer, "POST", |
| 969 |
"/data/cores/pc/uws.xml", { |
| 970 |
"opre": ["1"], "opim": ["3"], "powers": ["1 2 3"], |
| 971 |
"upload": ["stuff,param:foo"], |
| 972 |
"foo": trialhelpers.FakeFile( |
| 973 |
"wronsky", b"abc, die Katze lief im Schnee.\n"), |
| 974 |
} |
| 975 |
).addCallback(checkParameters) |
| 976 |
|
| 977 |
def testMultiUpload(self): |
| 978 |
def assertOverwritten(result, jobURL): |
| 979 |
self.assertEqual(result[1].code, 200) |
| 980 |
self.assertEqual(b"overwritten", result[0]) |
| 981 |
return trialhelpers.runQuery(self.renderer, "DELETE", jobURL, {} |
| 982 |
).addCallback(lambda res: reactor.disconnectAll()) |
| 983 |
|
| 984 |
def retrieveOverwritten(result, jobURL): |
| 985 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 986 |
jobURL+"/results/stuff", {} |
| 987 |
).addCallback(assertOverwritten, jobURL) |
| 988 |
|
| 989 |
def assertFilePresent(result, jobURL): |
| 990 |
self.assertEqual(b"abc, die Katze lief im Schnee.\n", |
| 991 |
result[0]) |
| 992 |
return trialhelpers.runQuery(self.renderer, "POST", |
| 993 |
jobURL+"/parameters", {"UPLOAD": ["stuff,param:overwrite"], |
| 994 |
"overwrite": trialhelpers.FakeFile("bye", b"overwritten")} |
| 995 |
).addCallback(retrieveOverwritten, jobURL) |
| 996 |
|
| 997 |
def assertParams(result, jobURL): |
| 998 |
self.assertTrue(re.search(b'<uws:parameter byReference="True"' |
| 999 |
b' id="stuff">http://localhost:8080/data/cores/uc/async/[^/]*/' |
| 1000 |
b'results/stuff</uws:parameter>', result[0]), "stuff") |
| 1001 |
self.assertTrue(re.search(b'<uws:parameter byReference="True"' |
| 1002 |
b' id="other">http://localhost:8080/data/cores/uc/async/[^/]*/' |
| 1003 |
b'results/other</uws:parameter>', result[0]), "other") |
| 1004 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 1005 |
jobURL+"/results/stuff", {} |
| 1006 |
).addCallback(assertFilePresent, jobURL) |
| 1007 |
|
| 1008 |
def getJobURL(result): |
| 1009 |
jobURL = _nukeHostPart( |
| 1010 |
result[1].getLocationValue()) |
| 1011 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 1012 |
jobURL, {} |
| 1013 |
).addCallback(assertParams, jobURL) |
| 1014 |
|
| 1015 |
return trialhelpers.runQuery(self.renderer, "POST", |
| 1016 |
"/data/cores/uc/uws.xml", { |
| 1017 |
"UPLOAD": ["stuff,param:foo", "other,param:bar"], |
| 1018 |
"foo": trialhelpers.FakeFile("foo", b"abc, die Katze lief im Schnee.\n"), |
| 1019 |
"bar": trialhelpers.FakeFile("bar", b"Other stuff"), |
| 1020 |
} |
| 1021 |
).addCallback(getJobURL) |
| 1022 |
|
| 1023 |
|
| 1024 |
def _setUser(username, password=None): |
| 1025 |
def _(request): |
| 1026 |
request.user = utils.bytify(username) |
| 1027 |
request.password = password or username |
| 1028 |
return _ |
| 1029 |
|
| 1030 |
|
| 1031 |
class TestUWSAuth(trialhelpers.ArchiveTest): |
| 1032 |
def testAuthBig(self): |
| 1033 |
def cleanupNext(result, jobURL2): |
| 1034 |
return trialhelpers.runQuery(self.renderer, "DELETE", |
| 1035 |
jobURL2, {}) |
| 1036 |
|
| 1037 |
def assertUnauthenticatedSeesAll(result, jobURL, jobURL2): |
| 1038 |
self.assertTrue(utils.bytify(jobURL) in result[0]) |
| 1039 |
self.assertTrue(utils.bytify(jobURL2) in result[0]) |
| 1040 |
return trialhelpers.runQuery(self.renderer, "DELETE", |
| 1041 |
jobURL, {} |
| 1042 |
).addCallback(cleanupNext, jobURL2) |
| 1043 |
|
| 1044 |
def assertOnlyOwnedJobsVisible(result, jobURL, jobURL2): |
| 1045 |
self.assertTrue(utils.bytify(jobURL) in result[0]) |
| 1046 |
self.assertFalse(utils.bytify(jobURL2) in result[0], |
| 1047 |
"public job in private job list") |
| 1048 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 1049 |
"/".join(jobURL.split("/")[:-1]), {} |
| 1050 |
).addCallback(assertUnauthenticatedSeesAll, jobURL, jobURL2) |
| 1051 |
|
| 1052 |
def queryJobList(result, jobURL): |
| 1053 |
jobURL2 = _nukeHostPart( |
| 1054 |
result[1].getLocationValue()) |
| 1055 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 1056 |
"/".join(jobURL.split("/")[:-1]), {}, |
| 1057 |
requestMogrifier=_setUser("testing") |
| 1058 |
).addCallback(assertOnlyOwnedJobsVisible, jobURL, jobURL2) |
| 1059 |
|
| 1060 |
def assertNoAccessWithBadCreds(result, jobURL): |
| 1061 |
self.assertEqual(result[1].code, 401) |
| 1062 |
|
| 1063 |
# now create an anonymous job so you can see whether it's visible |
| 1064 |
return trialhelpers.runQuery(self.renderer, "POST", |
| 1065 |
"/data/cores/pc/uws.xml", { |
| 1066 |
"opre": ["5"], "opim": ["7"], "powers": ["8", "9", "10"]} |
| 1067 |
).addCallback(queryJobList, jobURL) |
| 1068 |
|
| 1069 |
def assertNoGeneralAccess(result, jobURL): |
| 1070 |
self.assertEqual(result[1].code, 401) |
| 1071 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 1072 |
jobURL, {}, requestMogrifier=_setUser("testing", b"notright") |
| 1073 |
).addCallback(assertNoAccessWithBadCreds, jobURL) |
| 1074 |
|
| 1075 |
def assertAuthenticatedAccess(result, jobURL): |
| 1076 |
self.assertTrue(b"<uws:ownerId>testing</uws:ownerId>" in result[0]) |
| 1077 |
self.assertTrue(b"1 2 3</uws:parame" in result[0]) |
| 1078 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 1079 |
jobURL, {}, |
| 1080 |
).addCallback(assertNoGeneralAccess, jobURL) |
| 1081 |
|
| 1082 |
def assertOwnerSet(result, jobURL): |
| 1083 |
self.assertEqual(result[0], b"testing") |
| 1084 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 1085 |
jobURL, {}, requestMogrifier=_setUser("testing") |
| 1086 |
).addCallback(assertAuthenticatedAccess, jobURL) |
| 1087 |
|
| 1088 |
def assertPosted(result): |
| 1089 |
request = result[1] |
| 1090 |
jobURL = _nukeHostPart(request.getLocationValue()) |
| 1091 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 1092 |
jobURL+"/owner", {}, requestMogrifier=_setUser("testing") |
| 1093 |
).addCallback(assertOwnerSet, jobURL) |
| 1094 |
|
| 1095 |
return trialhelpers.runQuery(self.renderer, "POST", |
| 1096 |
"/data/cores/pc/uws.xml", { |
| 1097 |
"opre": ["1"], "opim": ["3"], "powers": ["-20", "1 2 3"]}, |
| 1098 |
requestMogrifier=_setUser("testing") |
| 1099 |
).addCallback(assertPosted) |
| 1100 |
|
| 1101 |
|
| 1102 |
def _nukeServicePart(url): |
| 1103 |
# remove service from UWS URL so we can test for visibility of services |
| 1104 |
# in different user uwses below |
| 1105 |
return '/'.join(url.split('/')[-2:]) |
| 1106 |
|
| 1107 |
class UserUWSJoblistTest(trialhelpers.ArchiveTest): |
| 1108 |
|
| 1109 |
# visisbility of UWS jobs created with auth: pcJob is private, |
| 1110 |
# ucJob is public. |
| 1111 |
def testJoblist(self): |
| 1112 |
def assertPCJoblist(result, pcJobURL, ucJobURL): |
| 1113 |
self.assertStringsIn(result, [_nukeServicePart(pcJobURL)]) |
| 1114 |
self.assertStringsIn(result, [_nukeServicePart(ucJobURL)], inverse=True) |
| 1115 |
trialhelpers.runQuery(self.renderer, "DELETE", |
| 1116 |
pcJobURL, {}, |
| 1117 |
requestMogrifier=_setUser("testing")) |
| 1118 |
return trialhelpers.runQuery(self.renderer, "DELETE", |
| 1119 |
ucJobURL, {}) |
| 1120 |
|
| 1121 |
def assertUCJoblist(result, pcJobURL, ucJobURL): |
| 1122 |
self.assertStringsIn(result, [_nukeServicePart(ucJobURL)]) |
| 1123 |
self.assertStringsIn(result, [_nukeServicePart(pcJobURL)], inverse=True) |
| 1124 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 1125 |
"/data/cores/pc/uws.xml", {} |
| 1126 |
).addCallback(assertPCJoblist, pcJobURL, ucJobURL) |
| 1127 |
|
| 1128 |
def getJoblist(result, pcJobURL): |
| 1129 |
ucJobURL = _nukeHostPart(result[1].getLocationValue()) |
| 1130 |
return trialhelpers.runQuery(self.renderer, "GET", |
| 1131 |
"/data/cores/uc/uws.xml", {}, |
| 1132 |
).addCallback(assertUCJoblist, pcJobURL, ucJobURL) |
| 1133 |
|
| 1134 |
def postOther(result): |
| 1135 |
pcJobURL = _nukeHostPart(result[1].getLocationValue()) |
| 1136 |
return trialhelpers.runQuery(self.renderer, "POST", |
| 1137 |
"/data/cores/uc/uws.xml", { |
| 1138 |
"UPLOAD": ["stuff,param:foo", "other,param:bar"], |
| 1139 |
"foo": trialhelpers.FakeFile( |
| 1140 |
"knöterich", b"abc, die Katze lief im Schnee.\n"), |
| 1141 |
"bar": trialhelpers.FakeFile("/schnalle/", b"Other stuff"), |
| 1142 |
}, |
| 1143 |
).addCallback(getJoblist, pcJobURL) |
| 1144 |
|
| 1145 |
return trialhelpers.runQuery(self.renderer, "POST", |
| 1146 |
"/data/cores/pc/uws.xml", { |
| 1147 |
"opre": ["1"], "opim": ["3"], "powers": ["1", "2", "3"]}, |
| 1148 |
requestMogrifier=_setUser("testing") |
| 1149 |
).addCallback(postOther) |
| 1150 |
|
| 1151 |
|
| 1152 |
def _requestCompression(request): |
| 1153 |
request.requestHeaders.addRawHeader("Accept-Encoding", "gzip") |
| 1154 |
|
| 1155 |
|
| 1156 |
class LogInOutTest(trialhelpers.ArchiveTest): |
| 1157 |
def testLoginRequest(self): |
| 1158 |
def assertStatus(res): |
| 1159 |
self.assertEqual(res[1].code, 401) |
| 1160 |
|
| 1161 |
return self.assertGETHasStrings("/login", {}, [ |
| 1162 |
"<p>Please enter your credentials (by reloading this page)"] |
| 1163 |
).addCallback(assertStatus) |
| 1164 |
|
| 1165 |
def testLoginSuccessful(self): |
| 1166 |
def assertNextURL(res): |
| 1167 |
self.assertTrue(b'<a href="http://localhost:8080/rompp">' in res[0]) |
| 1168 |
self.assertEqual( |
| 1169 |
res[1].getLocationValue(), |
| 1170 |
"http://localhost:8080/rompp") |
| 1171 |
return res |
| 1172 |
|
| 1173 |
return self.assertStatus("/login", 303, {"nextURL": ["/rompp"]}, |
| 1174 |
rm=trialhelpers.addCreds |
| 1175 |
).addCallback(assertNextURL) |
| 1176 |
|
| 1177 |
def testLogoutAuth(self): |
| 1178 |
def assertStatus(res): |
| 1179 |
self.assertEqual(res[1].code, 401) |
| 1180 |
|
| 1181 |
return self.assertGETHasStrings("/login", {"nextURL": ["/rompp"], |
| 1182 |
"relog": ["True"]}, [ |
| 1183 |
"<title>Unittest Suite -- Authentication Required</title>"], |
| 1184 |
rm=trialhelpers.addCreds, |
| 1185 |
).addCallback(assertStatus) |
| 1186 |
|
| 1187 |
def testLogoutNonauth(self): |
| 1188 |
def assertNextURL(res): |
| 1189 |
self.assertEqual( |
| 1190 |
res[1].getLocationValue(), |
| 1191 |
"http://localhost:8080/rompp") |
| 1192 |
return res |
| 1193 |
|
| 1194 |
return self.assertStatus("/login", 303, {"nextURL": ["/rompp"], |
| 1195 |
"relog": "True"}, |
| 1196 |
).addCallback(assertNextURL) |
| 1197 |
|
| 1198 |
def testLogoutNoNextURL(self): |
| 1199 |
return self.assertGETHasStrings("/login", {"relog": ["True"]}, [ |
| 1200 |
"<p>The resource you are trying to access is protected, and "], |
| 1201 |
rm=trialhelpers.addCreds) |
| 1202 |
|
| 1203 |
def testNoBogusCredsInBrowser(self): |
| 1204 |
return self.assertGETHasStrings("/__system__/adql/query/form", {}, |
| 1205 |
[">Log in<"], |
| 1206 |
rm=lambda req: trialhelpers.addCreds(req, "admin", "admin")) |
| 1207 |
|
| 1208 |
|
| 1209 |
class AuthcheckTest(trialhelpers.ArchiveTest): |
| 1210 |
def _assert401(self, res): |
| 1211 |
self.assertEqual(res[1].code, 401) |
| 1212 |
return res |
| 1213 |
|
| 1214 |
def _assert200(self, res): |
| 1215 |
self.assertEqual(res[1].code, 200) |
| 1216 |
return res |
| 1217 |
|
| 1218 |
def _assertHasChallenge(self, res): |
| 1219 |
ch = res[1].responseHeaders.getRawHeaders("www-authenticate")[0] |
| 1220 |
self.assertEqual(ch, 'Basic realm="dachsunit"') |
| 1221 |
return res |
| 1222 |
|
| 1223 |
def _assertHasNoChallenge(self, res): |
| 1224 |
self.assertEqual( |
| 1225 |
res[1].responseHeaders.getRawHeaders("www-authenticate"), |
| 1226 |
None) |
| 1227 |
return res |
| 1228 |
|
| 1229 |
def _assertAuthUser(self, user): |
| 1230 |
def _(res): |
| 1231 |
u = res[1].responseHeaders.getRawHeaders("x-vo-authenticated")[0] |
| 1232 |
self.assertEqual(u, user) |
| 1233 |
return res |
| 1234 |
return _ |
| 1235 |
|
| 1236 |
def _assertNoAuthUser(self, res): |
| 1237 |
self.assertEqual( |
| 1238 |
res[1].responseHeaders.getRawHeaders("x-vo-authenticated"), |
| 1239 |
None) |
| 1240 |
return res |
| 1241 |
|
| 1242 |
def testLimitToOk(self): |
| 1243 |
return self.assertGETHasStrings("/data/cores/keepout/capabilities", {}, |
| 1244 |
["<accessURL"], |
| 1245 |
rm=trialhelpers.addCreds |
| 1246 |
).addCallback(self._assert200 |
| 1247 |
).addCallback(self._assertHasNoChallenge |
| 1248 |
).addCallback(self._assertAuthUser("testing")) |
| 1249 |
|
| 1250 |
def testLimitToBadPass(self): |
| 1251 |
return self.assertGETHasStrings("/data/cores/keepout/capabilities", {}, |
| 1252 |
["access is protected"], |
| 1253 |
rm=lambda req:trialhelpers.addCreds(req, password="wrong") |
| 1254 |
).addCallback(self._assert401 |
| 1255 |
).addCallback(self._assertHasChallenge |
| 1256 |
).addCallback(self._assertNoAuthUser) |
| 1257 |
|
| 1258 |
def testLimitToBadUser(self): |
| 1259 |
return self.assertGETHasStrings("/data/cores/keepout/capabilities", {}, |
| 1260 |
["access is protected"], |
| 1261 |
rm=lambda req:trialhelpers.addCreds(req, user="other", password="other") |
| 1262 |
).addCallback(self._assert401 |
| 1263 |
).addCallback(self._assertHasChallenge |
| 1264 |
).addCallback(self._assertNoAuthUser) |
| 1265 |
|
| 1266 |
def testNocredClosed(self): |
| 1267 |
return self.assertGETHasStrings("/data/cores/keepout/capabilities", {}, |
| 1268 |
["access is protected"], |
| 1269 |
).addCallback(self._assert401 |
| 1270 |
).addCallback(self._assertHasChallenge |
| 1271 |
).addCallback(self._assertNoAuthUser) |
| 1272 |
|
| 1273 |
def testOpenBadPass(self): |
| 1274 |
return self.assertGETHasStrings("/data/cores/coltest/capabilities", {}, |
| 1275 |
["<accessURL"], |
| 1276 |
rm=lambda req:trialhelpers.addCreds(req, password="wrong") |
| 1277 |
).addCallback(self._assert401 |
| 1278 |
).addCallback(self._assertHasChallenge |
| 1279 |
).addCallback(self._assertNoAuthUser) |
| 1280 |
|
| 1281 |
def testOpenGoodPass(self): |
| 1282 |
return self.assertGETHasStrings("/data/cores/coltest/capabilities", {}, |
| 1283 |
["<accessURL"], |
| 1284 |
rm=lambda req:trialhelpers.addCreds(req, user="other", password="other") |
| 1285 |
).addCallback(self._assert200 |
| 1286 |
).addCallback(self._assertHasNoChallenge |
| 1287 |
).addCallback(self._assertAuthUser("other")) |
| 1288 |
|
| 1289 |
def testOnTAP(self): |
| 1290 |
return self.assertGETHasStrings("/tap/capabilities", {}, |
| 1291 |
["<accessURL"], |
| 1292 |
rm=lambda req:trialhelpers.addCreds(req, user="other", password="other") |
| 1293 |
).addCallback(self._assert200 |
| 1294 |
).addCallback(self._assertHasNoChallenge |
| 1295 |
).addCallback(self._assertAuthUser("other")) |
| 1296 |
|
| 1297 |
def testNocredOpen(self): |
| 1298 |
return self.assertGETHasStrings("/data/cores/coltest/capabilities", {}, |
| 1299 |
["<accessURL"], |
| 1300 |
).addCallback(self._assert200 |
| 1301 |
).addCallback(self._assertHasChallenge |
| 1302 |
).addCallback(self._assertNoAuthUser) |
| 1303 |
|
| 1304 |
def testHeaderAdded(self): |
| 1305 |
return self.assertGETHasStrings("/data/cores/keepout/form", {}, |
| 1306 |
['<form id="genForm" action="data/cores/keepout/form"'], |
| 1307 |
rm=trialhelpers.addCreds).addCallback( |
| 1308 |
self._assertAuthUser("testing")) |
| 1309 |
|
| 1310 |
|
| 1311 |
@contextlib.contextmanager |
| 1312 |
def listeningToHTTPS(listening): |
| 1313 |
lastval = base.LISTENING_TO_HTTPS |
| 1314 |
try: |
| 1315 |
base.LISTENING_TO_HTTPS = listening |
| 1316 |
yield |
| 1317 |
finally: |
| 1318 |
base.LISTENING_TO_HTTPS = lastval |
| 1319 |
|
| 1320 |
|
| 1321 |
class UpgradeInsecureRequestsTest(trialhelpers.ArchiveTest): |
| 1322 |
@staticmethod |
| 1323 |
def _addUIR(request): |
| 1324 |
request.requestHeaders.setRawHeaders( |
| 1325 |
"Upgrade-Insecure-Requests", ["1"]) |
| 1326 |
|
| 1327 |
def testNoUpgradesWithoutHTTPS(self): |
| 1328 |
with listeningToHTTPS(False): |
| 1329 |
return self.assertGETHasStrings("/", {}, |
| 1330 |
["<h2>Services available here</h2>"], |
| 1331 |
rm=self._addUIR) |
| 1332 |
|
| 1333 |
def testBasicUpgrade(self): |
| 1334 |
def assertRedirection(res): |
| 1335 |
self.assertEqual(res[1].getLocationValue(), |
| 1336 |
"https://localhost/__system__/adql/query/form") |
| 1337 |
self.assertEqual(res[1].code, 307) |
| 1338 |
|
| 1339 |
with listeningToHTTPS(True): |
| 1340 |
return self.assertGETHasStrings("/__system__/adql/query/form", {}, |
| 1341 |
["redirected to an https version\r\n"], |
| 1342 |
rm=self._addUIR |
| 1343 |
).addCallback(assertRedirection) |
| 1344 |
|
| 1345 |
def testNoUpgradeOnPOST(self): |
| 1346 |
with listeningToHTTPS(True): |
| 1347 |
return self.assertPOSTHasStrings("/__system__/adql/query/form", |
| 1348 |
{"__nevow_form__": "genForm"}, |
| 1349 |
["ADQL query : </strong>Required</li>"], |
| 1350 |
rm=self._addUIR) |
| 1351 |
|
| 1352 |
def testNoUpgradeWhenSecure(self): |
| 1353 |
def makeSecure(req): |
| 1354 |
self._addUIR(req) |
| 1355 |
req.secure = True |
| 1356 |
|
| 1357 |
with listeningToHTTPS(True): |
| 1358 |
return self.assertGETHasStrings("/", {}, |
| 1359 |
["<h2>Services available here</h2>"], |
| 1360 |
rm=makeSecure) |
| 1361 |
|
| 1362 |
|
| 1363 |
class CachingTest(trialhelpers.ArchiveTest): |
| 1364 |
def assertWarmComesFromCache(self, res): |
| 1365 |
creationStamp = utils.debytify( |
| 1366 |
res[1].responseHeaders.getRawHeaders("x-cache-creation")[0]) |
| 1367 |
self.assertTrue(abs(float(creationStamp)-time.time())<1) |
| 1368 |
return res |
| 1369 |
|
| 1370 |
def assertColdNotFromCache(self, res): |
| 1371 |
self.assertFalse(res[1].responseHeaders.hasHeader("x-cache-creation")) |
| 1372 |
return self.assertGETHasStrings("data/seeded/hooray/info", |
| 1373 |
{}, [ |
| 1374 |
"<title>Information on Service 'Injection testing'</title>"] |
| 1375 |
).addCallback(self.assertWarmComesFromCache) |
| 1376 |
|
| 1377 |
def testBasicCaching(self): |
| 1378 |
base.caches.clearForName("data/seeded") |
| 1379 |
return self.assertGETHasStrings("data/seeded/hooray/info", |
| 1380 |
{}, [ |
| 1381 |
"<title>Information on Service 'Injection testing'</title>"] |
| 1382 |
).addCallback(self.assertColdNotFromCache) |
| 1383 |
|
| 1384 |
|
| 1385 |
def assertWithUserNotFromCache(self, res, again): |
| 1386 |
self.assertFalse(res[1].responseHeaders.hasHeader("x-cache-creation")) |
| 1387 |
if again: |
| 1388 |
return self.assertGETHasStrings("data/seeded/hooray/info", |
| 1389 |
{}, [ |
| 1390 |
"<title>Information on Service 'Injection testing'</title>"], |
| 1391 |
rm=trialhelpers.addCreds |
| 1392 |
).addCallback(self.assertWithUserNotFromCache, 0) |
| 1393 |
return res |
| 1394 |
|
| 1395 |
def testNoCachingWithUser(self): |
| 1396 |
return self.assertGETHasStrings("data/seeded/hooray/info", |
| 1397 |
{}, [ |
| 1398 |
"<title>Information on Service 'Injection testing'</title>"], |
| 1399 |
rm=trialhelpers.addCreds |
| 1400 |
).addCallback(self.assertWithUserNotFromCache, 1) |
| 1401 |
|
| 1402 |
|
| 1403 |
def assertOverfullCacheRejects(self, res): |
| 1404 |
pc = base.caches.getPageCache("data/seeded") |
| 1405 |
self.assertEqual(pc.cacheDict, {}) |
| 1406 |
pc.maxSize = 1000000 |
| 1407 |
return res |
| 1408 |
|
| 1409 |
def testCacheLimit(self): |
| 1410 |
base.caches.clearForName("data/seeded") |
| 1411 |
_ = base.caches.getRD("data/seeded") |
| 1412 |
base.caches.getPageCache("data/seeded").maxSize = 10 |
| 1413 |
|
| 1414 |
return self.assertGETHasStrings("data/seeded/hooray/info", |
| 1415 |
{}, [ |
| 1416 |
"<title>Information on Service 'Injection testing'</title>"] |
| 1417 |
).addCallback(self.assertOverfullCacheRejects) |
| 1418 |
|
| 1419 |
|
| 1420 |
class ThirdPartyCacheTest(trialhelpers.ArchiveTest): |
| 1421 |
def setUp(self): |
| 1422 |
prevCache = os.path.join(base.getConfig("cacheDir"), "3rdparty", "testing") |
| 1423 |
if os.path.exists(prevCache): |
| 1424 |
os.unlink(prevCache) |
| 1425 |
|
| 1426 |
def testRemoteFailure(self): |
| 1427 |
d = trialhelpers.DataServer("Something.\n") |
| 1428 |
url = d.__enter__() |
| 1429 |
ifpages.ThirdPartyCachePage.urlMapping[b"testing"] = utils.bytify( |
| 1430 |
url+"/does-not-exist") |
| 1431 |
return self.assertGETHasStrings("3rdparty/testing", {}, [ |
| 1432 |
'<div class="errmsg">404 Not Found</div>']).addBoth( |
| 1433 |
lambda res: d.__exit__(None, None, None) or res) |
| 1434 |
|
| 1435 |
def testRemoteSuccess(self): |
| 1436 |
d = trialhelpers.DataServer("Something.\n") |
| 1437 |
url = d.__enter__() |
| 1438 |
ifpages.ThirdPartyCachePage.urlMapping[b"testing"] = utils.bytify(url) |
| 1439 |
return self.assertGETHasStrings("3rdparty/testing", {}, [ |
| 1440 |
"Something.\n"]).addBoth( |
| 1441 |
lambda res: d.__exit__(None, None, None) or res) |
| 1442 |
|
| 1443 |
def test404(self): |
| 1444 |
return self.assertStatus("3rdparty/does_not_exist", 404) |
| 1445 |
|
| 1446 |
|
| 1447 |
_SUPERSEDED_RD_LITERAL = """<resource schema="data"> |
| 1448 |
<meta name="superseded" format="rst"> |
| 1449 |
This stuff is stuffy. Go here_ instead. |
| 1450 |
|
| 1451 |
.. _here: http://blog.g-vo.org |
| 1452 |
</meta> |
| 1453 |
<table id="dust"><column name="in_the_wind"/></table></resource>""" |
| 1454 |
|
| 1455 |
|
| 1456 |
class SupersededTest(trialhelpers.ArchiveTest): |
| 1457 |
def testRDSuperseded(self): |
| 1458 |
def assert404(res): |
| 1459 |
self.assertEqual(res[1].code, 404) |
| 1460 |
return res |
| 1461 |
|
| 1462 |
tmprd = trialhelpers.testFile("withsup.rd", _SUPERSEDED_RD_LITERAL, |
| 1463 |
inDir=api.getConfig("inputsDir")) |
| 1464 |
tmprd.__enter__() |
| 1465 |
|
| 1466 |
return self.assertGETHasStrings("withsup/s/wop", {}, [ |
| 1467 |
'class="superseded-message"', |
| 1468 |
"<p>This stuff is stuffy", |
| 1469 |
'href="http://blog.g-vo.org"'] |
| 1470 |
).addCallback(assert404 |
| 1471 |
).addBoth(lambda res: tmprd.__exit__(None, None, None) or res) |
| 1472 |
|
| 1473 |
|
| 1474 |
def testServiceSuperseded(self): |
| 1475 |
def assert404(res): |
| 1476 |
self.assertEqual(res[1].code, 404) |
| 1477 |
return res |
| 1478 |
|
| 1479 |
return self.assertGETHasStrings("data/cores/zombie/scs.xml", {}, [ |
| 1480 |
'href="https://en.wikipedia.org/wiki/George_A._Romero"', |
| 1481 |
"<p>Brains! Brains! <a "] |
| 1482 |
).addCallback(assert404) |
| 1483 |
|
| 1484 |
|
| 1485 |
class SchemaTest(trialhelpers.ArchiveTest): |
| 1486 |
def test404(self): |
| 1487 |
return self.assertGETHasStrings("schemata/../../", {}, [ |
| 1488 |
"div class=\"errmsg\">'..' is neither a prefix nor a" |
| 1489 |
" schema name known here</div>"]) |
| 1490 |
|
| 1491 |
def testByPrefix(self): |
| 1492 |
def assertRedirect(result): |
| 1493 |
self.assertEqual(result[1].getLocationValue(), |
| 1494 |
"http://vo.ari.uni-heidelberg.de/docs/schemata/VOResource.xsd") |
| 1495 |
return result |
| 1496 |
|
| 1497 |
return self.assertGETHasStrings("schemata/vr", {}, [ |
| 1498 |
"This is supposed to redirect to http://vo.ari.uni-heidelberg.de" |
| 1499 |
"/docs/schemata/VOResource.xsd"] |
| 1500 |
).addCallback(assertRedirect) |
| 1501 |
|
| 1502 |
def testBySchemaName(self): |
| 1503 |
def assertRedirect(result): |
| 1504 |
self.assertEqual(result[1].getLocationValue(), |
| 1505 |
"http://vo.ari.uni-heidelberg.de/docs/schemata/VOResource.xsd") |
| 1506 |
return result |
| 1507 |
|
| 1508 |
return self.assertGETHasStrings("schemata/VOResource.xsd", {}, [ |
| 1509 |
"This is supposed to redirect to http://vo.ari.uni-heidelberg.de" |
| 1510 |
"/docs/schemata/VOResource.xsd"] |
| 1511 |
).addCallback(assertRedirect) |
| 1512 |
|
| 1513 |
def testSchemaIndex(self): |
| 1514 |
return self.assertGETHasStrings("schemata", {}, [ |
| 1515 |
'<a href="http://vo.ari.uni-heidelberg.de/docs/schemata/OAI-PMH' |
| 1516 |
'.xsd">oai</a>']) |
| 1517 |
|
| 1518 |
|
| 1519 |
class BlockingTest(trialhelpers.ArchiveTest): |
| 1520 |
def testNoMessageBlocking(self): |
| 1521 |
root.BLOCKED_HOSTS.add("this.is.no.ip.address") |
| 1522 |
def cleanup(res): |
| 1523 |
root.BLOCKED_HOSTS.pop() |
| 1524 |
return res |
| 1525 |
|
| 1526 |
return self.assertGETHasStrings("", {}, [ |
| 1527 |
"Sorry, you're blocked.\nThere should"]).addBoth(cleanup) |
| 1528 |
|
| 1529 |
def testWithMessageBlocking(self): |
| 1530 |
root.BLOCKED_HOSTS.add("this.is.no.ip.address") |
| 1531 |
msgFile = api.getConfig("stateDir") / "block-message.txt" |
| 1532 |
with open(msgFile, "w") as f: |
| 1533 |
f.write("Call us for good advice: ++49 6221 54 1837\n") |
| 1534 |
|
| 1535 |
def cleanup(res): |
| 1536 |
root.BLOCKED_HOSTS.pop() |
| 1537 |
os.unlink(msgFile) |
| 1538 |
return res |
| 1539 |
|
| 1540 |
return self.assertGETHasStrings("", {}, [ |
| 1541 |
"all us for good advice: ++49 6221 54 1837\n"]).addBoth(cleanup) |