forked from deployd/deployd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser-collection.unit.js
160 lines (149 loc) · 4.68 KB
/
user-collection.unit.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var UserCollection = require('../lib/resources/user-collection')
, db = require('../lib/db');
describe('UserCollection', function() {
describe('.handle(ctx)', function() {
beforeEach(function() {
var test = this;
var properties = {
name: {type: 'string'}
};
this.uc = new UserCollection('users', {db: db.create(TEST_DB), config: {properties: properties}});
this.ctx = {
req: {url: '/users', body: {}},
res: {},
done: function(err, res) {
if(test.complete) test.complete(err, res);
}
};
});
it('should login a user when credentials are POSTed to "/login"', function(done) {
var test = this;
this.ctx.url = '/login';
this.ctx.query = {};
this.ctx.session = {
set: function(changes) {
expect(changes).to.eql({uid: '123', path: '/users'});
return this;
},
save: function(fn) {
expect(fn).to.be.a('function');
fn();
}
};
this.ctx.req.url = '/users/login';
this.ctx.req.method = 'POST';
this.ctx.req.body.email = 'foo@bar.com';
this.ctx.req.body.password = 'abcd';
this.uc.store.find = function(query, fn) {
expect(query).to.eql({email: 'foo@bar.com', password: 'abcd'});
fn(null, {id: '123', email: 'foo@bar.com'});
};
this.complete = function(err, res) {
done();
};
this.uc.handle(this.ctx);
});
it('should logout a user when "/logout" is requested', function(done) {
var test = this;
var removed = false;
this.ctx.url = '/logout';
this.ctx.session = {
remove: function(fn) {
removed = true;
expect(fn).to.be.a('function');
fn();
}
};
this.ctx.req.url = '/users/logout';
this.ctx.req.method = 'POST';
this.ctx.req.body.email = 'foo@bar.com';
this.ctx.req.body.password = 'abcd';
this.complete = function(err, res) {
expect(removed).to.equal(true);
done();
};
this.uc.handle(this.ctx);
});
it('should perform GET with including $fields', function(done) {
this.ctx.url = '/';
this.ctx.query = {$fields: {username: 1, email: 1}};
this.ctx.req.url = '/users';
this.ctx.req.method = 'GET';
function test(e, r) {
expect(e).to.not.exist;
this.uc.handle(this.ctx);
}
this.complete = function(err, res) {
expect(err).to.not.exist;
expect(res[0]).to.have.keys(['id','username', 'email']);
expect(res[0].username).to.eql('foo');
expect(res[0].email).to.eql('foo@bar.com');
this.uc.store.remove(function (err) {
done(err);
});
};
var testData = {username: 'foo', password: 'abcd', email:'foo@bar.com', name: 'foo'};
this.uc.store.insert(testData, test.bind(this));
});
it('should perform GET with excluding $fields', function(done) {
this.ctx.url = '/';
this.ctx.query = {$fields: {username: 0, email: 0}};
this.ctx.req.url = '/users';
this.ctx.req.method = 'GET';
function test(e, r) {
expect(e).to.not.exist;
this.uc.handle(this.ctx);
}
this.complete = function(err, res) {
expect(err).to.not.exist;
expect(res[0]).to.have.keys(['id','name']);
expect(res[0].name).to.eql('foo');
this.uc.store.remove(function (err) {
done(err);
});
};
var testData = {username: 'foo', password: 'abcd', email:'foo@bar.com', name: 'foo'};
this.uc.store.insert(testData, test.bind(this));
});
it('should hide password when $fields{password: 1}', function(done) {
this.ctx.url = '/';
this.ctx.query = {$fields: {password: 1}};
this.ctx.req.url = '/users';
this.ctx.req.method = 'GET';
function test(e, r) {
expect(e).to.not.exist;
this.uc.handle(this.ctx);
}
this.complete = function(err, res) {
expect(err).to.not.exist;
expect(res[0]).to.have.keys(['id','name', 'email', 'username']);
this.uc.store.remove(function (err) {
done(err);
});
};
var testData = {username: 'foo', password: 'abcd', email:'foo@bar.com', name: 'foo'};
this.uc.store.insert(testData, test.bind(this));
});
});
describe('.handleSession(ctx)', function() {
it('should attach the current user to the session', function(done) {
var properties = {
name: {type: 'string'}
};
var uc = new UserCollection('users', {db: db.create(TEST_DB), config: { properties: properties } } );
var ctx = {
session: {data: {path: '/users', uid: '123'}}
};
var found = false;
uc.store.find = function(query, fn) {
expect(query).to.eql({id: '123', $fields: {password: 0}});
found = true;
fn();
};
uc.handleSession(ctx, function() {
expect(found).to.equal(true);
done();
});
});
});
});