def test_fetch_friends(self): response = http.Response(httplib.OK, simplejson.dumps({ 'startIndex': 0, 'totalResults': 3, 'entry': [ { 'id': '102', 'name': {'givenName': 'Stan', 'familyName': 'Marsh'}, }, { 'id': '103', 'name': {'givenName': 'Kyle', 'familyName': 'Broflovski'}, }, { 'id': '104', 'name': {'givenName': 'Eric', 'familyName': 'Cartman'}, } ] })) self.urlfetch.add_response(response) friends = self.container.fetch_friends('@me') request = self.urlfetch.get_request() self.assertEqual(0, friends.startIndex) self.assertEqual(3, friends.totalResults) self.assertEqual('102', friends[0].get_id()) self.assertEqual('Stan Marsh', friends[0].get_display_name()) self.assertEqual('103', friends[1].get_id()) self.assertEqual('Kyle Broflovski', friends[1].get_display_name()) self.assertEqual('104', friends[2].get_id()) self.assertEqual('Eric Cartman', friends[2].get_display_name()) self.assertEqual('http://www.foo.com/rest/people/@me/@friends', request.get_normalized_url())
def test_fetch_friends(self): response = http.Response( httplib.OK, simplejson.dumps( { "startIndex": 0, "totalResults": 3, "entry": [ {"id": "102", "name": {"givenName": "Stan", "familyName": "Marsh"}}, {"id": "103", "name": {"givenName": "Kyle", "familyName": "Broflovski"}}, {"id": "104", "name": {"givenName": "Eric", "familyName": "Cartman"}}, ], } ), ) self.urlfetch.add_response(response) friends = self.container.fetch_friends("@me") request = self.urlfetch.get_request() self.assertEqual(0, friends.startIndex) self.assertEqual(3, friends.totalResults) self.assertEqual("102", friends[0].get_id()) self.assertEqual("Stan Marsh", friends[0].get_display_name()) self.assertEqual("103", friends[1].get_id()) self.assertEqual("Kyle Broflovski", friends[1].get_display_name()) self.assertEqual("104", friends[2].get_id()) self.assertEqual("Eric Cartman", friends[2].get_display_name()) self.assertEqual("http://www.foo.com/rest/people/@me/@friends", request.get_normalized_url())
def test_unauthorized_request(self): response = http.Response(httplib.OK, simplejson.dumps({"error": {"code": httplib.UNAUTHORIZED}})) self.urlfetch.add_response(response) self.assertRaises(UnauthorizedRequestError, self.container.fetch_friends, "102") request = self.urlfetch.get_request() self.assertEqual("102", request.get_parameter("xoauth_requestor_id")) self.assertEqual("http://www.foo.com/rest/people/102/@friends", request.get_normalized_url())
def test_body_hash(self): post_body = "I am a post body" post_body_json = simplejson.dumps(post_body) post_body_hash = b64encode(hashlib.sha1(post_body_json).digest()) post_body_param = simplejson.dumps(post_body) request = http.Request("http://example.com", "POST", post_body=post_body) request.set_body_as_signing_parameter(False) request.sign_request(self.consumer, self.signature_method) self.assertEquals(post_body_hash, request.get_parameter('oauth_body_hash')) headers = request.get_headers() self.assertEquals("application/json", headers['Content-Type']) try: request.get_parameter(post_body_param) except oauth.OAuthError: return self.fail()
def post(self): oauth_version = self.request.get('oauth_version') oauth_nonce = self.request.get('oauth_nonce') oauth_timestamp = self.request.get('oauth_timestamp') oauth_consumer_key = self.request.get('oauth_consumer_key') oauth_signature = self.request.get('oauth_signature') xoauth_requestor_id = self.request.get('xoauth_requestor_id') opensocial_method = self.request.get('opensocial_method') post_body = self.request.body post_data = simplejson.loads(post_body)[0] id = post_data.get('id') params = {} for key, value in self.request.params.mixed().items(): params[key] = value.encode('utf-8', 'ignore') oauth_request = oauth.OAuthRequest.from_request(self.request.method, self.request.url, params) consumer = oauth.OAuthConsumer('oauth.org:123456789', 'not_a_secret') signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1() built_signature = signature_method.build_signature(oauth_request, consumer, None) verified = built_signature == oauth_signature data = { 'request': { 'method': self.request.method, 'url': self.request.url, 'oauth_version': oauth_version, 'oauth_nonce': oauth_nonce, 'oauth_timestamp': oauth_timestamp, 'oauth_consumer_key': oauth_consumer_key, 'oauth_signature': oauth_signature, 'post_body': post_body }, 'response': { 'built_signature': built_signature }, 'verified': str(verified), } json = { 'id': id, 'data': data } output = simplejson.dumps([json]) if verified: logging.info(output) else: logging.error(output) self.response.out.write(output)
def test_fetch_person(self): response = http.Response( httplib.OK, simplejson.dumps({"entry": {"id": "101", "name": {"givenName": "Kenny", "familyName": "McCormick"}}}), ) self.urlfetch.add_response(response) person = self.container.fetch_person("@me") request = self.urlfetch.get_request() self.assertEqual("101", person.get_id()) self.assertEqual("Kenny McCormick", person.get_display_name()) self.assertEqual("http://www.foo.com/rest/people/@me/@self", request.get_normalized_url())
def test_unauthorized_request(self): response = http.Response(httplib.OK, simplejson.dumps({ 'error' : { 'code': httplib.UNAUTHORIZED } })) self.urlfetch.add_response(response) self.assertRaises(UnauthorizedRequestError, self.container.fetch_friends, '102') request = self.urlfetch.get_request() self.assertEqual('102', request.get_parameter('xoauth_requestor_id')) self.assertEqual('http://www.foo.com/rest/people/102/@friends', request.get_normalized_url())
def test_fetch_person(self): response = http.Response(httplib.OK, simplejson.dumps({ 'entry': { 'id' : '101', 'name' : { 'givenName': 'Kenny', 'familyName': 'McCormick'} }, })) self.urlfetch.add_response(response) person = self.container.fetch_person('@me') request = self.urlfetch.get_request() self.assertEqual('101', person.get_id()) self.assertEqual('Kenny McCormick', person.get_display_name()) self.assertEqual('http://www.foo.com/rest/people/@me/@self', request.get_normalized_url())
def test_body_as_signing_param(self): post_body = "I am a post body" post_body_param = simplejson.dumps(post_body) post_body_param_quoted = urllib.quote(post_body_param) request = http.Request("http://example.com", "POST", post_body=post_body) request.set_body_as_signing_parameter(True) request.sign_request(self.consumer, self.signature_method) param_location = request.get_url().find(post_body_param_quoted) self.assertTrue(param_location > -1) self.assertEquals("", request.get_parameter(post_body_param)) try: request.get_parameter('oauth_body_hash') except oauth.OAuthError: return self.fail()
class TestContainerContext(unittest.TestCase): viewer_response = http.Response(httplib.OK, simplejson.dumps(test_data.VIEWER_FIELDS)) friends_response = http.Response( httplib.OK, simplejson.dumps(test_data.FRIEND_COLLECTION_FIELDS)) noauth_response = http.Response(httplib.OK, simplejson.dumps(test_data.NO_AUTH)) def add_canned_response(self, request_url, http_response, requestor_id=None): http_request = http.Request(request_url) if requestor_id: http_request.set_parameter('xoauth_requestor_id', requestor_id) http_request.set_parameter('opensocial_method', 'GET') self.urlfetch.add_response(http_request, http_response) def setUp(self): self.urlfetch = mock_http.MockUrlFetch() self.container = ContainerContext(TEST_CONFIG, self.urlfetch) self.add_canned_response('http://www.foo.com/rest/people/@me/@self', TestContainerContext.viewer_response) self.add_canned_response('http://www.foo.com/rest/people/@me/@friends', TestContainerContext.friends_response) self.add_canned_response('http://www.foo.com/rest/people/102/@friends', TestContainerContext.noauth_response, requestor_id='102') self.add_canned_response('http://www.foo.com/rest/people/103/@friends', http.Response(httplib.NOT_FOUND, 'Error'), requestor_id='103') def test_supports_rpc(self): self.assertEqual(False, self.container.supports_rpc()) def test_fetch_person(self): person = self.container.fetch_person('@me') self.assertEqual(test_data.VIEWER.get_id(), person.get_id()) self.assertEqual(test_data.VIEWER.get_display_name(), person.get_display_name()) def test_fetch_friends(self): friends = self.container.fetch_friends('@me') self.assertEqual(friends.startIndex, test_data.FRIENDS.startIndex) self.assertEqual(friends.totalResults, test_data.FRIENDS.totalResults) for i in range(len(friends)): person = friends[i] test_person = test_data.FRIENDS[i] self.assertEqual(person.get_id(), test_person.get_id()) self.assertEqual(person.get_display_name(), test_person.get_display_name()) def test_unauthorized_request(self): self.assertRaises(UnauthorizedRequestError, self.container.fetch_friends, '102') def test_bad_request(self): self.assertRaises(BadRequestError, self.container.fetch_friends, '103') self.assertRaises(BadRequestError, self.container.fetch_friends, '??')
class TestContainerContext(unittest.TestCase): friends_response = http.Response(httplib.OK, simplejson.dumps( test_data.FRIEND_COLLECTION_FIELDS)) noauth_response = http.Response(httplib.OK, simplejson.dumps(test_data.NO_AUTH)) def setUp(self): self.urlfetch = mock_http.MockUrlFetch() self.container = ContainerContext(TEST_CONFIG, self.urlfetch) def test_supports_rpc(self): self.assertEqual(False, self.container.supports_rpc()) def test_fetch_person(self): response = http.Response(httplib.OK, simplejson.dumps({ 'entry': { 'id' : '101', 'name' : { 'givenName': 'Kenny', 'familyName': 'McCormick'} }, })) self.urlfetch.add_response(response) person = self.container.fetch_person('@me') request = self.urlfetch.get_request() self.assertEqual('101', person.get_id()) self.assertEqual('Kenny McCormick', person.get_display_name()) self.assertEqual('http://www.foo.com/rest/people/@me/@self', request.get_normalized_url()) def test_fetch_friends(self): response = http.Response(httplib.OK, simplejson.dumps({ 'startIndex': 0, 'totalResults': 3, 'entry': [ { 'id': '102', 'name': {'givenName': 'Stan', 'familyName': 'Marsh'}, }, { 'id': '103', 'name': {'givenName': 'Kyle', 'familyName': 'Broflovski'}, }, { 'id': '104', 'name': {'givenName': 'Eric', 'familyName': 'Cartman'}, } ] })) self.urlfetch.add_response(response) friends = self.container.fetch_friends('@me') request = self.urlfetch.get_request() self.assertEqual(0, friends.startIndex) self.assertEqual(3, friends.totalResults) self.assertEqual('102', friends[0].get_id()) self.assertEqual('Stan Marsh', friends[0].get_display_name()) self.assertEqual('103', friends[1].get_id()) self.assertEqual('Kyle Broflovski', friends[1].get_display_name()) self.assertEqual('104', friends[2].get_id()) self.assertEqual('Eric Cartman', friends[2].get_display_name()) self.assertEqual('http://www.foo.com/rest/people/@me/@friends', request.get_normalized_url()) def test_unauthorized_request(self): response = http.Response(httplib.OK, simplejson.dumps({ 'error' : { 'code': httplib.UNAUTHORIZED } })) self.urlfetch.add_response(response) self.assertRaises(UnauthorizedRequestError, self.container.fetch_friends, '102') request = self.urlfetch.get_request() self.assertEqual('102', request.get_parameter('xoauth_requestor_id')) self.assertEqual('http://www.foo.com/rest/people/102/@friends', request.get_normalized_url()) def test_bad_request(self): response = http.Response(httplib.NOT_FOUND, 'Error'); self.urlfetch.add_response(response) self.assertRaises(BadRequestError, self.container.fetch_friends, '103') request = self.urlfetch.get_request() self.assertEqual('103', request.get_parameter('xoauth_requestor_id')) self.assertEqual('http://www.foo.com/rest/people/103/@friends', request.get_normalized_url())