Ejemplo n.º 1
0
    def test_responseful(self):
        request = {
            'uri': mox.Func(self.saver('uri')),
            'method': 'POST',
            'headers': mox.Func(self.saver('headers')),
            'body': mox.Func(self.saver('body')),
        }
        response = {
            'status': 200,
            'content-type': 'application/json',
        }
        response_content = '{"blahdeblah": true, "anotherthing": "2010-07-06T16:17:05Z"}'

        http = typepad.TypePadClient()
        typepad.client = http
        http.add_credentials(
            OAuthConsumer('consumertoken', 'consumersecret'),
            OAuthToken('tokentoken', 'tokensecret'),
            domain='api.typepad.com',
        )

        mock = mox.Mox()
        mock.StubOutWithMock(http, 'request')
        http.request(**request).AndReturn((httplib2.Response(response), response_content))
        mock.ReplayAll()

        class Moose(typepad.TypePadObject):

            class Snert(typepad.TypePadObject):
                volume = typepad.fields.Field()
                target = typepad.fields.Object('User')
            class SnertResponse(typepad.TypePadObject):
                blahdeblah = typepad.fields.Field()
                anotherthing = typepad.fields.Datetime()
            snert = typepad.fields.ActionEndpoint(api_name='snert', post_type=Snert, response_type=SnertResponse)

        moose = Moose()
        moose._location = 'https://api.typepad.com/meese/7.json'

        ret = moose.snert(volume=10, target=typepad.User(display_name='fred'))
        self.assert_(ret is not None)
        self.assert_(isinstance(ret, Moose.SnertResponse))

        mock.VerifyAll()

        self.assert_(self.uri)
        self.assertEquals(self.uri, 'https://api.typepad.com/meese/7/snert.json')
        self.assert_(self.headers)
        self.assert_(self.body)

        self.assert_(utils.json_equals({
            'volume': 10,
            'target': {
                'displayName': 'fred',
                'objectType': 'User',
            },
        }, self.body))
Ejemplo n.º 2
0
    def test_responseless(self):
        request = {
            'uri': mox.Func(self.saver('uri')),
            'method': 'POST',
            'headers': mox.Func(self.saver('headers')),
            'body': mox.Func(self.saver('body')),
        }
        response = {
            'status': 204,  # no content
        }

        http = typepad.TypePadClient()
        typepad.client = http
        http.add_credentials(
            OAuthConsumer('consumertoken', 'consumersecret'),
            OAuthToken('tokentoken', 'tokensecret'),
            domain='api.typepad.com',
        )

        mock = mox.Mox()
        mock.StubOutWithMock(http, 'request')
        http.request(**request).AndReturn((httplib2.Response(response), ''))
        mock.ReplayAll()

        class Moose(typepad.TypePadObject):

            class Snert(typepad.TypePadObject):
                volume = typepad.fields.Field()
            snert = typepad.fields.ActionEndpoint(api_name='snert', post_type=Snert)

        moose = Moose()
        moose._location = 'https://api.typepad.com/meese/7.json'

        ret = moose.snert(volume=10)
        self.assert_(ret is None)

        mock.VerifyAll()

        self.assert_(self.uri)
        self.assertEquals(self.uri, 'https://api.typepad.com/meese/7/snert.json')
        self.assert_(self.headers)
        self.assert_(self.body)

        self.assert_(utils.json_equals({
            'volume': 10
        }, self.body))
Ejemplo n.º 3
0
    def test_basic(self):
        request = {
            'uri': mox.Func(self.saver('uri')),
            'method': 'POST',
            'headers': mox.Func(self.saver('headers')),
            'body': mox.Func(self.saver('body')),
        }
        response = {
            'status': 201,  # created
        }

        http = typepad.TypePadClient()
        typepad.client = http
        http.add_credentials(
            OAuthConsumer('consumertoken', 'consumersecret'),
            OAuthToken('tokentoken', 'tokensecret'),
            domain='api.typepad.com',
        )

        mock = mox.Mox()
        mock.StubOutWithMock(http, 'request')
        http.request(**request).AndReturn((response, ''))
        mock.ReplayAll()

        asset = typepad.Photo()
        asset.title = "Fake photo"
        asset.content = "This is a made-up photo for testing automated browser style upload."

        fileobj = StringIO('hi hello pretend file')
        brupload = typepad.BrowserUploadEndpoint()
        brupload.upload(asset, fileobj, "image/png",
            post_type='photo')

        mock.VerifyAll()

        self.assert_(self.uri)
        # We added credentials, so it should be a secure URL.
        self.assert_(self.uri.startswith('https://api.typepad.com/browser-upload.json'))
        uriparts = list(urlparse(self.uri))
        querydict = cgi.parse_qs(uriparts[4])

        # TODO: really verify the signature

        # Verify the headers and body.
        self.assert_(self.headers)
        self.assert_(self.body)
        responsemsg = self.message_from_response(self.headers, self.body)

        content_type = responsemsg.get_content_type()
        self.assert_(content_type)
        self.assert_(not responsemsg.defects)

        # Check that the unparsed body has its internal mime headers
        # separated by full CRLFs, not just LFs.
        self.assert_('\r\nContent-Type:' in self.body)
        # Check that boundaries are separated by full CRLFs too.
        boundary = responsemsg.get_param('boundary')
        self.assert_(boundary + '\r\n' in self.body)

        # Make sure we're only putting credentials in the query string, not
        # the headers.
        self.assert_('oauth_signature' in querydict)
        self.assert_('authorization' not in responsemsg)
        self.assert_('Authorization' not in responsemsg)

        bodyparts = responsemsg.get_payload()
        self.assertEquals(len(bodyparts), 3)
        bodyparts = dict((part.get_param('name', header='content-disposition'),
            part) for part in bodyparts)

        self.assertEquals(bodyparts['post_type'].get_payload(), 'photo')
        self.assert_('redirect_to' not in bodyparts)

        asset_json = bodyparts['asset'].get_payload()
        self.assert_(utils.json_equals({
            'title': 'Fake photo',
            'content': 'This is a made-up photo for testing automated browser style upload.',
            'objectType': 'Photo',
        }, asset_json))

        filepart = bodyparts['file']
        self.assertEquals(filepart.get_payload(decode=False), 'hi hello pretend file')
        filelength = filepart.get('content-length')
        self.assertEquals(int(filelength), len('hi hello pretend file'))
        filename = filepart.get_param('filename', header='content-disposition')
        self.assert_(filename)
Ejemplo n.º 4
0
 def confirm_equals_data(text):
     return utils.json_equals(data, text)
Ejemplo n.º 5
0
 def confirm_equals_data(text):
     return utils.json_equals(data, text)