def testCreateKey(self):
        session = {
            'user': User('admin'),
        }
        Body = urlencode(dict(username='******', description="A User", formUrl='/apikeyform'))
        self.assertEquals(['admin'], self.pwd.listUsernames())

        result = joco(self.apiKey.handleRequest(session=session, Body=Body, path='/action/create', Method='POST'))
        headers, body = result.split(CRLF*2)

        self.assertTrue(' 302 ' in headers, headers)
        self.assertEquals('/apikeyform', parseHeaders(headers)['Location'])
        self.assertEquals(['admin', 'user'], sorted(self.pwd.listUsernames()))

        aList = self.apiKey.listApiKeysAndData()
        self.assertEquals(1, len(aList))
        apiKey, userdata = aList[0]
        self.assertEquals('user', userdata['username'])
        self.assertTrue(16, len(apiKey))

        result = joco(self.apiKey.handleRequest(session=session, Body=Body, path='/action/create', Method='POST'))
        headers, body = result.split(CRLF*2)

        self.assertTrue(' 302 ' in headers, headers)
        self.assertEquals('/apikeyform', parseHeaders(headers)['Location'])
        self.assertEquals(['admin', 'user'], sorted(self.pwd.listUsernames()))
        self.assertEquals({'message': {'class': 'error', 'text': 'User already exists.'}}, session['ApiKey.formValues'])

        b = ApiKey(databaseFile=join(self.tempdir, 'db'))
        self.assertEquals(aList, list(b.listApiKeysAndData()))
    def testChangeDescription(self):
        session = {
            'user': User('admin'),
        }
        Body = urlencode(dict(username='******', formUrl='/apikeyform'))

        result = joco(self.apiKey.handleRequest(session=session, Body=Body, path='/action/create', Method='POST'))
        headers, body = result.split(CRLF*2)
        self.assertEquals(['admin', 'user'], sorted(self.pwd.listUsernames()))

        aList = self.apiKey.listApiKeysAndData()
        apiKey = aList[0][0]

        result = joco(self.apiKey.handleRequest(
            session=session, 
            Body=urlencode(dict(
                apiKey=apiKey, 
                formUrl="/apikeyform", 
                description="This is the description")), 
            path="/action/update", 
            Method="POST"))

        aList = self.apiKey.listApiKeysAndData()
        self.assertEquals(1, len(aList))
        apiKey, data = aList[0]
        self.assertEquals('This is the description', data['description'])
 def testIdentifier(self):
     self.observer.returnValues['isAvailable'] = (True, True)
     self.observer.returnValues['getStream'] = StringIO("<xml/>")
     header, body = joco(self.dna.all.handleRequest(arguments={'identifier': ['an:identifier']})).split('\r\n'*2, 1)
     self.assertTrue('200' in header, header)
     self.assertEquals(['isAvailable', 'getStream', 'add'], [m.name for m in self.observer.calledMethods])
     kwargs = self.observer.calledMethods[2].kwargs
     self.assertEquals('an:identifier', kwargs['identifier'])
     self.assertEquals('rdf', kwargs['partname'])
     self.assertTrue('lxmlNode' in kwargs)
    def testCreateLoggedIn(self):
        session = {
            'user': User('admin'),
        }
        Body = urlencode(dict(repository='repoId1', formUrl='/harvester_dashboard?repository=%(repository)s'))
        result = joco(self.dashboard.handleRequest(path="/create", Client=('127.0.0.1', 1234), Method='POST', session=session, Body=Body))
        header, body = result.split(CRLF*2)
        self.assertTrue('302' in header)
        self.assertTrue('Location: /harvester_dashboard?repository=repoId1' in header, header)

        self.assertEquals("repoId1", list(self.env.getRepositories())[0].name)
    def testDeleteRepository(self):
        session = dict(user=User('admin'))
        
        joco(self.dashboard.handleRequest(
            path="/create", 
            Client=('127.0.0.1', 1234), 
            Method='POST', 
            session=session, 
            Body=urlencode(dict(repository='repoId1', formUrl='/harvester_dashboard'))))
        self.assertEquals(1, len(list(self.env.getRepositories())))
        result = joco(self.dashboard.handleRequest(
            path="/delete", 
            Client=('127.0.0.1', 1234), 
            Method='POST', 
            session=session, 
            Body=urlencode(dict(repository='repoId1', formUrl='/harvester_dashboard'))))
        self.assertEquals(0, len(list(self.env.getRepositories())))
        header, body = result.split(CRLF*2)

        self.assertTrue('302' in header)
        self.assertTrue('Location: /harvester_dashboard' in header, header)
    def testGetForApiKey(self):
        self.assertEquals(None, self.apiKey.getForApiKey('nonexistent'))

        result = joco(self.apiKey.handleRequest(
            session={ 'user': User('admin')},
            Body=urlencode(dict(username='******', formUrl='/apikeyform')), 
            path='/action/create', 
            Method='POST'))
        [(apiKey, userdata)] = self.apiKey.listApiKeysAndData()

        dataByApiKey = self.apiKey.getForApiKey(apiKey)
        self.assertEquals(userdata, dataByApiKey)
    def testWithoutAdminUserLoggedIn(self):
        session = {
            'user': User('nobody'),
        }
        Body = urlencode(dict(username='******', formUrl='/apikeyform'))

        result = joco(self.apiKey.handleRequest(session=session, Body=Body, path='/action/create', Method='POST'))
        headers, body = result.split(CRLF*2)

        self.assertTrue(' 302 ' in headers, headers)
        self.assertEquals('/apikeyform', parseHeaders(headers)['Location'])
        self.assertEquals([], list(self.apiKey.listApiKeysAndData()))
        self.assertEquals({'message': {'text': 'No admin privileges.', 'class': 'error'}}, session['ApiKey.formValues'])
    def testEditRepository(self):
        session = dict(user=User('admin'))
        
        joco(self.dashboard.handleRequest(
            path="/create", 
            Client=('127.0.0.1', 1234), 
            Method='POST', 
            session=session, 
            Body=urlencode(dict(repository='repoId1', formUrl='/harvester_dashboard'))))

        self.assertEquals("", list(self.env.getRepositories())[0].baseUrl)
        self.assertEquals("", list(self.env.getRepositories())[0].metadataPrefix)
        self.assertEquals("", list(self.env.getRepositories())[0].setSpec)
        self.assertEquals("", list(self.env.getRepositories())[0].apiKey)
        self.assertEquals(False, list(self.env.getRepositories())[0].active)
        joco(self.dashboard.handleRequest(
            path="/something/update", 
            Client=('127.0.0.1', 1234), 
            Method='POST', 
            session=session, 
            Body=urlencode(dict(
                repository='repoId1', 
                baseUrl='http://localhost/oai',
                metadataPrefix='aprefix',
                setSpec='aset',
                apiKey='an api key',
                active='on',
                formUrl='/harvester_dashboard'))))
        self.assertEquals("http://localhost/oai", list(self.env.getRepositories())[0].baseUrl)
        self.assertEquals("aprefix", list(self.env.getRepositories())[0].metadataPrefix)
        self.assertEquals("aset", list(self.env.getRepositories())[0].setSpec)
        self.assertEquals("an api key", list(self.env.getRepositories())[0].apiKey)
        self.assertEquals(True, list(self.env.getRepositories())[0].active)

        joco(self.dashboard.handleRequest(
            path="/something/update", 
            Client=('127.0.0.1', 1234), 
            Method='POST', 
            session=session, 
            Body=urlencode(dict(
                repository='repoId1', 
                baseUrl='http://localhost/oai',
                metadataPrefix='aprefix',
                setSpec='aset',
                apiKey='an api key',
                formUrl='/harvester_dashboard'))))
        self.assertEquals(False, list(self.env.getRepositories())[0].active)
 def testCreateNotLoggedIn(self):
     result = joco(self.dashboard.handleRequest(path="/create", Client=('127.0.0.1', 1234), Method='POST', session={}))
     header, body = result.split(CRLF*2)
     self.assertTrue('302' in header)
     self.assertTrue('Location: /' in header, header)
 def testRedirectOnGet(self):
     result = joco(self.dashboard.handleRequest(path='/whatever', Client=('127.0.0.1', 3451), Method='GET', session={}))
     header, body = result.split(CRLF*2)
     self.assertTrue('302' in header)
     self.assertTrue('Location: /' in header, header)
 def testIdentifierNotFound(self):
     self.observer.returnValues['isAvailable'] = (False, False)
     header, body = joco(self.dna.all.handleRequest(arguments={'identifier': ['an:identifier']})).split('\r\n'*2, 1)
     self.assertTrue('500' in header, header)
     self.assertEquals(1, len(self.observer.calledMethods))
     self.assertEquals(['isAvailable'], [m.name for m in self.observer.calledMethods])
 def testEmptyIdentifier(self):
     header, body = joco(self.dna.all.handleRequest(arguments={'identifier': ['']})).split('\r\n'*2, 1)
     self.assertTrue('500' in header, header)
     self.assertEquals(0, len(self.observer.calledMethods))