def test_instance_from_environment(self): os.environ.update(dict(LASTFM_API_KEY="a")) config = Config.instance() expected = { "api_key": "a", "api_secret": None, "username": None, "password": None, "session": None, } self.assertDictEqual(expected, config.to_dict()) os.environ.update( dict( LASTFM_API_KEY="a", LASTFM_API_SECRET="b", LASTFM_USERNAME="******", LASTFM_PASSWORD="******", LASTFM_SESSION="e", )) new_config = Config.instance() self.assertEqual(config, new_config) Config._instance = None expected = { "api_key": "a", "api_secret": "b", "username": "******", "password": md5("d"), "session": "e", } self.assertDictEqual(expected, Config.instance().to_dict())
def sign(params: Dict) -> str: """ Last.fm signing formula for webservice calls. Exclude format, sort params, append the api secret key and hash the params string. :param Dict params: :rtype: str """ keys = sorted(params.keys()) keys.remove("format") signature = [str(k) + str(params[k]) for k in keys if params.get(k)] signature.append(str(Config.instance().api_secret)) return utils.md5("".join(signature)) # type: ignore
def test_instance_from_arguments(self): config = Config.instance("key") expected = { "api_key": "key", "api_secret": None, "password": None, "session": None, "username": None, } self.assertDictEqual(expected, config.to_dict()) self.assertEqual(config, Config.instance()) new_config = Config.instance("a", "b", "c", "d", "e") expected = { "api_key": "a", "api_secret": "b", "username": "******", "password": md5("d"), "session": "e", } self.assertDictEqual(expected, new_config.to_dict()) self.assertEqual(new_config, Config._instance) self.assertNotEqual(config, Config._instance)
def test_md5(self): self.assertEqual("3858f62230ac3c915f300c664312c63f", md5("foobar")) self.assertRegex(md5("foobar"), "^[a-z0-9]{32}$") self.assertEqual("", md5("")) self.assertIsNone(md5(None))
def auth_token(self): return md5(str(self.username) + str(self.password))