This repository was archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest.js
More file actions
151 lines (138 loc) · 5.17 KB
/
test.js
File metadata and controls
151 lines (138 loc) · 5.17 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
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.model(loopback.User, { dataSource: 'db'});
testApp.model(loopback.Role, { dataSource: 'db'});
testApp.model(loopback.RoleMapping, { dataSource: 'db'});
testApp.use(loopback.rest());
helpers.beforeEach.withApp(testApp);
describe('helpers.it', function() {
['shouldBeAllowed',
'shouldBeDenied',
'shouldNotBeFound',
'shouldBeAllowedWhenCalledAnonymously',
'shouldBeDeniedWhenCalledAnonymously',
'shouldBeAllowedWhenCalledUnauthenticated',
'shouldBeDeniedWhenCalledUnauthenticated',
'shouldBeAllowedWhenCalledByUser',
'shouldBeDeniedWhenCalledByUser']
.forEach(function(func) {
it('should have a method named ' + func, function () {
assert.equal(typeof helpers.it[func], 'function');
});
});
});
describe('helpers.describe', function() {
['staticMethod',
'instanceMethod',
'whenLoggedInAsUser',
'whenCalledByUser',
'whenCalledAnonymously',
'whenCalledUnauthenticated']
.forEach(function(func) {
it('should have a method named ' + func, function () {
assert.equal(typeof helpers.describe[func], 'function');
});
});
});
describe('helpers.beforeEach', function() {
['withArgs',
'givenModel',
'givenUser',
'givenLoggedInUser',
'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);
});
});
});
});
describe('givenUserWithRole', function() {
helpers.beforeEach.givenUserWithRole(
{id: 1, email: "abc@abc.com", password: "abc"},
{id: 2, name: "testRole"});
it("should create a user instance with default userModel with the given role", function() {
assert.equal(this['User'].id, 1);
assert.equal(this.userRole.id, 2);
assert.equal(this.userRole.name, "testRole");
assert(this.userRoleMapping);
});
});
describe('withUserModel', function() {
helpers.beforeEach.withUserModel('xxx-test-model');
it("should set the user model name", function() {
assert.equal(this.userModel, 'xxx-test-model');
});
describe('givenUser', function() {
helpers.beforeEach.givenUser();
it("should create a new instance of specified User model", function() {
assert(this[this.userModel]);
});
});
describe('givenUserWithRole', function() {
helpers.beforeEach.givenUserWithRole({id: 1}, {id: 2, name: "testRole"});
it("should create a user instance (of specified User model) with the given role", function() {
assert.equal(this[this.userModel].id, 1);
assert.equal(this.userRole.id, 2);
assert.equal(this.userRole.name, "testRole");
assert(this.userRoleMapping);
});
});
});
});