def _create_steam_user(self, display_name, indexed_name): """Utility method for creating a Steam user.""" steam_id = self._next_steam_id self._next_steam_id += 1 user_id, new_user = db.steam_user_logged_in( steam_id, display_name, indexed_name, None, None, None) return steam_id, user_id, new_user
def complete_log_in_steam(response): # Get the user's Steam identifier from the OpenID response. steam_id_match = _GET_STEAM_ID_REGEX.search(response.identity_url) if not steam_id_match: flask.abort(requests.codes.server_error) steam_id = int(steam_id_match.group('steam_id')) # Given the user's Steam ID, get the user's information. user_url = ('%s/?%s' % ( _STEAM_PLAYER_SUMMARY_URL, urllib.urlencode({ 'key': _STEAM_WEB_API_KEY, 'format': 'json', 'steamids': steam_id }) )) response = requests.get(user_url) if response.status_code != requests.codes.ok: flask.abort(requests.codes.server_error) response_json = response.json() if ('response' not in response_json) or not response_json['response']['players']: flask.abort(requests.codes.server_error) player = response_json['response']['players'][0] personaname = player['personaname'] indexed_name = _get_indexed_name(personaname) profile_url = player.get('profileurl', None) avatar = player.get('avatar', None) avatar_full = player.get('avatarfull', None) # Get the user's identifier and update the session so logged in. user_id, new_user = db.steam_user_logged_in( steam_id, personaname, indexed_name, profile_url, avatar, avatar_full) return _finish_login(user_id, personaname, 'steam', new_user)
def test_show_steam_user(self): # Create a new Steam user. steam_id = 456 personaname = 'personaname' community_id = 'community_id' profile_url = 'steamcommunity.com/id/%s' % community_id avatar = 'avatar' avatar_full = 'avatar_full' user_id = db.steam_user_logged_in( steam_id, personaname, profile_url, avatar, avatar_full, self.now) # Get the user by its Steam identifier. with app.test_client() as client: response = client.get('/user/steam_id/%s' % steam_id) self.assertEqual(requests.codes.ok, response.status_code) # Get the user by its Steam name. with app.test_client() as client: response = client.get('/user/steam/%s' % community_id) self.assertEqual(requests.codes.ok, response.status_code)
def complete_steam_auth(response, session=None): # Get the user's Steam identifier from the OpenID response. steam_id_match = _GET_STEAM_ID_REGEX.search(response.identity_url) if not steam_id_match: # TODO return steam_id = int(steam_id_match.group('steam_id')) # Given the user's Steam ID, get the user's information. user_url = ('%s/?%s' % ( _STEAM_PLAYER_SUMMARY_URL, urllib.urlencode({ 'key': _STEAM_WEB_API_KEY, 'format': 'json', 'steamids': steam_id }) )) response = requests.get(user_url) if response.status_code != requests.codes.ok: # TODO return if ('response' not in response.json) or not response.json['response']['players']: # TODO return player = response.json['response']['players'][0] personaname = player['personaname'] profile_url = player.get('profileurl', None) avatar = player.get('avatar', None) avatar_full = player.get('avatarfull', None) # Log in the Steam user. user_id = db.steam_user_logged_in( steam_id, personaname, profile_url, avatar, avatar_full) # Write the Steam user to the session. _write_steam_user_to_session( user_id, steam_id, personaname, profile_url, avatar, avatar_full, session)
def _create_steam_user(self, name): steam_id = self._next_steam_id self._next_steam_id += 1 user_id = db.steam_user_logged_in(steam_id, name, None, None, None) return steam_id, user_id
def test_steam_user_name_is_unique(self): community_id = 'community_id' profile_url = 'steamcommunity.com/id/%s' % community_id # Create the first Steam user. add_time1 = self.now + timedelta(minutes=10) steam_id1 = 123 personaname1 = 'personaname1' avatar1 = 'avatar1' avatar_full1 = 'avatar_full1' user_id1 = db.steam_user_logged_in( steam_id1, personaname1, profile_url, avatar1, avatar_full1, add_time1) # Create the second Steam user with the same name. add_time2 = self.now + timedelta(minutes=20) steam_id2 = 456 personaname2 = 'personaname2' avatar2 = 'avatar2' avatar_full2 = 'avatar_full2' user_id2 = db.steam_user_logged_in( steam_id2, personaname2, profile_url, avatar2, avatar_full2, add_time2) # XXX """ # Assert that the second Steam user is returned by the shared name. displayed_steam_user = db.get_displayed_steam_user_by_name(None, community_id) self._assert_displayed_steam_user(displayed_steam_user, user_id2, personaname2, steam_id2, profile_url, image_url_small=avatar2, image_url_large=avatar_full2) """ # Assert that the first Steam user is no longer associated with the shared name. steam_user = self._get_steam_user(steam_id1) self.assertEqual('s:%s' % steam_id1, steam_user.user.url_by_id) self.assertIsNone(steam_user.user.url_by_name) self.assertEqual(add_time1, steam_user.user.created) self.assertEqual(add_time1, steam_user.user.last_seen) # Assert that the second Steam user is associated with the shared name. steam_user = self._get_steam_user(steam_id2) self.assertEqual('s:%s' % steam_id2, steam_user.user.url_by_id) self.assertEqual('s:%s' % community_id, steam_user.user.url_by_name) self.assertEqual(add_time2, steam_user.user.created) self.assertEqual(add_time2, steam_user.user.last_seen) # Log in the first Steam user again with the same name. add_time3 = self.now + timedelta(minutes=30) db.steam_user_logged_in( steam_id1, personaname1, profile_url, avatar1, avatar_full1, add_time3) # XXX """ # Assert that the first Steam user is returned by the shared name. displayed_steam_user = db.get_displayed_steam_user_by_name(None, community_id) self._assert_displayed_steam_user(displayed_steam_user, user_id1, personaname1, steam_id1, profile_url, image_url_small=avatar1, image_url_large=avatar_full1) """ # Assert that the first Steam user is associated with the shared name. steam_user = self._get_steam_user(steam_id1) self.assertEqual('s:%s' % steam_id1, steam_user.user.url_by_id) self.assertEqual('s:%s' % community_id, steam_user.user.url_by_name) self.assertEqual(add_time1, steam_user.user.created) self.assertEqual(add_time3, steam_user.user.last_seen) # Assert that the second Steam user is no longer associated with the shared name. steam_user = self._get_steam_user(steam_id2) self.assertEqual('s:%s' % steam_id2, steam_user.user.url_by_id) self.assertIsNone(steam_user.user.url_by_name) self.assertEqual(add_time2, steam_user.user.created) self.assertEqual(add_time2, steam_user.user.last_seen)
def test_steam_user_logged_in(self): # Create a new Steam user. steam_id = 456 personaname = 'personaname' community_id = 'community_id' profile_url = 'steamcommunity.com/id/%s' % community_id avatar = 'avatar' avatar_full = 'avatar_full' user_id = db.steam_user_logged_in( steam_id, personaname, profile_url, avatar, avatar_full, self.now) self.assertIsNotNone(user_id) # XXX """ # Get the Steam user by both Steam ID and by name. for displayed_steam_user in ( db.get_displayed_steam_user_by_id(None, steam_id), db.get_displayed_steam_user_by_name(None, community_id)): # Assert that the created Steam user was returned. self._assert_displayed_steam_user(displayed_steam_user, user_id, personaname, steam_id, profile_url, image_url_small=avatar, image_url_large=avatar_full) """ # Assert that the fields not returned in a DisplayedSteamUser are correct. steam_user = self._get_steam_user(steam_id) self.assertEqual('s:%s' % steam_id, steam_user.user.url_by_id) self.assertEqual('s:%s' % community_id, steam_user.user.url_by_name) self.assertEqual(self.now, steam_user.user.created) self.assertEqual(self.now, steam_user.user.last_seen) # Update the Steam user. updated_personaname = 'updated_personaname' updated_community_id = 'updated_community_id' updated_profile_url = 'steamcommunity.com/id/%s' % updated_community_id updated_avatar = 'updated_avatar' updated_avatar_full = 'updated_avatar_full' updated_time = self.now + timedelta(minutes=10) updated_user_id = db.steam_user_logged_in( steam_id, updated_personaname, updated_profile_url, updated_avatar, updated_avatar_full, updated_time) self.assertEqual(updated_user_id, user_id) # XXX """ # Get the Steam user by both Steam ID and by updated name. for displayed_steam_user in ( db.get_displayed_steam_user_by_id(None, steam_id), db.get_displayed_steam_user_by_name(None, updated_community_id)): # Assert that the updated Steam user was returned. self._assert_displayed_steam_user(displayed_steam_user, user_id, updated_personaname, steam_id, updated_profile_url, image_url_small=updated_avatar, image_url_large=updated_avatar_full) """ # Assert that the fields not returned in a DisplayedSteamUser are correct. steam_user = self._get_steam_user(steam_id) self.assertEqual('s:%s' % steam_id, steam_user.user.url_by_id) self.assertEqual('s:%s' % updated_community_id, steam_user.user.url_by_name) self.assertEqual(self.now, steam_user.user.created) self.assertEqual(updated_time, steam_user.user.last_seen) # XXX """