forked from strongloop-archive/loopback-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
130 lines (120 loc) · 4.3 KB
/
test.js
File metadata and controls
130 lines (120 loc) · 4.3 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
// Copyright IBM Corp. 2013,2015. All Rights Reserved.
// Node module: loopback-testing
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
var loopback = require('loopback');
var helpers = require('../');
var assert = require('assert');
describe('helpers', function () {
var testApp = loopback();
var db = testApp.dataSource('db', {connector: loopback.Memory});
var testModel = testApp.model('xxx-test-model', {dataSource: 'db'});
testApp.use(loopback.rest());
helpers.beforeEach.withApp(testApp);
describe('helpers.it', function() {
['shouldBeAllowed',
'shouldBeDenied',
'shouldNotBeFound',
'shouldBeAllowedWhenCalledAnonymously',
'shouldBeDeniedWhenCalledAnonymously',
'shouldBeAllowedWhenCalledUnauthenticated',
'shouldBeDeniedWhenCalledUnauthenticated',
'shouldBeAllowedWhenCalledByUser',
'shouldBeDeniedWhenCalledByUser',
'shouldBeAllowedWhenCalledByUserWithRole',
'shouldBeDeniedWhenCalledByUserWithRole']
.forEach(function(func) {
it('should have a method named ' + func, function () {
assert.equal(typeof helpers.it[func], 'function');
});
});
});
describe('helpers.describe', function() {
['staticMethod',
'instanceMethod',
'whenCalledRemotely',
'whenLoggedInAsUser',
'whenLoggedInAsUserWithRole',
'whenCalledByUser',
'whenCalledByUserWithRole',
'whenCalledAnonymously',
'whenCalledUnauthenticated']
.forEach(function(func) {
it('should have a method named ' + func, function () {
assert.equal(typeof helpers.describe[func], 'function');
});
});
});
describe('helpers.beforeEach', function() {
['withApp',
'cleanDatasource',
'withArgs',
'givenModel',
'withUserModel',
'withAccessTokenModel',
'withRoleModel',
'withRoleMappingModel',
'givenUser',
'givenUserWithRole',
'givenLoggedInUser',
'givenLoggedInUserWithRole',
'givenAnUnauthenticatedToken',
'givenAnAnonymousToken']
.forEach(function(func) {
it('should have a helper method named ' + func, function () {
assert.equal(typeof helpers.beforeEach[func], 'function');
});
});
});
describe('helpers.beforeEach.givenModel', function() {
helpers.beforeEach.givenModel('xxx-test-model');
it('should have an xxx-test-model property', function () {
assert(this['xxx-test-model']);
assert(this['xxx-test-model'].id);
});
});
describe('whenCalledRemotely', function() {
helpers.describe.staticMethod('create', function() {
helpers.beforeEach.withArgs({foo: 'bar'});
helpers.describe.whenCalledRemotely('POST', '/xxx-test-models', function() {
it('should call the method over rest', function () {
assert.equal(this.res.statusCode, 200);
});
});
});
helpers.describe.staticMethod('findById', function() {
helpers.beforeEach.givenModel('xxx-test-model', {foo: 'bar'});
helpers.describe.whenCalledRemotely('GET', function () {
return '/xxx-test-models/' + this['xxx-test-model'].id;
}, function() {
it('should retrieve the expected model in the first test', function () {
assert.equal(this.res.body.id, this['xxx-test-model'].id);
});
it('should retrieve the expected model in subsequent tests', function () {
assert.equal(this.res.body.id, this['xxx-test-model'].id);
});
});
});
});
describe('cleanDatasource', function() {
helpers.describe.staticMethod('create', function() {
helpers.beforeEach.withArgs({foo: 'bar'});
helpers.describe.whenCalledRemotely('POST', '/xxx-test-models', function() {
it('should call the method over rest', function () {
assert.equal(this.res.statusCode, 200);
});
});
});
helpers.describe.staticMethod('findById', function() {
helpers.beforeEach.givenModel('xxx-test-model', {foo: 'bar'});
helpers.beforeEach.cleanDatasource();
helpers.describe.whenCalledRemotely('GET', function () {
return '/xxx-test-models/' + this['xxx-test-model'].id;
}, function() {
it('should not find the given model', function () {
assert.equal(this.res.statusCode, 404);
});
});
});
});
});