Beispiel #1
0
    def test_client_shutdown(self):
        data = doRequest(self.jslink, data=[{
            'type': 'link',
            'args' : {'test':'foo'},
            'id' : 42,
            'name' : 'linkMe',
            'clientId' : self.clientId,
            }])
        self.sync()
        assert DB.find_one(self.ctx.database.links,
                           {'client': ObjectId(self.clientId), 'link': 42})
        assert DB.find_one(self.ctx.database.clients,
                           {'_id': ObjectId(self.clientId)})

        answer = doRequest(self.jslink, data=[{
                        'type': 'client_deactivate',
                        'clientId' : self.clientId,
                        }])
        assert answer == {'client_deactivate': self.clientId}
        self.sync()

        assert not DB.find_one(self.ctx.database.links,
                               {'client': ObjectId(self.clientId), 'link': 42})
        assert not DB.find_one(self.ctx.database.clients,
                               {'_id': ObjectId(self.clientId)})
Beispiel #2
0
 def uptodate(self):
     self.sync()
     link = mongo.find_one(self.database.links, {
         'client': self.clientId,
         'link': self.linkId
     })
     return link and not link.get('outdatedBy')
Beispiel #3
0
 def _getParams(self):
     self.sync()
     link = mongo.find_one(self.database.links, {
         'client': self.clientId,
         'link': self.linkId
     })
     if link:
         return link['params']
Beispiel #4
0
 def _getState(self):
     self.sync()
     link = mongo.find_one(self.database.links, {
         'client': self.clientId,
         'link': self.linkId
     })
     if link:
         return link['state']
Beispiel #5
0
 def test_commit_with_interest_error(self):
     commitId = self._commit(interested=[1, 2],
                             result=[None],
                             error='error')
     commit = mongo.find_one(self.database.commits, {'_id': commitId})
     assert commit['handled_by'] == commit['interested'] == [1, 2]
     assert commit['state'] == 'done'
     assert commit['results'] == [None]
     assert commit['error'] == 'error'
Beispiel #6
0
    def test_remove(self):
        link = self.factory.create('Persistent',
                                   self.clientId,
                                   self.linkId,
                                   params={'apa': 'bepa'})
        link.run()
        self.sync()

        # sanity checks
        assert mongo.find_one(self.database.links, {'link': self.linkId})
        client = mongo.find_one(self.database.clients, {'_id': self.clientId})

        link = self.factory.create('Persistent', self.clientId, self.linkId)
        link.remove()
        self.sync()

        assert not mongo.find_one(self.database.links, {'link': self.linkId})
        client = mongo.find_one(self.database.clients, {'_id': self.clientId})
Beispiel #7
0
 def _getLink(self):
     if self._link is Link._link:
         self._link = mongo.find_one(self.database.links,
                                     {'client': self.clientId,
                                      'link': self.linkId,
                                      'type': self.__class__.__name__})
     if not self._link:
         self._link = {'_id': ObjectId()}
     return self._link
Beispiel #8
0
 def test_runCommit_error_with_interest(self):
     op = commit.CallBlm('testcommit', 'broken', [])
     cctx = self.newcontext()
     interested = ObjectId()
     cmt = cctx.runCommit([op], interested=interested)
     assert cmt.error
     self.sync()
     stored = mongo.find_one(self.database.commits, {'_id': cmt._id})
     assert type(cmt.error) == type(stored['error'])
     assert cmt.error.args == stored['error'].args
Beispiel #9
0
    def create(self, _name, _clientid, _linkid, **kw):
        if _name:
            return getattr(self, _name)(_clientid, _linkid, **kw)

        doc = mongo.find_one(self._links, {'client': _clientid,
                                           'link': _linkid})
        if doc:
            link = globals()[doc['type']](_clientid, _linkid, **kw)
            link.link = doc
            return link
Beispiel #10
0
    def test_grant(self, monkeypatch):
        monkeypatch.setattr(time, 'time', self.time)
        toid = ObjectId()
        data = doRequest(self.jslink, data=[{
                        'type': 'grant',
                        'clientId': self.clientId,
                        'toid': str(toid)}])
        self.sync()

        assert len(data) == 1
        auth = data['auth']

        assert DB.find_one(self.ctx.database.grants, {'auth': auth, 'toid': toid,
                                                      'timestamp': 1})
Beispiel #11
0
    def test_commit_blobval_reference_handling(self):
        commitId = ObjectId()
        bv1 = BlobVal('foo')
        bv2 = BlobVal('bar')
        bv3 = BlobVal('baz')
        bv1.large_blob = bv2.large_blob = bv3.large_blob = 2
        bv1.addref(commitId)
        bv3.addref(commitId)

        op = commit.CallToi(ObjectId(), 'foo', [[bv1, bv3]])
        result = [[bv2, bv3, 'baz']]
        commitId = self._commit(interested=[1, 2],
                                _id=commitId,
                                operations=[op],
                                result=result)
        cmt = mongo.find_one(self.database.commits, {'_id': commitId})

        py.test.raises(gridfs.NoFile,
                       bv1.gridfs(self.database).get, bv1.value._id)
        assert cmt['results'][0][0].references == {commitId}
        assert cmt['results'][0][1].references == {commitId}
