コード例 #1
0
ファイル: db_test_case.py プロジェクト: mgp/streamhighlights
	def _create_twitch_user(self, display_name, indexed_name):
		"""Utility method for creating a Twitch user."""
		twitch_id = self._next_twitch_id
		self._next_twitch_id += 1
		user_id, new_user = db.twitch_user_logged_in(
				twitch_id, display_name, indexed_name, None, None, None)
		return twitch_id, user_id, new_user
コード例 #2
0
	def _create_twitch_user(self, display_name, indexed_name):
		"""Utility method for creating a Twitch user."""
		twitch_id = self._next_twitch_id
		self._next_twitch_id += 1
		name = display_name
		user_id, new_user = db.twitch_user_logged_in(
				twitch_id, name, display_name, indexed_name, None)
		db.toggle_can_stream(user_id, True)
		return twitch_id, user_id, new_user
コード例 #3
0
ファイル: views_tests.py プロジェクト: mgp/streamhighlights
	def test_show_twitch_user(self):
		# Create a new Twitch user.
		twitch_id = 123
		name = 'name'
		display_name = 'display_name'
		logo = 'logo_url'
		access_token = 'access_token'
		user_id = db.twitch_user_logged_in(
				twitch_id, name, display_name, logo, access_token, self.now)

		# Get the user by its Twitch identifier.
		with app.test_client() as client:
			response = client.get('/user/twitch_id/%s' % twitch_id)
			self.assertEqual(requests.codes.ok, response.status_code)

		# Get the user by its Twitch name.
		with app.test_client() as client:
			response = client.get('/user/twitch/%s' % name)
			self.assertEqual(requests.codes.ok, response.status_code)
コード例 #4
0
ファイル: views.py プロジェクト: mgp/streamhighlights
def complete_twitch_auth():
	# Given the code, get the access token for this user.
	code = flask.request.args['code']
	params = {
			'client_id': _TWITCH_CLIENT_ID,
			'redirect_uri': _TWITCH_REDIRECT_URI,
			'client_secret': _TWITCH_CLIENT_SECRET,
			'grant_type': 'authorization_code',
			'code': code
	}
	response = requests.post(_TWITCH_OAUTH_ACCESS_TOKEN_URL, params)
	if response.status_code != requests.codes.ok:
		return flask.render_template('complete_twitch_auth.html', error=True)
	elif _TWITCH_USER_READ_SCOPE not in response.json.get('scope', ()):
		# The client did not grant read-only access for basic information.
		return flask.render_template('complete_twitch_auth.html', error=True)
	access_token = response.json['access_token']

	# Given the access code for this user, get the user's information.
	headers = {
			'accept': 'application/vnd.twitchtv.v1+json',
			'authorization': 'OAuth %s' % access_token
	}
	response = requests.get(_TWITCH_AUTHENTICATED_USER_URL, headers=headers)
	if response.status_code != requests.codes.ok:
		return flask.render_template('complete_twitch_auth.html', error=True)
	elif 'error' in response.json:
		return flask.render_template('complete_twitch_auth.html', error=True)
	twitch_id = response.json['twitch_id']
	name = response.json['name']
	display_name = response.json['display_name']
	logo = response.json['logo']
	
	# Log in the Twitch user.
	user_id = db.twitch_user_logged_in(
			twitch_id, name, display_name, logo, access_token)
	# Write the Twitch user to the session.
	_write_twitch_user_to_session(
			user_id, twitch_id, name, display_name, logo, access_token)
	# TODO: Redirect the user.
	return flask.render_template('complete_twitch_auth.html')
