From 2e4ef2074485d3c04098fd814746feffd79064dc Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Mon, 14 Jun 2021 21:45:44 +0200 Subject: [PATCH] PICARD-2232: Add helper functions to get official server for submission Use those functions in addrelease instead of custom implementation --- picard/browser/addrelease.py | 11 +++----- picard/util/mbserver.py | 50 ++++++++++++++++++++++++++++++++++ test/test_util_mbserver.py | 53 ++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 picard/util/mbserver.py create mode 100644 test/test_util_mbserver.py diff --git a/picard/browser/addrelease.py b/picard/browser/addrelease.py index 0c440661b..f1432a326 100644 --- a/picard/browser/addrelease.py +++ b/picard/browser/addrelease.py @@ -23,12 +23,11 @@ from secrets import token_bytes from PyQt5.QtCore import QCoreApplication from picard import log -from picard.config import get_config -from picard.const import MUSICBRAINZ_SERVERS from picard.util import ( format_time, htmlescape, ) +from picard.util.mbserver import get_submission_host from picard.util.webbrowser2 import open @@ -140,11 +139,9 @@ def _find_file(path): def _mbserver_url(path): - config = get_config() - host = config.setting["server_host"] - if host not in MUSICBRAINZ_SERVERS: - host = MUSICBRAINZ_SERVERS[0] # Submission only works to official servers - return "https://%s%s" % (host, path) + host, port = get_submission_host() + protocol = 'https' if port == 443 else 'http' + return "%s://%s:%i%s" % (protocol, host, port, path) def _get_cluster_form(cluster): diff --git a/picard/util/mbserver.py b/picard/util/mbserver.py new file mode 100644 index 000000000..dda2b5a56 --- /dev/null +++ b/picard/util/mbserver.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# +# Picard, the next-generation MusicBrainz tagger +# +# Copyright (C) 2021 Philipp Wolfer +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +from picard.config import get_config +from picard.const import MUSICBRAINZ_SERVERS + + +def is_official_server(host): + """Returns True, if host is an official MusicBrainz server for the primary database. + + Args: + host: the hostname + + Returns: True, if host is an official MusicBrainz server, False otherwise + """ + return host in MUSICBRAINZ_SERVERS + + +def get_submission_host(): + """Returns the host and port used for data submission. + + Data submission usually should be done against the primary database. This function + will return the hostname configured as `server_host` if it is an official MusicBrainz + server, otherwise it will return the primary official server. + + Returns: Tuple of hostname and port number, e.g. `('musicbrainz.org', 443)` + """ + config = get_config() + host = config.setting['server_host'] + if is_official_server(host): + return (host, 443) + else: + return (MUSICBRAINZ_SERVERS[0], 443) diff --git a/test/test_util_mbserver.py b/test/test_util_mbserver.py new file mode 100644 index 000000000..042aceb2b --- /dev/null +++ b/test/test_util_mbserver.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# +# Picard, the next-generation MusicBrainz tagger +# +# Copyright (C) 2021 Philipp Wolfer +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + +from test.picardtestcase import PicardTestCase + +from picard.const import MUSICBRAINZ_SERVERS +from picard.util.mbserver import ( + get_submission_host, + is_official_server, +) + + +class IsOfficialServerTest(PicardTestCase): + + def test_official(self): + for host in MUSICBRAINZ_SERVERS: + self.assertTrue(is_official_server(host)) + + def test_not_official(self): + self.assertFalse(is_official_server('test.musicbrainz.org')) + self.assertFalse(is_official_server('example.com')) + self.assertFalse(is_official_server('127.0.0.1')) + self.assertFalse(is_official_server('localhost')) + + +class test_get_submission_host(PicardTestCase): + + def test_official(self): + for host in MUSICBRAINZ_SERVERS: + self.set_config_values(setting={'server_host': host, 'server_port': 80}) + self.assertEqual((host, 443), get_submission_host()) + + def test_unofficial(self): + self.set_config_values(setting={'server_host': 'test.musicbrainz.org', 'server_port': 80}) + self.assertEqual((MUSICBRAINZ_SERVERS[0], 443), get_submission_host())