Beispiel #12
0
    def test_handshake_with_register(self, monkeypatch):
        calls = []
        def register(client, request):
            calls.append(client)

        jslink = JsLink.JsLink(lambda x:None, registerClient=register)

        monkeypatch.setattr(time, 'time', self.time)

        data = doRequest(jslink, data=[{"type": "handshake"}])

        clientId = data['clientId']
        assert data['extraInfo'] == {}

        assert calls == [ObjectId(clientId)]

        self.sync()
        client = DB.find_one(self.database.clients)
        assert str(client['_id']) == clientId
        assert client['timestamp'] == 1
        assert client['updates'] == []
Beispiel #13
0
    def test_persistent_db_update(self, monkeypatch):
        # create an existing link
        link = self.factory.create('Persistent',
                                   self.clientId,
                                   self.linkId,
                                   params=dict(toid='27', attrs=['foo',
                                                                 'bar']))
        link.run()
        mongo.update_one(self.database.clients, {'_id': self.clientId},
                         {'$set': {
                             'updates': []
                         }})
        self.sync()

        # no new data, but uptodate should be set
        monkeypatch.setattr(LinkPersistent, 'result', None)
        link = self.factory.create('Persistent', self.clientId, self.linkId)
        link.run()
        self.sync()

        clientdata = mongo.find_one(self.database.clients,
                                    {'_id': self.clientId})
        assert clientdata['updates'] == []
        linkdata = mongo.find_one(self.database.links, {
            'client': self.clientId,
            'link': self.linkId
        })
        assert linkdata['outdatedBy'] == None
        monkeypatch.undo()

        monkeypatch.setattr(LinkPersistent, 'state', {'sallad': 'paprika'})

        # now update it
        link = self.factory.create('Persistent', self.clientId, self.linkId)
        link.run()
        self.sync()
        assert link._state == {'gurka': 'tomat'}

        linkdata, = list(
            self.database.links.find({
                'client': self.clientId,
                'link': self.linkId
            }))
        clientdata, = list(self.database.clients.find({'_id': self.clientId}))

        linkdata.pop('_id')  # don't care
        assert linkdata == {
            'type': 'LinkPersistent',
            'client': self.clientId,
            'link': self.linkId,
            'state': {
                'sallad': 'paprika'
            },
            'outdatedBy': None,
            'allowRead': [self.user.id[0]],
            'timestamp': self.time,
            'ancient': False,
            # same params as first time:
            'params': dict(toid='27', attrs=['foo', 'bar'])
        }

        assert clientdata['updates'] == [{
            'type': 'update',
            'id': self.linkId,
            'args': {
                'foo': 'bar'
            }
        }]
Beispiel #14
0
 def _getLinkData(self):
     self.sync()
     return mongo.find_one(self.database.links, {
         'client': self.clientId,
         'link': self.linkId
     })
Beispiel #15
0
 def test_commit_with_interest_successful(self):
     commitId = self._commit(interested=[1, 2])
     commit = mongo.find_one(self.database.commits, {'_id': commitId})
     assert commit['handled_by'] == commit['interested'] == [1, 2]
     assert commit['state'] == 'done'
     assert commit['results'] == [['result'], 42]
Beispiel #16
0
 def test_commit_without_interest_successful(self):
     commitId = self._commit()
     assert not mongo.find_one(self.database.commits, {'_id': commitId})
Beispiel #17
0
 def find_one(self, query, collection=None):
     collection = collection or self.database.tois
     return mongo.find_one(collection, query)
Beispiel #18
0
    def test_link(self):
        data = doRequest(self.jslink, data=[{
            'type': 'link',
            'args' : {'test':'foo'},
            'id' : 42,
            'name' : 'linkMe',
            'clientId' : self.clientId,
            }])
        assert data == {'new': 42}

        self.link.update({'foo': 'bar'})
        self.sync()
        data = doPoll(self.jslink, self.clientId)
        assert data['args'] == {'foo':'bar'}

        # defunct link id
        data = doRequest(self.jslink, data=[{
            'type': 'link_update',
            'args' : {'foo':'bar', 'apa':'bepa'},
            'id' : 99,
            'name' : 'doUpdate',
            'clientId' : self.clientId,
            }])
        assert data == {}

        data = doRequest(self.jslink, data=[{
            'type': 'link_update',
            'args' : {'foo':'bar', 'apa':'bepa'},
            'id' : 42,
            'name' : 'doUpdate',
            'clientId' : self.clientId,
            }])
        assert data == {'link_update': 42}
        self.sync()

        data = doPoll(self.jslink, self.clientId)
        assert data['args'] == {'foo':'bar', 'apa':'bepa'}

        # defunct link id
        data = doRequest(self.jslink, data=[{
            'type': 'link_deactivate',
            'args' : {},
            'id' : 99,
            'name' : 'deactivate',
            'clientId' : self.clientId,
            }])
        assert data == {}

        data = doRequest(self.jslink, data=[{
            'type': 'link_deactivate',
            'args' : {},
            'id' : 42,
            'name' : 'deactivate',
            'clientId' : self.clientId,
            }])
        assert data == {'link_deactivate': 42}
        self.sync()

        assert not DB.find_one(self.ctx.database.links,
                               {'client': self.clientId, 'link': 42})
        assert self.link.removed