Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replication + ACLs #1202

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/persisted-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ PersistedModel.setup = function setupPersistedModel() {
}

PersistedModel.setupRemoting();

var originalCheckAccess = PersistedModel.checkAccess;

PersistedModel.checkAccess = function(token, modelId, sharedMethod, ctx, callback) {
// placeholder
originalCheckAccess.apply(this, arguments);
}
};

/*!
Expand Down Expand Up @@ -958,6 +965,8 @@ function tryReplicate(sourceModel, targetModel, since, options, callback) {
function checkpoints() {
var cb = arguments[arguments.length - 1];
sourceModel.checkpoint(function(err, source) {
if(err) return cb(err);

newSourceCp = source.seq;
targetModel.checkpoint(function(err, target) {
newTargetCp = target.seq;
Expand Down Expand Up @@ -1065,8 +1074,6 @@ PersistedModel.bulkUpdate = function(updates, callback) {
switch (update.type) {
case Change.UPDATE:
case Change.CREATE:
// var model = new Model(update.data);
// tasks.push(model.save.bind(model));
tasks.push(function(cb) {
var model = new Model(update.data);
model.save(cb);
Expand Down
90 changes: 90 additions & 0 deletions test/remote-replication.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
describe('Remote Replication', function() {
beforeEach(function(done) {
var test = this;

test.serverApp = loopback();
test.serverApp.use(function(req, res, next) {
console.log(req.method, req.url);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps use debug logs here? require('debug')('test')('%s %s', req.method, req.url)

next();
});
test.serverApp.use(loopback.rest());
test.serverApp.enableAuth();
test.clientApp = loopback();
this.serverApp.set('legacyExplorer', false);
var settings = {
base: 'PersistedModel',
trackChanges: true,
dataSource: null,
http: {
path: 'my-model'
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC this setting is not honoured by the remoting connector, see strongloop/loopback-connector-remote#16. Perhaps I am wrong?

};
test.ServerModel = test.serverApp.model('ServerModel', settings);
test.ClientModel = test.clientApp.model('ClientModel', settings);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use unique model names. Registering multiple models with the same name causes all sorts of weird behaviour.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What? They are unique...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. This code is called before each test, and every time you call this code, a new model class is created and registered in the global static registry under the same name "ServerModel". It's safer to use a name that is unique per test, see replication tests for an example:

SourceModel = this.SourceModel = PersistedModel.extend(
'SourceModel-' + tid,
{ id: { id: true, type: String, defaultFn: 'guid' } },
{ trackChanges: true });
SourceModel.attachTo(dataSource);
TargetModel = this.TargetModel = PersistedModel.extend(
'TargetModel-' + tid,
{ id: { id: true, type: String, defaultFn: 'guid' } },
{ trackChanges: true });

This is not strictly necessary, in many cases it's ok to override model with the same name. It's just that the case where it's not ok are difficult to identify, because the errors are very subtle.

var server = test.server = this.serverApp.listen(function(err) {
if(err) return done(err);
test.port = server.address().port;
done();
});
});

afterEach(function(done) {
this.server.close(done);
});

beforeEach(function(done) {
var test = this;

test.remotes = loopback.createDataSource({
connector: 'remote',
url: 'http://localhost:' + test.port
});

this.ClientModel.attachTo(test.remotes);
this.ServerModel.attachTo(loopback.createDataSource({
connector: 'memory'
}));

this.ClientModel.create({foo: 'bar'}, done);
});

describe('client to server replication', function() {
it('should replicate a simple change from client to server', function(done) {
simpleReplication(this, done);
});

describe('with access controls in place', function() {
beforeEach(function() {
this.ServerModel.settings.acls = [{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW",
}];
});
it('should fail without a logged in user', function(done) {
simpleReplication(this, function(err) {
expect(err).to.exist;
expect(err.message).to.equal('Authorization Required');
done();
});
});
});
});
});


function simpleReplication(test, done) {
test.ClientModel.replicate(test.ServerModel, function(err) {
if(err) return done(err);
test.ServerModel.findOne(function(err, model) {
expect(model.foo).to.equal('bar');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic string :( Please save the created model in the beforeeach hook and rework this check to expect(model.toObject()).to.eql(test.model.toObject())

done();
});
});
}