diff --git a/app/services/user-accounts-service.js b/app/services/user-accounts-service.js index d8d32528..3a910926 100644 --- a/app/services/user-accounts-service.js +++ b/app/services/user-accounts-service.js @@ -336,67 +336,68 @@ exports.addCreatedByUserAccountToAll = async function(attackObjects) { } } -exports.retrieveTeamsByUserId = function(userAccountId ,options, callback) { - if (!userAccountId) { - const error = new Error(errors.missingParameter); - error.parameterName = 'userId'; - return callback(error); - } +exports.retrieveTeamsByUserId = function(userAccountId, options, callback) { + if (!userAccountId) { + const error = new Error(errors.missingParameter); + error.parameterName = 'userId'; + return callback(error); + } + + // Build the aggregation + const aggregation = [ + { $sort: { 'name': 1 } }, + ]; - // Build the aggregation - const aggregation = [ - { $sort: { 'name': 1 } }, - ]; + const match = { + $match: { + userIDs: { $in: [userAccountId] } + } + }; + + aggregation.push(match); - const match = { - $match: { - userIDs: {$in : [userAccountId]} + const facet = { + $facet: { + totalCount: [{ $count: 'totalCount' }], + documents: [] + } + }; + if (options.offset) { + facet.$facet.documents.push({ $skip: options.offset }); } - }; - - aggregation.push(match); - - const facet = { - $facet: { - totalCount: [ { $count: 'totalCount' }], - documents: [ ] - } - }; - if (options.offset) { - facet.$facet.documents.push({ $skip: options.offset }); - } - else { - facet.$facet.documents.push({ $skip: 0 }); - } - if (options.limit) { - facet.$facet.documents.push({ $limit: options.limit }); - } - aggregation.push(facet); - - // Retrieve the documents - Team.aggregate(aggregation, function(err, results) { - if (err) { - return callback(err); - } - else { - const teams = results[0].documents; - if (options.includePagination) { - let derivedTotalCount = 0; - if (results[0].totalCount.length > 0) { - derivedTotalCount = results[0].totalCount[0].totalCount; - } - const returnValue = { - pagination: { - total: derivedTotalCount, - offset: options.offset, - limit: options.limit - }, - data: teams - }; - return callback(null, returnValue); - } else { - return callback(null, teams); - } - } - }); -}; \ No newline at end of file + else { + facet.$facet.documents.push({ $skip: 0 }); + } + if (options.limit) { + facet.$facet.documents.push({ $limit: options.limit }); + } + aggregation.push(facet); + + // Retrieve the documents + Team.aggregate(aggregation, function (err, results) { + if (err) { + return callback(err); + } + else { + const teams = results[0].documents; + if (options.includePagination) { + let derivedTotalCount = 0; + if (results[0].totalCount.length > 0) { + derivedTotalCount = results[0].totalCount[0].totalCount; + } + const returnValue = { + pagination: { + total: derivedTotalCount, + offset: options.offset, + limit: options.limit + }, + data: teams + }; + return callback(null, returnValue); + } + else { + return callback(null, teams); + } + } + }); +}; diff --git a/app/tests/api/user-accounts/user-accounts.spec.js b/app/tests/api/user-accounts/user-accounts.spec.js index 495bef54..7a156a54 100644 --- a/app/tests/api/user-accounts/user-accounts.spec.js +++ b/app/tests/api/user-accounts/user-accounts.spec.js @@ -8,6 +8,7 @@ const database = require('../../../lib/database-in-memory'); const databaseConfiguration = require('../../../lib/database-configuration'); const UserAccount = require('../../../models/user-account-model'); const Team = require('../../../models/team-model'); +const teamsService = require('../../../services/teams-service'); const login = require('../../shared/login'); @@ -318,50 +319,52 @@ describe('User Accounts API', function () { }); }); - it('GET /api/user-accounts/{id}/teams returns an array with the list of accounts a user is associated with, empty array if no teams are found', function (done) { - request(app) - .get(`/api/user-accounts/${anonymousUserId}/teams`) - .set('Accept', 'application/json') - .set('Cookie', `${ login.passportCookieName }=${ passportCookie.value }`) - .expect(200) - .expect('Content-Type', /json/) - .end(function (err, res) { - if (err) { - done(err); - } else { - const teams = res.body; - expect(teams).toBeDefined(); - expect(Array.isArray(teams)).toBe(true); - expect(teams.length).toBe(0); - done(); - } - }); - }); - - it('GET /api/user-accounts/{id}/teams returns an array with the list of accounts a user is associated with', function (done) { - Team.create({userIDs:[anonymousUserId], name: 'Example', created: new Date(), modified: new Date()}).then(newTeam => { + it('GET /api/user-accounts/{id}/teams should return an empty array when no teams have been associated with a user', function (done) { request(app) - .get(`/api/user-accounts/${anonymousUserId}/teams`) - .set('Accept', 'application/json') - .set('Cookie', `${ login.passportCookieName }=${ passportCookie.value }`) - .expect(200) - .expect('Content-Type', /json/) - .end(function (err, res) { - if (err) { - done(err); - } else { - const teams = res.body; - expect(teams).toBeDefined(); - expect(Array.isArray(teams)).toBe(true); - expect(teams.length).toBe(1); - expect(teams[0].name).toBe(newTeam.name); - expect(teams[0].userIDs.length).toBe(1); - expect(teams[0].userIDs[0]).toBe(anonymousUserId); - done(); - } - }); - }); - }); + .get(`/api/user-accounts/${ anonymousUserId }/teams`) + .set('Accept', 'application/json') + .set('Cookie', `${ login.passportCookieName }=${ passportCookie.value }`) + .expect(200) + .expect('Content-Type', /json/) + .end(function (err, res) { + if (err) { + done(err); + } else { + const teams = res.body; + expect(teams).toBeDefined(); + expect(Array.isArray(teams)).toBe(true); + expect(teams.length).toBe(0); + done(); + } + }); + }); + + it('GET /api/user-accounts/{id}/teams should return an array with the list of teams a user is associated with', async function () { + // Add a new team to the database with the anonymous user as a member + const timestamp = new Date().toISOString(); + const teamData = { + userIDs: [ anonymousUserId ], + name: 'Example Team', + created: timestamp, + modified: timestamp + }; + await teamsService.create(teamData); + + const res = await request(app) + .get(`/api/user-accounts/${ anonymousUserId }/teams`) + .set('Accept', 'application/json') + .set('Cookie', `${login.passportCookieName}=${passportCookie.value}`) + .expect(200) + .expect('Content-Type', /json/) + + const teams = res.body; + expect(teams).toBeDefined(); + expect(Array.isArray(teams)).toBe(true); + expect(teams.length).toBe(1); + expect(teams[0].name).toBe(teamData.name); + expect(teams[0].userIDs.length).toBe(1); + expect(teams[0].userIDs[0]).toBe(anonymousUserId); + }); after(async function() { await database.closeConnection();