Skip to content

Commit

Permalink
Modify indentation to match rest of code.
Browse files Browse the repository at this point in the history
Change test function to async and use await on function calls.
Add test team using the teamsService instead of the TeamModel.
  • Loading branch information
ElJocko committed Jun 1, 2023
1 parent 0861f34 commit d300d5d
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 104 deletions.
123 changes: 62 additions & 61 deletions app/services/user-accounts-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});
};
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);
}
}
});
};
89 changes: 46 additions & 43 deletions app/tests/api/user-accounts/user-accounts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d300d5d

Please sign in to comment.