Skip to content
Merged
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
5 changes: 4 additions & 1 deletion common/models/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@
"accessTokens": {
"type": "hasMany",
"model": "AccessToken",
"foreignKey": "userId"
"foreignKey": "userId",
"options": {
"disableInclude": true
}
}
}
}
18 changes: 18 additions & 0 deletions test/fixtures/user-integration-app/common/models/blog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "blog",
"base": "PersistedModel",
"properties": {
"title": {
"type": "string"
},
"content": {
"type": "string"
}
},
"relations": {
"user": {
"type": "belongsTo",
"model": "myUser"
}
}
}
18 changes: 18 additions & 0 deletions test/fixtures/user-integration-app/common/models/my-user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "myUser",
"base": "User",
"relations": {
"blogs": {
"model": "blog",
"type": "hasMany",
"foreignKey": "userId"
}
},
"acls": [
{
"permission": "ALLOW",
"principalType": "ROLE",
"principalId": "$owner"
}
]
}
21 changes: 21 additions & 0 deletions test/fixtures/user-integration-app/common/models/post.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "post",
"base": "PersistedModel",
"properties": {
"title": {
"type": "string"
},
"content": {
"type": "string"
},
"notes": {
"type": "string"
}
},
"relations": {
"user": {
"type": "belongsTo",
"model": "User"
}
}
}
15 changes: 15 additions & 0 deletions test/fixtures/user-integration-app/server/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"port": 3000,
"host": "0.0.0.0",
"cookieSecret": "2d13a01d-44fb-455c-80cb-db9cb3cd3cd0",
"remoting": {
"json": {
"limit": "1kb",
"strict": false
},
"urlencoded": {
"limit": "8kb"
}
},
"legacyExplorer": false
}
11 changes: 11 additions & 0 deletions test/fixtures/user-integration-app/server/datasources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"db": {
"connector": "memory"
},
"mail": {
"connector": "mail",
"transports": [
{"type": "STUB"}
]
}
}
58 changes: 58 additions & 0 deletions test/fixtures/user-integration-app/server/model-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"_meta": {
"sources": [
"../common/models",
"./models"
]
},
"Email": {
"dataSource": "mail",
"public": false
},
"User": {
"dataSource": "db",
"public": true,
"relations": {
"posts": {
"model": "post",
"type": "hasMany",
"foreignKey": "userId"
}
},
"acls": [
{
"permission": "ALLOW",
"principalType": "ROLE",
"principalId": "$owner"
}
]
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false
},
"Role": {
"dataSource": "db",
"public": false
},
"myUser": {
"dataSource": "db",
"public": true
},
"blog": {
"dataSource": "db",
"public": true
},
"post": {
"dataSource": "db",
"public": true
}
}
10 changes: 10 additions & 0 deletions test/fixtures/user-integration-app/server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var loopback = require('../../../../index');
var boot = require('loopback-boot');
var app = module.exports = loopback();
app.enableAuth();
boot(app, __dirname);
app.use(loopback.token({model: app.models.AccessToken}));
var apiPath = '/api';
app.use(apiPath, loopback.rest());
app.use(loopback.urlNotFound());
app.use(loopback.errorHandler());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you please build the app in test/user.integration.js and don't use loopback-boot?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Why? The purpose of the user.integration.js is to test a LB app's built-in user model.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The purpose of the user.integration.js is to test a LB app's built-in user model.

As you wrote yourself, there is nothing related to loopback-boot in the scenario you are testing.

It is a good practice to write the test code in such way that it's easy to quickly understand what is the scenario being tested. The test scenario should be also as minimal as possible and include only things that are absolutely necessary for the test.

By using a fixture defined in several files living outside of the test file, you are making it difficult to understand what is exactly being tested. By including lots of unrelated stuff in your fixture, you make it difficult to see what is the important (test-specific) part and what is just boilerplate. Last but not least, all these extra unneeded bits are making the maintenance more expensive.

This is already visible in this pull request, as the server/server.js file is not following the project structure as scaffolded by recent versions of loopback-workspace.

To test the built-in user model, you need just few lines of code:

var app = loopback();
app.dataSource('db', { connector: 'memory' });
app.model(loopback.User, { dataSource: 'db' });
app.model(loopback.AccessToken, { dataSource: 'db' });
app.model(loopback.ACL, { dataSource: 'db' });
app.model(loopback.Role, { dataSource: 'db' });
app.model(loopback.RoleMapping, { dataSource: 'db' }); // this may be not necessary

app.enableAuth();

app.use(loopback.rest());
request = supertest(app);

I admit this setup is not as short as it should be, let's discuss the best API in #1323 and keep the code above for the purposes of this PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There are more models such as post and blog are needed. I'll merge the PR as is and defer the cleanup of tests.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am rather unhappy that you have added tests that always pass, regardless of whether the underlying implementation works or not. Such tests are giving false sense of security 👎

