Skip to content

Commit

Permalink
test "all-keys" argument
Browse files Browse the repository at this point in the history
  • Loading branch information
ric-evans committed Feb 11, 2021
1 parent 117fae0 commit 8fd2d28
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- run: |
. env/bin/activate &&
resources/enable_profiling.py &&
pytest --tb=short &&
pytest -vvvvv --tb=short &&
resources/profile_queries.py
deploy:
docker:
Expand Down
7 changes: 3 additions & 4 deletions file_catalog/argbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ def build_files_query(kwargs: Dict[str, Any]) -> None:

def build_keys(kwargs: Dict[str, Any]) -> None:
"""Build `"keys"` list, potentially using `"all-keys"` keyword."""
if "keys" not in kwargs:
return
use_all_keys = kwargs.pop("all-keys", None) in ["True", "true", 1]

if kwargs.get("all-keys", None):
if use_all_keys:
kwargs["keys"] = AllKeys()
else:
elif "keys" in kwargs:
kwargs["keys"] = kwargs["keys"].split("|")
94 changes: 87 additions & 7 deletions tests/test_files.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# fmt:off

from __future__ import absolute_import, division, print_function

import hashlib
import os
import unittest
import hashlib

from tornado.escape import json_encode,json_decode
from tornado.escape import json_decode, json_encode

# local imports
from rest_tools.client import RestClient

from .test_server import TestServerAPI


def hex(data):
if isinstance(data, str):
data = data.encode('utf-8')
Expand Down Expand Up @@ -68,6 +73,76 @@ def test_11_files_count(self):
self.assertIn('files', data)
self.assertEqual(data['files'], 1)

def test_12_files_keys(self):
"""Test the 'keys' and all-keys' arguments."""
self.start_server()
token = self.get_token()
r = RestClient(self.address, token, timeout=1, retries=1)

metadata = {
"logical_name": "blah",
"checksum": {"sha512": hex("foo bar")},
"file_size": 1,
"locations": [{"site": "test", "path": "blah.dat"}],
"extra": "foo",
"supplemental": ["green", "eggs", "ham"],
}
data = r.request_seq("POST", "/api/files", metadata)
self.assertIn("_links", data)
self.assertIn("self", data["_links"])
self.assertIn("file", data)
assert "extra" not in data
assert "supplemental" not in data
url = data["file"]
uid = url.split("/")[-1]

# w/o all-keys
data = r.request_seq("GET", "/api/files")
assert set(data["files"][0].keys()) == {"logical_name", "uuid"}

# w/ all-keys
body = {"all-keys": True}
data = r.request_seq("GET", "/api/files", body)
assert set(data["files"][0].keys()) == {
"logical_name",
"uuid",
"checksum",
"file_size",
"locations",
"extra",
"supplemental",
"meta_modify_date"
}

# w/ all-keys = False
body = {"all-keys": False}
data = r.request_seq("GET", "/api/files", body)
assert set(data["files"][0].keys()) == {"logical_name", "uuid"}

# w/ all-keys & keys
body = {"all-keys": True, "keys": "checksum|file_size"}
data = r.request_seq("GET", "/api/files", body)
assert set(data["files"][0].keys()) == {
"logical_name",
"uuid",
"checksum",
"file_size",
"locations",
"extra",
"supplemental",
"meta_modify_date"
}

# w/ all-keys = False & keys
body = {"all-keys": False, "keys": "checksum|file_size"}
data = r.request_seq("GET", "/api/files", body)
assert set(data["files"][0].keys()) == {"checksum", "file_size"}

# w/ just keys
body = {"keys": "checksum|file_size"}
data = r.request_seq("GET", "/api/files", body)
assert set(data["files"][0].keys()) == {"checksum", "file_size"}

def test_15_files_auth(self):
self.start_server(config_override={'SECRET':'secret'})
token = self.get_token()
Expand Down Expand Up @@ -1197,7 +1272,8 @@ def test_68_patch_files_uuid_locations_NxN(self):
r.request_seq('PATCH', '/api/files/' + uid, patch1)

def test_70_abuse_post_files_locations(self):
"""Abuse the POST /api/files/UUID/locations route to test error handling."""
"""Abuse the POST /api/files/UUID/locations route to test error
handling."""
self.start_server()
token = self.get_token()
r = RestClient(self.address, token, timeout=1, retries=1)
Expand Down Expand Up @@ -1243,7 +1319,8 @@ def test_70_abuse_post_files_locations(self):
r.request_seq('POST', '/api/files/' + uid + '/locations', {"locations": "bobsyeruncle"})

def test_71_post_files_locations_duplicate(self):
"""Test that POST /api/files/UUID/locations is a no-op for non-distinct locations."""
"""Test that POST /api/files/UUID/locations is a no-op for non-distinct
locations."""
self.start_server()
token = self.get_token()
r = RestClient(self.address, token, timeout=1, retries=1)
Expand Down Expand Up @@ -1287,7 +1364,8 @@ def test_71_post_files_locations_duplicate(self):
self.assertEqual(mmd, rec2["meta_modify_date"])

def test_72_post_files_locations_conflict(self):
"""Test that POST /api/files/UUID/locations returns an error on conflicting duplicate locations."""
"""Test that POST /api/files/UUID/locations returns an error on
conflicting duplicate locations."""
self.start_server()
token = self.get_token()
r = RestClient(self.address, token, timeout=1, retries=1)
Expand Down Expand Up @@ -1338,7 +1416,8 @@ def test_72_post_files_locations_conflict(self):
rec2 = r.request_seq('POST', '/api/files/' + uid + '/locations', conflicting_locations)

def test_73_post_files_locations(self):
"""Test that POST /api/files/UUID/locations can add distinct non-conflicting locations."""
"""Test that POST /api/files/UUID/locations can add distinct non-
conflicting locations."""
self.start_server()
token = self.get_token()
r = RestClient(self.address, token, timeout=1, retries=1)
Expand Down Expand Up @@ -1385,7 +1464,8 @@ def test_73_post_files_locations(self):
self.assertIn(loc1d, rec2["locations"])

def test_74_post_files_locations_just_one(self):
"""Test that POST /api/files/UUID/locations can add distinct non-conflicting locations."""
"""Test that POST /api/files/UUID/locations can add distinct non-
conflicting locations."""
self.start_server()
token = self.get_token()
r = RestClient(self.address, token, timeout=1, retries=1)
Expand Down

0 comments on commit 8fd2d28

Please # to comment.