Skip to content

Commit

Permalink
Merge pull request #38 from astro-datalab/devsp
Browse files Browse the repository at this point in the history
exclude UNPUBLISHED and add test for SSO
  • Loading branch information
jacquesalice authored Jun 3, 2024
2 parents 587b3ca + 9dde569 commit c26b4ee
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 24 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ dynamic = ["version", "description"]
[tool.flit.module]
name = "sparcl"

[tool.flit.sdist]
exclude = ["**/UNPUBLISHED/", "**/OBSOLETE/"]

[tool.black]
# https://black.readthedocs.io/en/stable/usage_and_configuration/index.html
line-length = 79
Expand Down
2 changes: 1 addition & 1 deletion sparcl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
# __version__ = "1.2.1b3"
# __version__ = "1.2.1"
# FIRST uncommented value will be used! (so only leave one uncommented)
__version__ = "1.2.2b8"
__version__ = "1.2.2b9"
2 changes: 1 addition & 1 deletion sparcl/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ def missing_specids(
are NOT stored in the SPARCL database.
Example:
>>> client = SparclClient(url=_PAT)
>>> client = SparclClient()
>>> found = client.find(outfields=['specid'], limit=2)
>>> specids = [f.specid for f in found.records]
>>> client.missing_specids(specids + ['bad_id'])
Expand Down
90 changes: 68 additions & 22 deletions tests/tests_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import unittest
from unittest import skip, skipUnless, skipIf
import datetime
import requests

#!import time
from contextlib import redirect_stdout
Expand Down Expand Up @@ -106,6 +107,7 @@

show_run_context = True


@contextmanager
def streamhandler_to_console(lggr):
# Use 'up to date' value of sys.stdout for StreamHandler,
Expand Down Expand Up @@ -133,11 +135,18 @@ def testcase_log_console(*args, **kwargs):
def load_tests(loader, tests, ignore):
import doctest

print(f"Arranging to run doctests against: sparcl.client")
tests.addTests(doctest.DocTestSuite(sparcl.client))
if serverurl == _PROD:
print(f"Arranging to run doctests against: sparcl.client")
tests.addTests(doctest.DocTestSuite(sparcl.client))

print(f"Arranging to run doctests against: sparcl.gather_2d")
tests.addTests(doctest.DocTestSuite(sparcl.gather_2d))
else:
print(
"Not running doctests since you are not running client"
" against the PRODUCTION server."
)

print(f"Arranging to run doctests against: sparcl.gather_2d")
tests.addTests(doctest.DocTestSuite(sparcl.gather_2d))
return tests


Expand Down Expand Up @@ -353,8 +362,10 @@ def test_retrieve_5(self):
"""Limit number of records returned by retrieve_by_specid."""
drs = ["SDSS-DR16", "BOSS-DR16", "DESI-EDR"]
res = self.client.retrieve_by_specid(
self.specid_list5, include=["specid", "ivar"], dataset_list=drs,
limit=2
self.specid_list5,
include=["specid", "ivar"],
dataset_list=drs,
limit=2,
)
actual = len(res.records)
if showact:
Expand All @@ -371,10 +382,10 @@ def test_find_0(self):
# To get suitable constraints (in sparc-shell on Server):
# sorted(FitsRecord.objects.all().values('ra','dec'),
# key=lambda r: r['dec'])
#if serverurl in DEV_SERVERS:
# if serverurl in DEV_SERVERS:
# !constraints = {"ra": [246.0, 247.0], "dec": [+34.7, +34.8]}
# constraints = {"ra": [194.0, 195.0], "dec": [+27.5, +27.6]}
#else:
# else:
# constraints = {"ra": [340.0, 341.0], "dec": [+3.0, +4.0]}
constraints = {"ra": [134.288, 134.291], "dec": [+28.34, +28.351]}
found = self.client.find(outfields, constraints=constraints, limit=3)
Expand All @@ -386,11 +397,14 @@ def test_find_0(self):
def test_find_1(self):
"""Get metadata using search spec."""
outfields = ["data_release", "specid"]
constraints = ({"redshift": [0.191, 0.192],
"exptime": [2100.2, 2100.31],
"data_release": ['SDSS-DR16']})
found = self.client.find(outfields, constraints=constraints,
limit=1, sort="specid") # @@@
constraints = {
"redshift": [0.191, 0.192],
"exptime": [2100.2, 2100.31],
"data_release": ["SDSS-DR16"],
}
found = self.client.find(
outfields, constraints=constraints, limit=1, sort="specid"
) # @@@
actual = sorted(found.records, key=lambda rec: rec["specid"])
if showact:
print(f"find_1: actual={pf(actual)}")
Expand Down Expand Up @@ -522,7 +536,7 @@ def test_reorder_2a(self):
actual = [f["sparcl_id"] for f in res_reorder.records]
if showact:
print(f"reorder_2a: actual={pf(actual)}")
self.assertEqual(actual, ids[:2] + ['None'], msg="Actual to Expected")
self.assertEqual(actual, ids[:2] + ["None"], msg="Actual to Expected")

def test_reorder_2b(self):
"""Reorder records when specid is missing from database, after
Expand All @@ -539,8 +553,9 @@ def test_reorder_2b(self):
actual = [f["specid"] for f in res_reorder.records]
if showact:
print(f"reorder_2b: actual={pf(actual)}")
self.assertEqual(actual, specids[:2] + [None],
msg="Actual to Expected")
self.assertEqual(
actual, specids[:2] + [None], msg="Actual to Expected"
)

def test_reorder_3a(self):
"""Test for expected Exception when a list of sparcl_ids with
Expand Down Expand Up @@ -780,18 +795,18 @@ def setUpClass(cls):
sort="sparcl_id",
)
).ids
#cls.uuid_pub = ( # cls.uuid_sdssdr16
# cls.uuid_pub = ( # cls.uuid_sdssdr16
# cls.client.find(
# outfields=out,
# constraints={"data_release": cls.Pub},
# limit=2,
# sort="sparcl_id",
# )
#).ids
# ).ids
cls.uuid_pub = (
cls.client.find(
outfields=out,
constraints={"data_release": ['BOSS-DR16']},
constraints={"data_release": ["BOSS-DR16"]},
limit=2,
sort="sparcl_id",
)
Expand Down Expand Up @@ -822,6 +837,39 @@ def silent_logout(cls):
def tearDownClass(cls):
pass

# curl -X 'POST' \
# 'http://localhost:8050/sparc/get_token/' \
# -H 'Content-Type: application/json' \
# -d '{"email": "[email protected]", "password": "XX"}'; echo
#
# > Could not get token from SSO server:
# HTTPSConnectionPool(host='docker1.csdc.noirlab.edu', port=443):
# Max retries exceeded with url: /api/token/
# (Caused by SSLError(SSLCertVerificationError(1,
# '[SSL: CERTIFICATE_VERIFY_FAILED]
# certificate verify failed: unable to get local issuer certificate
# (_ssl.c:997)')))

def test_sso_server(self):
sso_server = "https://sso.csdc.noirlab.edu/"
response = requests.get(sso_server)
self.assertEqual(response.status_code, 200, response.content.decode())

def test_get_token(self):
"""Make sure we can get expected SSO token."""
json = {"email": self.auth_user, "password": usrpw}
if showact:
print(f"test_get_token: {json=}")

expected = 281
res = requests.post(f"{self.client.apiurl}/get_token/", json=json)
self.assertEqual(res.status_code, 200, res.content.decode())
token = res.content.decode()
actual = len(token)
if showact:
print(f"test_get_token: ({len(token)}) {token=!s}")
self.assertEqual(actual, expected, msg="Actual to Expected")

def test_authorized_1(self):
"""Test authorized method with authorized user signed in"""
self.silent_login(self.auth_user, usrpw)
Expand Down Expand Up @@ -891,9 +939,7 @@ def auth_retrieve(self, user, drs, expvar):
inc = ["sparcl_id", "data_release"]
try:
if drs is None:
got = self.client.retrieve(
uuid_list=ids, include=inc
)
got = self.client.retrieve(uuid_list=ids, include=inc)
else:
got = self.client.retrieve(
uuid_list=ids, include=inc, dataset_list=drs
Expand Down

0 comments on commit c26b4ee

Please sign in to comment.