|
1 | 1 | import { expect } from 'chai';
|
2 | 2 |
|
3 |
| -import { switchMap, map, tap, catchError, mergeMap, toArray } from 'rxjs/operators'; |
| 3 | +import { switchMap, map, tap, catchError, mergeMap, toArray, concatMap } from 'rxjs/operators'; |
4 | 4 |
|
5 | 5 | import { CachedDB, mongodbService } from '../api/service';
|
6 | 6 | import { config } from '../api/config';
|
7 |
| -import { connectObs, dropObs, insertManyObs } from 'observable-mongo'; |
| 7 | +import { connectObs, dropObs, insertManyObs, deleteObs } from 'observable-mongo'; |
8 | 8 | import { ServiceNames } from '../service-names';
|
9 | 9 | import { ERRORS } from '../api/errors';
|
10 |
| -import { of, from } from 'rxjs'; |
| 10 | +import { of, from, forkJoin } from 'rxjs'; |
11 | 11 | import { getPasswordHash$ } from '../lib/observables';
|
12 | 12 | import { Collection } from 'mongodb';
|
| 13 | +import { addUsersWithRole } from '../api/authentication-api'; |
13 | 14 |
|
14 |
| -describe('Authentication operations', () => { |
| 15 | +describe('1.0 - Authentication operations', () => { |
15 | 16 | it('loads the users collection and then authenticates one valid user', done => {
|
16 | 17 | const USERS = [{ user: 'abc', pwd: 'cde' }, { user: '123', pwd: '456' }];
|
17 | 18 | const validCredentials = USERS[0];
|
@@ -71,3 +72,137 @@ describe('Authentication operations', () => {
|
71 | 72 | export function laodUsers(usersColl: Collection<any>, users: any[]) {
|
72 | 73 | return dropObs(usersColl).pipe(switchMap(() => insertManyObs(users, usersColl)));
|
73 | 74 | }
|
| 75 | + |
| 76 | +describe('1.1 - Voting Event Authentication operations', () => { |
| 77 | + it('load some users from file with no pwd specified and then authenticate some of them', done => { |
| 78 | + const VOTING_EVENT_USERS = [ |
| 79 | + { user: 'Mary', role: 'architect' }, |
| 80 | + { user: 'Mary', role: 'admin' }, |
| 81 | + { user: 'John', role: 'architect' }, |
| 82 | + ]; |
| 83 | + |
| 84 | + const cachedDb: CachedDB = { dbName: config.dbname, client: null, db: null }; |
| 85 | + |
| 86 | + let _client; |
| 87 | + let _userColl; |
| 88 | + const votingEventName = 'event to test login of users'; |
| 89 | + let votingEventId: string; |
| 90 | + |
| 91 | + const firstTimePwd = 'I am the password used for the first login'; |
| 92 | + let errorMissingRoleEncountered = false; |
| 93 | + let errorWrongPwdEncountered = false; |
| 94 | + let errorUserUnknownEncountered = false; |
| 95 | + |
| 96 | + connectObs(config.mongoUri) |
| 97 | + // clean the test data |
| 98 | + .pipe( |
| 99 | + tap(client => { |
| 100 | + _client = client; |
| 101 | + _userColl = client.db(config.dbname).collection(config.usersCollection); |
| 102 | + }), |
| 103 | + concatMap(() => forkJoin(VOTING_EVENT_USERS.map(user => deleteObs({ user: user.user }, _userColl)))), |
| 104 | + concatMap(() => addUsersWithRole(_userColl, VOTING_EVENT_USERS)), |
| 105 | + concatMap(() => |
| 106 | + mongodbService(cachedDb, ServiceNames.getVotingEvents).pipe( |
| 107 | + map(votingEvents => votingEvents.filter(ve => ve.name === votingEventName)), |
| 108 | + concatMap(votingEvents => { |
| 109 | + const votingEventsDeleteObs = votingEvents.map(ve => |
| 110 | + mongodbService(cachedDb, ServiceNames.cancelVotingEvent, { _id: ve._id, hard: true }), |
| 111 | + ); |
| 112 | + return votingEvents.length > 0 ? forkJoin(votingEventsDeleteObs) : of(null); |
| 113 | + }), |
| 114 | + ), |
| 115 | + ), |
| 116 | + concatMap(() => mongodbService(cachedDb, ServiceNames.createVotingEvent, { name: votingEventName })), |
| 117 | + tap(id => (votingEventId = id.toHexString())), |
| 118 | + ) |
| 119 | + // run the real test logic |
| 120 | + .pipe( |
| 121 | + // I do the first login - no password yet set |
| 122 | + concatMap(() => { |
| 123 | + const user = VOTING_EVENT_USERS[0].user; |
| 124 | + return mongodbService(cachedDb, ServiceNames.authenticateForVotingEvent, { |
| 125 | + user, |
| 126 | + pwd: firstTimePwd, |
| 127 | + votingEventId, |
| 128 | + }); |
| 129 | + }), |
| 130 | + tap(({ token, pwdInserted }) => { |
| 131 | + expect(token).to.be.not.undefined; |
| 132 | + expect(pwdInserted).to.be.true; |
| 133 | + }), |
| 134 | + // I do a second login - the password has been already set and enchripted |
| 135 | + concatMap(() => { |
| 136 | + const user = VOTING_EVENT_USERS[0].user; |
| 137 | + return mongodbService(cachedDb, ServiceNames.authenticateForVotingEvent, { |
| 138 | + user, |
| 139 | + pwd: firstTimePwd, |
| 140 | + votingEventId, |
| 141 | + }); |
| 142 | + }), |
| 143 | + tap(({ token, pwdInserted }) => { |
| 144 | + expect(token).to.be.not.undefined; |
| 145 | + expect(pwdInserted).to.be.false; |
| 146 | + }), |
| 147 | + // I do a login requesting a role I do not have |
| 148 | + concatMap(() => { |
| 149 | + const user = VOTING_EVENT_USERS[0].user; |
| 150 | + return mongodbService(cachedDb, ServiceNames.authenticateForVotingEvent, { |
| 151 | + user, |
| 152 | + pwd: firstTimePwd, |
| 153 | + role: 'the boss', |
| 154 | + votingEventId, |
| 155 | + }); |
| 156 | + }), |
| 157 | + catchError(err => { |
| 158 | + errorMissingRoleEncountered = true; |
| 159 | + expect(err).to.equal(ERRORS.userWithNotTheReuqestedRole); |
| 160 | + return of(null); |
| 161 | + }), |
| 162 | + // I do a login with a wrong pwd |
| 163 | + concatMap(() => { |
| 164 | + const user = VOTING_EVENT_USERS[0].user; |
| 165 | + return mongodbService(cachedDb, ServiceNames.authenticateForVotingEvent, { |
| 166 | + user, |
| 167 | + pwd: 'wrong pwd', |
| 168 | + votingEventId, |
| 169 | + }); |
| 170 | + }), |
| 171 | + catchError(err => { |
| 172 | + errorWrongPwdEncountered = true; |
| 173 | + expect(err).to.equal(ERRORS.pwdInvalid); |
| 174 | + return of(null); |
| 175 | + }), |
| 176 | + // I do a login with a no existing user |
| 177 | + concatMap(() => { |
| 178 | + return mongodbService(cachedDb, ServiceNames.authenticateForVotingEvent, { |
| 179 | + user: 'I do not exist', |
| 180 | + pwd: 'pwd', |
| 181 | + votingEventId, |
| 182 | + }); |
| 183 | + }), |
| 184 | + catchError(err => { |
| 185 | + errorUserUnknownEncountered = true; |
| 186 | + expect(err).to.equal(ERRORS.userUnknown); |
| 187 | + return of(err); |
| 188 | + }), |
| 189 | + ) |
| 190 | + .subscribe( |
| 191 | + () => {}, |
| 192 | + err => { |
| 193 | + console.error(err); |
| 194 | + cachedDb.client.close(); |
| 195 | + _client.close(); |
| 196 | + done(err); |
| 197 | + }, |
| 198 | + () => { |
| 199 | + expect(errorMissingRoleEncountered).to.be.true; |
| 200 | + expect(errorWrongPwdEncountered).to.be.true; |
| 201 | + expect(errorUserUnknownEncountered).to.be.true; |
| 202 | + cachedDb.client.close(); |
| 203 | + _client.close(); |
| 204 | + done(); |
| 205 | + }, |
| 206 | + ); |
| 207 | + }).timeout(10000); |
| 208 | +}); |
0 commit comments