コード例 #5
0
ファイル: views.py プロジェクト: mgp/match-stream-guide
def complete_log_in_twitch():
	# Given the code, get the access token for this user.
	code = flask.request.args['code']
	params = {
		'client_id': _TWITCH_CLIENT_ID,
		'client_secret': _TWITCH_CLIENT_SECRET,
		'grant_type': 'authorization_code',
		'redirect_uri': _TWITCH_REDIRECT_URI,
		'code': code,
	}
	response = requests.post(_TWITCH_OAUTH_ACCESS_TOKEN_URL, params)
	if response.status_code != requests.codes.ok:
		flask.abort(requests.codes.server_error)
	response_json = response.json()
	access_token = response_json['access_token']

	# Given the access code for this user, get the user's information.
	headers = {
		'accept': 'application/vnd.twitchtv.v1+json',
		'authorization': 'OAuth %s' % access_token
	}
	response = requests.get(_TWITCH_AUTHENTICATED_USER_URL, headers=headers)
	if response.status_code != requests.codes.ok:
		flask.abort(requests.codes.server_error)
	response_json = response.json()
	if 'error' in response_json:
		flask.abort(requests.codes.server_error)
	twitch_id = response_json['_id']
	name = response_json['name']
	display_name = response_json['display_name']
	indexed_name = _get_indexed_name(display_name)
	logo = response_json['logo']
	
	# Get the user's identifier and update the session so logged in.
	user_id, new_user = db.twitch_user_logged_in(
			twitch_id, name, display_name, indexed_name, logo)
	return _finish_login(user_id, display_name, 'twitch', new_user)
