2018-01-03 04:05:53 +00:00
|
|
|
// Copyright IBM Corp. 2015,2018. All Rights Reserved.
|
2016-05-03 22:50:21 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2019-10-07 09:45:34 +00:00
|
|
|
const async = require('async');
|
|
|
|
const debug = require('debug')('test');
|
|
|
|
const extend = require('util')._extend;
|
|
|
|
const loopback = require('../');
|
|
|
|
const expect = require('./helpers/expect');
|
|
|
|
const supertest = require('supertest');
|
2015-04-03 14:41:32 +00:00
|
|
|
|
|
|
|
describe('Replication over REST', function() {
|
2016-08-08 13:12:56 +00:00
|
|
|
this.timeout(10000);
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const ALICE = {id: 'a', username: 'alice', email: 'a@t.io', password: 'p'};
|
|
|
|
const PETER = {id: 'p', username: 'peter', email: 'p@t.io', password: 'p'};
|
|
|
|
const EMERY = {id: 'e', username: 'emery', email: 'e@t.io', password: 'p'};
|
2015-04-03 14:41:32 +00:00
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
/* eslint-disable one-var */
|
2019-10-07 09:45:34 +00:00
|
|
|
let serverApp, serverUrl, ServerUser, ServerCar, serverCars;
|
|
|
|
let aliceId, peterId, aliceToken, peterToken, emeryToken, request;
|
|
|
|
let clientApp, LocalUser, LocalCar, RemoteUser, RemoteCar, clientCars;
|
|
|
|
let conflictedCarId;
|
2016-04-01 09:14:26 +00:00
|
|
|
/* eslint-enable one-var */
|
2015-04-03 14:41:32 +00:00
|
|
|
|
|
|
|
before(setupServer);
|
|
|
|
before(setupClient);
|
|
|
|
beforeEach(seedServerData);
|
|
|
|
beforeEach(seedClientData);
|
|
|
|
|
|
|
|
describe('the replication scenario scaffolded for the tests', function() {
|
|
|
|
describe('Car model', function() {
|
|
|
|
it('rejects anonymous READ', function(done) {
|
|
|
|
listCars().expect(401, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects anonymous WRITE', function(done) {
|
|
|
|
createCar().expect(401, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows EMERY to READ', function(done) {
|
|
|
|
listCars()
|
|
|
|
.set('Authorization', emeryToken)
|
|
|
|
.expect(200, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('denies EMERY to WRITE', function(done) {
|
|
|
|
createCar()
|
|
|
|
.set('Authorization', emeryToken)
|
|
|
|
.expect(401, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows ALICE to READ', function(done) {
|
|
|
|
listCars()
|
|
|
|
.set('Authorization', aliceToken)
|
|
|
|
.expect(200, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('denies ALICE to WRITE', function(done) {
|
|
|
|
createCar()
|
|
|
|
.set('Authorization', aliceToken)
|
|
|
|
.expect(401, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows PETER to READ', function(done) {
|
|
|
|
listCars()
|
|
|
|
.set('Authorization', peterToken)
|
|
|
|
.expect(200, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows PETER to WRITE', function(done) {
|
|
|
|
createCar()
|
2017-12-12 08:33:15 +00:00
|
|
|
.set('Authorization', peterToken)
|
|
|
|
.expect(200, done);
|
2015-04-03 14:41:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function listCars() {
|
|
|
|
return request.get('/Cars');
|
|
|
|
}
|
|
|
|
|
|
|
|
function createCar() {
|
2016-11-15 21:46:23 +00:00
|
|
|
return request.post('/Cars').send({model: 'a-model'});
|
2015-04-03 14:41:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('sync with model-level permissions', function() {
|
|
|
|
describe('as anonymous user', function() {
|
|
|
|
it('rejects pull from server', function(done) {
|
|
|
|
RemoteCar.replicate(LocalCar, expectHttpError(401, done));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects push to the server', function(done) {
|
|
|
|
LocalCar.replicate(RemoteCar, expectHttpError(401, done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('as user with READ-only permissions', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
setAccessToken(emeryToken);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects pull from server', function(done) {
|
|
|
|
RemoteCar.replicate(LocalCar, expectHttpError(401, done));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects push to the server', function(done) {
|
|
|
|
LocalCar.replicate(RemoteCar, expectHttpError(401, done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('as user with REPLICATE-only permissions', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
setAccessToken(aliceToken);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows pull from server', function(done) {
|
|
|
|
RemoteCar.replicate(LocalCar, function(err, conflicts, cps) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
if (conflicts.length) return done(conflictError(conflicts));
|
|
|
|
|
|
|
|
LocalCar.find(function(err, list) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
expect(list.map(carToString)).to.include.members(serverCars);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects push to the server', function(done) {
|
|
|
|
LocalCar.replicate(RemoteCar, expectHttpError(401, done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('as user with READ and WRITE permissions', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
setAccessToken(peterToken);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows pull from server', function(done) {
|
|
|
|
RemoteCar.replicate(LocalCar, function(err, conflicts, cps) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
if (conflicts.length) return done(conflictError(conflicts));
|
|
|
|
|
|
|
|
LocalCar.find(function(err, list) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
expect(list.map(carToString)).to.include.members(serverCars);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows push to the server', function(done) {
|
|
|
|
LocalCar.replicate(RemoteCar, function(err, conflicts, cps) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
if (conflicts.length) return done(conflictError(conflicts));
|
|
|
|
|
|
|
|
ServerCar.find(function(err, list) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
expect(list.map(carToString)).to.include.members(clientCars);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-04-09 10:44:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('conflict resolution with model-level permissions', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
let LocalConflict, RemoteConflict;
|
2015-04-09 10:44:29 +00:00
|
|
|
|
|
|
|
before(function setupConflictModels() {
|
|
|
|
LocalConflict = LocalCar.getChangeModel().Conflict;
|
|
|
|
RemoteConflict = RemoteCar.getChangeModel().Conflict;
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(seedConflict);
|
|
|
|
|
|
|
|
describe('as anonymous user', function() {
|
|
|
|
it('rejects resolve() on the client', function(done) {
|
|
|
|
// simulate replication Client->Server
|
2019-10-07 09:45:34 +00:00
|
|
|
const conflict = new LocalConflict(
|
2015-04-09 10:44:29 +00:00
|
|
|
conflictedCarId,
|
|
|
|
LocalCar,
|
2018-08-08 15:22:20 +00:00
|
|
|
RemoteCar
|
|
|
|
);
|
2015-04-09 10:44:29 +00:00
|
|
|
conflict.resolveUsingSource(expectHttpError(401, done));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects resolve() on the server', function(done) {
|
|
|
|
// simulate replication Server->Client
|
2019-10-07 09:45:34 +00:00
|
|
|
const conflict = new RemoteConflict(
|
2015-04-09 10:44:29 +00:00
|
|
|
conflictedCarId,
|
|
|
|
RemoteCar,
|
2018-08-08 15:22:20 +00:00
|
|
|
LocalCar
|
|
|
|
);
|
2015-04-09 10:44:29 +00:00
|
|
|
conflict.resolveUsingSource(expectHttpError(401, done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('as user with READ-only permissions', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
setAccessToken(emeryToken);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows resolve() on the client', function(done) {
|
|
|
|
// simulate replication Client->Server
|
2019-10-07 09:45:34 +00:00
|
|
|
const conflict = new LocalConflict(
|
2015-04-09 10:44:29 +00:00
|
|
|
conflictedCarId,
|
|
|
|
LocalCar,
|
2018-08-08 15:22:20 +00:00
|
|
|
RemoteCar
|
|
|
|
);
|
2015-04-09 10:44:29 +00:00
|
|
|
conflict.resolveUsingSource(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects resolve() on the server', function(done) {
|
|
|
|
// simulate replication Server->Client
|
2019-10-07 09:45:34 +00:00
|
|
|
const conflict = new RemoteConflict(
|
2015-04-09 10:44:29 +00:00
|
|
|
conflictedCarId,
|
|
|
|
RemoteCar,
|
2018-08-08 15:22:20 +00:00
|
|
|
LocalCar
|
|
|
|
);
|
2015-04-09 10:44:29 +00:00
|
|
|
conflict.resolveUsingSource(expectHttpError(401, done));
|
|
|
|
});
|
|
|
|
});
|
2015-04-03 14:41:32 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
describe('as user with REPLICATE-only permissions', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
setAccessToken(aliceToken);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows reverse resolve() on the client', function(done) {
|
|
|
|
RemoteCar.replicate(LocalCar, function(err, conflicts) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
expect(conflicts, 'conflicts').to.have.length(1);
|
|
|
|
|
|
|
|
// By default, conflicts are always resolved by modifying
|
|
|
|
// the source datasource, so that the next replication run
|
|
|
|
// replicates the resolved data.
|
|
|
|
// However, when replicating changes from a datasource that
|
|
|
|
// users are not authorized to change, the users have to resolve
|
|
|
|
// conflicts at the target, discarding any changes made in
|
|
|
|
// the target datasource only.
|
|
|
|
conflicts[0].swapParties().resolveUsingTarget(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
RemoteCar.replicate(LocalCar, function(err, conflicts) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
if (conflicts.length) return done(conflictError(conflicts));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects resolve() on the server', function(done) {
|
|
|
|
RemoteCar.replicate(LocalCar, function(err, conflicts) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
expect(conflicts, 'conflicts').to.have.length(1);
|
|
|
|
conflicts[0].resolveUsingSource(expectHttpError(401, done));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('as user with READ and WRITE permissions', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
setAccessToken(peterToken);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows resolve() on the client', function(done) {
|
|
|
|
LocalCar.replicate(RemoteCar, function(err, conflicts) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
expect(conflicts).to.have.length(1);
|
|
|
|
|
|
|
|
conflicts[0].resolveUsingSource(function(err) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
LocalCar.replicate(RemoteCar, function(err, conflicts) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
if (conflicts.length) return done(conflictError(conflicts));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows resolve() on the server', function(done) {
|
|
|
|
RemoteCar.replicate(LocalCar, function(err, conflicts) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
expect(conflicts).to.have.length(1);
|
|
|
|
|
|
|
|
conflicts[0].resolveUsingSource(function(err) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
RemoteCar.replicate(LocalCar, function(err, conflicts) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
if (conflicts.length) return done(conflictError(conflicts));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-04-03 14:41:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe.skip('sync with instance-level permissions', function() {
|
|
|
|
it('pulls only authorized records', function(done) {
|
|
|
|
setAccessToken(aliceToken);
|
|
|
|
RemoteUser.replicate(LocalUser, function(err, conflicts, cps) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
if (conflicts.length) return done(conflictError(conflicts));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
LocalUser.find(function(err, users) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const userNames = users.map(function(u) { return u.username; });
|
2015-04-03 14:41:32 +00:00
|
|
|
expect(userNames).to.eql([ALICE.username]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows push of authorized records', function(done) {
|
|
|
|
async.series([
|
|
|
|
setupModifiedLocalCopyOfAlice,
|
|
|
|
|
|
|
|
function replicateAsCurrentUser(next) {
|
|
|
|
setAccessToken(aliceToken);
|
|
|
|
LocalUser.replicate(RemoteUser, function(err, conflicts) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
if (conflicts.length) return next(conflictError(conflicts));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
function verify(next) {
|
|
|
|
RemoteUser.findById(aliceId, function(err, found) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
expect(found.toObject())
|
|
|
|
.to.have.property('fullname', 'Alice Smith');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
next();
|
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2015-04-03 14:41:32 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects push of unauthorized records', function(done) {
|
|
|
|
async.series([
|
|
|
|
setupModifiedLocalCopyOfAlice,
|
|
|
|
|
|
|
|
function replicateAsDifferentUser(next) {
|
|
|
|
setAccessToken(peterToken);
|
|
|
|
LocalUser.replicate(RemoteUser, function(err, conflicts) {
|
|
|
|
if (!err)
|
|
|
|
return next(new Error('Replicate should have failed.'));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
expect(err).to.have.property('statusCode', 401); // or 403?
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
function verify(next) {
|
|
|
|
ServerUser.findById(aliceId, function(err, found) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
expect(found.toObject())
|
|
|
|
.to.not.have.property('fullname');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
next();
|
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2015-04-03 14:41:32 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
// TODO(bajtos) verify conflict resolution
|
2015-04-03 14:41:32 +00:00
|
|
|
|
|
|
|
function setupModifiedLocalCopyOfAlice(done) {
|
|
|
|
// Replicate directly, bypassing REST+AUTH layers
|
|
|
|
replicateServerToLocal(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
LocalUser.updateAll(
|
2016-11-15 21:46:23 +00:00
|
|
|
{id: aliceId},
|
|
|
|
{fullname: 'Alice Smith'},
|
2018-08-08 15:22:20 +00:00
|
|
|
done
|
|
|
|
);
|
2015-04-03 14:41:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const USER_PROPS = {
|
2016-11-15 21:46:23 +00:00
|
|
|
id: {type: 'string', id: true},
|
2015-04-03 14:41:32 +00:00
|
|
|
};
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const USER_OPTS = {
|
2015-04-03 14:41:32 +00:00
|
|
|
base: 'User',
|
|
|
|
plural: 'Users', // use the same REST path in all models
|
|
|
|
trackChanges: true,
|
2015-04-09 10:44:29 +00:00
|
|
|
strict: 'throw',
|
2016-04-01 09:14:26 +00:00
|
|
|
persistUndefinedAsNull: true,
|
2016-11-15 21:46:23 +00:00
|
|
|
// Speed up the password hashing algorithm for tests
|
|
|
|
saltWorkFactor: 4,
|
2015-04-03 14:41:32 +00:00
|
|
|
};
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const CAR_PROPS = {
|
2016-11-15 21:46:23 +00:00
|
|
|
id: {type: 'string', id: true, defaultFn: 'guid'},
|
|
|
|
model: {type: 'string', required: true},
|
|
|
|
maker: {type: 'string'},
|
2015-04-03 14:41:32 +00:00
|
|
|
};
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const CAR_OPTS = {
|
2015-04-03 14:41:32 +00:00
|
|
|
base: 'PersistedModel',
|
|
|
|
plural: 'Cars', // use the same REST path in all models
|
|
|
|
trackChanges: true,
|
2015-04-09 10:44:29 +00:00
|
|
|
strict: 'throw',
|
2015-04-03 14:41:32 +00:00
|
|
|
persistUndefinedAsNull: true,
|
|
|
|
acls: [
|
|
|
|
// disable anonymous access
|
|
|
|
{
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: '$everyone',
|
2016-04-01 09:14:26 +00:00
|
|
|
permission: 'DENY',
|
2015-04-03 14:41:32 +00:00
|
|
|
},
|
|
|
|
// allow all authenticated users to read data
|
|
|
|
{
|
|
|
|
principalType: 'ROLE',
|
|
|
|
principalId: '$authenticated',
|
|
|
|
permission: 'ALLOW',
|
2016-04-01 09:14:26 +00:00
|
|
|
accessType: 'READ',
|
2015-04-03 14:41:32 +00:00
|
|
|
},
|
|
|
|
// allow Alice to pull changes
|
|
|
|
{
|
|
|
|
principalType: 'USER',
|
|
|
|
principalId: ALICE.id,
|
|
|
|
permission: 'ALLOW',
|
2016-04-01 09:14:26 +00:00
|
|
|
accessType: 'REPLICATE',
|
2015-04-03 14:41:32 +00:00
|
|
|
},
|
|
|
|
// allow Peter to write data
|
|
|
|
{
|
|
|
|
principalType: 'USER',
|
|
|
|
principalId: PETER.id,
|
|
|
|
permission: 'ALLOW',
|
2016-04-01 09:14:26 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
},
|
|
|
|
],
|
2015-04-03 14:41:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function setupServer(done) {
|
2017-02-24 13:58:40 +00:00
|
|
|
serverApp = loopback({localRegistry: true, loadBuiltinModels: true});
|
2016-11-15 21:46:23 +00:00
|
|
|
serverApp.set('remoting', {errorHandler: {debug: true, log: false}});
|
|
|
|
serverApp.dataSource('db', {connector: 'memory'});
|
2015-04-03 14:41:32 +00:00
|
|
|
|
|
|
|
// Setup a custom access-token model that is not shared
|
|
|
|
// with the client app
|
2019-10-07 09:45:34 +00:00
|
|
|
const ServerToken = serverApp.registry.createModel('ServerToken', {}, {
|
2015-04-03 14:41:32 +00:00
|
|
|
base: 'AccessToken',
|
|
|
|
relations: {
|
|
|
|
user: {
|
|
|
|
type: 'belongsTo',
|
|
|
|
model: 'ServerUser',
|
2016-04-01 09:14:26 +00:00
|
|
|
foreignKey: 'userId',
|
|
|
|
},
|
|
|
|
},
|
2015-04-03 14:41:32 +00:00
|
|
|
});
|
2016-11-15 21:46:23 +00:00
|
|
|
serverApp.model(ServerToken, {dataSource: 'db', public: false});
|
2015-04-03 14:41:32 +00:00
|
|
|
|
2017-02-24 13:58:40 +00:00
|
|
|
ServerUser = serverApp.registry.createModel('ServerUser', USER_PROPS, USER_OPTS);
|
2015-04-03 14:41:32 +00:00
|
|
|
serverApp.model(ServerUser, {
|
|
|
|
dataSource: 'db',
|
|
|
|
public: true,
|
2016-11-15 21:46:23 +00:00
|
|
|
relations: {accessTokens: {model: 'ServerToken'}},
|
2015-04-03 14:41:32 +00:00
|
|
|
});
|
|
|
|
|
2017-02-24 13:58:40 +00:00
|
|
|
serverApp.enableAuth({dataSource: 'db'});
|
|
|
|
|
|
|
|
ServerCar = serverApp.registry.createModel('ServerCar', CAR_PROPS, CAR_OPTS);
|
2016-11-15 21:46:23 +00:00
|
|
|
serverApp.model(ServerCar, {dataSource: 'db', public: true});
|
2015-04-03 14:41:32 +00:00
|
|
|
|
|
|
|
serverApp.use(function(req, res, next) {
|
|
|
|
debug(req.method + ' ' + req.path);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
next();
|
|
|
|
});
|
2016-11-15 21:46:23 +00:00
|
|
|
serverApp.use(loopback.token({model: ServerToken}));
|
2015-04-03 14:41:32 +00:00
|
|
|
serverApp.use(loopback.rest());
|
|
|
|
|
|
|
|
serverApp.set('port', 0);
|
|
|
|
serverApp.set('host', '127.0.0.1');
|
|
|
|
serverApp.listen(function() {
|
|
|
|
serverUrl = serverApp.get('url').replace(/\/+$/, '');
|
|
|
|
request = supertest(serverUrl);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupClient() {
|
2017-02-24 13:58:40 +00:00
|
|
|
clientApp = loopback({localRegistry: true, loadBuiltinModels: true});
|
2016-11-15 21:46:23 +00:00
|
|
|
clientApp.dataSource('db', {connector: 'memory'});
|
2015-04-03 14:41:32 +00:00
|
|
|
clientApp.dataSource('remote', {
|
|
|
|
connector: 'remote',
|
2016-04-01 09:14:26 +00:00
|
|
|
url: serverUrl,
|
2015-04-03 14:41:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// NOTE(bajtos) At the moment, all models share the same Checkpoint
|
|
|
|
// model. This causes the in-process replication to work differently
|
|
|
|
// than client-server replication.
|
|
|
|
// As a workaround, we manually setup unique Checkpoint for ClientModel.
|
2019-10-07 09:45:34 +00:00
|
|
|
const ClientCheckpoint = clientApp.registry.createModel({
|
2017-02-24 13:58:40 +00:00
|
|
|
name: 'ClientCheckpoint',
|
|
|
|
base: 'Checkpoint',
|
|
|
|
});
|
2015-04-03 14:41:32 +00:00
|
|
|
ClientCheckpoint.attachTo(clientApp.dataSources.db);
|
|
|
|
|
2017-02-24 13:58:40 +00:00
|
|
|
LocalUser = clientApp.registry.createModel('LocalUser', USER_PROPS, USER_OPTS);
|
2015-04-03 14:41:32 +00:00
|
|
|
if (LocalUser.Change) LocalUser.Change.Checkpoint = ClientCheckpoint;
|
2016-11-15 21:46:23 +00:00
|
|
|
clientApp.model(LocalUser, {dataSource: 'db'});
|
2015-04-03 14:41:32 +00:00
|
|
|
|
2017-02-24 13:58:40 +00:00
|
|
|
LocalCar = clientApp.registry.createModel('LocalCar', CAR_PROPS, CAR_OPTS);
|
2015-04-03 14:41:32 +00:00
|
|
|
LocalCar.Change.Checkpoint = ClientCheckpoint;
|
2016-11-15 21:46:23 +00:00
|
|
|
clientApp.model(LocalCar, {dataSource: 'db'});
|
2015-04-03 14:41:32 +00:00
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
let remoteOpts = createRemoteModelOpts(USER_OPTS);
|
2017-02-24 13:58:40 +00:00
|
|
|
RemoteUser = clientApp.registry.createModel('RemoteUser', USER_PROPS, remoteOpts);
|
2016-11-15 21:46:23 +00:00
|
|
|
clientApp.model(RemoteUser, {dataSource: 'remote'});
|
2015-04-03 14:41:32 +00:00
|
|
|
|
|
|
|
remoteOpts = createRemoteModelOpts(CAR_OPTS);
|
2017-02-24 13:58:40 +00:00
|
|
|
RemoteCar = clientApp.registry.createModel('RemoteCar', CAR_PROPS, remoteOpts);
|
2016-11-15 21:46:23 +00:00
|
|
|
clientApp.model(RemoteCar, {dataSource: 'remote'});
|
2015-04-03 14:41:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createRemoteModelOpts(modelOpts) {
|
|
|
|
return extend(modelOpts, {
|
|
|
|
// Disable change tracking, server will call rectify/rectifyAll
|
|
|
|
// after each change, because it's tracking the changes too.
|
|
|
|
trackChanges: false,
|
|
|
|
// Enable remote replication in order to get remoting API metadata
|
|
|
|
// used by the remoting connector
|
2016-04-01 09:14:26 +00:00
|
|
|
enableRemoteReplication: true,
|
2015-04-03 14:41:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function seedServerData(done) {
|
|
|
|
async.series([
|
|
|
|
function(next) {
|
|
|
|
serverApp.dataSources.db.automigrate(next);
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
ServerUser.create([ALICE, PETER, EMERY], function(err, created) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
aliceId = created[0].id;
|
|
|
|
peterId = created[1].id;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
ServerUser.login(ALICE, function(err, token) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
aliceToken = token.id;
|
|
|
|
|
|
|
|
ServerUser.login(PETER, function(err, token) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
peterToken = token.id;
|
|
|
|
|
|
|
|
ServerUser.login(EMERY, function(err, token) {
|
|
|
|
emeryToken = token.id;
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
ServerCar.create(
|
|
|
|
[
|
2017-12-12 08:33:15 +00:00
|
|
|
{id: 'Ford-Mustang', maker: 'Ford', model: 'Mustang'},
|
2016-11-15 21:46:23 +00:00
|
|
|
{id: 'Audi-R8', maker: 'Audi', model: 'R8'},
|
2015-04-03 14:41:32 +00:00
|
|
|
],
|
|
|
|
function(err, cars) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
serverCars = cars.map(carToString);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
next();
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2015-04-03 14:41:32 +00:00
|
|
|
], done);
|
|
|
|
}
|
|
|
|
|
|
|
|
function seedClientData(done) {
|
2015-04-09 10:44:29 +00:00
|
|
|
async.series([
|
|
|
|
function(next) {
|
|
|
|
clientApp.dataSources.db.automigrate(next);
|
|
|
|
},
|
|
|
|
function(next) {
|
2015-04-03 14:41:32 +00:00
|
|
|
LocalCar.create(
|
2016-11-15 21:46:23 +00:00
|
|
|
[{maker: 'Local', model: 'Custom'}],
|
2015-04-03 14:41:32 +00:00
|
|
|
function(err, cars) {
|
2015-04-09 10:44:29 +00:00
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
clientCars = cars.map(carToString);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
next();
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2015-04-09 10:44:29 +00:00
|
|
|
},
|
|
|
|
], done);
|
|
|
|
}
|
|
|
|
|
|
|
|
function seedConflict(done) {
|
|
|
|
LocalCar.replicate(ServerCar, function(err, conflicts) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
if (conflicts.length) return done(conflictError(conflicts));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
ServerCar.replicate(LocalCar, function(err, conflicts) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
if (conflicts.length) return done(conflictError(conflicts));
|
|
|
|
|
|
|
|
// Hard-coded, see the seed data above
|
|
|
|
conflictedCarId = 'Ford-Mustang';
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
new LocalCar({id: conflictedCarId})
|
|
|
|
.updateAttributes({model: 'Client'}, function(err, c) {
|
2015-04-09 10:44:29 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
new ServerCar({id: conflictedCarId})
|
|
|
|
.updateAttributes({model: 'Server'}, done);
|
2015-04-03 14:41:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setAccessToken(token) {
|
|
|
|
clientApp.dataSources.remote.connector.remotes.auth = {
|
|
|
|
bearer: new Buffer(token).toString('base64'),
|
2016-04-01 09:14:26 +00:00
|
|
|
sendImmediately: true,
|
2015-04-03 14:41:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectHttpError(code, done) {
|
|
|
|
return function(err) {
|
|
|
|
if (!err) return done(new Error('The method should have failed.'));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
expect(err).to.have.property('statusCode', code);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
done();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function replicateServerToLocal(next) {
|
|
|
|
ServerUser.replicate(LocalUser, function(err, conflicts) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
if (conflicts.length) return next(conflictError(conflicts));
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-03 14:41:32 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function conflictError(conflicts) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const err = new Error('Unexpected conflicts\n' +
|
2015-04-03 14:41:32 +00:00
|
|
|
conflicts.map(JSON.stringify).join('\n'));
|
|
|
|
err.name = 'ConflictError';
|
|
|
|
}
|
|
|
|
|
|
|
|
function carToString(c) {
|
|
|
|
return c.maker ? c.maker + ' ' + c.model : c.model;
|
|
|
|
}
|
|
|
|
});
|