soundcloud python/php apis, recorder python script so far.
This commit is contained in:
parent
b3e111b0a0
commit
f68a8f67ea
109 changed files with 24297 additions and 10 deletions
0
python_apps/soundcloud-api/scapi/tests/__init__.py
Normal file
0
python_apps/soundcloud-api/scapi/tests/__init__.py
Normal file
BIN
python_apps/soundcloud-api/scapi/tests/knaster.mp3
Normal file
BIN
python_apps/soundcloud-api/scapi/tests/knaster.mp3
Normal file
Binary file not shown.
563
python_apps/soundcloud-api/scapi/tests/scapi_tests.py
Normal file
563
python_apps/soundcloud-api/scapi/tests/scapi_tests.py
Normal file
|
@ -0,0 +1,563 @@
|
|||
from __future__ import with_statement
|
||||
|
||||
import os
|
||||
import urllib2
|
||||
import itertools
|
||||
from textwrap import dedent
|
||||
import pkg_resources
|
||||
import logging
|
||||
import webbrowser
|
||||
from unittest import TestCase
|
||||
|
||||
from configobj import ConfigObj
|
||||
from validate import Validator
|
||||
|
||||
|
||||
import scapi
|
||||
import scapi.authentication
|
||||
|
||||
logger = logging.getLogger("scapi.tests")
|
||||
|
||||
api_logger = logging.getLogger("scapi")
|
||||
|
||||
|
||||
class SCAPITests(TestCase):
|
||||
|
||||
CONFIG_NAME = "test.ini"
|
||||
TOKEN = None
|
||||
SECRET = None
|
||||
CONSUMER = None
|
||||
CONSUMER_SECRET = None
|
||||
API_HOST = None
|
||||
USER = None
|
||||
PASSWORD = None
|
||||
AUTHENTICATOR = None
|
||||
RUN_INTERACTIVE_TESTS = False
|
||||
RUN_LONG_TESTS = False
|
||||
|
||||
def setUp(self):
|
||||
self._load_config()
|
||||
assert pkg_resources.resource_exists("scapi.tests.test_connect", "knaster.mp3")
|
||||
self.data = pkg_resources.resource_stream("scapi.tests.test_connect", "knaster.mp3")
|
||||
self.artwork_data = pkg_resources.resource_stream("scapi.tests.test_connect", "spam.jpg")
|
||||
|
||||
CONFIGSPEC=dedent("""
|
||||
[api]
|
||||
token=string
|
||||
secret=string
|
||||
consumer=string
|
||||
consumer_secret=string
|
||||
api_host=string
|
||||
user=string
|
||||
password=string
|
||||
authenticator=option('oauth', 'base', default='oauth')
|
||||
|
||||
[proxy]
|
||||
use_proxy=boolean(default=false)
|
||||
proxy=string(default=http://127.0.0.1:10000/)
|
||||
|
||||
[logging]
|
||||
test_logger=string(default=ERROR)
|
||||
api_logger=string(default=ERROR)
|
||||
|
||||
[test]
|
||||
run_interactive_tests=boolean(default=false)
|
||||
""")
|
||||
|
||||
|
||||
def _load_config(self):
|
||||
"""
|
||||
Loads the configuration by looking from
|
||||
|
||||
- the environment variable SCAPI_CONFIG
|
||||
- the installation location upwards until it finds test.ini
|
||||
- the current working directory upwards until it finds test.ini
|
||||
|
||||
Raises an error if there is no config found
|
||||
"""
|
||||
config_name = self.CONFIG_NAME
|
||||
|
||||
name = None
|
||||
|
||||
if "SCAPI_CONFIG" in os.environ:
|
||||
if os.path.exists(os.environ["SCAPI_CONFIG"]):
|
||||
name = os.environ["SCAPI_CONFIG"]
|
||||
|
||||
def search_for_config(current):
|
||||
while current:
|
||||
name = os.path.join(current, config_name)
|
||||
if os.path.exists(name):
|
||||
return name
|
||||
new_current = os.path.dirname(current)
|
||||
if new_current == current:
|
||||
return
|
||||
current = new_current
|
||||
|
||||
if name is None:
|
||||
name = search_for_config(os.path.dirname(__file__))
|
||||
if name is None:
|
||||
name = search_for_config(os.getcwd())
|
||||
|
||||
if not name:
|
||||
raise Exception("No test configuration file found!")
|
||||
|
||||
parser = ConfigObj(name, configspec=self.CONFIGSPEC.split("\n"))
|
||||
val = Validator()
|
||||
if not parser.validate(val):
|
||||
raise Exception("Config file validation error")
|
||||
|
||||
api = parser['api']
|
||||
self.TOKEN = api.get('token')
|
||||
self.SECRET = api.get('secret')
|
||||
self.CONSUMER = api.get('consumer')
|
||||
self.CONSUMER_SECRET = api.get('consumer_secret')
|
||||
self.API_HOST = api.get('api_host')
|
||||
self.USER = api.get('user', None)
|
||||
self.PASSWORD = api.get('password', None)
|
||||
self.AUTHENTICATOR = api.get("authenticator")
|
||||
|
||||
# reset the hard-coded values in the api
|
||||
if self.API_HOST:
|
||||
scapi.AUTHORIZATION_URL = "http://%s/oauth/authorize" % self.API_HOST
|
||||
scapi.REQUEST_TOKEN_URL = 'http://%s/oauth/request_token' % self.API_HOST
|
||||
scapi.ACCESS_TOKEN_URL = 'http://%s/oauth/access_token' % self.API_HOST
|
||||
|
||||
if "proxy" in parser and parser["proxy"]["use_proxy"]:
|
||||
scapi.USE_PROXY = True
|
||||
scapi.PROXY = parser["proxy"]["proxy"]
|
||||
|
||||
if "logging" in parser:
|
||||
logger.setLevel(getattr(logging, parser["logging"]["test_logger"]))
|
||||
api_logger.setLevel(getattr(logging, parser["logging"]["api_logger"]))
|
||||
|
||||
self.RUN_INTERACTIVE_TESTS = parser["test"]["run_interactive_tests"]
|
||||
|
||||
|
||||
@property
|
||||
def root(self):
|
||||
"""
|
||||
Return the properly configured root-scope.
|
||||
"""
|
||||
if self.AUTHENTICATOR == "oauth":
|
||||
authenticator = scapi.authentication.OAuthAuthenticator(self.CONSUMER,
|
||||
self.CONSUMER_SECRET,
|
||||
self.TOKEN,
|
||||
self.SECRET)
|
||||
elif self.AUTHENTICATOR == "base":
|
||||
authenticator = scapi.authentication.BasicAuthenticator(self.USER, self.PASSWORD, self.CONSUMER, self.CONSUMER_SECRET)
|
||||
else:
|
||||
raise Exception("Unknown authenticator setting: %s", self.AUTHENTICATOR)
|
||||
|
||||
connector = scapi.ApiConnector(host=self.API_HOST,
|
||||
authenticator=authenticator)
|
||||
|
||||
logger.debug("RootScope: %s authenticator: %s", self.API_HOST, self.AUTHENTICATOR)
|
||||
return scapi.Scope(connector)
|
||||
|
||||
|
||||
def test_connect(self):
|
||||
"""
|
||||
test_connect
|
||||
|
||||
Tries to connect & performs some read-only operations.
|
||||
"""
|
||||
sca = self.root
|
||||
# quite_a_few_users = list(itertools.islice(sca.users(), 0, 127))
|
||||
|
||||
# logger.debug(quite_a_few_users)
|
||||
# assert isinstance(quite_a_few_users, list) and isinstance(quite_a_few_users[0], scapi.User)
|
||||
user = sca.me()
|
||||
logger.debug(user)
|
||||
assert isinstance(user, scapi.User)
|
||||
contacts = list(user.contacts())
|
||||
assert isinstance(contacts, list)
|
||||
if contacts:
|
||||
assert isinstance(contacts[0], scapi.User)
|
||||
logger.debug(contacts)
|
||||
tracks = list(user.tracks())
|
||||
assert isinstance(tracks, list)
|
||||
if tracks:
|
||||
assert isinstance(tracks[0], scapi.Track)
|
||||
logger.debug(tracks)
|
||||
|
||||
|
||||
def test_access_token_acquisition(self):
|
||||
"""
|
||||
This test is commented out because it needs user-interaction.
|
||||
"""
|
||||
if not self.RUN_INTERACTIVE_TESTS:
|
||||
return
|
||||
oauth_authenticator = scapi.authentication.OAuthAuthenticator(self.CONSUMER,
|
||||
self.CONSUMER_SECRET,
|
||||
None,
|
||||
None)
|
||||
|
||||
sca = scapi.ApiConnector(host=self.API_HOST, authenticator=oauth_authenticator)
|
||||
token, secret = sca.fetch_request_token()
|
||||
authorization_url = sca.get_request_token_authorization_url(token)
|
||||
webbrowser.open(authorization_url)
|
||||
oauth_verifier = raw_input("please enter verifier code as seen in the browser:")
|
||||
|
||||
oauth_authenticator = scapi.authentication.OAuthAuthenticator(self.CONSUMER,
|
||||
self.CONSUMER_SECRET,
|
||||
token,
|
||||
secret)
|
||||
|
||||
sca = scapi.ApiConnector(self.API_HOST, authenticator=oauth_authenticator)
|
||||
token, secret = sca.fetch_access_token(oauth_verifier)
|
||||
logger.info("Access token: '%s'", token)
|
||||
logger.info("Access token secret: '%s'", secret)
|
||||
# force oauth-authentication with the new parameters, and
|
||||
# then invoke some simple test
|
||||
self.AUTHENTICATOR = "oauth"
|
||||
self.TOKEN = token
|
||||
self.SECRET = secret
|
||||
self.test_connect()
|
||||
|
||||
|
||||
def test_track_creation(self):
|
||||
sca = self.root
|
||||
track = sca.Track.new(title='bar', asset_data=self.data)
|
||||
assert isinstance(track, scapi.Track)
|
||||
|
||||
|
||||
def test_track_update(self):
|
||||
sca = self.root
|
||||
track = sca.Track.new(title='bar', asset_data=self.data)
|
||||
assert isinstance(track, scapi.Track)
|
||||
track.title='baz'
|
||||
track = sca.Track.get(track.id)
|
||||
assert track.title == "baz"
|
||||
|
||||
|
||||
def test_scoped_track_creation(self):
|
||||
sca = self.root
|
||||
user = sca.me()
|
||||
track = user.tracks.new(title="bar", asset_data=self.data)
|
||||
assert isinstance(track, scapi.Track)
|
||||
|
||||
|
||||
def test_upload(self):
|
||||
sca = self.root
|
||||
sca = self.root
|
||||
track = sca.Track.new(title='bar', asset_data=self.data)
|
||||
assert isinstance(track, scapi.Track)
|
||||
|
||||
|
||||
def test_contact_list(self):
|
||||
sca = self.root
|
||||
user = sca.me()
|
||||
contacts = list(user.contacts())
|
||||
assert isinstance(contacts, list)
|
||||
if contacts:
|
||||
assert isinstance(contacts[0], scapi.User)
|
||||
|
||||
|
||||
def test_permissions(self):
|
||||
sca = self.root
|
||||
user = sca.me()
|
||||
tracks = itertools.islice(user.tracks(), 1)
|
||||
for track in tracks:
|
||||
permissions = list(track.permissions())
|
||||
logger.debug(permissions)
|
||||
assert isinstance(permissions, list)
|
||||
if permissions:
|
||||
assert isinstance(permissions[0], scapi.User)
|
||||
|
||||
|
||||
def test_setting_permissions(self):
|
||||
sca = self.root
|
||||
me = sca.me()
|
||||
track = sca.Track.new(title='bar', sharing="private", asset_data=self.data)
|
||||
assert track.sharing == "private"
|
||||
users = itertools.islice(sca.users(), 10)
|
||||
users_to_set = [user for user in users if user != me]
|
||||
assert users_to_set, "Didn't find any suitable users"
|
||||
track.permissions = users_to_set
|
||||
assert set(track.permissions()) == set(users_to_set)
|
||||
|
||||
|
||||
def test_setting_comments(self):
|
||||
sca = self.root
|
||||
user = sca.me()
|
||||
track = sca.Track.new(title='bar', sharing="private", asset_data=self.data)
|
||||
comment = sca.Comment.create(body="This is the body of my comment", timestamp=10)
|
||||
track.comments = comment
|
||||
assert track.comments().next().body == comment.body
|
||||
|
||||
|
||||
def test_setting_comments_the_way_shawn_says_its_correct(self):
|
||||
sca = self.root
|
||||
track = sca.Track.new(title='bar', sharing="private", asset_data=self.data)
|
||||
cbody = "This is the body of my comment"
|
||||
track.comments.new(body=cbody, timestamp=10)
|
||||
assert list(track.comments())[0].body == cbody
|
||||
|
||||
|
||||
def test_contact_add_and_removal(self):
|
||||
sca = self.root
|
||||
me = sca.me()
|
||||
for user in sca.users():
|
||||
if user != me:
|
||||
user_to_set = user
|
||||
break
|
||||
|
||||
contacts = list(me.contacts())
|
||||
if user_to_set in contacts:
|
||||
me.contacts.remove(user_to_set)
|
||||
|
||||
me.contacts.append(user_to_set)
|
||||
|
||||
contacts = list(me.contacts() )
|
||||
assert user_to_set.id in [c.id for c in contacts]
|
||||
|
||||
me.contacts.remove(user_to_set)
|
||||
|
||||
contacts = list(me.contacts() )
|
||||
assert user_to_set not in contacts
|
||||
|
||||
|
||||
def test_favorites(self):
|
||||
sca = self.root
|
||||
me = sca.me()
|
||||
|
||||
favorites = list(me.favorites())
|
||||
assert favorites == [] or isinstance(favorites[0], scapi.Track)
|
||||
|
||||
track = None
|
||||
for user in sca.users():
|
||||
if user == me:
|
||||
continue
|
||||
for track in user.tracks():
|
||||
break
|
||||
if track is not None:
|
||||
break
|
||||
|
||||
me.favorites.append(track)
|
||||
|
||||
favorites = list(me.favorites())
|
||||
assert track in favorites
|
||||
|
||||
me.favorites.remove(track)
|
||||
|
||||
favorites = list(me.favorites())
|
||||
assert track not in favorites
|
||||
|
||||
|
||||
def test_large_list(self):
|
||||
if not self.RUN_LONG_TESTS:
|
||||
return
|
||||
|
||||
sca = self.root
|
||||
|
||||
tracks = list(sca.tracks())
|
||||
if len(tracks) < scapi.ApiConnector.LIST_LIMIT:
|
||||
for i in xrange(scapi.ApiConnector.LIST_LIMIT):
|
||||
sca.Track.new(title='test_track_%i' % i, asset_data=self.data)
|
||||
all_tracks = sca.tracks()
|
||||
assert not isinstance(all_tracks, list)
|
||||
all_tracks = list(all_tracks)
|
||||
assert len(all_tracks) > scapi.ApiConnector.LIST_LIMIT
|
||||
|
||||
|
||||
|
||||
def test_filtered_list(self):
|
||||
if not self.RUN_LONG_TESTS:
|
||||
return
|
||||
|
||||
sca = self.root
|
||||
|
||||
tracks = list(sca.tracks(params={
|
||||
"bpm[from]" : "180",
|
||||
}))
|
||||
if len(tracks) < scapi.ApiConnector.LIST_LIMIT:
|
||||
for i in xrange(scapi.ApiConnector.LIST_LIMIT):
|
||||
sca.Track.new(title='test_track_%i' % i, asset_data=self.data)
|
||||
all_tracks = sca.tracks()
|
||||
assert not isinstance(all_tracks, list)
|
||||
all_tracks = list(all_tracks)
|
||||
assert len(all_tracks) > scapi.ApiConnector.LIST_LIMIT
|
||||
|
||||
|
||||
def test_events(self):
|
||||
events = list(self.root.events())
|
||||
assert isinstance(events, list)
|
||||
assert isinstance(events[0], scapi.Event)
|
||||
|
||||
|
||||
def test_me_having_stress(self):
|
||||
sca = self.root
|
||||
for _ in xrange(20):
|
||||
self.setUp()
|
||||
sca.me()
|
||||
|
||||
|
||||
def test_non_global_api(self):
|
||||
root = self.root
|
||||
me = root.me()
|
||||
assert isinstance(me, scapi.User)
|
||||
|
||||
# now get something *from* that user
|
||||
list(me.favorites())
|
||||
|
||||
|
||||
def test_playlists(self):
|
||||
sca = self.root
|
||||
playlists = list(itertools.islice(sca.playlists(), 0, 127))
|
||||
for playlist in playlists:
|
||||
tracks = playlist.tracks
|
||||
if not isinstance(tracks, list):
|
||||
tracks = [tracks]
|
||||
for trackdata in tracks:
|
||||
print trackdata
|
||||
#user = trackdata.user
|
||||
#print user
|
||||
#print user.tracks()
|
||||
print playlist.user
|
||||
break
|
||||
|
||||
|
||||
|
||||
|
||||
def test_playlist_creation(self):
|
||||
sca = self.root
|
||||
sca.Playlist.new(title="I'm so happy, happy, happy, happy!")
|
||||
|
||||
|
||||
|
||||
def test_groups(self):
|
||||
if not self.RUN_LONG_TESTS:
|
||||
return
|
||||
|
||||
sca = self.root
|
||||
groups = list(itertools.islice(sca.groups(), 0, 127))
|
||||
for group in groups:
|
||||
users = group.users()
|
||||
for user in users:
|
||||
pass
|
||||
|
||||
|
||||
def test_track_creation_with_email_sharers(self):
|
||||
sca = self.root
|
||||
emails = [dict(address="deets@web.de"), dict(address="hannes@soundcloud.com")]
|
||||
track = sca.Track.new(title='bar', asset_data=self.data,
|
||||
shared_to=dict(emails=emails)
|
||||
)
|
||||
assert isinstance(track, scapi.Track)
|
||||
|
||||
|
||||
|
||||
def test_track_creation_with_artwork(self):
|
||||
sca = self.root
|
||||
track = sca.Track.new(title='bar',
|
||||
asset_data=self.data,
|
||||
artwork_data=self.artwork_data,
|
||||
)
|
||||
assert isinstance(track, scapi.Track)
|
||||
|
||||
track.title = "foobarbaz"
|
||||
|
||||
|
||||
|
||||
def test_oauth_get_signing(self):
|
||||
sca = self.root
|
||||
|
||||
url = "http://api.soundcloud.dev/oauth/test_request"
|
||||
params = dict(foo="bar",
|
||||
baz="padamm",
|
||||
)
|
||||
url += sca._create_query_string(params)
|
||||
signed_url = sca.oauth_sign_get_request(url)
|
||||
|
||||
|
||||
res = urllib2.urlopen(signed_url).read()
|
||||
assert "oauth_nonce" in res
|
||||
|
||||
|
||||
def test_streaming(self):
|
||||
sca = self.root
|
||||
|
||||
track = sca.tracks(params={
|
||||
"filter" : "streamable",
|
||||
}).next()
|
||||
|
||||
|
||||
assert isinstance(track, scapi.Track)
|
||||
|
||||
stream_url = track.stream_url
|
||||
|
||||
signed_url = track.oauth_sign_get_request(stream_url)
|
||||
|
||||
|
||||
def test_downloadable(self):
|
||||
sca = self.root
|
||||
|
||||
track = sca.tracks(params={
|
||||
"filter" : "downloadable",
|
||||
}).next()
|
||||
|
||||
|
||||
assert isinstance(track, scapi.Track)
|
||||
|
||||
download_url = track.download_url
|
||||
|
||||
signed_url = track.oauth_sign_get_request(download_url)
|
||||
|
||||
data = urllib2.urlopen(signed_url).read()
|
||||
assert data
|
||||
|
||||
|
||||
|
||||
def test_modifying_playlists(self):
|
||||
sca = self.root
|
||||
|
||||
me = sca.me()
|
||||
my_tracks = list(me.tracks())
|
||||
|
||||
assert my_tracks
|
||||
|
||||
playlist = me.playlists().next()
|
||||
# playlist = sca.Playlist.get(playlist.id)
|
||||
|
||||
assert isinstance(playlist, scapi.Playlist)
|
||||
|
||||
pl_tracks = playlist.tracks
|
||||
|
||||
playlist.title = "foobarbaz"
|
||||
|
||||
|
||||
|
||||
def test_track_deletion(self):
|
||||
sca = self.root
|
||||
track = sca.Track.new(title='bar', asset_data=self.data,
|
||||
)
|
||||
|
||||
sca.tracks.remove(track)
|
||||
|
||||
|
||||
|
||||
def test_track_creation_with_updated_artwork(self):
|
||||
sca = self.root
|
||||
track = sca.Track.new(title='bar',
|
||||
asset_data=self.data,
|
||||
)
|
||||
assert isinstance(track, scapi.Track)
|
||||
|
||||
track.artwork_data = self.artwork_data
|
||||
|
||||
def test_update_own_description(self):
|
||||
sca = self.root
|
||||
me = sca.me()
|
||||
|
||||
new_description = "This is my new description"
|
||||
old_description = "This is my old description"
|
||||
|
||||
if me.description == new_description:
|
||||
change_to_description = old_description
|
||||
else:
|
||||
change_to_description = new_description
|
||||
|
||||
me.description = change_to_description
|
||||
|
||||
user = sca.User.get(me.id)
|
||||
assert user.description == change_to_description
|
BIN
python_apps/soundcloud-api/scapi/tests/spam.jpg
Normal file
BIN
python_apps/soundcloud-api/scapi/tests/spam.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 83 KiB |
334
python_apps/soundcloud-api/scapi/tests/test_connect.py
Normal file
334
python_apps/soundcloud-api/scapi/tests/test_connect.py
Normal file
|
@ -0,0 +1,334 @@
|
|||
from __future__ import with_statement
|
||||
import os
|
||||
import tempfile
|
||||
import itertools
|
||||
from ConfigParser import SafeConfigParser
|
||||
import pkg_resources
|
||||
import scapi
|
||||
import scapi.authentication
|
||||
import logging
|
||||
import webbrowser
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
_logger = logging.getLogger("scapi")
|
||||
#_logger.setLevel(logging.DEBUG)
|
||||
|
||||
RUN_INTERACTIVE_TESTS = False
|
||||
USE_OAUTH = True
|
||||
|
||||
TOKEN = "FjNE9aRTg8kpxuOjzwsX8Q"
|
||||
SECRET = "NP5PGoyKcQv64E0aZgV4CRNzHfPwR4QghrWoqEgEE"
|
||||
CONSUMER = "EEi2URUfM97pAAxHTogDpQ"
|
||||
CONSUMER_SECRET = "NFYd8T3i4jVKGZ9TMy9LHaBQB3Sh8V5sxBiMeMZBow"
|
||||
API_HOST = "api.soundcloud.dev:3000"
|
||||
USER = ""
|
||||
PASSWORD = ""
|
||||
|
||||
CONFIG_NAME = "soundcloud.cfg"
|
||||
|
||||
CONNECTOR = None
|
||||
ROOT = None
|
||||
def setup():
|
||||
global CONNECTOR, ROOT
|
||||
# load_config()
|
||||
#scapi.ApiConnector(host='192.168.2.101:3000', user='tiga', password='test')
|
||||
#scapi.ApiConnector(host='sandbox-api.soundcloud.com:3030', user='tiga', password='test')
|
||||
scapi.USE_PROXY = False
|
||||
scapi.PROXY = 'http://127.0.0.1:10000/'
|
||||
|
||||
if USE_OAUTH:
|
||||
authenticator = scapi.authentication.OAuthAuthenticator(CONSUMER,
|
||||
CONSUMER_SECRET,
|
||||
TOKEN,
|
||||
SECRET)
|
||||
else:
|
||||
authenticator = scapi.authentication.BasicAuthenticator(USER, PASSWORD, CONSUMER, CONSUMER_SECRET)
|
||||
|
||||
logger.debug("API_HOST: %s", API_HOST)
|
||||
CONNECTOR = scapi.ApiConnector(host=API_HOST,
|
||||
authenticator=authenticator)
|
||||
ROOT = scapi.Scope(CONNECTOR)
|
||||
|
||||
def load_config(config_name=None):
|
||||
global TOKEN, SECRET, CONSUMER_SECRET, CONSUMER, API_HOST, USER, PASSWORD
|
||||
if config_name is None:
|
||||
config_name = CONFIG_NAME
|
||||
parser = SafeConfigParser()
|
||||
current = os.getcwd()
|
||||
while current:
|
||||
name = os.path.join(current, config_name)
|
||||
if os.path.exists(name):
|
||||
parser.read([name])
|
||||
TOKEN = parser.get('global', 'accesstoken')
|
||||
SECRET = parser.get('global', 'accesstoken_secret')
|
||||
CONSUMER = parser.get('global', 'consumer')
|
||||
CONSUMER_SECRET = parser.get('global', 'consumer_secret')
|
||||
API_HOST = parser.get('global', 'host')
|
||||
USER = parser.get('global', 'user')
|
||||
PASSWORD = parser.get('global', 'password')
|
||||
logger.debug("token: %s", TOKEN)
|
||||
logger.debug("secret: %s", SECRET)
|
||||
logger.debug("consumer: %s", CONSUMER)
|
||||
logger.debug("consumer_secret: %s", CONSUMER_SECRET)
|
||||
logger.debug("user: %s", USER)
|
||||
logger.debug("password: %s", PASSWORD)
|
||||
logger.debug("host: %s", API_HOST)
|
||||
break
|
||||
new_current = os.path.dirname(current)
|
||||
if new_current == current:
|
||||
break
|
||||
current = new_current
|
||||
|
||||
|
||||
def test_load_config():
|
||||
base = tempfile.mkdtemp()
|
||||
oldcwd = os.getcwd()
|
||||
cdir = os.path.join(base, "foo")
|
||||
os.mkdir(cdir)
|
||||
os.chdir(cdir)
|
||||
test_config = """
|
||||
[global]
|
||||
host=host
|
||||
consumer=consumer
|
||||
consumer_secret=consumer_secret
|
||||
accesstoken=accesstoken
|
||||
accesstoken_secret=accesstoken_secret
|
||||
user=user
|
||||
password=password
|
||||
"""
|
||||
with open(os.path.join(base, CONFIG_NAME), "w") as cf:
|
||||
cf.write(test_config)
|
||||
load_config()
|
||||
assert TOKEN == "accesstoken" and SECRET == "accesstoken_secret" and API_HOST == 'host'
|
||||
assert CONSUMER == "consumer" and CONSUMER_SECRET == "consumer_secret"
|
||||
assert USER == "user" and PASSWORD == "password"
|
||||
os.chdir(oldcwd)
|
||||
load_config()
|
||||
|
||||
|
||||
def test_connect():
|
||||
sca = ROOT
|
||||
quite_a_few_users = list(itertools.islice(sca.users(), 0, 127))
|
||||
|
||||
logger.debug(quite_a_few_users)
|
||||
assert isinstance(quite_a_few_users, list) and isinstance(quite_a_few_users[0], scapi.User)
|
||||
user = sca.me()
|
||||
logger.debug(user)
|
||||
assert isinstance(user, scapi.User)
|
||||
contacts = list(user.contacts())
|
||||
assert isinstance(contacts, list)
|
||||
assert isinstance(contacts[0], scapi.User)
|
||||
logger.debug(contacts)
|
||||
tracks = list(user.tracks())
|
||||
assert isinstance(tracks, list)
|
||||
assert isinstance(tracks[0], scapi.Track)
|
||||
logger.debug(tracks)
|
||||
|
||||
|
||||
def test_access_token_acquisition():
|
||||
"""
|
||||
This test is commented out because it needs user-interaction.
|
||||
"""
|
||||
if not RUN_INTERACTIVE_TESTS:
|
||||
return
|
||||
oauth_authenticator = scapi.authentication.OAuthAuthenticator(CONSUMER,
|
||||
CONSUMER_SECRET,
|
||||
None,
|
||||
None)
|
||||
|
||||
sca = scapi.ApiConnector(host=API_HOST, authenticator=oauth_authenticator)
|
||||
token, secret = sca.fetch_request_token()
|
||||
authorization_url = sca.get_request_token_authorization_url(token)
|
||||
webbrowser.open(authorization_url)
|
||||
raw_input("please press return")
|
||||
oauth_authenticator = scapi.authentication.OAuthAuthenticator(CONSUMER,
|
||||
CONSUMER_SECRET,
|
||||
token,
|
||||
secret)
|
||||
|
||||
sca = scapi.ApiConnector(API_HOST, authenticator=oauth_authenticator)
|
||||
token, secret = sca.fetch_access_token()
|
||||
logger.info("Access token: '%s'", token)
|
||||
logger.info("Access token secret: '%s'", secret)
|
||||
oauth_authenticator = scapi.authentication.OAuthAuthenticator(CONSUMER,
|
||||
CONSUMER_SECRET,
|
||||
token,
|
||||
secret)
|
||||
|
||||
sca = scapi.ApiConnector(API_HOST, authenticator=oauth_authenticator)
|
||||
test_track_creation()
|
||||
|
||||
def test_track_creation():
|
||||
sca = ROOT
|
||||
track = sca.Track.new(title='bar')
|
||||
assert isinstance(track, scapi.Track)
|
||||
|
||||
def test_track_update():
|
||||
sca = ROOT
|
||||
track = sca.Track.new(title='bar')
|
||||
assert isinstance(track, scapi.Track)
|
||||
track.title='baz'
|
||||
track = sca.Track.get(track.id)
|
||||
assert track.title == "baz"
|
||||
|
||||
def test_scoped_track_creation():
|
||||
sca = ROOT
|
||||
user = sca.me()
|
||||
track = user.tracks.new(title="bar")
|
||||
assert isinstance(track, scapi.Track)
|
||||
|
||||
def test_upload():
|
||||
assert pkg_resources.resource_exists("scapi.tests.test_connect", "knaster.mp3")
|
||||
data = pkg_resources.resource_stream("scapi.tests.test_connect", "knaster.mp3")
|
||||
sca = ROOT
|
||||
user = sca.me()
|
||||
logger.debug(user)
|
||||
asset = sca.assets.new(filedata=data)
|
||||
assert isinstance(asset, scapi.Asset)
|
||||
logger.debug(asset)
|
||||
tracks = list(user.tracks())
|
||||
track = tracks[0]
|
||||
track.assets.append(asset)
|
||||
|
||||
def test_contact_list():
|
||||
sca = ROOT
|
||||
user = sca.me()
|
||||
contacts = list(user.contacts())
|
||||
assert isinstance(contacts, list)
|
||||
assert isinstance(contacts[0], scapi.User)
|
||||
|
||||
def test_permissions():
|
||||
sca = ROOT
|
||||
user = sca.me()
|
||||
tracks = itertools.islice(user.tracks(), 1)
|
||||
for track in tracks:
|
||||
permissions = list(track.permissions())
|
||||
logger.debug(permissions)
|
||||
assert isinstance(permissions, list)
|
||||
if permissions:
|
||||
assert isinstance(permissions[0], scapi.User)
|
||||
|
||||
def test_setting_permissions():
|
||||
sca = ROOT
|
||||
me = sca.me()
|
||||
track = sca.Track.new(title='bar', sharing="private")
|
||||
assert track.sharing == "private"
|
||||
users = itertools.islice(sca.users(), 10)
|
||||
users_to_set = [user for user in users if user != me]
|
||||
assert users_to_set, "Didn't find any suitable users"
|
||||
track.permissions = users_to_set
|
||||
assert set(track.permissions()) == set(users_to_set)
|
||||
|
||||
def test_setting_comments():
|
||||
sca = ROOT
|
||||
user = sca.me()
|
||||
track = sca.Track.new(title='bar', sharing="private")
|
||||
comment = sca.Comment.create(body="This is the body of my comment", timestamp=10)
|
||||
track.comments = comment
|
||||
assert track.comments().next().body == comment.body
|
||||
|
||||
|
||||
def test_setting_comments_the_way_shawn_says_its_correct():
|
||||
sca = ROOT
|
||||
track = sca.Track.new(title='bar', sharing="private")
|
||||
cbody = "This is the body of my comment"
|
||||
track.comments.new(body=cbody, timestamp=10)
|
||||
assert list(track.comments())[0].body == cbody
|
||||
|
||||
def test_contact_add_and_removal():
|
||||
sca = ROOT
|
||||
me = sca.me()
|
||||
for user in sca.users():
|
||||
if user != me:
|
||||
user_to_set = user
|
||||
break
|
||||
|
||||
contacts = list(me.contacts())
|
||||
if user_to_set in contacts:
|
||||
me.contacts.remove(user_to_set)
|
||||
|
||||
me.contacts.append(user_to_set)
|
||||
|
||||
contacts = list(me.contacts() )
|
||||
assert user_to_set.id in [c.id for c in contacts]
|
||||
|
||||
me.contacts.remove(user_to_set)
|
||||
|
||||
contacts = list(me.contacts() )
|
||||
assert user_to_set not in contacts
|
||||
|
||||
|
||||
def test_favorites():
|
||||
sca = ROOT
|
||||
me = sca.me()
|
||||
|
||||
favorites = list(me.favorites())
|
||||
assert favorites == [] or isinstance(favorites[0], scapi.Track)
|
||||
|
||||
track = None
|
||||
for user in sca.users():
|
||||
if user == me:
|
||||
continue
|
||||
for track in user.tracks():
|
||||
break
|
||||
if track is not None:
|
||||
break
|
||||
|
||||
me.favorites.append(track)
|
||||
|
||||
favorites = list(me.favorites())
|
||||
assert track in favorites
|
||||
|
||||
me.favorites.remove(track)
|
||||
|
||||
favorites = list(me.favorites())
|
||||
assert track not in favorites
|
||||
|
||||
def test_large_list():
|
||||
sca = ROOT
|
||||
tracks = list(sca.tracks())
|
||||
if len(tracks) < scapi.ApiConnector.LIST_LIMIT:
|
||||
for i in xrange(scapi.ApiConnector.LIST_LIMIT):
|
||||
scapi.Track.new(title='test_track_%i' % i)
|
||||
all_tracks = sca.tracks()
|
||||
assert not isinstance(all_tracks, list)
|
||||
all_tracks = list(all_tracks)
|
||||
assert len(all_tracks) > scapi.ApiConnector.LIST_LIMIT
|
||||
|
||||
|
||||
def test_events():
|
||||
events = list(ROOT.events())
|
||||
assert isinstance(events, list)
|
||||
assert isinstance(events[0], scapi.Event)
|
||||
|
||||
def test_me_having_stress():
|
||||
sca = ROOT
|
||||
for _ in xrange(20):
|
||||
setup()
|
||||
sca.me()
|
||||
|
||||
def test_non_global_api():
|
||||
root = scapi.Scope(CONNECTOR)
|
||||
me = root.me()
|
||||
assert isinstance(me, scapi.User)
|
||||
|
||||
# now get something *from* that user
|
||||
favorites = list(me.favorites())
|
||||
assert favorites
|
||||
|
||||
def test_playlists():
|
||||
sca = ROOT
|
||||
playlists = list(itertools.islice(sca.playlists(), 0, 127))
|
||||
found = False
|
||||
for playlist in playlists:
|
||||
tracks = playlist.tracks
|
||||
if not isinstance(tracks, list):
|
||||
tracks = [tracks]
|
||||
for trackdata in tracks:
|
||||
print trackdata
|
||||
user = trackdata.user
|
||||
print user
|
||||
print user.tracks()
|
||||
print playlist.user
|
||||
break
|
36
python_apps/soundcloud-api/scapi/tests/test_oauth.py
Normal file
36
python_apps/soundcloud-api/scapi/tests/test_oauth.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
import pkg_resources
|
||||
import scapi
|
||||
import scapi.authentication
|
||||
import urllib
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
_logger = logging.getLogger("scapi")
|
||||
_logger.setLevel(logging.DEBUG)
|
||||
|
||||
TOKEN = "QcciYu1FSwDSGKAG2mNw"
|
||||
SECRET = "gJ2ok6ULUsYQB3rsBmpHCRHoFCAPOgK8ZjoIyxzris"
|
||||
CONSUMER = "Cy2eLPrIMp4vOxjz9icdQ"
|
||||
CONSUMER_SECRET = "KsBa272x6M2to00Vo5FdvZXt9kakcX7CDIPJoGwTro"
|
||||
|
||||
def test_base64_connect():
|
||||
scapi.USE_PROXY = True
|
||||
scapi.PROXY = 'http://127.0.0.1:10000/'
|
||||
scapi.SoundCloudAPI(host='192.168.2.31:3000', authenticator=scapi.authentication.BasicAuthenticator('tiga', 'test'))
|
||||
sca = scapi.Scope()
|
||||
assert isinstance(sca.me(), scapi.User)
|
||||
|
||||
|
||||
def test_oauth_connect():
|
||||
scapi.USE_PROXY = True
|
||||
scapi.PROXY = 'http://127.0.0.1:10000/'
|
||||
scapi.SoundCloudAPI(host='192.168.2.31:3000',
|
||||
authenticator=scapi.authentication.OAuthAuthenticator(CONSUMER,
|
||||
CONSUMER_SECRET,
|
||||
TOKEN, SECRET))
|
||||
|
||||
sca = scapi.Scope()
|
||||
assert isinstance(sca.me(), scapi.User)
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue