def test_record_listen_now_playing(self): """ Tests if listen of type 'nowplaying' is recorded correctly if valid information is provided. """ token = Token.generate(self.lfm_user.api_key) token.approve(self.lfm_user.name) session = Session.create(token) data = { 'method': 'track.updateNowPlaying', 'api_key': self.lfm_user.api_key, 'sk': session.sid, 'artist[0]': 'Kishore Kumar', 'track[0]': 'Saamne Ye Kaun Aya', 'album[0]': 'Jawani Diwani', 'duration[0]': 300, 'timestamp[0]': int(time.time()), } r = self.client.post(url_for('api_compat.api_methods'), data=data) self.assert200(r) response = xmltodict.parse(r.data) self.assertEqual(response['lfm']['@status'], 'ok') self.assertIsNotNone(response['lfm']['nowplaying'])
def test_session_create(self): user = User.load_by_id(db_user.create("test")) token = Token.generate(user.api_key) token.approve(user.name) session = Session.create(token) self.assertIsInstance(session, Session) self.assertDictEqual(user.__dict__, session.user.__dict__)
def get_session(request, data): """ Create new session after validating the API_key and token. """ output_format = data.get('format', 'xml') try: api_key = data['api_key'] token = Token.load(data['token'], api_key) except KeyError: raise InvalidAPIUsage(CompatError.INVALID_PARAMETERS, output_format=output_format) # Missing Required Params if not token: if not Token.is_valid_api_key(api_key): raise InvalidAPIUsage(CompatError.INVALID_API_KEY, output_format=output_format) # Invalid API_key raise InvalidAPIUsage(CompatError.INVALID_TOKEN, output_format=output_format) # Invalid token if token.has_expired(): raise InvalidAPIUsage(CompatError.TOKEN_EXPIRED, output_format=output_format) # Token expired if not token.user: raise InvalidAPIUsage(CompatError.UNAUTHORIZED_TOKEN, output_format=output_format) # Unauthorized token session = Session.create(token) doc, tag, text = Doc().tagtext() with tag('lfm', status='ok'): with tag('session'): with tag('name'): text(session.user.name) with tag('key'): text(session.sid) with tag('subscriber'): text('0') return format_response('<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue()), data.get('format', "xml"))
def test_session_create(self): user = User.load_by_id(db_user.create(1, "test")) token = Token.generate(user.api_key) token.approve(user.name) session = Session.create(token) self.assertIsInstance(session, Session) self.assertDictEqual(user.__dict__, session.user.__dict__)
def test_record_listen(self): """ Tests if listen is recorded correctly if valid information is provided. """ token = Token.generate(self.lfm_user.api_key) token.approve(self.lfm_user.name) session = Session.create(token) timestamp = int(time.time()) data = { 'method': 'track.scrobble', 'api_key': self.lfm_user.api_key, 'sk': session.sid, 'artist[0]': 'Kishore Kumar', 'track[0]': 'Saamne Ye Kaun Aya', 'album[0]': 'Jawani Diwani', 'duration[0]': 300, 'timestamp[0]': timestamp, } r = self.client.post(url_for('api_compat.api_methods'), data=data) self.assert200(r) response = xmltodict.parse(r.data) self.assertEqual(response['lfm']['@status'], 'ok') self.assertEqual(response['lfm']['scrobbles']['@accepted'], '1') # Check if listen reached the timescale listenstore time.sleep(2) listens, _, _ = self.ls.fetch_listens(self.lb_user['musicbrainz_id'], from_ts=timestamp - 1) self.assertEqual(len(listens), 1)
def test_record_listen(self): """ Tests if listen is recorded correctly if valid information is provided. """ token = Token.generate(self.lfm_user.api_key) token.approve(self.lfm_user.name) session = Session.create(token) timestamp = int(time.time()) data = { 'method': 'track.scrobble', 'api_key': self.lfm_user.api_key, 'sk': session.sid, 'artist[0]': 'Kishore Kumar', 'track[0]': 'Saamne Ye Kaun Aya', 'album[0]': 'Jawani Diwani', 'duration[0]': 300, 'timestamp[0]': timestamp, } r = self.client.post(url_for('api_compat.api_methods'), data=data) self.assert200(r) response = xmltodict.parse(r.data) self.assertEqual(response['lfm']['@status'], 'ok') self.assertEqual(response['lfm']['scrobbles']['@accepted'], '1') # Check if listen reached the influx listenstore time.sleep(1) listens = self.ls.fetch_listens(self.lb_user['musicbrainz_id'], from_ts=timestamp-1) self.assertEqual(len(listens), 1)
def test_user_getinfo_no_listenstore(self): """If this listenstore is unavailable, performing a query that gets user information (touches the listenstore for user count) should return an error message in the requested format""" timescale_connection._ts = None token = Token.generate(self.lfm_user.api_key) token.approve(self.lfm_user.name) session = Session.create(token) data = { 'method': 'user.getInfo', 'api_key': self.lfm_user.api_key, 'sk': session.sid } r = self.client.post(url_for('api_compat.api_methods'), data=data) self.assert200(r) self.assertEqual(r.headers["Content-type"], "application/xml; charset=utf-8") expected_message = b"""<?xml version="1.0" encoding="utf-8"?> <lfm status="failed"> <error code="16">The service is temporarily unavailable, please try again.</error> </lfm>""" assert r.data == expected_message
def test_session_load(self): user = User.load_by_id(db_user.create("test")) token = Token.generate(user.api_key) token.approve(user.name) session = Session.create(token) self.assertIsInstance(session, Session) self.assertDictEqual(user.__dict__, session.user.__dict__) session.user = None # Load with session_key + api_key session2 = Session.load(session.sid, session.api_key) self.assertDictEqual(user.__dict__, session2.__dict__['user'].__dict__) session2.user = None self.assertDictEqual(session.__dict__, session2.__dict__)
def test_session_load(self): user = User.load_by_id(db_user.create(1, "test")) token = Token.generate(user.api_key) token.approve(user.name) session = Session.create(token) self.assertIsInstance(session, Session) self.assertDictEqual(user.__dict__, session.user.__dict__) session.user = None # Load with session_key + api_key session2 = Session.load(session.sid) self.assertDictEqual(user.__dict__, session2.__dict__['user'].__dict__) session2.user = None self.assertDictEqual(session.__dict__, session2.__dict__)
def get_session(request, data): """ Create new session after validating the API_key and token. """ output_format = data.get('format', 'xml') try: api_key = data['api_key'] token = Token.load(data['token'], api_key) except KeyError: raise InvalidAPIUsage( CompatError.INVALID_PARAMETERS, output_format=output_format) # Missing Required Params if not token: if not Token.is_valid_api_key(api_key): raise InvalidAPIUsage( CompatError.INVALID_API_KEY, output_format=output_format) # Invalid API_key raise InvalidAPIUsage(CompatError.INVALID_TOKEN, output_format=output_format) # Invalid token if token.has_expired(): raise InvalidAPIUsage(CompatError.TOKEN_EXPIRED, output_format=output_format) # Token expired if not token.user: raise InvalidAPIUsage( CompatError.UNAUTHORIZED_TOKEN, output_format=output_format) # Unauthorized token session = Session.create(token) doc, tag, text = Doc().tagtext() with tag('lfm', status='ok'): with tag('session'): with tag('name'): text(session.user.name) with tag('key'): text(session.sid) with tag('subscriber'): text('0') return format_response( '<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue()), data.get('format', "xml"))
def test_record_invalid_listen(self): """ Tests that error is raised if submited data contains unicode null """ token = Token.generate(self.lfm_user.api_key) token.approve(self.lfm_user.name) session = Session.create(token) timestamp = int(time.time()) data = { 'method': 'track.scrobble', 'api_key': self.lfm_user.api_key, 'sk': session.sid, 'artist[0]': '\u0000Kishore Kumar', 'track[0]': 'Saamne Ye Kaun Aya', 'album[0]': 'Jawani Diwani', 'duration[0]': 300, 'timestamp[0]': timestamp, } r = self.client.post(url_for('api_compat.api_methods'), data=data) self.assert400(r) self.assertEqual(r.json["error"], "\u0000Kishore Kumar contains a unicode null")