def user_info(request, data): """ Gives information about the user specified in the parameters. """ try: api_key = data['api_key'] output_format = data.get('format', 'xml') sk = data.get('sk') username = data.get('user') if not (sk or username): raise KeyError if not Token.is_valid_api_key(api_key): raise InvalidAPIUsage( CompatError.INVALID_API_KEY, output_format=output_format) # Invalid API key user = User.load_by_sessionkey(sk, api_key) if not user: raise InvalidAPIUsage( CompatError.INVALID_SESSION_KEY, output_format=output_format) # Invalid Session key query_user = User.load_by_name(username) if ( username and username != user.name) else user if not query_user: raise InvalidAPIUsage( CompatError.INVALID_RESOURCE, output_format=output_format) # Invalid resource specified except KeyError: raise InvalidAPIUsage( CompatError.INVALID_PARAMETERS, output_format=output_format) # Missing required params doc, tag, text = Doc().tagtext() with tag('lfm', status='ok'): with tag('user'): with tag('name'): text(query_user.name) with tag('realname'): text(query_user.name) with tag('url'): text('http://listenbrainz.org/user/' + query_user.name) with tag('playcount'): text(User.get_play_count(query_user.id)) with tag('registered', unixtime=str(query_user.created.strftime("%s"))): text(str(query_user.created)) return format_response( '<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue()), data.get('format', "xml"))
def api_auth_approve(): """ Authenticate the user token provided. """ user = User.load_by_name(current_user.musicbrainz_id) if "token" not in request.form: return render_template( "user/auth.html", user_id=current_user.musicbrainz_id, msg="Missing required parameters. Please provide correct parameters and try again." ) token = Token.load(request.form['token']) if not token: return render_template( "user/auth.html", user_id=current_user.musicbrainz_id, msg="Either this token is already used or invalid. Please try again." ) if token.user: return render_template( "user/auth.html", user_id=current_user.musicbrainz_id, msg="This token is already approved. Please check the token and try again." ) if token.has_expired(): return render_template( "user/auth.html", user_id=current_user.musicbrainz_id, msg="This token has expired. Please create a new token and try again." ) token.approve(user.name) return render_template( "user/auth.html", user_id=current_user.musicbrainz_id, msg="Token %s approved for user %s, press continue in client." % (token.token, current_user.musicbrainz_id) )
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 setUp(self): super(TestAPICompatUserClass, self).setUp() self.log = logging.getLogger(__name__) self.logstore = init_influx_connection({ 'INFLUX_HOST': config.INFLUX_HOST, 'INFLUX_PORT': config.INFLUX_PORT, 'INFLUX_DB_NAME': config.INFLUX_DB_NAME, 'REDIS_HOST': config.REDIS_HOST, 'REDIS_PORT': config.REDIS_PORT, }) # Create a user uid = db.user.create("test") self.assertIsNotNone(db.user.get(uid)) with db.engine.connect() as connection: result = connection.execute( text(""" SELECT * FROM "user" WHERE id = :id """), { "id": uid, }) row = result.fetchone() self.user = User(row['id'], row['created'], row['musicbrainz_id'], row['auth_token']) # Insert some listens date = datetime(2015, 9, 3, 0, 0, 0) self.log.info("Inserting test data...") test_data = generate_data(date, 100) self.logstore.insert(test_data) self.log.info("Test data inserted")
def session_info(request, data): try: sk = data['sk'] api_key = data['api_key'] output_format = data.get('format', 'xml') username = data['username'] except KeyError: raise InvalidAPIUsage(6, output_format=output_format) # Missing Required Params session = Session.load(sk, api_key) if (not session) or User.load_by_name(username).id != session.user.id: raise InvalidAPIUsage(9, output_format=output_format) # Invalid Session KEY print("SESSION INFO for session %s, user %s" % (session.id, session.user.name)) doc, tag, text = Doc().tagtext() with tag('lfm', status='ok'): with tag('application'): with tag('session'): with tag('name'): text(session.user.name) with tag('key'): text(session.id) with tag('subscriber'): text('0') with tag('country'): text('US') return format_response('<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue()), output_format)
def user_info(request, data): """ Gives information about the user specified in the parameters. """ try: api_key = data['api_key'] output_format = data.get('format', 'xml') sk = data.get('sk') username = data.get('user') if not (sk or username): raise KeyError if not Token.is_valid_api_key(api_key): raise InvalidAPIUsage(10, output_format=output_format) # Invalid API key user = User.load_by_sessionkey(sk, api_key) if not user: raise InvalidAPIUsage(9, output_format=output_format) # Invalid Session key query_user = User.load_by_name(username) if (username and username != user.name) else user if not query_user: raise InvalidAPIUsage(7, output_format=output_format) # Invalid resource specified except KeyError: raise InvalidAPIUsage(6, output_format=output_format) # Missing required params doc, tag, text = Doc().tagtext() with tag('lfm', status='ok'): with tag('user'): with tag('name'): text(query_user.name) with tag('realname'): text(query_user.name) with tag('url'): text('http://listenbrainz.org/user/' + query_user.name) with tag('playcount'): text(User.get_play_count(query_user.id)) with tag('registered', unixtime=str(query_user.created.strftime("%s"))): text(str(query_user.created)) return format_response('<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue()), data.get('format', "xml"))
def setUp(self): super(TestAPICompatTokenClass, self).setUp() self.log = logging.getLogger(__name__) # Create a user uid = db.user.create("test") self.assertIsNotNone(db.user.get(uid)) with db.engine.connect() as connection: result = connection.execute(text('SELECT * FROM "user" WHERE id = :id'), {"id": uid}) row = result.fetchone() self.user = User(row['id'], row['created'], row['musicbrainz_id'], row['auth_token'])
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 setUp(self): super(TestAPICompatUserClass, self).setUp() self.log = logging.getLogger(__name__) self.logstore = init_postgres_connection( self.config.SQLALCHEMY_DATABASE_URI) # Create a user uid = db.user.create("test") self.assertIsNotNone(db.user.get(uid)) with db.engine.connect() as connection: result = connection.execute( text('SELECT * FROM "user" WHERE id = :id'), {"id": uid}) row = result.fetchone() self.user = User(row['id'], row['created'], row['musicbrainz_id'], row['auth_token']) # Insert some listens date = datetime(2015, 9, 3, 0, 0, 0) self.log.info("Inserting test data...") test_data = generate_data(date, 100) self.logstore.insert(test_data) self.log.info("Test data inserted")
def api_auth_approve(): """ Authenticate the user token provided. """ user = User.load_by_name(current_user.musicbrainz_id) if "token" not in request.form: return render_template( "user/auth.html", user_id=current_user.musicbrainz_id, msg= "Missing required parameters. Please provide correct parameters and try again." ) token = Token.load(request.form['token']) if not token: return render_template( "user/auth.html", user_id=current_user.musicbrainz_id, msg= "Either this token is already used or invalid. Please try again.") if token.user: return render_template( "user/auth.html", user_id=current_user.musicbrainz_id, msg= "This token is already approved. Please check the token and try again." ) if token.has_expired(): return render_template( "user/auth.html", user_id=current_user.musicbrainz_id, msg= "This token has expired. Please create a new token and try again.") token.approve(user.name) return render_template( "user/auth.html", user_id=current_user.musicbrainz_id, msg="Token %s approved for user %s, press continue in client." % (token.token, current_user.musicbrainz_id))
def session_info(request, data): try: sk = data['sk'] api_key = data['api_key'] output_format = data.get('format', 'xml') username = data['username'] except KeyError: raise InvalidAPIUsage( CompatError.INVALID_PARAMETERS, output_format=output_format) # Missing Required Params session = Session.load(sk, api_key) if (not session) or User.load_by_name(username).id != session.user.id: raise InvalidAPIUsage( CompatError.INVALID_SESSION_KEY, output_format=output_format) # Invalid Session KEY print("SESSION INFO for session %s, user %s" % (session.id, session.user.name)) doc, tag, text = Doc().tagtext() with tag('lfm', status='ok'): with tag('application'): with tag('session'): with tag('name'): text(session.user.name) with tag('key'): text(session.id) with tag('subscriber'): text('0') with tag('country'): text('US') return format_response( '<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue()), output_format)
def test_user_get_id(self): uid = User.get_id(self.user.name) self.assertEqual(uid, self.user.id)
def test_user_load_by_id(self): user = User.load_by_id(self.user.id) assert isinstance(user, User) == True self.assertDictEqual(user.__dict__, self.user.__dict__)
def test_user_get_play_count(self): count = User.get_play_count(self.user.id) self.assertEqual(count, 100)