Esempio n. 1
0
class TestMixcloud(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        httpretty.enable()

    def setUp(self):
        self.client_id = 'qwerty'
        self.client_secret = 'ytrewq'
        self.redirect_uri = 'http://localhost/mixcloud-callback'
        self.o = mixcloud.MixcloudOauth(
            client_id=self.client_id, client_secret=self.client_secret,
            redirect_uri=self.redirect_uri)
        self.m = mixcloud.Mixcloud()
        httpretty.reset()
        self.mc = MockServer()

    def testArtist(self):
        self.mc.register_artist(afx)
        resp = self.m.artist('aphex-twin')
        self.assertEqual(resp.name, 'Aphex Twin')

    def testUser(self):
        self.mc.register_user(spartacus)
        resp = self.m.user('spartacus')
        self.assertEqual(resp.name, 'Spartacus')

    def testCloudcast(self):
        self.mc.register_cloudcast(spartacus, partytime)
        resp = self.m.user('spartacus')
        cc = resp.cloudcast('party-time')
        self.assertEqual(cc.name, 'Party Time')
        secs = cc.sections()
        self.assertEqual(len(secs), 9)
        sec = secs[1]
        self.assertEqual(sec.start_time, 416)
        self.assertEqual(sec.track.name, 'Refresher')
        self.assertEqual(sec.track.artist.name, 'Time of your life')
        self.assertEqual(cc.tags, ['Funky house', 'Funk', 'Soul'])
        self.assertEqual(cc.description(), 'Bla bla')
        self.assertTrue(cc.picture().startswith('http'))
        self.assertEqual(cc.user.key, 'spartacus')
        self.assertEqual(cc.user.name, 'Spartacus')

    def testCloudcasts(self):
        self.mc.register_cloudcast(spartacus, partytime)
        resp = self.m.user('spartacus')
        ccs = resp.cloudcasts()
        self.assertEqual(len(ccs), 1)
        cc = ccs[0]
        self.assertEqual(cc.name, 'Party Time')

    def testLogin(self):
        self.mc.i_am(spartacus)
        user = self.m.me()
        self.assertEqual(user.key, spartacus.key)
        self.assertEqual(user.name, spartacus.name)

    def testUpload(self):
        self.mc.i_am(spartacus)

        def upload_callback(request, uri, headers):
            data = parse_multipart(request.body)
            self.assertIn('mp3', data)
            self.assertIn('picture', data)
            name = data['name']
            key = mixcloud.slugify(name)
            sections, tags = parse_headers(data)
            description = data['description']
            me = self.m.me()
            created_time = datetime.datetime.now()
            cc = mixcloud.Cloudcast(key, name, sections, tags,
                                    description, me, created_time)
            self.mc.register_cloudcast(me, cc)
            return (200, headers, '{}')

        self.mc.handle_upload(upload_callback)
        mp3file = io.StringIO(u'\x00' * 30)
        picturefile = io.StringIO(u'\x00' * 30)
        r = self.m.upload(partytime, mp3file, picturefile=picturefile)
        self.assertEqual(r.status_code, 200)
        me = self.m.me()
        cc = me.cloudcast('party-time')
        self.assertEqual(cc.name, 'Party Time')
        secs = cc.sections()
        self.assertEqual(len(secs), 9)
        sec = secs[3]
        self.assertEqual(sec.start_time, 1061)
        self.assertEqual(sec.track.name, 'Definition of House')
        self.assertEqual(sec.track.artist.name, 'Minimal Funk')
        self.assertEqual(cc.tags, ['Funky house', 'Funk', 'Soul'])
        self.assertEqual(cc.description(), 'Bla bla')

    def testCloudcastsSection(self):
        self.mc.register_cloudcast(spartacus, partytime)
        u = self.m.user('spartacus')
        ccs = u.cloudcasts()
        cc = ccs[0]
        secs = cc.sections()
        self.assertEqual(secs[7].track.name, 'Dancin')

    def testCloudcastsDescription(self):
        self.mc.register_cloudcast(spartacus, partytime)
        u = self.m.user('spartacus')
        ccs = u.cloudcasts()
        cc = ccs[0]
        self.assertEqual(cc.description(), 'Bla bla')

    def testLimit(self):
        self.mc.register_cloudcasts(spartacus, [partytime, lambiance])
        u = self.m.user('spartacus')
        ccs = u.cloudcasts()
        self.assertEqual(len(ccs), 2)
        ccs = u.cloudcasts(limit=1)
        self.assertEqual(len(ccs), 1)
        self.assertEqual(ccs[0].key, 'party-time')
        ccs = u.cloudcasts(offset=1)
        self.assertEqual(len(ccs), 1)
        self.assertEqual(ccs[0].key, 'lambiance')

    def testYaml(self):
        self.mc.i_am(spartacus)
        self.mc.mock_upload(self.m.me())
        mp3file = io.StringIO(u'\x00' * 30)
        with open('example.yml') as f:
            self.m.upload_yml_file(f, mp3file)
        u = self.m.user('spartacus')
        ccs = u.cloudcasts()
        self.assertEqual(len(ccs), 1)
        cc = ccs[0]
        sections = cc.sections()
        self.assertEqual(len(sections), 16)
        section = sections[6]
        self.assertEqual(section.start_time, 688)
        self.assertEqual(section.track.artist.name, 'Menelik & No Se')
        self.assertEqual(section.track.name, 'Quelle aventure')
        tags = ['Sample chain', 'Samples', 'Hip hop', 'Pop']
        self.assertEqual(cc.tags, tags)
        self.assertIn("In this mix we jump", cc.description())

    def testOauthUrl(self):
        full_url = self.o.authorize_url()
        # Check URL without parameters.
        self.assertEqual(
            full_url.split('?')[0], mixcloud.OAUTH_ROOT + '/authorize')
        # Check query string parameters.
        u = urlsplit(full_url)
        params = parse_qs(u.query)
        self.assertEqual(params['client_id'][0], self.client_id)
        self.assertEqual(params['redirect_uri'][0], self.redirect_uri)

    def testOauthExchange(self):
        self.mc.oauth_exchange()
        access_token = self.o.exchange_token('my_code')
        self.assertEqual(access_token, 'my_access_token')

    def testOauthExchangeServerFail(self):
        self.mc.oauth_exchange_fail()
        with self.assertRaises(mixcloud.MixcloudOauthError):
            self.o.exchange_token('my_code')

    @mock.patch('netrc.netrc.authenticators')
    @mock.patch('netrc.netrc.__init__')
    def testNetrc(self, netrc_init, netrc_authenticators):
        netrc_init.return_value = None  # Don't blow up when netrc missing.
        netrc_authenticators.return_value = ('', None, 'my_access_token')

        m = mixcloud.Mixcloud()
        self.assertEqual(m.access_token, 'my_access_token')
Esempio n. 2
0
class TestMixcloud(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        httpretty.enable()

    def setUp(self):
        self.m = mixcloud.Mixcloud()
        httpretty.reset()
        self.mc = MockServer()

    def testArtist(self):
        self.mc.register_artist(afx)
        resp = self.m.artist('aphex-twin')
        self.assertEqual(resp.name, 'Aphex Twin')

    def testUser(self):
        self.mc.register_user(spartacus)
        resp = self.m.user('spartacus')
        self.assertEqual(resp.name, 'Spartacus')

    def testCloudcast(self):
        self.mc.register_cloudcast(spartacus, partytime)
        resp = self.m.user('spartacus')
        cc = resp.cloudcast('party-time')
        self.assertEqual(cc.name, 'Party Time')
        secs = cc.sections()
        self.assertEqual(len(secs), 9)
        sec = secs[1]
        self.assertEqual(sec.start_time, 416)
        self.assertEqual(sec.track.name, 'Refresher')
        self.assertEqual(sec.track.artist.name, 'Time of your life')
        self.assertEqual(cc.tags, ['Funky house', 'Funk', 'Soul'])
        self.assertEqual(cc.description(), 'Bla bla')
        self.assertTrue(cc.picture().startswith('http'))
        self.assertEqual(cc.user.key, 'spartacus')
        self.assertEqual(cc.user.name, 'Spartacus')

    def testCloudcasts(self):
        self.mc.register_cloudcast(spartacus, partytime)
        resp = self.m.user('spartacus')
        ccs = resp.cloudcasts()
        self.assertEqual(len(ccs), 1)
        cc = ccs[0]
        self.assertEqual(cc.name, 'Party Time')

    def testLogin(self):
        self.mc.i_am(spartacus)
        user = self.m.me()
        self.assertEqual(user.key, spartacus.key)
        self.assertEqual(user.name, spartacus.name)

    def testUpload(self):
        self.mc.i_am(spartacus)

        def upload_callback(request, uri, headers):
            data = parse_multipart(request.body)
            self.assertIn('mp3', data)
            self.assertIn('picture', data)
            name = data['name']
            key = mixcloud.slugify(name)
            sections, tags = parse_headers(data)
            description = data['description']
            me = self.m.me()
            created_time = datetime.datetime.now()
            cc = mixcloud.Cloudcast(key, name, sections, tags,
                                    description, me, created_time)
            self.mc.register_cloudcast(me, cc)
            return (200, headers, '{}')

        self.mc.handle_upload(upload_callback)
        mp3file = io.StringIO(u'\x00' * 30)
        picturefile = io.StringIO(u'\x00' * 30)
        r = self.m.upload(partytime, mp3file, picturefile=picturefile)
        self.assertEqual(r.status_code, 200)
        me = self.m.me()
        cc = me.cloudcast('party-time')
        self.assertEqual(cc.name, 'Party Time')
        secs = cc.sections()
        self.assertEqual(len(secs), 9)
        sec = secs[3]
        self.assertEqual(sec.start_time, 1061)
        self.assertEqual(sec.track.name, 'Definition of House')
        self.assertEqual(sec.track.artist.name, 'Minimal Funk')
        self.assertEqual(cc.tags, ['Funky house', 'Funk', 'Soul'])
        self.assertEqual(cc.description(), 'Bla bla')

    def testCloudcastsSection(self):
        self.mc.register_cloudcast(spartacus, partytime)
        u = self.m.user('spartacus')
        ccs = u.cloudcasts()
        cc = ccs[0]
        secs = cc.sections()
        self.assertEqual(secs[7].track.name, 'Dancin')

    def testCloudcastsDescription(self):
        self.mc.register_cloudcast(spartacus, partytime)
        u = self.m.user('spartacus')
        ccs = u.cloudcasts()
        cc = ccs[0]
        self.assertEqual(cc.description(), 'Bla bla')

    def testLimit(self):
        self.mc.register_cloudcasts(spartacus, [partytime, lambiance])
        u = self.m.user('spartacus')
        ccs = u.cloudcasts()
        self.assertEqual(len(ccs), 2)
        ccs = u.cloudcasts(limit=1)
        self.assertEqual(len(ccs), 1)
        self.assertEqual(ccs[0].key, 'party-time')
        ccs = u.cloudcasts(offset=1)
        self.assertEqual(len(ccs), 1)
        self.assertEqual(ccs[0].key, 'lambiance')

    def testYaml(self):
        self.mc.i_am(spartacus)
        self.mc.mock_upload(self.m.me())
        mp3file = io.StringIO(u'\x00' * 30)
        with open('example.yml') as f:
            self.m.upload_yml_file(f, mp3file)
        u = self.m.user('spartacus')
        ccs = u.cloudcasts()
        self.assertEqual(len(ccs), 1)
        cc = ccs[0]
        sections = cc.sections()
        self.assertEqual(len(sections), 16)
        section = sections[6]
        self.assertEqual(section.start_time, 688)
        self.assertEqual(section.track.artist.name, 'Menelik & No Se')
        self.assertEqual(section.track.name, 'Quelle aventure')
        tags = ['Sample chain', 'Samples', 'Hip hop', 'Pop']
        self.assertEqual(cc.tags, tags)
        self.assertIn("In this mix we jump", cc.description())
Esempio n. 3
0
class TestMixcloud(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        httpretty.enable()

    def setUp(self):
        self.client_id = 'qwerty'
        self.client_secret = 'ytrewq'
        self.redirect_uri = 'http://localhost/mixcloud-callback'
        self.o = mixcloud.MixcloudOauth(client_id=self.client_id,
                                        client_secret=self.client_secret,
                                        redirect_uri=self.redirect_uri)
        self.m = mixcloud.Mixcloud()
        httpretty.reset()
        self.mc = MockServer()

    def testArtist(self):
        self.mc.register_artist(afx)
        resp = self.m.artist('aphex-twin')
        self.assertEqual(resp.name, 'Aphex Twin')

    def testUser(self):
        self.mc.register_user(spartacus)
        resp = self.m.user('spartacus')
        self.assertEqual(resp.name, 'Spartacus')

    def testCloudcast(self):
        self.mc.register_cloudcast(spartacus, partytime)
        resp = self.m.user('spartacus')
        cc = resp.cloudcast('party-time')
        self.assertEqual(cc.name, 'Party Time')
        secs = cc.sections()
        self.assertEqual(len(secs), 9)
        sec = secs[1]
        self.assertEqual(sec.start_time, 416)
        self.assertEqual(sec.track.name, 'Refresher')
        self.assertEqual(sec.track.artist.name, 'Time of your life')
        self.assertEqual(cc.tags, ['Funky house', 'Funk', 'Soul'])
        self.assertEqual(cc.description(), 'Bla bla')
        self.assertTrue(cc.picture().startswith('http'))
        self.assertEqual(cc.user.key, 'spartacus')
        self.assertEqual(cc.user.name, 'Spartacus')

    def testCloudcasts(self):
        self.mc.register_cloudcast(spartacus, partytime)
        resp = self.m.user('spartacus')
        ccs = resp.cloudcasts()
        self.assertEqual(len(ccs), 1)
        cc = ccs[0]
        self.assertEqual(cc.name, 'Party Time')

    def testLogin(self):
        self.mc.i_am(spartacus)
        user = self.m.me()
        self.assertEqual(user.key, spartacus.key)
        self.assertEqual(user.name, spartacus.name)

    def testUpload(self):
        self.mc.i_am(spartacus)

        def upload_callback(request, uri, headers):
            data = parse_multipart(request.body)
            self.assertIn('mp3', data)
            self.assertIn('picture', data)
            name = data['name']
            key = mixcloud.slugify(name)
            sections, tags = parse_headers(data)
            description = data['description']
            me = self.m.me()
            created_time = datetime.datetime.now()
            cc = mixcloud.Cloudcast(key, name, sections, tags, description, me,
                                    created_time)
            self.mc.register_cloudcast(me, cc)
            return (200, headers, '{}')

        self.mc.handle_upload(upload_callback)
        mp3file = io.StringIO(u'\x00' * 30)
        picturefile = io.StringIO(u'\x00' * 30)
        r = self.m.upload(partytime, mp3file, picturefile=picturefile)
        self.assertEqual(r.status_code, 200)
        me = self.m.me()
        cc = me.cloudcast('party-time')
        self.assertEqual(cc.name, 'Party Time')
        secs = cc.sections()
        self.assertEqual(len(secs), 9)
        sec = secs[3]
        self.assertEqual(sec.start_time, 1061)
        self.assertEqual(sec.track.name, 'Definition of House')
        self.assertEqual(sec.track.artist.name, 'Minimal Funk')
        self.assertEqual(cc.tags, ['Funky house', 'Funk', 'Soul'])
        self.assertEqual(cc.description(), 'Bla bla')

    def testCloudcastsSection(self):
        self.mc.register_cloudcast(spartacus, partytime)
        u = self.m.user('spartacus')
        ccs = u.cloudcasts()
        cc = ccs[0]
        secs = cc.sections()
        self.assertEqual(secs[7].track.name, 'Dancin')

    def testCloudcastsDescription(self):
        self.mc.register_cloudcast(spartacus, partytime)
        u = self.m.user('spartacus')
        ccs = u.cloudcasts()
        cc = ccs[0]
        self.assertEqual(cc.description(), 'Bla bla')

    def testLimit(self):
        self.mc.register_cloudcasts(spartacus, [partytime, lambiance])
        u = self.m.user('spartacus')
        ccs = u.cloudcasts()
        self.assertEqual(len(ccs), 2)
        ccs = u.cloudcasts(limit=1)
        self.assertEqual(len(ccs), 1)
        self.assertEqual(ccs[0].key, 'party-time')
        ccs = u.cloudcasts(offset=1)
        self.assertEqual(len(ccs), 1)
        self.assertEqual(ccs[0].key, 'lambiance')

    def testYaml(self):
        self.mc.i_am(spartacus)
        self.mc.mock_upload(self.m.me())
        mp3file = io.StringIO(u'\x00' * 30)
        with open('example.yml') as f:
            self.m.upload_yml_file(f, mp3file)
        u = self.m.user('spartacus')
        ccs = u.cloudcasts()
        self.assertEqual(len(ccs), 1)
        cc = ccs[0]
        sections = cc.sections()
        self.assertEqual(len(sections), 16)
        section = sections[6]
        self.assertEqual(section.start_time, 688)
        self.assertEqual(section.track.artist.name, 'Menelik & No Se')
        self.assertEqual(section.track.name, 'Quelle aventure')
        tags = ['Sample chain', 'Samples', 'Hip hop', 'Pop']
        self.assertEqual(cc.tags, tags)
        self.assertIn("In this mix we jump", cc.description())

    def testOauthUrl(self):
        full_url = self.o.authorize_url()
        # Check URL without parameters.
        self.assertEqual(
            full_url.split('?')[0], mixcloud.OAUTH_ROOT + '/authorize')
        # Check query string parameters.
        u = urlsplit(full_url)
        params = parse_qs(u.query)
        self.assertEqual(params['client_id'][0], self.client_id)
        self.assertEqual(params['redirect_uri'][0], self.redirect_uri)

    def testOauthExchange(self):
        self.mc.oauth_exchange()
        access_token = self.o.exchange_token('my_code')
        self.assertEqual(access_token, 'my_access_token')

    def testOauthExchangeServerFail(self):
        self.mc.oauth_exchange_fail()
        with self.assertRaises(mixcloud.MixcloudOauthError):
            self.o.exchange_token('my_code')

    @mock.patch('netrc.netrc.authenticators')
    @mock.patch('netrc.netrc.__init__')
    def testNetrc(self, netrc_init, netrc_authenticators):
        netrc_init.return_value = None  # Don't blow up when netrc missing.
        netrc_authenticators.return_value = ('', None, 'my_access_token')

        m = mixcloud.Mixcloud()
        self.assertEqual(m.access_token, 'my_access_token')