liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
 * Copyright JS Foundation and other contributors, http://js.foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
 
var should = require("should");
var sinon = require('sinon');
 
var NR_TEST_UTILS = require("nr-test-utils");
 
var Users = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/users");
 
describe("api/auth/users", function() {
    after(function() {
        Users.init({});
    })
    describe('Initalised with a credentials object, no anon',function() {
        before(function() {
            Users.init({
                type:"credentials",
                users:{
                    username:"fred",
                    password:'$2a$08$LpYMefvGZ3MjAfZGzcoyR.1BcfHh4wy4NpbN.cEny5aHnWOqjKOXK',
                    // 'password' -> require('bcryptjs').hashSync('password', 8);
                    permissions:"*"
                }
            });
        });
        describe('#get',function() {
            it('returns known user',function(done) {
                Users.get("fred").then(function(user) {
                    try {
                        user.should.have.a.property("username","fred");
                        user.should.have.a.property("permissions","*");
                        user.should.not.have.a.property("password");
                        done();
                    } catch(err) {
                        done(err);
                    }
                });
            });
 
            it('returns null for unknown user', function(done) {
                Users.get("barney").then(function(user) {
                    try {
                        should.not.exist(user);
                        done();
                    } catch(err) {
                        done(err);
                    }
                });
            });
        });
 
        describe('#default',function() {
            it('returns null for default user', function(done) {
                Users.default().then(function(user) {
                    try {
                        should.not.exist(user);
                        done();
                    } catch(err) {
                        done(err);
                    }
                });
            });
        });
 
        describe('#authenticate',function() {
            it('authenticates a known user', function(done) {
                Users.authenticate('fred','password').then(function(user) {
                    try {
                        user.should.have.a.property("username","fred");
                        user.should.have.a.property("permissions","*");
                        user.should.not.have.a.property("password");
                        done();
                    } catch(err) {
                        done(err);
                    }
                });
            });
            it('rejects invalid password for a known user', function(done) {
                Users.authenticate('fred','wrong').then(function(user) {
                    try {
                        should.not.exist(user);
                        done();
                    } catch(err) {
                        done(err);
                    }
                });
            });
 
            it('rejects invalid user', function(done) {
                Users.authenticate('barney','wrong').then(function(user) {
                    try {
                        should.not.exist(user);
                        done();
                    } catch(err) {
                        done(err);
                    }
                });
            });
        });
    });
 
    describe('Initalised with a credentials object including anon',function() {
        before(function() {
            Users.init({
                type:"credentials",
                users:[],
                default: { permissions: "*" }
            });
        });
        describe('#default',function() {
            it('returns default user', function(done) {
                Users.default().then(function(user) {
                    try {
                        user.should.have.a.property('anonymous',true);
                        user.should.have.a.property('permissions','*');
                        done();
                    } catch(err) {
                        done(err);
                    }
                });
            });
        });
    });
 
    describe('Initialised with a credentials object with user functions',function() {
       var authUsername = '';
       var authPassword = '';
       before(function() {
           Users.init({
               type:"credentials",
               users:function(username) {
                   return Promise.resolve({'username':'dave','permissions':'read'});
               },
               authenticate: function(username,password) {
                   authUsername = username;
                   authPassword = password;
                   return Promise.resolve({'username':'pete','permissions':'write'});
               }
           });
       });
 
        describe('#get',function() {
            it("returns null for tokenHeader", function() {
                should.not.exist(Users.tokenHeader());
            });
 
            it('delegates get user',function(done) {
                Users.get('dave').then(function(user) {
                    try {
                        user.should.have.a.property("username","dave");
                        user.should.have.a.property("permissions","read");
                        user.should.not.have.a.property("password");
                        done();
                    } catch(err) {
                        done(err);
                    }
                });
            });
            it('delegates authenticate user',function(done) {
                Users.authenticate('pete','secret').then(function(user) {
                    try {
                        user.should.have.a.property("username","pete");
                        user.should.have.a.property("permissions","write");
                        user.should.not.have.a.property("password");
                        authUsername.should.equal('pete');
                        authPassword.should.equal('secret');
                        done();
                    } catch(err) {
                        done(err);
                    }
                });
            });
        });
    });
 
    describe('Initialised with bad settings to test else cases',function() {
        before(function() {
            Users.init({
                type:"foo",
                users:{
                        username:"fred",
                        password:'$2a$08$LpYMefvGZ3MjAfZGzcoyR.1BcfHh4wy4NpbN.cEny5aHnWOqjKOXK',
                        permissions:"*"
                    }
            });
        });
        describe('#get',function() {
            it('should fail to return user fred',function(done) {
                Users.get("fred").then(function(userf) {
                    try {
                        should.not.exist(userf);
                        done();
                    } catch(err) {
                        done(err);
                    }
                });
            });
        });
    });
 
    describe('Initialised with default set as function',function() {
        before(function() {
            Users.init({
                type:"credentials",
                default: function() { return("Done"); }
            });
        });
        after(function() {
            Users.init({});
        });
        describe('#default',function() {
            it('handles api.default being a function',function(done) {
                Users.should.have.property('default').which.is.a.Function();
                (Users.default()).should.equal("Done");
                done();
            });
        });
    });
 
    describe('Initialised with tokens set as function',function() {
        before(function() {
            Users.init({
                type:"strategy",
                tokens: function(token) { return("Done-"+token); }
            });
        });
        after(function() {
            Users.init({});
        });
        describe('#tokens',function() {
            it('handles api.tokens being a function',function(done) {
                Users.should.have.property('tokens').which.is.a.Function();
                (Users.tokens("1234")).should.equal("Done-1234");
                (Users.tokenHeader()).should.equal("authorization");
                done();
            });
        });
    });
 
    describe('Initialised with tokens set as function and tokenHeader set as token header name',function() {
        before(function() {
            Users.init({
                type:"strategy",
                tokens: function(token) { return("Done-"+token); },
                tokenHeader: "X-TEST-TOKEN"
            });
        });
        after(function() {
            Users.init({});
        });
        describe('#tokens',function() {
            it('handles api.tokens being a function and api.tokenHeader being a header name',function(done) {
                Users.should.have.property('tokens').which.is.a.Function();
                (Users.tokens("1234")).should.equal("Done-1234");
                Users.should.have.property('tokenHeader').which.is.a.Function();
                (Users.tokenHeader()).should.equal("x-test-token");
                done();
            });
        });
    });
 
});