-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest-serverjs.js
More file actions
161 lines (144 loc) · 5.27 KB
/
test-serverjs.js
File metadata and controls
161 lines (144 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var loopback = require('loopback');
var lt = require('loopback-testing');
var chai = require('chai');
var expect = chai.expect;
// Create a new loopback app.
var app = loopback();
// import our ReadOnly mixin.
require('../lib')(app);
describe('loopback datasource readonly property (server.js)', function() {
beforeEach(function(done) {
// A model with 1 readonly property.
var Product = this.Product = loopback.PersistedModel.extend('product',
{ name: String, type: String, status: String },
{ mixins: { ReadOnly: { status: true } } }
);
Product.attachTo(loopback.memory());
app.model(Product);
// A model with 2 readonly properties.
var Person = this.Person = loopback.PersistedModel.extend('person',
{ name: String, status: String, role: String },
{ mixins: { ReadOnly: { status: true, role: "createOnly" } } }
);
Person.attachTo(loopback.memory());
app.model(Person);
// A model that is fully readony.
var AuditTrail = this.AuditTrail = loopback.PersistedModel.extend('audittrail',
{ event: String, user: String },
{ mixins: { ReadOnly: true } }
);
AuditTrail.attachTo(loopback.memory());
app.model(AuditTrail);
app.use(loopback.rest());
app.set('legacyExplorer', false);
done();
});
lt.beforeEach.withApp(app);
describe('when called internally', function() {
lt.beforeEach.givenModel('product', {name: 'some book', type: 'book', status: 'pending'});
it('should save readonly properties on create.', function(done) {
expect(this.product.name).to.equal('some book');
expect(this.product.type).to.equal('book');
expect(this.product.status).to.equal('pending');
done();
});
it('should change readonly properties on update.', function(done) {
var self = this;
self.product.name = 'some other book';
self.product.status = 'disabled';
self.product.save(function(err, p) {
expect(err).to.not.exist;
expect(p.name).to.equal(self.product.name);
expect(p.type).to.equal(self.product.type);
expect(p.status).to.equal(self.product.status);
done();
});
});
});
describe('when called remotely', function() {
lt.beforeEach.givenModel('product', {name: 'some book', type: 'book', status: 'pending'});
lt.beforeEach.givenModel('person', {name: 'Tom', status: 'disabled', role: 'user'});
lt.beforeEach.givenModel('audittrail', {event: 'edit', user: 'tom'});
lt.beforeEach.givenModel('product', {name: 'book 1', type: 'book', status: 'disabled'}, 'book1');
lt.beforeEach.givenModel('product', {name: 'book 12', type: 'book', status: 'pending'}, 'book2');
it('should not save readonly properties on create.', function(done) {
var product = this.product;
this.post('/products')
.send({
name: 'test product',
status: 'active'
})
.expect(200)
.end(function(err, res) {
expect(err).to.not.exist;
expect(res.body.name).to.equal('test product');
expect(res.body.status).to.not.exist;
done();
});
});
it('should not change readonly properties on update (single readonly property)', function(done) {
var product = this.product;
this.put('/products/' + product.id)
.send({
name: 'updated name',
status: 'disabled'
})
.expect(200)
.end(function(err, res) {
expect(err).to.not.exist;
expect(res.body.name).to.equal('updated name');
expect(res.body.status).to.equal('pending');
done();
});
});
it('should not change readonly properties on update (multiple readonly properties)', function(done) {
var person = this.person;
this.put('/people/' + person.id)
.send({
name: 'Tom (edited)',
status: 'active',
role: 'user'
})
.expect(200)
.end(function(err, res) {
expect(err).to.not.exist;
expect(res.body.name).to.equal('Tom (edited)');
expect(res.body.status).to.equal('disabled');
expect(res.body.role).to.equal('user');
done();
});
});
it('should not change readonly properties on update (full read only model)', function(done) {
var audittrail = this.audittrail;
this.put('/audittrails/' + audittrail.id)
.send({
event: 'update',
user: 'john'
})
.expect(403)
.end(done);
});
it('should not change readonly properties with bulk updates', function(done) {
var self = this;
var data = { 'status': 'disabled' };
var query = { 'where': {'type': 'book' }};
self.post('/products/update')
.query(query)
.send(data)
.set('Accept', 'application/json')
.expect(200)
.end(function(err, res) {
expect(err).to.not.exist;
self.Product.findById(self.book1.id, function(err, b1) {
expect(err).to.not.exist;
expect(b1.status).to.equal(self.book1.status);
self.Product.findById(self.book2.id, function(err, b2) {
expect(err).to.not.exist;
expect(b2.status).to.equal(self.book2.status);
done();
});
});
});
});
});
});