def testAllBadActions(self): headers = { 'accept': 'application/json', } self.addBasicAuthHeader(headers) for category, path in ((defaults.namespaceCategoryName, [ defaults.namespaceCategoryName, defaults.adminUsername ]), (defaults.tagCategoryName, [defaults.tagCategoryName] + paths.aboutPath()), (defaults.tagInstanceSetCategoryName, [defaults.tagInstanceSetCategoryName] + paths.aboutPath())): path = defaults.sep.join(path) allowedActions = permissions.actionsByCategory[category] for action in list(permissions.allActions) + ['unknown-action']: if action not in allowedActions: # We provide 'create' to maintain backwards compatibility. # In order to make it work here we break lots of other # things. This is the path of least resistance. -jkakar if (path == 'tag-values/fluiddb/about' and action == 'create'): continue d = self.getPage(path, headers=headers, queryDict={'action': action}) d.addErrback(self.checkErrorStatus, http.BAD_REQUEST) self.failUnlessFailure(d, Error) yield d
def testAllBadActions(self): headers = { 'accept': 'application/json', } self.addBasicAuthHeader(headers) for category, path in ( (defaults.namespaceCategoryName, [defaults.namespaceCategoryName, defaults.adminUsername]), (defaults.tagCategoryName, [defaults.tagCategoryName] + paths.aboutPath()), (defaults.tagInstanceSetCategoryName, [defaults.tagInstanceSetCategoryName] + paths.aboutPath())): path = defaults.sep.join(path) allowedActions = permissions.actionsByCategory[category] for action in list(permissions.allActions) + ['unknown-action']: if action not in allowedActions: # We provide 'create' to maintain backwards compatibility. # In order to make it work here we break lots of other # things. This is the path of least resistance. -jkakar if (path == 'tag-values/fluiddb/about' and action == 'create'): continue d = self.getPage(path, headers=headers, queryDict={'action': action}) d.addErrback(self.checkErrorStatus, http.BAD_REQUEST) self.failUnlessFailure(d, Error) yield d
def testSimpleAboutJSONP(self): basicAuth = 'Basic %s' % b64encode('%s:%s' % ('testuser1', 'secret')) headers = {'accept': 'application/json', 'authorization': basicAuth} uri = '%s/%s' % (self.endpoint, defaults.httpObjectCategoryName) aboutStr = 'random string' data = {'about': aboutStr} callback = 'foo' payload = json.dumps(data) params = { 'callback': callback, 'verb': 'POST', 'payload': payload, 'payload-type': 'application/json', 'payload-length': len(payload), } uriCB = uri + '?' + urlencode(params) status, responseHeaders, responseCB = yield getPage(uriCB, headers=headers) m = re.match('%s\((.*)\)' % callback, responseCB) self.assertNotEqual(None, m) # XXX JSON hardcoded d = json.loads(m.group(1)) # XXX hardcoded id field oid = str(d['id']) uriValue = defaults.sep.join( [uri, oid, defaults.sep.join(paths.aboutPath())]) status, responseHeaders, responseValue = yield getPage(uriValue) # XXX JSON hardcoded self.assertEqual(aboutStr, json.loads(responseValue))
def testAnonUserDeletesAbout(self): path = defaults.sep.join(paths.aboutPath()) d = self.deleteTag(path, requesterUsername=defaults.anonUsername, requesterPassword=defaults.anonPassword) d.addErrback(self.checkErrorStatus, http.UNAUTHORIZED) self.failUnlessFailure(d, Error) return d
def testAnonUserDeletesAbout(self): path = defaults.sep.join(paths.aboutPath()) d = self.deleteTag(path, requesterUsername=defaults.anonUsername, requesterPassword=defaults.anonPassword) d.addErrback(self.checkErrorStatus, http.UNAUTHORIZED) self.failUnlessFailure(d, Error) return d
def testNonType4ObjectId(self): aboutPath = defaults.sep.join(paths.aboutPath()) d = self.getTagValue(aboutPath, base.nonType4ObjectIdStr()) d.addErrback(self.checkErrorStatus, http.NOT_FOUND) d.addErrback( self.checkErrorHeaders, {buildHeader('Error-Class'): TNoInstanceOnObject.__name__}) self.failUnlessFailure(d, Error) return d
def testRidiculousObjectId(self): objectId = 'hey!' d = self.deleteTagValue(defaults.sep.join(paths.aboutPath()), objectId) d.addErrback(self.checkErrorStatus, http.NOT_FOUND) d.addErrback( self.checkErrorHeaders, {buildHeader('Error-Class'): error.NoSuchResource.__name__}) self.failUnlessFailure(d, Error) return d
def testQueryUnicodeAbout(self): aboutStr = u'\xf8' objectId = yield self.createObject(about=aboutStr) results = yield self.query( '%s = "%s"' % (defaults.sep.join(paths.aboutPath()), aboutStr)) self.assertEqual(len(results), 1) self.assertEqual(objectId, results[0]) objectInfo = yield self.getObject(str(objectId), showAbout=True) self.assertEqual(objectInfo['about'], aboutStr)
def testAdminNewTagOnObjectWithAboutThenGET(self): path = 'fluiddb/testing/test1' aboutPath = defaults.sep.join(paths.aboutPath()) # An object with an about. objectId = yield self.createObject('hello') value = '5' try: yield self.setTagValue(path, objectId, value) # Check the value is there. result = yield self.getTagValue(path, objectId) self.assertEqual(value, result) # Make sure the path is now in the tagPaths. objectInfo = yield self.getObject(str(objectId)) aboutPath = defaults.sep.join(paths.aboutPath()) self.assertIn(path, objectInfo['tagPaths']) self.assertIn(aboutPath, objectInfo['tagPaths']) finally: result = yield self.deleteTagValue(path, objectId)
def testNoAcceptHeader(self): headers = {} self.addBasicAuthHeader(headers) q = '%s = "xxx"' % defaults.sep.join(paths.aboutPath()) d = getPage('%s/%s?query=%s' % (self.endpoint, defaults.httpObjectCategoryName, urllib.quote_plus(q)), headers=headers, method='GET') d.addCallback(self.checkHeaders, {'content-type': 'application/json'}) return d
def testAnonUserCannotGETAdminNamespaceOrAbout(self): headers = { 'accept': 'application/json', } # Test the admin namespace path = defaults.sep.join( [defaults.namespaceCategoryName, defaults.adminUsername]) for action in \ permissions.actionsByCategory[defaults.namespaceCategoryName]: d = self.getPage(path, headers=headers, queryDict={'action': action}) d.addErrback(self.checkErrorStatus, http.UNAUTHORIZED) self.failUnlessFailure(d, Error) yield d # Test the about tag. path = defaults.sep.join([defaults.tagCategoryName] + paths.aboutPath()) for action in permissions.actionsByCategory[defaults.tagCategoryName]: d = self.getPage(path, headers=headers, queryDict={'action': action}) d.addErrback(self.checkErrorStatus, http.UNAUTHORIZED) self.failUnlessFailure(d, Error) yield d # Test the about tag instances. path = defaults.sep.join([defaults.tagInstanceSetCategoryName] + paths.aboutPath()) for action in (permissions.actionsByCategory[ defaults.tagInstanceSetCategoryName]): d = self.getPage(path, headers=headers, queryDict={'action': action}) d.addErrback(self.checkErrorStatus, http.UNAUTHORIZED) self.failUnlessFailure(d, Error) yield d
def testAnonUserCannotGETAdminNamespaceOrAbout(self): headers = { 'accept': 'application/json', } # Test the admin namespace path = defaults.sep.join([defaults.namespaceCategoryName, defaults.adminUsername]) for action in \ permissions.actionsByCategory[defaults.namespaceCategoryName]: d = self.getPage(path, headers=headers, queryDict={'action': action}) d.addErrback(self.checkErrorStatus, http.UNAUTHORIZED) self.failUnlessFailure(d, Error) yield d # Test the about tag. path = defaults.sep.join([defaults.tagCategoryName] + paths.aboutPath()) for action in permissions.actionsByCategory[ defaults.tagCategoryName]: d = self.getPage(path, headers=headers, queryDict={'action': action}) d.addErrback(self.checkErrorStatus, http.UNAUTHORIZED) self.failUnlessFailure(d, Error) yield d # Test the about tag instances. path = defaults.sep.join([defaults.tagInstanceSetCategoryName] + paths.aboutPath()) for action in (permissions.actionsByCategory[ defaults.tagInstanceSetCategoryName]): d = self.getPage(path, headers=headers, queryDict={'action': action}) d.addErrback(self.checkErrorStatus, http.UNAUTHORIZED) self.failUnlessFailure(d, Error) yield d
def testAboutTag(self): headers = { 'accept': 'application/json', } self.addBasicAuthHeader(headers) path = defaults.sep.join([defaults.tagCategoryName] + paths.aboutPath()) for action in permissions.actionsByCategory[ defaults.tagCategoryName]: d = self.getPage(path, headers=headers, queryDict={'action': action}) d.addCallback(self.checkStatus, http.OK) d.addCallback(self.checkPayloadHas, { 'policy': permissions.CLOSED, 'exceptions': []}) yield d
def testAboutTag(self): headers = { 'accept': 'application/json', } self.addBasicAuthHeader(headers) path = defaults.sep.join([defaults.tagCategoryName] + paths.aboutPath()) for action in permissions.actionsByCategory[defaults.tagCategoryName]: d = self.getPage(path, headers=headers, queryDict={'action': action}) d.addCallback(self.checkStatus, http.OK) d.addCallback(self.checkPayloadHas, { 'policy': permissions.CLOSED, 'exceptions': [] }) yield d
def testAboutTagInstances(self): # The about tag has an open policy for READ, and closed policies # for all else. openPolicyActions = (permissions.READ,) headers = { 'accept': 'application/json', } self.addBasicAuthHeader(headers) path = defaults.sep.join([defaults.tagInstanceSetCategoryName] + paths.aboutPath()) for action in permissions.actionsByCategory[ defaults.tagInstanceSetCategoryName]: d = self.getPage(path, headers=headers, queryDict={'action': action}) d.addCallback(self.checkStatus, http.OK) d.addCallback(self.checkPayloadHas, { 'policy': (permissions.OPEN if action in openPolicyActions else permissions.CLOSED), 'exceptions': []}) yield d
def testAboutTagInstances(self): # The about tag has an open policy for READ, and closed policies # for all else. openPolicyActions = (permissions.READ, ) headers = { 'accept': 'application/json', } self.addBasicAuthHeader(headers) path = defaults.sep.join([defaults.tagInstanceSetCategoryName] + paths.aboutPath()) for action in permissions.actionsByCategory[ defaults.tagInstanceSetCategoryName]: d = self.getPage(path, headers=headers, queryDict={'action': action}) d.addCallback(self.checkStatus, http.OK) d.addCallback( self.checkPayloadHas, { 'policy': (permissions.OPEN if action in openPolicyActions else permissions.CLOSED), 'exceptions': [] }) yield d
def testNonType4ObjectId(self): aboutPath = defaults.sep.join(paths.aboutPath()) exists = yield self.hasTagValue(aboutPath, base.nonType4ObjectIdStr()) self.assertFalse(exists)
def testTagNotPresent(self): objectId = yield self.createObject() yield self.deleteTagValue(defaults.sep.join(paths.aboutPath()), objectId)
def testNonExistentObjectId(self): objectId = base.randomObjectIdStr() return self.deleteTagValue(defaults.sep.join(paths.aboutPath()), objectId)
def testNonExistentObjectId(self): aboutPath = defaults.sep.join(paths.aboutPath()) objectId = base.randomObjectIdStr() exists = yield self.hasTagValue(aboutPath, objectId) self.assertFalse(exists)
from fluiddb.common.defaults import sep, contentTypeForPrimitiveJSON from fluiddb.common import paths from fluiddb.common.types_thrift.ttypes import ( TObjectInfo, TNoInstanceOnObject) from fluiddb.web.query import createThriftValue, guessValue from fluiddb.web import objects from fluiddb.web.about import ( AboutResource, AboutObjectResource, AboutTagInstanceResource) from fluiddb.web.resource import ErrorResource from fluiddb.web.test.test_objects import FakeFacade as SimpleFakeFacade from fluiddb.testing.doubles import FakeSession from fluiddb.web.util import buildHeader from fluiddb.testing.basic import FluidinfoTestCase _aboutPath = sep.join(paths.aboutPath()) class FakeRequest(object): """ I pretend to be an HTTP request, with a handful of required methods used by Twisted resource classes (e.g., write, finish, setResponseCode). """ _fluidDB_reqid = None body = '' def __init__(self, method, d=None, headers=None, hostname=None): """ Initialize. d (if not None) is a deferred that will fire with the
def testRidiculousObjectId(self): aboutPath = defaults.sep.join(paths.aboutPath()) exists = yield self.hasTagValue(aboutPath, 'dummy') self.assertFalse(exists)
from fluiddb.common import error, util from fluiddb.common.defaults import sep, contentTypeForPrimitiveJSON from fluiddb.common import paths from fluiddb.common.types_thrift.ttypes import (TObjectInfo, TNoInstanceOnObject) from fluiddb.web.query import createThriftValue, guessValue from fluiddb.web import objects from fluiddb.web.about import (AboutResource, AboutObjectResource, AboutTagInstanceResource) from fluiddb.web.resource import ErrorResource from fluiddb.web.test.test_objects import FakeFacade as SimpleFakeFacade from fluiddb.testing.doubles import FakeSession from fluiddb.web.util import buildHeader from fluiddb.testing.basic import FluidinfoTestCase _aboutPath = sep.join(paths.aboutPath()) class FakeRequest(object): """ I pretend to be an HTTP request, with a handful of required methods used by Twisted resource classes (e.g., write, finish, setResponseCode). """ _fluidDB_reqid = None body = '' def __init__(self, method, d=None, headers=None, hostname=None): """ Initialize. d (if not None) is a deferred that will fire with the
NOT_FOUND = httpCode('NOT_FOUND') PRECONDITION_FAILED = httpCode('PRECONDITION_FAILED') REQUEST_ENTITY_TOO_LARGE = httpCode('REQUEST_ENTITY_TOO_LARGE') NOT_ACCEPTABLE = httpCode('NOT_ACCEPTABLE') UNSUPPORTED_MEDIA_TYPE = httpCode('UNSUPPORTED_MEDIA_TYPE') LIST = spanWrap('perm', 'LIST') CREATE = spanWrap('perm', 'CREATE') READ = spanWrap('perm', 'READ') UPDATE = spanWrap('perm', 'UPDATE') DELETE = spanWrap('perm', 'DELETE') CONTROL = spanWrap('perm', 'CONTROL') ADMIN_ATTR = spanWrap('tag', sep.join(paths.usernamePath())) ABOUT = spanWrap('tag', aboutTagName) ABOUT_TAG = spanWrap('tag', sep.join(paths.aboutPath())) LOCATION = spanWrap('httpHeader', 'Location') ACCEPT = spanWrap('httpHeader', 'Accept') CONTENT_ENCODING = spanWrap('httpHeader', 'Content-Encoding') CONTENT_TYPE = spanWrap('httpHeader', 'Content-type') ABOUTSTR = spanWrap('var', 'aboutstr') ID = spanWrap('var', 'id') NS1 = spanWrap('var', 'namespace1') NS2 = spanWrap('var', 'namespace2') TAG = spanWrap('var', 'tag') USERNAME = spanWrap('var', 'username') PROFILE = spanWrap('var', 'profile') CATEGORY = spanWrap('var', 'category') ACTION = spanWrap('var', 'action') NS_NS = '/' + NS1 + '/' + NS2 NS_NS_TAG = NS_NS + '/' + TAG