166 changes: 166 additions & 0 deletions test/user.integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*jshint -W030 */
var loopback = require('../');
var lt = require('loopback-testing');
var path = require('path');
var SIMPLE_APP = path.join(__dirname, 'fixtures', 'user-integration-app');
var app = require(path.join(SIMPLE_APP, 'server/server.js'));
var expect = require('chai').expect;

describe('users - integration', function() {

lt.beforeEach.withApp(app);

before(function(done) {
// HACK: [rfeng] We have to reset the relations as they are polluted by
// other tests
app.models.User.hasMany(app.models.post);
app.models.User.hasMany(app.models.AccessToken,
{options: {disableInclude: true}});
app.models.AccessToken.belongsTo(app.models.User);
app.models.User.destroyAll(function(err) {
if (err) return done(err);
app.models.post.destroyAll(function(err) {
if (err) return done(err);
app.models.blog.destroyAll(function(err) {
if (err) return done(err);
done();
});
});
});
});

describe('base-user', function() {
var userId;
var accessToken;

it('should create a new user', function(done) {
var url = '/api/users';

this.post(url)
.send({username: 'x', email: 'x@y.com', password: 'x'})
.expect(200, function(err, res) {
if (err) {
return done(err);
}
expect(res.body.id).to.exist;
userId = res.body.id;
done();
});
});

it('should log into the user', function(done) {
var url = '/api/users/login';

this.post(url)
.send({username: 'x', email: 'x@y.com', password: 'x'})
.expect(200, function(err, res) {
if (err) {
return done(err);
}
expect(res.body.id).to.exist;
accessToken = res.body.id;
done();
});
});

it('should create post for a given user', function(done) {
var url = '/api/users/' + userId + '/posts?access_token=' + accessToken;
this.post(url)
.send({title: 'T1', content: 'C1'})
.expect(200, function(err, res) {
if (err) {
return done(err);
}
expect(res.body.title).to.be.eql('T1');
expect(res.body.content).to.be.eql('C1');
expect(res.body.userId).to.be.eql(userId);
done();
});
});

// FIXME: [rfeng] The test is passing if run alone. But it fails with
// `npm test` as the loopback models are polluted by other tests
it('should prevent access tokens from being included', function(done) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test is useless as it does not verify anything. If you per-app registry does not solve the problem of cross-test interference, then it's better to remove this test completely and use a different way to ensure that access tokens are not include-able by default.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It does verify with the User/AccessToken relation setup (disableInclude = true), the accessTokens cannot be included via a user, even though the pollution from other tests forces me to use the hack to restore the relations to mimic the original configuration from the test fixture.

I'm open to better ways to test the feature but it's better not to let the security hole drags too long.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My point is: if you remove disableInclude:true from common/models/user.json, all your tests will keep passing.

it's better not to let the security hole drags too long.

That's true. Let's land the fix with no unit tests then (just the small change in common/models/users.json) and implement tests in a follow-up pull request.

var url = '/api/posts?filter={"include":{"user":"accessTokens"}}';
this.get(url)
.expect(200, function(err, res) {
if (err) {
return done(err);
}
expect(res.body).to.have.property('length', 1);
var post = res.body[0];
expect(post.user).to.have.property('username', 'x');
expect(post.user).to.not.have.property('accessTokens');
done();
});
});
});

describe('sub-user', function() {
var userId;
var accessToken;

it('should create a new user', function(done) {
var url = '/api/myUsers';

this.post(url)
.send({username: 'x', email: 'x@y.com', password: 'x'})
.expect(200, function(err, res) {
if (err) {
return done(err);
}
expect(res.body.id).to.exist;
userId = res.body.id;
done();
});
});

it('should log into the user', function(done) {
var url = '/api/myUsers/login';

this.post(url)
.send({username: 'x', email: 'x@y.com', password: 'x'})
.expect(200, function(err, res) {
if (err) {
return done(err);
}
expect(res.body.id).to.exist;
accessToken = res.body.id;
done();
});
});

it('should create blog for a given user', function(done) {
var url = '/api/myUsers/' + userId + '/blogs?access_token=' + accessToken;
this.post(url)
.send({title: 'T1', content: 'C1'})
.expect(200, function(err, res) {
if (err) {
console.error(err);
return done(err);
}
expect(res.body.title).to.be.eql('T1');
expect(res.body.content).to.be.eql('C1');
expect(res.body.userId).to.be.eql(userId);
done();
});
});

it('should prevent access tokens from being included', function(done) {
var url = '/api/blogs?filter={"include":{"user":"accessTokens"}}';
this.get(url)
.expect(200, function(err, res) {
if (err) {
return done(err);
}
expect(res.body).to.have.property('length', 1);
var blog = res.body[0];
expect(blog.user).to.have.property('username', 'x');
expect(blog.user).to.not.have.property('accessTokens');
done();
});
});
});

});