From 553e38fc11adff4342aa9d9a6e91618788211d32 Mon Sep 17 00:00:00 2001 From: "Graham R. Cobb" Date: Mon, 8 Mar 2021 16:40:04 +0000 Subject: [PATCH 1/7] Do not do backslash substitution on regex queries As discussed in bug #3867, backslash replacement in query strings is a bit of a hack but it is useful (see #3566 and #3567 for more discussion). However, it breaks many regular expressions so this patch stops the replacement if the query term contains '::', indicating it is a regex match. This commit fixes #3867. Signed-off-by: Graham R. Cobb --- beetsplug/web/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/beetsplug/web/__init__.py b/beetsplug/web/__init__.py index e80c8c29e..c8f979fa6 100644 --- a/beetsplug/web/__init__.py +++ b/beetsplug/web/__init__.py @@ -244,7 +244,9 @@ class QueryConverter(PathConverter): def to_python(self, value): queries = value.split('/') - return [query.replace('\\', os.sep) for query in queries] + """Do not do path substitution on regex value tests""" + return [query if '::' in query else query.replace('\\', os.sep) + for query in queries] def to_url(self, value): return ','.join([v.replace(os.sep, '\\') for v in value]) From 38fb1b5f08af38ccb1e919f797e0ee28013d5a0e Mon Sep 17 00:00:00 2001 From: "Graham R. Cobb" Date: Mon, 8 Mar 2021 17:00:41 +0000 Subject: [PATCH 2/7] Add tests for several web queries, including the regex queries fixed in bugfix #3867. Signed-off-by: Graham R. Cobb --- test/test_web.py | 102 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 92 insertions(+), 10 deletions(-) diff --git a/test/test_web.py b/test/test_web.py index 88be31365..b673e4045 100644 --- a/test/test_web.py +++ b/test/test_web.py @@ -26,12 +26,12 @@ class WebPluginTest(_common.LibTestCase): # Add library elements. Note that self.lib.add overrides any "id=" # and assigns the next free id number. # The following adds will create items #1, #2 and #3 - self.lib.add(Item(title=u'title', path='/path_1', album_id=2)) - self.lib.add(Item(title=u'another title', path='/path_2')) - self.lib.add(Item(title=u'and a third')) + self.lib.add(Item(title=u'title', path='/path_1', album_id=2, artist='AAA Singers')) + self.lib.add(Item(title=u'another title', path='/somewhere/path_2', artist='AAA Singers')) + self.lib.add(Item(title=u'and a third', testattr='ABC', path='/somewhere-else/path_2', album_id=2)) # The following adds will create albums #1 and #2 - self.lib.add(Album(album=u'album')) - self.lib.add(Album(album=u'other album', artpath='/art_path_2')) + self.lib.add(Album(album=u'album', albumtest='xyz')) + self.lib.add(Album(album=u'other album', artpath='/somewhere-else/art_path_2')) web.app.config['TESTING'] = True web.app.config['lib'] = self.lib @@ -46,13 +46,17 @@ class WebPluginTest(_common.LibTestCase): self.assertEqual(response.status_code, 200) self.assertEqual(res_json['path'], u'/path_1') + web.app.config['INCLUDE_PATHS'] = False + def test_config_include_artpaths_true(self): web.app.config['INCLUDE_PATHS'] = True response = self.client.get('/album/2') res_json = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) - self.assertEqual(res_json['artpath'], u'/art_path_2') + self.assertEqual(res_json['artpath'], u'/somewhere-else/art_path_2') + + web.app.config['INCLUDE_PATHS'] = False def test_config_include_paths_false(self): web.app.config['INCLUDE_PATHS'] = False @@ -91,8 +95,8 @@ class WebPluginTest(_common.LibTestCase): self.assertEqual(response.status_code, 200) self.assertEqual(len(res_json['items']), 2) - response_titles = [item['title'] for item in res_json['items']] - assertCountEqual(self, response_titles, [u'title', u'another title']) + response_titles = {item['title'] for item in res_json['items']} + self.assertEqual(response_titles, {u'title', u'another title'}) def test_get_single_item_not_found(self): response = self.client.get('/item/4') @@ -116,6 +120,7 @@ class WebPluginTest(_common.LibTestCase): self.assertEqual(response.status_code, 404) def test_get_item_empty_query(self): + """ testing item query: """ response = self.client.get('/item/query/') res_json = json.loads(response.data.decode('utf-8')) @@ -123,6 +128,7 @@ class WebPluginTest(_common.LibTestCase): self.assertEqual(len(res_json['items']), 3) def test_get_simple_item_query(self): + """ testing item query: another """ response = self.client.get('/item/query/another') res_json = json.loads(response.data.decode('utf-8')) @@ -131,6 +137,49 @@ class WebPluginTest(_common.LibTestCase): self.assertEqual(res_json['results'][0]['title'], u'another title') + def test_query_item_string(self): + """ testing item query: testattr:ABC """ + response = self.client.get('/item/query/testattr%3aABC') + res_json = json.loads(response.data.decode('utf-8')) + + self.assertEqual(response.status_code, 200) + self.assertEqual(len(res_json['results']), 1) + self.assertEqual(res_json['results'][0]['title'], + u'and a third') + + def test_query_item_regex(self): + """ testing item query: testattr::[A-C]+ """ + response = self.client.get('/item/query/testattr%3a%3a[A-C]%2b') + res_json = json.loads(response.data.decode('utf-8')) + + self.assertEqual(response.status_code, 200) + self.assertEqual(len(res_json['results']), 1) + self.assertEqual(res_json['results'][0]['title'], + u'and a third') + + def test_query_item_regex_backslash(self): + #""" testing item query: testattr::\w+ """ + response = self.client.get('/item/query/testattr%3a%3a%5cw%2b') + res_json = json.loads(response.data.decode('utf-8')) + + self.assertEqual(response.status_code, 200) + self.assertEqual(len(res_json['results']), 1) + self.assertEqual(res_json['results'][0]['title'], + u'and a third') + + def test_query_item_path(self): + #""" testing item query: path:\somewhere """ + """ Note: path queries are special: the query item must match the path + from the root all the way to a directory, so this matches 1 item """ + """ Note: filesystem separators must be specified as '\' """ + response = self.client.get('/item/query/path:\\somewhere') + res_json = json.loads(response.data.decode('utf-8')) + + self.assertEqual(response.status_code, 200) + self.assertEqual(len(res_json['results']), 1) + self.assertEqual(res_json['results'][0]['title'], + u'another title') + def test_get_all_albums(self): response = self.client.get('/album/') res_json = json.loads(response.data.decode('utf-8')) @@ -177,10 +226,43 @@ class WebPluginTest(_common.LibTestCase): res_json = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) - self.assertEqual(len(res_json['items']), 1) + self.assertEqual(len(res_json['items']), 2) self.assertEqual(res_json['items'][0]['album'], u'other album') - self.assertEqual(res_json['items'][0]['id'], 1) + self.assertEqual(res_json['items'][1]['album'], + u'other album') + response_track_titles = {item['title'] for item in res_json['items']} + self.assertEqual(response_track_titles, {u'title', u'and a third'}) + + def test_query_album_string(self): + """ testing query: albumtest:xy """ + response = self.client.get('/album/query/albumtest%3axy') + res_json = json.loads(response.data.decode('utf-8')) + + self.assertEqual(response.status_code, 200) + self.assertEqual(len(res_json['results']), 1) + self.assertEqual(res_json['results'][0]['album'], + u'album') + + def test_query_album_artpath_regex(self): + """ testing query: artpath::art_ """ + response = self.client.get('/album/query/artpath%3a%3aart_') + res_json = json.loads(response.data.decode('utf-8')) + + self.assertEqual(response.status_code, 200) + self.assertEqual(len(res_json['results']), 1) + self.assertEqual(res_json['results'][0]['album'], + u'other album') + + def test_query_album_regex_backslash(self): + #""" testing query: albumtest::\w+ """ + response = self.client.get('/album/query/albumtest%3a%3a%5cw%2b') + res_json = json.loads(response.data.decode('utf-8')) + + self.assertEqual(response.status_code, 200) + self.assertEqual(len(res_json['results']), 1) + self.assertEqual(res_json['results'][0]['album'], + u'album') def test_get_stats(self): response = self.client.get('/stats') From 75f331e64bf47a688bb8842c4a025f353bede611 Mon Sep 17 00:00:00 2001 From: "Graham R. Cobb" Date: Mon, 8 Mar 2021 17:08:36 +0000 Subject: [PATCH 3/7] Update changelog with web regex query backslash fix for #3867. Signed-off-by: Graham R. Cobb --- docs/changelog.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index b9020621b..1e21ac37f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -186,6 +186,8 @@ New features: Fixes: +* :bug:`/plugins/web`: Allow use of backslash in regex web queries. + :bug:`3867` * :bug:`/plugins/web`: Fixed a small bug which caused album artpath to be redacted even when ``include_paths`` option is set. :bug:`3866` From 4d5c5c02ae38f991a797babf8f9b73427d891fba Mon Sep 17 00:00:00 2001 From: "Graham R. Cobb" Date: Mon, 8 Mar 2021 17:26:04 +0000 Subject: [PATCH 4/7] Lint fixes Signed-off-by: Graham R. Cobb --- test/test_web.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/test/test_web.py b/test/test_web.py index b673e4045..9d085650b 100644 --- a/test/test_web.py +++ b/test/test_web.py @@ -26,12 +26,22 @@ class WebPluginTest(_common.LibTestCase): # Add library elements. Note that self.lib.add overrides any "id=" # and assigns the next free id number. # The following adds will create items #1, #2 and #3 - self.lib.add(Item(title=u'title', path='/path_1', album_id=2, artist='AAA Singers')) - self.lib.add(Item(title=u'another title', path='/somewhere/path_2', artist='AAA Singers')) - self.lib.add(Item(title=u'and a third', testattr='ABC', path='/somewhere-else/path_2', album_id=2)) + self.lib.add(Item(title=u'title', + path='/path_1', + album_id=2, + artist='AAA Singers')) + self.lib.add(Item(title=u'another title', + path='/somewhere/path_2', + artist='AAA Singers')) + self.lib.add(Item(title=u'and a third', + testattr='ABC', + path='/somewhere-else/path_2', + album_id=2)) # The following adds will create albums #1 and #2 - self.lib.add(Album(album=u'album', albumtest='xyz')) - self.lib.add(Album(album=u'other album', artpath='/somewhere-else/art_path_2')) + self.lib.add(Album(album=u'album', + albumtest='xyz')) + self.lib.add(Album(album=u'other album', + artpath='/somewhere-else/art_path_2')) web.app.config['TESTING'] = True web.app.config['lib'] = self.lib @@ -158,7 +168,7 @@ class WebPluginTest(_common.LibTestCase): u'and a third') def test_query_item_regex_backslash(self): - #""" testing item query: testattr::\w+ """ + # """ testing item query: testattr::\w+ """ response = self.client.get('/item/query/testattr%3a%3a%5cw%2b') res_json = json.loads(response.data.decode('utf-8')) @@ -168,7 +178,7 @@ class WebPluginTest(_common.LibTestCase): u'and a third') def test_query_item_path(self): - #""" testing item query: path:\somewhere """ + # """ testing item query: path:\somewhere """ """ Note: path queries are special: the query item must match the path from the root all the way to a directory, so this matches 1 item """ """ Note: filesystem separators must be specified as '\' """ @@ -255,7 +265,7 @@ class WebPluginTest(_common.LibTestCase): u'other album') def test_query_album_regex_backslash(self): - #""" testing query: albumtest::\w+ """ + # """ testing query: albumtest::\w+ """ response = self.client.get('/album/query/albumtest%3a%3a%5cw%2b') res_json = json.loads(response.data.decode('utf-8')) From f19faae1dc437e779a3d81b0d23c446eddd78757 Mon Sep 17 00:00:00 2001 From: "Graham R. Cobb" Date: Tue, 9 Mar 2021 11:19:54 +0000 Subject: [PATCH 5/7] Attempt to make path-related tests portable to Windows Signed-off-by: Graham R. Cobb --- test/test_web.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/test_web.py b/test/test_web.py index 9d085650b..528cbf36d 100644 --- a/test/test_web.py +++ b/test/test_web.py @@ -27,21 +27,21 @@ class WebPluginTest(_common.LibTestCase): # and assigns the next free id number. # The following adds will create items #1, #2 and #3 self.lib.add(Item(title=u'title', - path='/path_1', + path=os.sep + os.path.join('path_1'), album_id=2, artist='AAA Singers')) self.lib.add(Item(title=u'another title', - path='/somewhere/path_2', + path=os.sep + os.path.join('somewhere', 'a'), artist='AAA Singers')) self.lib.add(Item(title=u'and a third', testattr='ABC', - path='/somewhere-else/path_2', + path=os.sep + os.path.join('somewhere', 'abc'), album_id=2)) # The following adds will create albums #1 and #2 self.lib.add(Album(album=u'album', albumtest='xyz')) self.lib.add(Album(album=u'other album', - artpath='/somewhere-else/art_path_2')) + artpath=os.sep + os.path.join('somewhere-else', 'art_path_2'))) web.app.config['TESTING'] = True web.app.config['lib'] = self.lib @@ -54,7 +54,7 @@ class WebPluginTest(_common.LibTestCase): res_json = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) - self.assertEqual(res_json['path'], u'/path_1') + self.assertEqual(res_json['path'], os.path.join(os.sep, u'path_1')) web.app.config['INCLUDE_PATHS'] = False @@ -64,7 +64,7 @@ class WebPluginTest(_common.LibTestCase): res_json = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) - self.assertEqual(res_json['artpath'], u'/somewhere-else/art_path_2') + self.assertEqual(res_json['artpath'], os.path.join(os.sep, u'somewhere-else', u'art_path_2')) web.app.config['INCLUDE_PATHS'] = False @@ -180,9 +180,9 @@ class WebPluginTest(_common.LibTestCase): def test_query_item_path(self): # """ testing item query: path:\somewhere """ """ Note: path queries are special: the query item must match the path - from the root all the way to a directory, so this matches 1 item """ - """ Note: filesystem separators must be specified as '\' """ - response = self.client.get('/item/query/path:\\somewhere') + from the root all the way to a directory, so this matches just 1 item """ + """ Note: filesystem separators in the query must be specified as '\' """ + response = self.client.get('/item/query/path:\\somewhere\\a') res_json = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) From ab2e858bce14fb9176dc3afbe0329714aba64210 Mon Sep 17 00:00:00 2001 From: "Graham R. Cobb" Date: Tue, 9 Mar 2021 11:40:14 +0000 Subject: [PATCH 6/7] Aaargh! Forgot to run lint again Signed-off-by: Graham R. Cobb --- test/test_web.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/test_web.py b/test/test_web.py index 528cbf36d..a4c10a193 100644 --- a/test/test_web.py +++ b/test/test_web.py @@ -41,7 +41,8 @@ class WebPluginTest(_common.LibTestCase): self.lib.add(Album(album=u'album', albumtest='xyz')) self.lib.add(Album(album=u'other album', - artpath=os.sep + os.path.join('somewhere-else', 'art_path_2'))) + artpath=os.sep + + os.path.join('somewhere2', 'art_path_2'))) web.app.config['TESTING'] = True web.app.config['lib'] = self.lib @@ -64,7 +65,8 @@ class WebPluginTest(_common.LibTestCase): res_json = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 200) - self.assertEqual(res_json['artpath'], os.path.join(os.sep, u'somewhere-else', u'art_path_2')) + self.assertEqual(res_json['artpath'], + os.path.join(os.sep, u'somewhere2', u'art_path_2')) web.app.config['INCLUDE_PATHS'] = False @@ -180,8 +182,8 @@ class WebPluginTest(_common.LibTestCase): def test_query_item_path(self): # """ testing item query: path:\somewhere """ """ Note: path queries are special: the query item must match the path - from the root all the way to a directory, so this matches just 1 item """ - """ Note: filesystem separators in the query must be specified as '\' """ + from the root all the way to a directory, so this matches 1 item """ + """ Note: filesystem separators in the query must be '\' """ response = self.client.get('/item/query/path:\\somewhere\\a') res_json = json.loads(response.data.decode('utf-8')) From 45cf9d00c73eb755e752d0497e6b945e2a4ee932 Mon Sep 17 00:00:00 2001 From: "Graham R. Cobb" Date: Tue, 9 Mar 2021 18:58:05 +0000 Subject: [PATCH 7/7] Improve handling of paths for Windows tests Signed-off-by: Graham R. Cobb --- test/test_web.py | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/test/test_web.py b/test/test_web.py index a4c10a193..606f1e243 100644 --- a/test/test_web.py +++ b/test/test_web.py @@ -13,11 +13,22 @@ from test import _common from beets.library import Item, Album from beetsplug import web +import platform + +from beets import logging + class WebPluginTest(_common.LibTestCase): def setUp(self): + super(WebPluginTest, self).setUp() + self.log = logging.getLogger('beets.web') + + if platform.system() == 'Windows': + self.path_prefix = u'C:' + else: + self.path_prefix = u'' # Add fixtures for track in self.lib.items(): @@ -26,23 +37,30 @@ class WebPluginTest(_common.LibTestCase): # Add library elements. Note that self.lib.add overrides any "id=" # and assigns the next free id number. # The following adds will create items #1, #2 and #3 + path1 = self.path_prefix + os.sep + \ + os.path.join(b'path_1').decode('utf-8') self.lib.add(Item(title=u'title', - path=os.sep + os.path.join('path_1'), + path=path1, album_id=2, artist='AAA Singers')) + path2 = self.path_prefix + os.sep + \ + os.path.join(b'somewhere', b'a').decode('utf-8') self.lib.add(Item(title=u'another title', - path=os.sep + os.path.join('somewhere', 'a'), + path=path2, artist='AAA Singers')) + path3 = self.path_prefix + os.sep + \ + os.path.join(b'somewhere', b'abc').decode('utf-8') self.lib.add(Item(title=u'and a third', testattr='ABC', - path=os.sep + os.path.join('somewhere', 'abc'), + path=path3, album_id=2)) # The following adds will create albums #1 and #2 self.lib.add(Album(album=u'album', albumtest='xyz')) + path4 = self.path_prefix + os.sep + \ + os.path.join(b'somewhere2', b'art_path_2').decode('utf-8') self.lib.add(Album(album=u'other album', - artpath=os.sep - + os.path.join('somewhere2', 'art_path_2'))) + artpath=path4)) web.app.config['TESTING'] = True web.app.config['lib'] = self.lib @@ -53,9 +71,11 @@ class WebPluginTest(_common.LibTestCase): web.app.config['INCLUDE_PATHS'] = True response = self.client.get('/item/1') res_json = json.loads(response.data.decode('utf-8')) + expected_path = self.path_prefix + os.sep \ + + os.path.join(b'path_1').decode('utf-8') self.assertEqual(response.status_code, 200) - self.assertEqual(res_json['path'], os.path.join(os.sep, u'path_1')) + self.assertEqual(res_json['path'], expected_path) web.app.config['INCLUDE_PATHS'] = False @@ -63,10 +83,11 @@ class WebPluginTest(_common.LibTestCase): web.app.config['INCLUDE_PATHS'] = True response = self.client.get('/album/2') res_json = json.loads(response.data.decode('utf-8')) + expected_path = self.path_prefix + os.sep \ + + os.path.join(b'somewhere2', b'art_path_2').decode('utf-8') self.assertEqual(response.status_code, 200) - self.assertEqual(res_json['artpath'], - os.path.join(os.sep, u'somewhere2', u'art_path_2')) + self.assertEqual(res_json['artpath'], expected_path) web.app.config['INCLUDE_PATHS'] = False @@ -180,11 +201,14 @@ class WebPluginTest(_common.LibTestCase): u'and a third') def test_query_item_path(self): - # """ testing item query: path:\somewhere """ + # """ testing item query: path:\somewhere\a """ """ Note: path queries are special: the query item must match the path from the root all the way to a directory, so this matches 1 item """ """ Note: filesystem separators in the query must be '\' """ - response = self.client.get('/item/query/path:\\somewhere\\a') + + response = self.client.get('/item/query/path:' + + self.path_prefix + + '\\somewhere\\a') res_json = json.loads(response.data.decode('utf-8')) self.assertEqual(response.status_code, 200)