Skip to content

Commit 21a1633

Browse files
author
Frederic Lavigne
committed
check if organization specified during login matches the user organization
#58
1 parent 610a397 commit 21a1633

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

.csignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
22
ui-react/node_modules
3+
design
4+
phone

config/passport.js

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ module.exports = function(passport, appEnv, readyCallback) {
6767

6868
// user was found, now determine if password matches
6969
const user = result.docs[0];
70+
71+
// ensure that the right organization is specified if needed
72+
if (user.organization !== req.body.organization) {
73+
console.log('Invalid organization specified');
74+
return done(null, null, 'Invalid organization');
75+
}
76+
7077
if (bcrypt.compareSync(password, user.password)) {
7178
console.log('Password matches');
7279
return done(null, user, null);

test/users.spec.js

+50
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ describe('Users', () => {
77
const username = `john-${new Date().getTime()}@acme.com`;
88
const password = '123';
99

10+
const orgUsername = `jim-${new Date().getTime()}@insurance.com`;
11+
const orgPassword = '456';
12+
const organization = `org-${new Date().getTime()}`;
13+
1014
let app;
1115
let api;
16+
let apiAnon;
1217

1318
before((done) => {
1419
require('../server')((err, readyApp) => {
1520
app = readyApp;
1621
api = supertest.agent(app); // .agent() persists cookies between calls
22+
apiAnon = supertest(app);
1723
done();
1824
});
1925
});
@@ -74,6 +80,17 @@ describe('Users', () => {
7480
});
7581
});
7682

83+
it('can not log in with an organization if it is not part of one', (done) => {
84+
api.post('/api/users/login')
85+
.send(`email=${username}`)
86+
.send(`password=${password}`)
87+
.send('organization=anOrg')
88+
.expect(401)
89+
.end((err) => {
90+
done(err);
91+
});
92+
});
93+
7794
it('can ensure if it is logged', (done) => {
7895
api.get('/api/users/isLoggedIn')
7996
.expect(200)
@@ -83,4 +100,37 @@ describe('Users', () => {
83100
done(err);
84101
});
85102
});
103+
104+
105+
it('can register an organization account', (done) => {
106+
apiAnon.post('/api/users/signup')
107+
.send(`email=${orgUsername}`)
108+
.send(`password=${orgPassword}`)
109+
.send(`organization=${organization}`)
110+
.expect(200)
111+
.end((err) => {
112+
done(err);
113+
});
114+
});
115+
116+
it('can login by specifying its organization', (done) => {
117+
apiAnon.post('/api/users/login')
118+
.send(`email=${orgUsername}`)
119+
.send(`password=${orgPassword}`)
120+
.send(`organization=${organization}`)
121+
.expect(200)
122+
.end((err) => {
123+
done(err);
124+
});
125+
});
126+
127+
it('can not login without specifying its organization', (done) => {
128+
apiAnon.post('/api/users/login')
129+
.send(`email=${orgUsername}`)
130+
.send(`password=${orgPassword}`)
131+
.expect(401)
132+
.end((err) => {
133+
done(err);
134+
});
135+
});
86136
});

0 commit comments

Comments
 (0)