-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
139 lines (122 loc) · 4.26 KB
/
test.js
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
'use strict';
const expect = require('chai').expect;
const plugin = require('./index.js');
describe('Normal behaviour', () => {
let schema, filterMethod;
let model = {
username: 'JamyDev',
password: 'Pass123',
email: '[email protected]',
id: 'asdfasdfadf',
tfa: true
};
before(() => {
schema = {
tree: {
id: {
type: String
},
username: {
type: String,
scopes: ['public']
},
password: {
type: String,
scopes: []
},
email: {
type: String,
scopes: ['private', 'writable']
},
tfa: {
type: Boolean,
scopes: ['writable']
}
},
options: {
defaultScope: 'public'
},
static: (token, method) => model.getScoped = (scopes) => method(model, scopes),
method: () => {}
};
plugin(schema);
});
it('should return default properties', () => {
expect(model.getScoped()).to.deep.equal({ id: 'asdfasdfadf', username: 'JamyDev' });
});
it('should return the id always', () => {
expect(model.getScoped()).to.have.property('id');
expect(model.getScoped('private')).to.have.property('id');
expect(model.getScoped('writable')).to.have.property('id');
});
it('should return properties for multiple scopes', () => {
expect(model.getScoped(['public', 'writable'])).to.deep.equal({ id: 'asdfasdfadf', username: 'JamyDev', email: '[email protected]', tfa: true });
});
it('should ignore invalid calls', () => {
expect(model.getScoped(true)).to.equal(null);
expect(model.getScoped({object: true})).to.equal(null);
expect(model.getScoped(NaN)).to.equal(null);
});
it('should ignore invalid scopes', () => {
expect(model.getScoped('this is not a scope')).to.deep.equal({ id: 'asdfasdfadf' });
expect(model.getScoped(['\' AND 1=1 %', 'false'])).to.deep.equal({ id: 'asdfasdfadf' });
});
});
describe('No default', () => {
let schema, filterMethod;
let model = {
username: 'JamyDev',
password: 'Pass123',
email: '[email protected]',
id: 'asdfasdfadf',
tfa: true
};
before(() => {
schema = {
tree: {
id: {
type: String
},
username: {
type: String,
scopes: ['public']
},
password: {
type: String,
scopes: []
},
email: {
type: String,
scopes: ['private', 'writable']
},
tfa: {
type: Boolean,
scopes: ['writable']
}
},
static: (token, method) => model.getScoped = (scopes) => method(model, scopes),
method: () => {}
};
plugin(schema);
});
it('should not return default properties', () => {
expect(model.getScoped()).to.equal(null);
});
it('should return the id always', () => {
expect(model.getScoped()).to.equal(null);
expect(model.getScoped('private')).to.have.property('id');
expect(model.getScoped('writable')).to.have.property('id');
});
it('should return properties for multiple scopes', () => {
expect(model.getScoped(['public', 'writable'])).to.deep.equal({ id: 'asdfasdfadf', username: 'JamyDev', email: '[email protected]', tfa: true });
});
it('should ignore invalid calls', () => {
expect(model.getScoped(true)).to.equal(null);
expect(model.getScoped({object: true})).to.equal(null);
expect(model.getScoped(NaN)).to.equal(null);
});
it('should ignore invalid scopes', () => {
expect(model.getScoped('this is not a scope')).to.deep.equal({ id: 'asdfasdfadf' });
expect(model.getScoped(['\' AND 1=1 %', 'false'])).to.deep.equal({ id: 'asdfasdfadf' });
});
});