def mb_user_deleter(musicbrainz_row_id): """ This endpoint is used by MusicBrainz to delete accounts once they are deleted on MusicBrainz too. See https://tickets.metabrainz.org/browse/MBS-9680 for details. Args: musicbrainz_row_id (int): the MusicBrainz row ID of the user to be deleted. Returns: 200 if the user has been successfully found and deleted from LB Raises: NotFound if the user is not found in the LB database Unauthorized if the MusicBrainz access token provided with the query is invalid """ _authorize_mb_user_deleter(request.args.get('access_token', '')) user = db_user.get_by_mb_row_id(musicbrainz_row_id) if user is None: raise NotFound('Could not find user with MusicBrainz Row ID: %d' % musicbrainz_row_id) delete_user(user['musicbrainz_id']) return jsonify({'status': 'ok'}), 200
def get_user(): """Function should fetch user data from database, or, if necessary, create it, and return it.""" try: s = _musicbrainz.get_auth_session(data={ 'code': _fetch_data('code'), 'grant_type': 'authorization_code', 'redirect_uri': url_for('login.musicbrainz_post', _external=True) }, decoder=musicbrainz_auth_session_decoder) data = s.get('oauth2/userinfo').json() musicbrainz_id = data.get('sub') musicbrainz_row_id = data.get('metabrainz_user_id') except KeyError: # get_auth_session raises a KeyError if it was unable to get the required data from `code` raise MusicBrainzAuthSessionError() user = db_user.get_by_mb_row_id(musicbrainz_row_id, musicbrainz_id) user_email = None if mb_engine: user_email = mb_editor.get_editor_by_id(musicbrainz_row_id)['email'] if user is None: # a new user is trying to sign up if current_app.config["REJECT_NEW_USERS_WITHOUT_EMAIL"] and user_email is None: # if flag is set to True and the user does not have an email do not allow to sign up raise MusicBrainzAuthNoEmailError() db_user.create(musicbrainz_row_id, musicbrainz_id, email=user_email) user = db_user.get_by_mb_id(musicbrainz_id, fetch_email=True) ts.set_empty_values_for_user(user["id"]) else: # an existing user is trying to log in # Other option is to change the return type of get_by_mb_row_id to a dict # but its used so widely that we would modifying huge number of tests user = dict(user) user["email"] = user_email # every time a user logs in, update the email in LB. db_user.update_user_details(user["id"], musicbrainz_id, user_email) return user
def test_get_by_musicbrainz_row_id(self): user_id = db_user.create(0, 'frank') user = db_user.get_by_mb_row_id(0) self.assertEqual(user['id'], user_id) user = db_user.get_by_mb_row_id(0, musicbrainz_id='frank') self.assertEqual(user['id'], user_id)