Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BinamB committed Feb 5, 2024
1 parent 2dc057e commit f558dde
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 227 deletions.
10 changes: 5 additions & 5 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -291,28 +291,28 @@
"filename": "tests/test_bundles.py",
"hashed_secret": "fd66f51cba49640055a05a6173764b5f0241c63e",
"is_verified": false,
"line_number": 137
"line_number": 143
},
{
"type": "Hex High Entropy String",
"filename": "tests/test_bundles.py",
"hashed_secret": "168762db39e35d49d630689f2ff453b5813a9255",
"is_verified": false,
"line_number": 152
"line_number": 160
},
{
"type": "Hex High Entropy String",
"filename": "tests/test_bundles.py",
"hashed_secret": "c5f0378cf93d896ecc394150943f13afa16ba766",
"is_verified": false,
"line_number": 174
"line_number": 184
},
{
"type": "Hex High Entropy String",
"filename": "tests/test_bundles.py",
"hashed_secret": "a2ca8b84f631b40d866b8e376d077da3527b1fe4",
"is_verified": false,
"line_number": 177
"line_number": 187
}
],
"tests/test_client.py": [
Expand Down Expand Up @@ -413,5 +413,5 @@
}
]
},
"generated_at": "2024-01-24T19:59:13Z"
"generated_at": "2024-02-05T18:09:32Z"
}
10 changes: 7 additions & 3 deletions indexd/alias/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ def get_alias():
if limit < 0 or limit > 1024:
raise UserError("limit must be between 0 and 1024")

aliases = blueprint.alias_driver.aliases(
start=start, limit=limit, size=size, hashes=hashes
)
try:
aliases = blueprint.alias_driver.aliases(
start=start, limit=limit, size=size, hashes=hashes
)
except Exception as e:
print("-------------------")
print(e)

base = {
"aliases": aliases,
Expand Down
38 changes: 21 additions & 17 deletions indexd/index/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,27 @@ def get_index(form=None):
negate_params=negate_params,
)
else:
records = blueprint.index_driver.ids(
start=start,
limit=limit,
page=page,
size=size,
file_name=file_name,
version=version,
urls=urls,
acl=acl,
authz=authz,
hashes=hashes,
uploader=uploader,
ids=ids,
metadata=metadata,
urls_metadata=urls_metadata,
negate_params=negate_params,
)
try:
records = blueprint.index_driver.ids(
start=start,
limit=limit,
page=page,
size=size,
file_name=file_name,
version=version,
urls=urls,
acl=acl,
authz=authz,
hashes=hashes,
uploader=uploader,
ids=ids,
metadata=metadata,
urls_metadata=urls_metadata,
negate_params=negate_params,
)
except Exception as e:
print("---------------------")
print(e)

base = {
"ids": ids,
Expand Down
63 changes: 58 additions & 5 deletions indexd/index/drivers/single_table_alchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,14 @@ def ids(

if urls_metadata:
for url_key, url_dict in urls_metadata.items():
matches = ""
for k, v in url_dict.items():
matches += '@.{} == "{}" && '.format(k, v)
if matches:
matches = matches.rstrip("&& ")
match_string = "$.* ? ({})".format(matches)
query = query.filter(
func.jsonb_path_match(
Record.url_metadata, '$.*.{} == "{}"'.format(k, v)
)
func.jsonb_path_exists(Record.url_metadata, match_string)
)

if negate_params:
Expand Down Expand Up @@ -918,7 +921,7 @@ def update(self, did, rev, changing_fields):
# ie file_name, version, etc
setattr(record, key, value)

record.rev = str(uuid.uuid4())[:8]
record.rev = str(uuid.uuid4())[:8]

record.updated_date = datetime.datetime.utcnow()

Expand Down Expand Up @@ -1151,7 +1154,8 @@ def update_all_versions(self, guid, acl=None, authz=None):

# User requires update permissions for all versions of the record
all_resources = []
all_resources.append([rec.authz] for rec in records)
for rec in records:
all_resources += rec.authz
auth.authorize("update", list(all_resources))

ret = []
Expand Down Expand Up @@ -1401,6 +1405,55 @@ def delete_bundle(self, bundle_id):

session.delete(record)

def query_urls(
self,
exclude=None,
include=None,
versioned=None,
offset=0,
limit=1000,
fields="did,urls",
**kwargs,
):
if kwargs:
raise UserError(
"Unexpected query parameter(s) {}".format(list(kwargs.keys()))
)

versioned = (
versioned.lower() in ["true", "t", "yes", "y"] if versioned else None
)

with self.driver.session as session:
query = session.query(Record.guid, func.string_agg(Record.urls, ","))
# add version filter if versioned is not None
if versioned is True: # retrieve only those with a version number
query = query.filter(~Record.version.isnot(None))
elif versioned is False: # retrieve only those without a version number
query = query.filter(~Record.version.isnot(None))

query = query.group_by(Record.guid)

# add url filters
if include and exclude:
query = query.having(
and_(
~func.string_agg(Record.urls, ",").contains(exclude),
func.string_agg(Record.urls, ",").contains(include),
)
)
elif include:
query = query.having(func.string_agg(Record.url, ",").contains(include))
elif exclude:
query = query.having(
~func.string_agg(Record.url, ",").contains(exclude)
)
print(query)
record_list = (
query.order_by(Record.guid.asc()).offset(offset).limit(limit).all()
)
return self._format_response(fields, record_list)


def check_url_metadata(url_metadata, record):
"""
Expand Down
13 changes: 5 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def clear_database():
"index_record_alias",
"index_record_metadata",
"alias_record_hash",
"alias_record_host_authority",
"alias_record",
"index_record",
"drs_bundle_record",
"base_version",
Expand Down Expand Up @@ -121,19 +123,18 @@ def user(app):
try:
driver.add("test", "test")
except Exception as e:
print(e)
pass

yield {
"Authorization": ("Basic " + base64.b64encode(b"test:test").decode("ascii")),
"Content-Type": "application/json",
}

try:
driver.add("test", "test")
driver.delete("test")
except Exception as e:
print("------------user test error --------------------")
print(e)
pass

engine.dispose()


Expand Down Expand Up @@ -178,12 +179,8 @@ def _use_mock_authz(allowed_permissions=None):
assert isinstance(allowed_permissions, list)

def mock_authz(method, resources):
print("=======mock authz==================")
print(resources)
print(allowed_permissions)
for resource in resources:
if (method, resource) not in allowed_permissions:
print("-------------method loop failed-------------")
raise AuthError(
"Mock indexd.auth.authz: ({},{}) is not one of the allowed permissions: {}".format(
method, resource, allowed_permissions
Expand Down
Loading

0 comments on commit f558dde

Please sign in to comment.