def test_validate_bulk_params_bad_mbid(self): # Return an error if an MBID is invalid params = "c5f4909e-1d7b-4f15-a6f6-1af376xxxx:1" with self.assertRaises( webserver.views.api.exceptions.APIBadRequest) as ex: core._parse_bulk_params(params) self.assertEquals( str(ex.exception), "'c5f4909e-1d7b-4f15-a6f6-1af376xxxx' is not a valid UUID")
def test_validate_bulk_params_bad_offset(self): # If a parameter is <0 or not an integer, replace it with 0 params = "c5f4909e-1d7b-4f15-a6f6-1af376bc01c9:-1;7f27d7a9-27f0-4663-9d20-2c9c40200e6d:foo" validated = core._parse_bulk_params(params) expected = [("c5f4909e-1d7b-4f15-a6f6-1af376bc01c9", 0), ("7f27d7a9-27f0-4663-9d20-2c9c40200e6d", 0)] self.assertEqual(expected, validated) params = "c5f4909e-1d7b-4f15-a6f6-1af376bc01c9:-1:another" with self.assertRaises(webserver.views.api.exceptions.APIBadRequest) as ex: core._parse_bulk_params(params) self.assertEquals(str(ex.exception), "More than 1 : in 'c5f4909e-1d7b-4f15-a6f6-1af376bc01c9:-1:another'")
def test_validate_bulk_params_bad_offset(self): # If a parameter is <0 or not an integer, replace it with 0 params = "c5f4909e-1d7b-4f15-a6f6-1af376bc01c9:-1;7f27d7a9-27f0-4663-9d20-2c9c40200e6d:foo" validated = core._parse_bulk_params(params) expected = [("c5f4909e-1d7b-4f15-a6f6-1af376bc01c9", 0), ("7f27d7a9-27f0-4663-9d20-2c9c40200e6d", 0)] self.assertEqual(expected, validated) params = "c5f4909e-1d7b-4f15-a6f6-1af376bc01c9:-1:another" with self.assertRaises(webserver.views.api.exceptions.APIBadRequest) as ex: validated = core._parse_bulk_params(params) self.assertEquals(ex.exception.message, "More than 1 : in 'c5f4909e-1d7b-4f15-a6f6-1af376bc01c9:-1:another'")
def test_validate_bulk_params(self): # Validate MBIDs, convert offsets to integers, and add offset-0 if not provided params = "c5f4909e-1d7b-4f15-a6f6-1af376bc01c9;7f27d7a9-27f0-4663-9d20-2c9c40200e6d:3;405a5ff4-7ee2-436b-95c1-90ce8a83b359:2" validated = core._parse_bulk_params(params) expected = [("c5f4909e-1d7b-4f15-a6f6-1af376bc01c9", 0), ("7f27d7a9-27f0-4663-9d20-2c9c40200e6d", 3), ("405a5ff4-7ee2-436b-95c1-90ce8a83b359", 2)] self.assertEqual(expected, validated)
def check_bad_request_between_recordings(): """ Check if a request for similarity between recordings is valid. The ?recording_ids parameter to the current flask request is checked to see if it is present and if it follows the format mbid:n;mbid:n where mbid is a recording MBID and n is an optional integer offset. If the offset is missing or non-integer, it is replaced with 0 Returns: a two-list of (mbid, offset) tuples representing the parsed query string. Raises: APIBadRequest if there is no recording_ids parameter, there are more than 2 MBIDs in the parameter, or the format of the mbids or offsets are invalid """ recording_ids = request.args.get("recording_ids") if not recording_ids: raise webserver.views.api.exceptions.APIBadRequest("Missing `recording_ids` parameter") recordings = _parse_bulk_params(recording_ids) recordings = [(mbid, offset) for _, mbid, offset in recordings] if not len(recordings) == 2: raise webserver.views.api.exceptions.APIBadRequest("Does not contain 2 recordings in the request") return recordings
def test_validate_bulk_params_normalise(self): # If an mbid is not lower-case, or is missing hyphens, reformat it params = "C5F4909E-1D7B-4F15-A6F6-1AF376BC01C9;7F27D7A927F046639D202C9C40200E6D" validated = core._parse_bulk_params(params) expected = [("C5F4909E-1D7B-4F15-A6F6-1AF376BC01C9", "c5f4909e-1d7b-4f15-a6f6-1af376bc01c9", 0), ("7F27D7A927F046639D202C9C40200E6D", "7f27d7a9-27f0-4663-9d20-2c9c40200e6d", 0)] self.assertEqual(expected, validated)
def test_validate_bulk_params(self): # Validate MBIDs, convert offsets to integers, and add offset-0 if not provided params = "c5f4909e-1d7b-4f15-a6f6-1af376bc01c9;7f27d7a9-27f0-4663-9d20-2c9c40200e6d:3;405a5ff4-7ee2-436b-95c1-90ce8a83b359:2" validated = core._parse_bulk_params(params) expected = [("c5f4909e-1d7b-4f15-a6f6-1af376bc01c9", 0), ("7f27d7a9-27f0-4663-9d20-2c9c40200e6d", 3), ("405a5ff4-7ee2-436b-95c1-90ce8a83b359", 2)] self.assertEqual(expected, validated)
def test_validate_bulk_params_deduplicate(self): # If the same mbid:offset is provided more than once, only return one params = "c5f4909e-1d7b-4f15-a6f6-1af376bc01c9;c5f4909e-1d7b-4f15-a6f6-1af376bc01c9:1;c5f4909e-1d7b-4f15-a6f6-1af376bc01c9:0" validated = core._parse_bulk_params(params) expected = [("c5f4909e-1d7b-4f15-a6f6-1af376bc01c9", 0), ("c5f4909e-1d7b-4f15-a6f6-1af376bc01c9", 1)] self.assertEqual(expected, validated)
def test_validate_bulk_params_emptyvalue(self): # If there is an extra ; in the string, causing an empty mbid, skip it params = "c5f4909e-1d7b-4f15-a6f6-1af376bc01c9;;:x" validated = core._parse_bulk_params(params) expected = [("c5f4909e-1d7b-4f15-a6f6-1af376bc01c9", "c5f4909e-1d7b-4f15-a6f6-1af376bc01c9", 0)] self.assertEqual(expected, validated)
def test_validate_bulk_params_deduplicate(self): # If the same mbid:offset is provided more than once, only return one params = "c5f4909e-1d7b-4f15-a6f6-1af376bc01c9;c5f4909e-1d7b-4f15-a6f6-1af376bc01c9:1;c5f4909e-1d7b-4f15-a6f6-1af376bc01c9:0" validated = core._parse_bulk_params(params) expected = [("c5f4909e-1d7b-4f15-a6f6-1af376bc01c9", 0), ("c5f4909e-1d7b-4f15-a6f6-1af376bc01c9", 1)] self.assertEqual(expected, validated)
def test_validate_bulk_params_bad_mbid(self): # Return an error if an MBID is invalid params = "c5f4909e-1d7b-4f15-a6f6-1af376xxxx:1" with self.assertRaises(webserver.views.api.exceptions.APIBadRequest) as ex: core._parse_bulk_params(params) self.assertEquals(str(ex.exception), "'c5f4909e-1d7b-4f15-a6f6-1af376xxxx' is not a valid UUID")