コード例 #6
0
ファイル: db_tests.py プロジェクト: mgp/streamhighlights
	def test_twitch_user_logged_in(self):
		# Create a new Twitch user.
		twitch_id = 123
		name = 'name'
		display_name = 'display_name'
		logo = 'logo_url'
		access_token = 'access_token'
		user_id = db.twitch_user_logged_in(
				twitch_id, name, display_name, logo, access_token, self.now)
		self.assertIsNotNone(user_id)

		# XXX
		"""
		# Get the Twitch user by both ID and by name.
		expected_link_url = 'http://www.twitch.tv/%s' % name
		for displayed_twitch_user in (
				db.get_displayed_twitch_user_by_id(None, twitch_id),
				db.get_displayed_twitch_user_by_name(None, name)):
			# Assert that the created Twitch user was returned.
			self._assert_displayed_twitch_user(displayed_twitch_user,
					user_id, display_name, twitch_id, expected_link_url, image_url_large=logo)
		"""

		# Assert that the fields not returned in a DisplayedTwitchUser are correct.
		twitch_user = self._get_twitch_user(twitch_id)
		self.assertEqual('t:%s' % twitch_id, twitch_user.user.url_by_id)
		self.assertEqual('t:%s' % name, twitch_user.user.url_by_name)
		self.assertEqual(self.now, twitch_user.user.created)
		self.assertEqual(self.now, twitch_user.user.last_seen)
		self.assertEqual(access_token, twitch_user.access_token)

		# Update the Twitch user by both ID and by updated name.
		updated_name = 'updated_name'
		updated_display_name = 'updated_display_name'
		updated_logo = 'updated_logo_url'
		updated_access_token = 'updated_access_token'
		updated_time = self.now + timedelta(minutes=10)
		updated_user_id = db.twitch_user_logged_in(
				twitch_id, updated_name, updated_display_name,
				updated_logo, updated_access_token, updated_time)
		self.assertEqual(updated_user_id, user_id)

		# XXX
		"""
		# Get the Twitch user.
		updated_expected_link_url = 'http://www.twitch.tv/%s' % updated_name
		for displayed_twitch_user in (
				db.get_displayed_twitch_user_by_id(None, twitch_id),
				db.get_displayed_twitch_user_by_name(None, updated_name)):
			# Assert that the updated Twitch user was returned.
			self._assert_displayed_twitch_user(displayed_twitch_user,
					user_id, updated_display_name, twitch_id,
					updated_expected_link_url, image_url_large=updated_logo)
		"""

		# Assert that the fields not returned in a DisplayedTwitchUser are correct.
		twitch_user = self._get_twitch_user(twitch_id)
		self.assertEqual('t:%s' % twitch_id, twitch_user.user.url_by_id)
		self.assertEqual('t:%s' % updated_name, twitch_user.user.url_by_name)
		self.assertEqual(self.now, twitch_user.user.created)
		self.assertEqual(updated_time, twitch_user.user.last_seen)
		self.assertEqual(updated_access_token, twitch_user.access_token)

		# XXX
		"""
コード例 #7
0
ファイル: db_tests.py プロジェクト: mgp/streamhighlights
	def test_twitch_user_name_is_unique(self):
		name = 'name'
		expected_link_url = 'http://www.twitch.tv/%s' % name

		# Create the first Twitch user.
		add_time1 = self.now + timedelta(minutes=10)
		twitch_id1 = 123
		display_name1 = 'display_name1'
		logo1 = 'logo_url1'
		access_token1 = 'access_token1'
		user_id1 = db.twitch_user_logged_in(
				twitch_id1, name, display_name1, logo1, access_token1, add_time1)

		# Create the second Twitch user.
		add_time2 = self.now + timedelta(minutes=20)
		twitch_id2 = 456
		display_name2 = 'display_name2'
		logo2 = 'logo_url2'
		access_token2 = 'access_token2'
		user_id2 = db.twitch_user_logged_in(
				twitch_id2, name, display_name2, logo2, access_token2, add_time2)

		# XXX
		"""
		# Assert that the second Twitch user is returned by the shared name.
		displayed_twitch_user = db.get_displayed_twitch_user_by_name(None, name)
		self._assert_displayed_twitch_user(displayed_twitch_user,
				user_id2, display_name2, twitch_id2, expected_link_url, image_url_large=logo2)
		"""

		# Assert that the first Twitch user is no longer associated with the shared name.
		twitch_user = self._get_twitch_user(twitch_id1)
		self.assertEqual('t:%s' % twitch_id1, twitch_user.user.url_by_id)
		self.assertIsNone(twitch_user.user.url_by_name)
		self.assertEqual(add_time1, twitch_user.user.created)
		self.assertEqual(add_time1, twitch_user.user.last_seen)
		self.assertEqual(access_token1, twitch_user.access_token)
		# Assert that the second Twitch user is associated with the shared name.
		twitch_user = self._get_twitch_user(twitch_id2)
		self.assertEqual('t:%s' % twitch_id2, twitch_user.user.url_by_id)
		self.assertEqual('t:%s' % name, twitch_user.user.url_by_name)
		self.assertEqual(add_time2, twitch_user.user.created)
		self.assertEqual(add_time2, twitch_user.user.last_seen)
		self.assertEqual(access_token2, twitch_user.access_token)

		# Log in the first Twitch user again with the same name.
		add_time3 = self.now + timedelta(minutes=30)
		db.twitch_user_logged_in(
				twitch_id1, name, display_name1, logo1, access_token1, add_time3)

		# XXX
		"""
		# Assert that the second Twitch user is returned by the shared name.
		displayed_twitch_user = db.get_displayed_twitch_user_by_name(None, name)
		self._assert_displayed_twitch_user(displayed_twitch_user,
				user_id1, display_name1, twitch_id1, expected_link_url, image_url_large=logo1)
		"""

		# Assert that the first Twitch user is associated with the shared name.
		twitch_user = self._get_twitch_user(twitch_id1)
		self.assertEqual('t:%s' % twitch_id1, twitch_user.user.url_by_id)
		self.assertEqual('t:%s' % name, twitch_user.user.url_by_name)
		self.assertEqual(add_time1, twitch_user.user.created)
		self.assertEqual(add_time3, twitch_user.user.last_seen)
		self.assertEqual(access_token1, twitch_user.access_token)
		# Assert that the second Twitch user is no longer associated with the shared name.
		twitch_user = self._get_twitch_user(twitch_id2)
		self.assertEqual('t:%s' % twitch_id2, twitch_user.user.url_by_id)
		self.assertIsNone(twitch_user.user.url_by_name)
		self.assertEqual(add_time2, twitch_user.user.created)
		self.assertEqual(add_time2, twitch_user.user.last_seen)
		self.assertEqual(access_token2, twitch_user.access_token)