def testAddDomain(self): header, body = parseResponse( asBytes( self.dna.all.handleRequest( user=CallTrace(returnValues=dict(isAdmin=False)), path="/actions/addDomain", Body=bytes(urlencode(dict(identifier="aap")), encoding="utf-8"), Method='Post'))) self.assertEqual(0, len(self.observable.calledMethods)) self.assertEqual("200", header['StatusCode']) self.assertEqual("application/json", header['Headers']['Content-Type']) response = JsonDict.loads(body) self.assertFalse(response['success']) self.assertEqual("Not allowed", response['message']) header, body = parseResponse( asBytes( self.dna.all.handleRequest( user=CallTrace(returnValues=dict(isAdmin=True)), path="/actions/addDomain", Body=bytes(urlencode(dict(identifier="aap")), encoding="utf-8"), Method='Post'))) self.assertEqual("200", header['StatusCode']) self.assertEqual("application/json", header['Headers']['Content-Type']) response = JsonDict.loads(body) self.assertTrue(response['success']) self.assertEqual(1, len(self.observable.calledMethods)) self.assertEqual("addDomain", self.observable.calledMethods[0].name) self.assertEqual(dict(identifier='aap'), self.observable.calledMethods[0].kwargs)
def testETag(self): fm = StaticFileMapping(mapping={"/x": join(self.tempdir, "x")}) headers, body = parseResponse( asBytes(fm.handleRequest(path="/x", Headers={}))) self.assertTrue("Etag" in headers['Headers'], headers) etag = headers['Headers']['Etag'] headers, body = parseResponse( asBytes(fm.handleRequest(path="/x", Headers={'etag': etag}))) self.assertEqual('304', headers['StatusCode'])
def testUpdateRepositoryGroup(self): header, body = parseResponse( asBytes( self.dna.all.handleRequest( user=CallTrace(returnValues=dict(isAdmin=True)), Method='POST', path='/somewhere/updateRepositoryGroup', Body=bUrlencode(dict( identifier='group', domainId='domain', nl_name="De nieuwe naam", en_name="The old name", ), doseq=True)))) self.assertEqual('200', header['StatusCode']) self.assertEqual(dict(success=True), JsonDict.loads(body)) self.assertEqual(1, len(self.observable.calledMethods)) self.assertEqual('updateRepositoryGroup', self.observable.calledMethods[0].name) self.assertEqual( { 'identifier': 'group', 'domainId': 'domain', 'name': { 'nl': 'De nieuwe naam', 'en': 'The old name' } }, self.observable.calledMethods[0].kwargs)
def _request(self, from_=None, path=None, xcount=None, validate=True, **arguments): httpMethod = getattr(self, 'httpMethod', 'GET') if from_: arguments['from'] = from_ if xcount: arguments['x-count'] = xcount RequestURI = 'http://example.org/oai' queryString = urlencode(arguments, doseq=True) if httpMethod == 'GET': RequestURI += '?' + queryString Body = None else: Body = bytes(queryString, encoding="utf-8") arguments = {} header, body = parseResponse(asBytes(compose(self.root.all.handleRequest( RequestURI=RequestURI, Headers={}, Body=Body, Client=('127.0.0.1', 1324), Method=httpMethod, port=9000, arguments=arguments, path='/oai' if path is None else path, )))) parsedBody = XML(body) if validate: assertValidOai(parsedBody) return header, parsedBody
def testEtagUpdated(self): reactor = Reactor() fm = StaticFileMapping(mapping={"/x": join(self.tempdir, "x")}, reactor=reactor) headers, body = parseResponse( asBytes(fm.handleRequest(path="/x", Headers={}))) self.assertTrue("Etag" in headers['Headers'], headers) etag = headers['Headers']['Etag'] with open(join(self.tempdir, "x"), "w") as fp: fp.write("x" * 200) reactor.step() headers, body = parseResponse( asBytes(fm.handleRequest(path="/x", Headers={}))) self.assertTrue("Etag" in headers['Headers'], headers) self.assertNotEqual(headers['Headers']['Etag'], etag)
def testUpdateRepositoryActionForm_Action(self): header, body = parseResponse( asBytes( self.dna.all.handleRequest( user=CallTrace(returnValues=dict(isAdmin=True)), Method='POST', path='/actions/updateRepositoryActionAttributes', Body=bUrlencode(dict( identifier='repo-id', domainId='domain-id', action="-", ), doseq=True)))) self.assertEqual('200', header['StatusCode']) self.assertEqual(dict(success=True), JsonDict.loads(body)) self.assertEqual(1, len(self.observable.calledMethods)) self.assertEqual('updateRepositoryAttributes', self.observable.calledMethods[0].name) self.assertEqual( { 'complete': False, 'continuous': None, 'domainId': 'domain-id', 'identifier': 'repo-id', 'maximumIgnore': 0, 'action': None, 'use': False }, self.observable.calledMethods[0].kwargs)
def testRetrieveCookie(self): sessions = [] class MyObserver(Observable): def handleRequest(self, *args, **kwargs): session = self.ctx.session sessions.append(session) yield utils.okHtml + '<html/>' self.handler.addObserver(MyObserver()) headers, _ = parseResponse( asBytes( self.handler.handleRequest(RequestURI='/path', Client=('127.0.0.1', 12345), Headers={}))) headers = headers['Headers'] self.assertTrue('Set-Cookie' in headers, headers) cookie = findCookies(headers, self.cookiestore.cookieName(), 'Set-Cookie')[0] consume( self.handler.handleRequest(RequestURI='/path', Client=('127.0.0.1', 12345), Headers={ 'Cookie': '{0}={1}'.format( self.cookiestore.cookieName(), cookie) })) self.assertEqual(sessions[0], sessions[1]) self.assertEqual(id(sessions[0]), id(sessions[1]))
def testGet(self): fm = StaticFileMapping(mapping={"/x": join(self.tempdir, "x")}) headers, body = parseResponse( asBytes(fm.handleRequest(path="/x", Headers={}))) self.assertEqual('200', headers['StatusCode']) self.assertEqual(100 * b"x", body)
def testAddClosingHours(self): header, body = parseResponse( asBytes( self.dna.all.handleRequest( user=CallTrace(returnValues=dict(isAdmin=True)), Method='POST', path='/actions/addRepositoryClosingHours', Body=bUrlencode(dict(repositoryId='repo-id', domainId='domain-id', week="*", day="1", startHour="10", endHour="14"), doseq=True)))) self.assertEqual('200', header['StatusCode']) self.assertEqual(dict(success=True), JsonDict.loads(body)) self.assertEqual(1, len(self.observable.calledMethods)) self.assertEqual('addClosingHours', self.observable.calledMethods[0].name) self.assertEqual( { 'day': '1', 'domainId': 'domain-id', 'endHour': '14', 'identifier': 'repo-id', 'startHour': '10', 'week': '*' }, self.observable.calledMethods[0].kwargs)
def testUpdateRepositoryFieldDefinition(self): header, body = parseResponse( asBytes( self.dna.all.handleRequest( user=CallTrace(returnValues=dict(isAdmin=True)), Method='POST', path='/actions/updateRepositoryFieldDefinitions', Body=bUrlencode(dict( identifier='repo-id', domainId='domain-id', extra_name="Herman in de zon op een terras", extra_no_such_field="Bestaat niet"), doseq=True)))) self.assertEqual('200', header['StatusCode']) self.assertEqual(dict(success=True), JsonDict.loads(body)) self.assertEqual(1, len(self.observable.calledMethods)) self.assertEqual('updateRepositoryFieldDefinitions', self.observable.calledMethods[0].name) self.assertEqual( { 'identifier': 'repo-id', 'domainId': 'domain-id', 'extra_no_such_field': 'Bestaat niet', 'extra_name': "Herman in de zon op een terras" }, self.observable.calledMethods[0].kwargs)
def do(self, pathPart, dataDict): header, body = parseResponse(asBytes(self.actions.handleRequest( user=self.user, path='/some/path/' + pathPart, Body=bytes(urlencode(dataDict), encoding='utf-8'), Headers={'Content-Type': 'application/x-www-form-urlencoded'}, Method='Post', key='value'))) self.assertEqual('200', header['StatusCode']) return body
def testUpdateFieldDefinition_error(self): header, body = parseResponse( asBytes( self.dna.all.handleRequest( user=CallTrace(returnValues=dict(isAdmin=True)), Method='POST', path='/actions/updateFieldDefinition', Body=bUrlencode(dict( domainId='domain-id', fieldDefinition='{"is no json"}', ), doseq=True)))) self.assertEqual('200', header['StatusCode']) self.assertEqual(dict(success=False, message='Ongeldige JSON'), JsonDict.loads(body)) self.assertEqual(0, len(self.observable.calledMethods))
def testInjectAnyCookie(self): sessions = [] class MyObserver(Observable): def handleRequest(self, session=None, *args, **kwargs): sessions.append(session) yield utils.okHtml + '<html/>' self.handler.addObserver(MyObserver()) headers, _ = parseResponse( asBytes( self.handler.handleRequest( RequestURI='/path', Client=('127.0.0.1', 12345), Headers={ 'Cookie': '%s=%s' % (self.cookiestore.cookieName(), 'injected_id') }))) headers = headers["Headers"] self.assertTrue('injected_id' not in ' '.join(headers['Set-Cookie']))
def testDeleteClosingHours(self): header, body = parseResponse( asBytes( self.dna.all.handleRequest( user=CallTrace(returnValues=dict(isAdmin=True)), Method='POST', path='/actions/deleteRepositoryClosingHours', Body=bUrlencode(dict(repositoryId='repo-id', domainId='domain-id', closingHour="0"), doseq=True)))) self.assertEqual('200', header['StatusCode']) self.assertEqual(dict(success=True), JsonDict.loads(body)) self.assertEqual(1, len(self.observable.calledMethods)) self.assertEqual('deleteClosingHours', self.observable.calledMethods[0].name) self.assertEqual( { 'domainId': 'domain-id', 'identifier': 'repo-id', 'closingHoursIndex': '0' }, self.observable.calledMethods[0].kwargs)
def testUpdateFieldDefinition(self): header, body = parseResponse( asBytes( self.dna.all.handleRequest( user=CallTrace(returnValues=dict(isAdmin=True)), Method='POST', path='/actions/updateFieldDefinition', Body=bUrlencode(dict( domainId='domain-id', fieldDefinition='{"is":"json"}', ), doseq=True)))) self.assertEqual('200', header['StatusCode']) self.assertEqual(dict(success=True), JsonDict.loads(body)) self.assertEqual(1, len(self.observable.calledMethods)) self.assertEqual('updateFieldDefinition', self.observable.calledMethods[0].name) self.assertEqual({ 'domainId': 'domain-id', 'data': { 'is': 'json' }, }, self.observable.calledMethods[0].kwargs)
def testCreateRepository(self): header, body = parseResponse( asBytes( self.dna.all.handleRequest( user=CallTrace(returnValues=dict(isAdmin=True)), Method='POST', path='/actions/addRepository', Body=bUrlencode(dict( identifier='repo-id', domainId='domain-id', repositoryGroupId='repositoryGroupId', ), doseq=True)))) self.assertEqual('200', header['StatusCode']) self.assertEqual(dict(success=True), JsonDict.loads(body)) self.assertEqual(1, len(self.observable.calledMethods)) self.assertEqual('addRepository', self.observable.calledMethods[0].name) self.assertEqual( { 'domainId': 'domain-id', 'identifier': 'repo-id', 'repositoryGroupId': 'repositoryGroupId' }, self.observable.calledMethods[0].kwargs)
def testUpdateRepositoryAttributes(self): header, body = parseResponse( asBytes( self.dna.all.handleRequest( user=CallTrace(returnValues=dict(isAdmin=True)), Method='POST', path='/actions/updateRepositoryAttributes', Body=bUrlencode(dict( identifier='repo-id', domainId='domain-id', userAgent="Herman in de zon op een terras", ), doseq=True)))) self.assertEqual('200', header['StatusCode']) self.assertEqual(dict(success=True), JsonDict.loads(body)) self.assertEqual(1, len(self.observable.calledMethods)) self.assertEqual('updateRepositoryAttributes', self.observable.calledMethods[0].name) self.assertEqual( { 'identifier': 'repo-id', 'domainId': 'domain-id', 'baseurl': None, 'set': None, 'metadataPrefix': None, 'userAgent': 'Herman in de zon op een terras', 'collection': None, 'authorizationKey': None, 'mappingId': None, 'targetId': None }, self.observable.calledMethods[0].kwargs) header, body = parseResponse( asBytes( self.dna.all.handleRequest( user=CallTrace(returnValues=dict(isAdmin=True)), Method='POST', path='/actions/updateRepositoryAttributes', Body=bUrlencode(dict( identifier='repo-id', domainId='domain-id', userAgent="", ), doseq=True)))) self.assertEqual('200', header['StatusCode']) self.assertEqual(dict(success=True), JsonDict.loads(body)) self.assertEqual(2, len(self.observable.calledMethods)) self.assertEqual('updateRepositoryAttributes', self.observable.calledMethods[1].name) self.assertEqual( { 'identifier': 'repo-id', 'domainId': 'domain-id', 'baseurl': None, 'set': None, 'metadataPrefix': None, 'userAgent': None, 'collection': None, 'authorizationKey': None, 'mappingId': None, 'targetId': None }, self.observable.calledMethods[1].kwargs)