diff --git a/app/Models/Hooks/ProjectForceDelete.js b/app/Models/Hooks/ProjectForceDelete.js index 474db2d..14affd2 100644 --- a/app/Models/Hooks/ProjectForceDelete.js +++ b/app/Models/Hooks/ProjectForceDelete.js @@ -12,26 +12,26 @@ const ProjectForceDelete = exports = module.exports = {}; */ ProjectForceDelete.removeSprints = async (project) => { - - // query the object again, with sprints - let projectQueried = await Project.with(['sprints']).find(project._id); - - // call the relationship - let sprints = await projectQueried.sprints().fetch(); - - // check for any matching sprints - if (sprints.rows.length > 0) - { - - // loop for the rows count. - for (let i = 0; i < sprints.rows.length; i++) - { - - // delete the sprint. - await sprints.rows[i].delete(); - } - - } + + // query the object again, with sprints + let projectQueried = await Project.with(['sprints']).find(project._id); + + // call the relationship + let sprints = await projectQueried.sprints().fetch(); + + // check for any matching sprints + if (sprints.rows.length > 0) + { + + // loop for the rows count. + for (let i = 0; i < sprints.rows.length; i++) + { + + // delete the sprint. + await sprints.rows[i].delete(); + } + + } }; /** @@ -40,27 +40,27 @@ ProjectForceDelete.removeSprints = async (project) => * @param project * @return {Promise} */ -ProjectForceDelete.removeReleases = async(project) => +ProjectForceDelete.removeReleases = async (project) => { - - // find the model - let projectQueried = await Project.with(['releases']).find(project._id); - - // fetch the relationships - let releases = await projectQueried.releases().fetch(); - - // check for the array - if (releases.rows.length > 0) - { - - // loop the instances - for (let i = 0; i < releases.rows.length; i++) - { - - // delete the model - await releases.rows[i].delete(); - } - - } - + + // find the model + let projectQueried = await Project.with(['releases']).find(project._id); + + // fetch the relationships + let releases = await projectQueried.releases().fetch(); + + // check for the array + if (releases.rows.length > 0) + { + + // loop the instances + for (let i = 0; i < releases.rows.length; i++) + { + + // delete the model + await releases.rows[i].delete(); + } + + } + }; diff --git a/app/Models/Hooks/SprintForceDelete.js b/app/Models/Hooks/SprintForceDelete.js index 7c94bda..410db7f 100644 --- a/app/Models/Hooks/SprintForceDelete.js +++ b/app/Models/Hooks/SprintForceDelete.js @@ -12,26 +12,26 @@ const SprintForceDelete = exports = module.exports = {}; */ SprintForceDelete.removeTickets = async (sprint) => { - - // Get the model instance - let sprintQueried = await Sprint.with(['tickets']).find(sprint._id); - - // get the model instance with with the related model instances - let tickets = await sprintQueried.tickets().fetch(); - - // get the rows count - if (tickets.rows.length > 0) - { - - // run the delete operation for every related instance - for (let i = 0; i < tickets.rows.length; i++) - { - - // delete the model. - await tickets.rows[i].delete(); - - } - - } - + + // Get the model instance + let sprintQueried = await Sprint.with(['tickets']).find(sprint._id); + + // get the model instance with with the related model instances + let tickets = await sprintQueried.tickets().fetch(); + + // get the rows count + if (tickets.rows.length > 0) + { + + // run the delete operation for every related instance + for (let i = 0; i < tickets.rows.length; i++) + { + + // delete the model. + await tickets.rows[i].delete(); + + } + + } + }; diff --git a/app/Models/Hooks/UserForceDeleteHook.js b/app/Models/Hooks/UserForceDeleteHook.js index 46795ce..2eaea37 100644 --- a/app/Models/Hooks/UserForceDeleteHook.js +++ b/app/Models/Hooks/UserForceDeleteHook.js @@ -14,26 +14,26 @@ const UserForceDeleteHook = exports = module.exports = {}; */ UserForceDeleteHook.removeProjects = async (user) => { - - // get a new instance of the model with relations - let userQueried = await User.with(['owned_projects']).find(user._id); - - // check for the count - if (userQueried.$relations.owned_projects.rows.length >= 1) - { - - // get the projects that a user is associated with - let projects = userQueried.$relations.owned_projects.rows; - - // loop through every project - for (let i = 0; i < userQueried.$relations.owned_projects.rows.length; i++) - { - - // delete the project from existence. - await projects[i].delete(); - - } - - } - + + // get a new instance of the model with relations + let userQueried = await User.with(['owned_projects']).find(user._id); + + // check for the count + if (userQueried.$relations.owned_projects.rows.length >= 1) + { + + // get the projects that a user is associated with + let projects = userQueried.$relations.owned_projects.rows; + + // loop through every project + for (let i = 0; i < userQueried.$relations.owned_projects.rows.length; i++) + { + + // delete the project from existence. + await projects[i].delete(); + + } + + } + }; diff --git a/app/Models/Project.js b/app/Models/Project.js index 1cf511b..2c0a8cd 100644 --- a/app/Models/Project.js +++ b/app/Models/Project.js @@ -1,86 +1,81 @@ -'use strict' +'use strict'; const Model = use('Model'); class Project extends Model { - - static boot() - { - super.boot(); - - // mongoDB hook for delete associates sprints. - this.addHook('beforeDelete', 'ProjectForceDelete.removeSprints'); - - // mongoDB hook for delete associated releases. - this.addHook('beforeDelete', 'ProjectForceDelete.removeReleases'); - } - - static get primaryKey() - { - return "_id"; - } - - static get objectIDs() - { - return ['_id', '_client_id', 'team_id']; - } - - /** - * Return the manager that belong to the project - * - * @returns {HasOne} - */ - manager() - { - return this.hasOne("App/Models/User", "_manager_id", "_id"); - } - - /** - * Get the client for the project - * - * @return {BelongsTo} - */ - client() - { - return this.belongsTo("App/Models/User", "_client_id", "_id"); - } - - /** - * Get the Phases of a project - * - * @return {HasMany} - */ - sprints() - { - return this.hasMany('App/Models/Sprint', '_id', '_project_id') - } - - /** - * Get the project version - * - * @return {HasMany} - */ - releases() - { - return this.hasMany('App/Models/Release', '_id', '_project_id'); - } - - team() - { - return this.hasOne('App/Models/Team', '_team_id', '_id'); - } - - /** - * Get the requirements of the project - * - * @return {HasMany} - */ - requirements() - { - return this.hasMany('App/Models/Requirement', '_id', '_project_id'); - } - + + static get primaryKey() + { + return "_id"; + } + + static get objectIDs() + { + return ['_id', '_client_id', '_manager_id', '_team_id']; + } + + static boot() + { + super.boot(); + + // mongoDB hook for delete associates sprints. + this.addHook('beforeDelete', 'ProjectForceDelete.removeSprints'); + + // mongoDB hook for delete associated releases. + this.addHook('beforeDelete', 'ProjectForceDelete.removeReleases'); + } + + /** + * Return the manager that belong to the project + * + * @returns {HasOne} + */ + manager() + { + return this.hasOne("App/Models/User", "_manager_id", "_id"); + } + + /** + * Get the client for the project + * + * @return {BelongsTo} + */ + client() + { + return this.belongsTo("App/Models/User", "_client_id", "_id"); + } + + /** + * Get the Phases of a project + * + * @return {HasMany} + */ + sprints() + { + return this.hasMany('App/Models/Sprint', '_id', '_project_id') + } + + /** + * Get the project version + * + * @return {HasMany} + */ + releases() + { + return this.hasMany('App/Models/Release', '_id', '_project_id'); + } + + /** + * Get the team that the project has been assigned to + * + * @return {HasOne} + */ + team() + { + return this.hasOne('App/Models/Team', '_team_id', '_id'); + } + } module.exports = Project; diff --git a/app/Models/Release.js b/app/Models/Release.js index 246935d..018cd63 100644 --- a/app/Models/Release.js +++ b/app/Models/Release.js @@ -4,29 +4,29 @@ const Model = use('Model'); class Release extends Model { - - static get primaryKey() - { - return "_id"; - } - - static get objectIDs() - { - return ['_id', '_project_id']; - } - - - /** - * Get the version that belongs to the project - * - * @return {HasOne} - */ - project() - { - return this.hasOne('App/Models/Project', '_project_id', '_id'); - } - - + + static get primaryKey() + { + return "_id"; + } + + static get objectIDs() + { + return ['_id', '_project_id']; + } + + + /** + * Get the version that belongs to the project + * + * @return {HasOne} + */ + project() + { + return this.hasOne('App/Models/Project', '_project_id', '_id'); + } + + } module.exports = Release; diff --git a/app/Models/Requirement.js b/app/Models/Requirement.js index 477a201..e074ee5 100644 --- a/app/Models/Requirement.js +++ b/app/Models/Requirement.js @@ -4,17 +4,17 @@ const Model = use('Model'); class Requirement extends Model { - - static get primaryKey() - { - return "_id"; - } - - project() - { - return this.hasOne('App/Models/Project', '_project_id', '_id'); - } - + + static get primaryKey() + { + return "_id"; + } + + project() + { + return this.hasOne('App/Models/Project', '_project_id', '_id'); + } + } module.exports = Requirement; diff --git a/app/Models/Sprint.js b/app/Models/Sprint.js index 3471b2b..2d2c07c 100644 --- a/app/Models/Sprint.js +++ b/app/Models/Sprint.js @@ -4,35 +4,39 @@ const Model = use('Model'); class Sprint extends Model { - - static boot() - { - super.boot(); - - this.addHook('beforeDelete', 'SprintForceDelete.removeTickets'); - } - - static get primaryKey() - { - return "_id"; - } - - - /** - * Get the project that the phase is belongs to. - * - * @return {BelongsTo} - */ - project() - { - return this.belongsTo('App/Models/Project', '_project_id', '_id'); - } - - tickets() - { - return this.hasMany('App/Models/Ticket', '_id', '_sprint_id') - } - + + static get primaryKey() + { + return "_id"; + } + + static get objectIDs() + { + return ['_id', '_project_id']; + } + + static boot() + { + super.boot(); + + this.addHook('beforeDelete', 'SprintForceDelete.removeTickets'); + } + + /** + * Get the project that the phase is belongs to. + * + * @return {BelongsTo} + */ + project() + { + return this.belongsTo('App/Models/Project', '_project_id', '_id'); + } + + tickets() + { + return this.hasMany('App/Models/Ticket', '_id', '_sprint_id') + } + } module.exports = Sprint; diff --git a/app/Models/Team.js b/app/Models/Team.js index e480c54..26c2d3d 100644 --- a/app/Models/Team.js +++ b/app/Models/Team.js @@ -4,32 +4,32 @@ const Model = use('Model'); class Team extends Model { - - static get primaryKey() - { - return "_id"; - } - - /** - * Get all the users in a team. - * - * @return {HasMany} - */ - users() - { - return this.hasMany('App/Models/User', "_id", "_team_id"); - } - - /** - * Get all the projects that the team has assigned to. - * - * @return {HasMany} - */ - projects() - { - return this.hasMany("App/Models/Project", "_id", "_team_id"); - } - + + static get primaryKey() + { + return "_id"; + } + + /** + * Get all the users in a team. + * + * @return {HasMany} + */ + users() + { + return this.hasMany('App/Models/User', "_id", "_team_id"); + } + + /** + * Get all the projects that the team has assigned to. + * + * @return {HasMany} + */ + projects() + { + return this.hasMany("App/Models/Project", "_id", "_team_id"); + } + } module.exports = Team; diff --git a/app/Models/Ticket.js b/app/Models/Ticket.js index d05d12b..490f447 100644 --- a/app/Models/Ticket.js +++ b/app/Models/Ticket.js @@ -12,7 +12,7 @@ class Ticket extends Model static get objectIDs() { - return ['_id', '_sprint_id', '_assignee_id']; + return ['_id', '_sprint_id', '_assignee_id', '_release_id']; } /** diff --git a/app/Models/TicketType.js b/app/Models/TicketType.js index 73ce2b2..6b5c4b2 100644 --- a/app/Models/TicketType.js +++ b/app/Models/TicketType.js @@ -4,18 +4,18 @@ const Model = use('Model'); class TicketType extends Model { - - static get primaryKey() - { - return "_id"; - } - - - ticket() - { - return this.belongsTo('App/Models/Ticket'); - } - + + static get primaryKey() + { + return "_id"; + } + + + ticket() + { + return this.belongsTo('App/Models/Ticket'); + } + } module.exports = TicketType; diff --git a/app/Models/Token.js b/app/Models/Token.js index 6d1bceb..8f2fa67 100644 --- a/app/Models/Token.js +++ b/app/Models/Token.js @@ -2,7 +2,8 @@ const Model = use('Model') -class Token extends Model { +class Token extends Model +{ } module.exports = Token diff --git a/app/Models/User.js b/app/Models/User.js index 50d28b5..eab44a7 100644 --- a/app/Models/User.js +++ b/app/Models/User.js @@ -5,88 +5,91 @@ const Model = use('Model') class User extends Model { - - static boot() - { - super.boot(); - - /** - * A hook to hash the user password before saving - * it to the database. - */ - this.addHook('beforeCreate', async (userInstance) => { - if (userInstance.password) { - userInstance.password = await Hash.make(userInstance.password) - } - }); - - this.addHook('beforeDelete', 'UserForceDeleteHook.removeProjects'); - } - - static get hidden () - { - return ['password'] - } - - static get primaryKey() - { - return "_id"; - } - - static get objectIDs() - { - return ['_id', '_team_id']; - } - - /** - * A relationship on tokens is required for auth to - * work. Since features like `refreshTokens` or - * `rememberToken` will be saved inside the - * tokens table. - * - * @method tokens - * - * @return {Object} - */ - tokens() { - return this.hasMany('App/Models/Token') - } - - /** - * Get the team of the user. - * - * @return {BelongsTo} - */ - team() - { - return this.belongsTo('App/Models/Team', '_team_id', '_id'); - } - - /** - * Projects that the user owns - * - * @return {HasMany} - */ - owned_projects() - { - return this.hasMany('App/Models/Project', '_id', '_client_id'); - } - - /** - * Get the projects that the user is managing. - * - * @return {HasMany} - */ - managing_projects() - { - return this.hasMany('App/Models/Project', '_id', '_manager_id'); - } - - tickets() - { - return this.hasMany('App/Models/Ticket', '_id', '_assignee_id') - } - + + static get hidden() + { + return ['password'] + } + + static get primaryKey() + { + return "_id"; + } + + static get objectIDs() + { + return ['_id', '_team_id']; + } + + static boot() + { + super.boot(); + + /** + * A hook to hash the user password before saving + * it to the database. + */ + this.addHook('beforeCreate', async (userInstance) => + { + if (userInstance.password) + { + userInstance.password = await Hash.make(userInstance.password) + } + }); + + this.addHook('beforeDelete', 'UserForceDeleteHook.removeProjects'); + } + + /** + * A relationship on tokens is required for auth to + * work. Since features like `refreshTokens` or + * `rememberToken` will be saved inside the + * tokens table. + * + * @method tokens + * + * @return {Object} + */ + tokens() + { + return this.hasMany('App/Models/Token') + } + + /** + * Get the team of the user. + * + * @return {BelongsTo} + */ + team() + { + return this.belongsTo('App/Models/Team', '_team_id', '_id'); + } + + /** + * Projects that the user owns + * + * @return {HasMany} + */ + owned_projects() + { + return this.hasMany('App/Models/Project', '_id', '_client_id'); + } + + /** + * Get the projects that the user is managing. + * + * @return {HasMany} + */ + managing_projects() + { + return this.hasMany('App/Models/Project', '_id', '_manager_id'); + } + + tickets() + { + return this.hasMany('App/Models/Ticket', '_id', '_assignee_id') + } + } module.exports = User; diff --git a/app/Validators/ProjectStoreUpdate.js b/app/Validators/ProjectStoreUpdate.js index 376f711..a485db4 100644 --- a/app/Validators/ProjectStoreUpdate.js +++ b/app/Validators/ProjectStoreUpdate.js @@ -2,21 +2,31 @@ class ProjectStoreUpdate { - - get rules () - { - return { - name: "required|min:3", - description: "required|min:10", - _client_id: "required", - _team_id: "required", - } - } - - async fails(errorMessages) - { - return this.ctx.response.send(errorMessages); - } + + get rules() + { + return { + name: "required|min:3", + description: "required|min:10", + start_date: "required|date", + end_date: "required|date", + + // relationships validators + _client_id: "required|exists:users,_id", + _manager_id: "required|exists:users,_id", + _team_id: "required|exists:teams,_id" + } + } + + get validateAll() + { + return true + } + + async fails(errorMessages) + { + return this.ctx.response.send(errorMessages); + } } module.exports = ProjectStoreUpdate; diff --git a/app/Validators/ReleaseCreateUpdate.js b/app/Validators/ReleaseCreateUpdate.js index e61dafc..66f413a 100644 --- a/app/Validators/ReleaseCreateUpdate.js +++ b/app/Validators/ReleaseCreateUpdate.js @@ -1,19 +1,28 @@ 'use strict'; -class ReleaseCreateUpdate { - - get rules() - { - return { - name: 'required|min:3', - number: 'required' - } - } - - async fails(errorMessages) - { - return this.ctx.response.send(errorMessages) - } +class ReleaseCreateUpdate +{ + + get rules() + { + return { + name: 'required|min:3', + number: 'required', + + // relationship validators + _project_id: 'required|exists:projects,_id' + } + } + + get validateAll() + { + return true + } + + async fails(errorMessages) + { + return this.ctx.response.send(errorMessages) + } } module.exports = ReleaseCreateUpdate; diff --git a/app/Validators/SprintCreateUpdate.js b/app/Validators/SprintCreateUpdate.js index c51e900..e8b35ac 100644 --- a/app/Validators/SprintCreateUpdate.js +++ b/app/Validators/SprintCreateUpdate.js @@ -2,24 +2,32 @@ class SprintCreateUpdate { - - get rules () - { - return { - - name: "required|min:3", - description: "required|min:10", - start_date: "required|date", - end_date: "required|date" - - }; - }; - - async fails(errorMessages) - { - return this.ctx.response.send(errorMessages); - } - + + get rules() + { + return { + + name: "required|min:3", + description: "required|min:10", + start_date: "required|date", + end_date: "required|date", + + // relationship validators + _project_id: "required|exists:projects,_id" + + }; + }; + + get validateAll() + { + return true + } + + async fails(errorMessages) + { + return this.ctx.response.send(errorMessages); + } + } module.exports = SprintCreateUpdate; diff --git a/app/Validators/TeamStoreUpdate.js b/app/Validators/TeamStoreUpdate.js index 4d5838f..2383045 100644 --- a/app/Validators/TeamStoreUpdate.js +++ b/app/Validators/TeamStoreUpdate.js @@ -2,12 +2,22 @@ class TeamStoreUpdate { - get rules() - { - return { - name: 'required|min:3' - } - } + get rules() + { + return { + name: 'required|min:3' + } + } + + get validateAll() + { + return true + } + + async fails(errorMessages) + { + return this.ctx.response.send(errorMessages); + } } module.exports = TeamStoreUpdate; diff --git a/app/Validators/TicketCreateUpdate.js b/app/Validators/TicketCreateUpdate.js index 3ff7cc9..c87cf1f 100644 --- a/app/Validators/TicketCreateUpdate.js +++ b/app/Validators/TicketCreateUpdate.js @@ -1,45 +1,46 @@ 'use strict'; -class TicketCreateUpdate { - - get rules() - { - return { - _sprint_id: 'required|exists:sprints,_id', - _assignee_id: 'required|exists:users,_id', - _release_id: 'required|exists:versions,_id', - ticket_type: 'required', - name: 'required|min:3', - description: 'required|min:10', - status: 'required|boolean', - priority: 'required|integer', - severity_level: 'required|integer', - - // validation for the ticket, if the ticket is a Epic ticket. - epic_sub_type: 'requiredWhen:ticket_type,EPIC', - - // validation for the ticket, if the ticket is Bug ticket - route_cause_analysis: 'requiredWhen:ticket_type,BUG', - bug_sub_type: 'requiredWhen:ticket_type,BUG', - description_of_fix: 'requiredWhen:ticket_type,BUG', - - // common for ticket types (EPIC and STORY ONLY) - requirement_sign_off_date: 'requiredWhen:tick_type,EPIC|requiredWhen:ticket_type,STORY', - design_review_date: 'requiredWhen:tick_type,EPIC|requiredWhen:ticket_type,STORY', - common_test_plan: 'requiredWhen:tick_type,EPIC|requiredWhen:ticket_type,STORY', - definition_of_done: 'requiredWhen:tick_type,EPIC|requiredWhen:ticket_type,STORY', - } - } - - get validateAll() - { - return true - } - - async fails(errorMessages) - { - return this.ctx.response.send(errorMessages); - } +class TicketCreateUpdate +{ + + get rules() + { + return { + _sprint_id: 'required|exists:sprints,_id', + _assignee_id: 'required|exists:users,_id', + _release_id: 'required|exists:releases,_id', + ticket_type: 'required', + name: 'required|min:3', + description: 'required|min:10', + status: 'required|boolean', + priority: 'required|integer', + severity_level: 'required|integer', + + // validation for the ticket, if the ticket is a Epic ticket. + epic_sub_type: 'requiredWhen:ticket_type,EPIC', + + // validation for the ticket, if the ticket is Bug ticket + route_cause_analysis: 'requiredWhen:ticket_type,BUG', + bug_sub_type: 'requiredWhen:ticket_type,BUG', + description_of_fix: 'requiredWhen:ticket_type,BUG', + + // common for ticket types (EPIC and STORY ONLY) + requirement_sign_off_date: 'requiredWhen:tick_type,EPIC|requiredWhen:ticket_type,STORY', + design_review_date: 'requiredWhen:tick_type,EPIC|requiredWhen:ticket_type,STORY', + common_test_plan: 'requiredWhen:tick_type,EPIC|requiredWhen:ticket_type,STORY', + definition_of_done: 'requiredWhen:tick_type,EPIC|requiredWhen:ticket_type,STORY', + } + } + + get validateAll() + { + return true + } + + async fails(errorMessages) + { + return this.ctx.response.send(errorMessages); + } } module.exports = TicketCreateUpdate; diff --git a/app/Validators/UserStoreUpdate.js b/app/Validators/UserStoreUpdate.js index d052d95..83f2c4f 100644 --- a/app/Validators/UserStoreUpdate.js +++ b/app/Validators/UserStoreUpdate.js @@ -1,32 +1,38 @@ -'use strict' +'use strict'; -class UserStoreUpdate { - - // async authorize () - // { - // if (!isAdmin) - // { - // this.ctx.response.unauthorized('Not authorized'); - // return false - // } - // - // return true - // } - - get rules() - { - return { - email: 'required|email', - name: 'required|min:3', - password: 'required|min:8', - type: 'in:client,user,manager,admin' - } - } - - async fails (errorMessages) - { - return this.ctx.response.send(errorMessages) - } +class UserStoreUpdate +{ + + // async authorize () + // { + // if (!isAdmin) + // { + // this.ctx.response.unauthorized('Not authorized'); + // return false + // } + // + // return true + // } + + get rules() + { + return { + email: 'required|email', + name: 'required|min:3', + password: 'required|min:8', + type: 'in:client,developer,manager,admin' + } + } + + get validateAll() + { + return true + } + + async fails(errorMessages) + { + return this.ctx.response.send(errorMessages) + } } module.exports = UserStoreUpdate; diff --git a/database/factory.js b/database/factory.js index 2c6bb8c..bf79e30 100644 --- a/database/factory.js +++ b/database/factory.js @@ -25,90 +25,107 @@ const Factory = use('Factory'); const Hash = use('Hash'); -Factory.blueprint('App/Models/Team', async (faker) => { - - return { - name: faker.name() - } - +Factory.blueprint('App/Models/Team', async (faker) => +{ + + return { + name: faker.name() + } + }); -Factory.blueprint('App/Models/User', async (faker) => { - - return { - name: faker.name(), - email: faker.email(), - type: "user", - password: "sithira123" - } - +Factory.blueprint('App/Models/User', async (faker, i, data) => +{ + + let type = "client"; + + if (data.hasOwnProperty("type")) + { + type = data.type; + } + + return { + name: faker.name(), + email: faker.email(), + type: type, + password: "sithira123" + } + }); -Factory.blueprint('App/Models/Project', async (faker) => { - - return { - name: faker.word(), - description: faker.paragraph(), - } - +Factory.blueprint('App/Models/Project', async (faker) => +{ + + return { + name: faker.word({length: 5}), + description: faker.paragraph({ sentences: 1 }), + start_date: faker.date(), + end_date: faker.date() + } + }); -Factory.blueprint('App/Models/Sprint', async (faker) => { - - return { - name: faker.word({ length: 5 }), - description: faker.paragraph(), - start_date: faker.date(), - end_date: faker.date() - } - +Factory.blueprint('App/Models/Sprint', async (faker) => +{ + + return { + name: faker.word({length: 5}), + description: faker.paragraph({ sentences: 1 }), + start_date: faker.date(), + end_date: faker.date() + } + }); -Factory.blueprint('App/Models/Release', async (faker) => { - - return { - name: faker.word({ length: 5 }), - number: faker.integer({ min: -20, max: 20 }) - } - +Factory.blueprint('App/Models/Release', async (faker) => +{ + + return { + name: faker.word({length: 5}), + number: faker.integer({min: -20, max: 20}) + } + }); -Factory.blueprint('App/Models/Ticket', async (faker) => { - - return { - - name: faker.word({ length: 5 }), - description: faker.paragraph(), - status: 1, - severity_level: faker.integer({ min: 1, max: 10 }), - priority: faker.integer({ min: 1, max: 10 }), - last_modified_date: faker.timestamp(), - - }; - +Factory.blueprint('App/Models/Ticket', async (faker) => +{ + + return { + + name: faker.word({length: 5}), + description: faker.paragraph({ sentences: 1 }), + status: 1, + severity_level: faker.integer({min: 1, max: 10}), + priority: faker.integer({min: 1, max: 10}), + last_modified_date: faker.timestamp(), + + }; + }); -Factory.blueprint('App/Models/Requirement', async (faker) => { - - return { - name: faker.word(), - description: faker.paragraph() - } - +Factory.blueprint('App/Models/Requirement', async (faker) => +{ + + return { + name: faker.word(), + description: faker.paragraph({ sentences: 1 }) + } + }); -Factory.blueprint('App/Models/Ticket', async (faker) => { - - return { - ticket_type: faker.word(), - name: faker.word(), - description: faker.paragraph(), - status: "STATUS", - priority: faker.integer(), - serverity_level: faker.integer(), - } - +Factory.blueprint('App/Models/Ticket', async (faker) => +{ + + return { + ticket_type: faker.word(), + name: faker.word(), + description: faker.paragraph({ sentences: 1 }), + status: "STATUS", + priority: faker.integer(), + serverity_level: faker.integer(), + } + }); diff --git a/test/unit/project-rest-test.spec.js b/test/unit/project-rest-test.spec.js index 696e546..378e1b8 100644 --- a/test/unit/project-rest-test.spec.js +++ b/test/unit/project-rest-test.spec.js @@ -1,133 +1,144 @@ 'use strict'; -const { test, trait } = use('Test/Suite')('Project R.E.S.T Test'); +const Suite = use('Test/Suite')('Project R.E.S.T Test'); -const Factory = use('Factory'); - -let dummyProject = null; - -let dummyUser = null; - -let dummyTeam = null; +const {before, test, trait} = Suite; trait('Test/ApiClient'); -test("All projects can be fetched via the API", async ({ client }) => { - - // send the request to the API - const response = await client.get('/api/v1/projects') - .end(); - - // check if we have a status of 200 - response.assertStatus(200); - - // check if the API return the status of "OK" - response.assertJSONSubset({ - status: "OK" - }); - -}); - - -test('A new project can be created via the API', async ({client}) => { - - // mock an object in the memory - const project = await Factory.model('App/Models/Project') - .make(); - - // create a dummy user - const user = await Factory.model('App/Models/User') - .make(); - - // create an team - const team = await Factory.model('App/Models/Team') - .make(); - - // response for the create user - const responseUser = await client.post('/api/v1/users') - .send(user.$attributes) - .end(); - - // parse the dummyUser - dummyUser = (JSON.parse(responseUser.text)).data; - - // add the client_id to the attributes - project.$attributes['_client_id'] = dummyUser._id; +trait('Auth/Client'); - // hit the API tp save the team - const responseTeam = await client.post('/api/v1/teams') - .send(team.$attributes) - .end(); - - // parse to JSON - dummyTeam = (JSON.parse(responseTeam.text)).data; - - // append to the project attributes - project.$attributes['_team_id'] = dummyTeam._id; - - // using it's data, we send a request to the API server. - const response = await client.post('/api/v1/projects') - .send(project.$attributes) - .end(); +const Factory = use('Factory'); - // convert the project to JSON - dummyProject = (JSON.parse(response.text)).data; +let authUser = null; - // status of 201 will be return upon success - response.assertStatus(201); +let dummyProject = null; - // check if the returned data matches the prev mocked object - response.assertJSONSubset({ - status: "OK", // check if we have a status of OK"" - data: { - name: project.$attributes.name - } - }); +let dummyTeam = null; +before(async () => +{ + // get an authenticated user. + authUser = await Factory.model('App/Models/User') + .create({type: "admin"}); }); -test('A project can be updated via the API', async ({ client }) => { - - - dummyProject.name = "Some updated project name"; - - const response = await client.patch('/api/v1/projects/' + dummyProject._id) - .send(dummyProject) - .end(); - - response.assertStatus(200); - - response.assertJSONSubset({ - data: { - name: dummyProject.name - } - }) - +test("All projects can be fetched via the API", async ({client}) => +{ + + // send the request to the API + const response = await client.get('/api/v1/projects') + .loginVia(authUser, 'jwt') + .end(); + + // check if we have a status of 200 + response.assertStatus(200); + + // check if the API return the status of "OK" + response.assertJSONSubset({ + status: "OK" + }); + }); -test('A project can be soft deleted vai the API', async ({ client }) => { - - const response = await client.delete('/api/v1/projects/' + dummyProject._id) - .end(); - - response.assertStatus(200); - - response.assertJSONSubset({ - status: "OK" - }); - +test('A new project can be created via the API', async ({client}) => +{ + + // mock an object in the memory + const project = await Factory.model('App/Models/Project') + .make(); + + // create an team + const team = await Factory.model('App/Models/Team') + .make(); + + // add the client_id to the attributes + project.$attributes['_client_id'] = authUser._id; + project.$attributes['_manager_id'] = authUser._id; + + // hit the API to save the team + const responseTeam = await client.post('/api/v1/teams') + .loginVia(authUser, 'jwt') + .send(team.$attributes) + .end(); + + // parse to JSON + dummyTeam = (JSON.parse(responseTeam.text)).data; + + // append to the project attributes + project.$attributes['_team_id'] = dummyTeam._id; + + // using it's data, we send a request to the API server. + const response = await client.post('/api/v1/projects') + .loginVia(authUser, 'jwt') + .send(project.$attributes) + .end(); + + // convert the project to JSON + dummyProject = (JSON.parse(response.text)).data; + + // status of 201 will be return upon success + response.assertStatus(201); + + // check if the returned data matches the prev mocked object + response.assertJSONSubset({ + status: "OK", // check if we have a status of OK"" + data: { + name: project.$attributes.name + } + }); + }); -test('A Project can be force deleted via the API', async ({ client }) => { - - const response = await client.delete('/api/v1/projects/' + dummyProject._id + "/?forceDestroy=true") - .end(); +test('A project can be updated via the API', async ({client}) => +{ + + + dummyProject.name = "Some updated project name"; + + const response = await client.patch('/api/v1/projects/' + dummyProject._id) + .loginVia(authUser, 'jwt') + .send(dummyProject) + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + data: { + name: dummyProject.name + } + }) + +}); - response.assertStatus(200); - response.assertJSONSubset({ - status: "OK" - }) +test('A project can be soft deleted vai the API', async ({client}) => +{ + + const response = await client.delete('/api/v1/projects/' + dummyProject._id) + .loginVia(authUser, 'jwt') + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + status: "OK" + }); + +}); +test('A Project can be force deleted via the API', async ({client}) => +{ + + const response = await client.delete('/api/v1/projects/' + dummyProject._id + "/?forceDestroy=true") + .loginVia(authUser, 'jwt') + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + status: "OK" + }) + }); \ No newline at end of file diff --git a/test/unit/release-rest-test.spec.js b/test/unit/release-rest-test.spec.js index 8c4cd27..44cc868 100644 --- a/test/unit/release-rest-test.spec.js +++ b/test/unit/release-rest-test.spec.js @@ -1,12 +1,16 @@ 'use strict'; -const { test, trait } = use('Test/Suite')('Release R.E.S.T Test'); +const Suite = use('Test/Suite')('Release R.E.S.T Test'); + +const {before, test, trait} = Suite; const Factory = use('Factory'); trait('Test/ApiClient'); -let dummyUser = null; +trait('Auth/Client'); + +let authUser = null; let dummyProject = null; @@ -14,146 +18,158 @@ let dummyRelease = null; let dummyTeam = null; -test("Add a Release to a existing project", async({ client }) => { - - // mock an object in the memory - const project = await Factory.model('App/Models/Project') - .make(); - - // create a dummy user - const user = await Factory.model('App/Models/User') - .make(); - - // create an team - const team = await Factory.model('App/Models/Team') - .make(); - - // response for the create user - const responseUser = await client.post('/api/v1/users') - .send(user.$attributes) - .end(); - - // parse the dummyUser - dummyUser = (JSON.parse(responseUser.text)).data; - - // add the client_id to the attributes - project.$attributes['_client_id'] = dummyUser._id; - - // hit the API tp save the team - const responseTeam = await client.post('/api/v1/teams') - .send(team.$attributes) - .end(); - - // parse to JSON - dummyTeam = (JSON.parse(responseTeam.text)).data; - - // append to the project attributes - project.$attributes['_team_id'] = dummyTeam._id; - // using it's data, we send a request to the API server. - const response = await client.post('/api/v1/projects') - .send(project.$attributes) - .end(); - - dummyProject = (JSON.parse(response.text)).data; - - // status of 201 will be return upon success - response.assertStatus(201); - - // check if the returned data matches the prev mocked object - response.assertJSONSubset({ - status: "OK", // check if we have a status of OK"" - data: { - name: project.$attributes.name - } - }); - - const Release = await Factory.model('App/Models/Release') - .make(); - - const responseRelease = await client.post('/api/v1/projects/' + dummyProject._id + '/releases') - .send(Release.$attributes) - .end(); - - dummyRelease = (JSON.parse(responseRelease.text)).data; - - responseRelease.assertStatus(201); - - responseRelease.assertJSONSubset({ - status: "OK", - data: { - name: dummyRelease.name - } - }); - +before(async () => +{ + // get an authenticated user. + authUser = await Factory.model('App/Models/User') + .create({type: "admin"}); }); -test("A Release exists on a project", async({ client }) => { - - const response = await client.get('/api/v1/projects/' + dummyProject._id + '/releases') - .end(); - - response.assertStatus(200); - - response.assertJSONSubset({ - status: "OK", - data: [ - { - name: dummyRelease.name - } - ] - }); - +test("Add a Release to a existing project", async ({client}) => +{ + + // mock an object in the memory + const project = await Factory.model('App/Models/Project') + .make(); + + // create an team + const team = await Factory.model('App/Models/Team') + .make(); + + const release = await Factory.model('App/Models/Release') + .make(); + + // add the client_id to the attributes + project.$attributes['_client_id'] = authUser._id; + project.$attributes['_manager_id'] = authUser._id; + + // hit the API tp save the team + const responseTeam = await client.post('/api/v1/teams') + .loginVia(authUser, 'jwt') + .send(team.$attributes) + .end(); + + // parse to JSON + dummyTeam = (JSON.parse(responseTeam.text)).data; + + // append to the project attributes + project.$attributes['_team_id'] = dummyTeam._id; + + // using it's data, we send a request to the API server. + const response = await client.post('/api/v1/projects') + .loginVia(authUser, 'jwt') + .send(project.$attributes) + .end(); + + dummyProject = (JSON.parse(response.text)).data; + + // status of 201 will be return upon success + response.assertStatus(201); + + // check if the returned data matches the prev mocked object + response.assertJSONSubset({ + status: "OK", // check if we have a status of OK"" + data: { + name: project.$attributes.name + } + }); + + release.$attributes._project_id = dummyProject._id; + + const responseRelease = await client.post('/api/v1/projects/' + dummyProject._id + '/releases') + .loginVia(authUser, 'jwt') + .send(release.$attributes) + .end(); + + dummyRelease = (JSON.parse(responseRelease.text)).data; + + responseRelease.assertStatus(201); + + responseRelease.assertJSONSubset({ + status: "OK", + data: { + name: dummyRelease.name + } + }); + }); -test("A Release can be updated when a Release and project is given", async({ client }) => { - - let name = "Updating name"; - - dummyRelease.name = name; - - const response = await client.patch('/api/v1/projects/' + dummyProject._id + '/releases/' + dummyRelease._id) - .send(dummyRelease) - .end(); - - response.assertStatus(200); - - response.assertJSONSubset({ - status: "OK", - data: { - name: name - } - }); - +test("A Release exists on a project", async ({client}) => +{ + + const response = await client.get('/api/v1/projects/' + dummyProject._id + '/releases') + .loginVia(authUser, 'jwt') + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + status: "OK", + data: [ + { + name: dummyRelease.name + } + ] + }); + }); - -test("A Release can be soft deleted from a project", async({ client }) => { - - const response = await client.delete('/api/v1/projects/' + dummyProject._id + '/releases/' + dummyRelease._id) - .end(); - - response.assertStatus(200); - - response.assertJSONSubset({ - status: "OK" - }); - +test("A Release can be updated when a Release and project is given", async ({client}) => +{ + + let name = "Updating name"; + + dummyRelease.name = name; + + const response = await client.patch('/api/v1/projects/' + dummyProject._id + '/releases/' + dummyRelease._id) + .loginVia(authUser, 'jwt') + .send(dummyRelease) + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + status: "OK", + data: { + name: name + } + }); + }); -test("A Release can be soft deleted from a project", async({ client }) => { - const response = await client.delete('/api/v1/projects/' + dummyProject._id + '/releases/' + dummyRelease._id + "/?forceDestroy=true") - .end(); - - response.assertStatus(200); - - response.assertJSONSubset({ - status: "OK" - }); - - const responseDelete = await client.delete('/api/v1/projects/' + dummyProject._id + "/?forceDestroy=true") - .end(); - - responseDelete.assertStatus(200); +test("A Release can be soft deleted from a project", async ({client}) => +{ + + const response = await client.delete('/api/v1/projects/' + dummyProject._id + '/releases/' + dummyRelease._id) + .loginVia(authUser, 'jwt') + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + status: "OK" + }); + +}); +test("A Release can be soft deleted from a project", async ({client}) => +{ + + const response = await client.delete('/api/v1/projects/' + dummyProject._id + '/releases/' + dummyRelease._id + "/?forceDestroy=true") + .loginVia(authUser, 'jwt') + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + status: "OK" + }); + + const responseDelete = await client.delete('/api/v1/projects/' + dummyProject._id + "/?forceDestroy=true") + .loginVia(authUser, 'jwt') + .end(); + + responseDelete.assertStatus(200); + }); \ No newline at end of file diff --git a/test/unit/sprint-rest-test.spec.js b/test/unit/sprint-rest-test.spec.js index ffb0506..b296b10 100644 --- a/test/unit/sprint-rest-test.spec.js +++ b/test/unit/sprint-rest-test.spec.js @@ -1,159 +1,177 @@ 'use strict'; -const { test, trait } = use('Test/Suite')('Unit Sprint R.E.S.T Test'); +const Suite = use('Test/Suite')('Sprint R.E.S.T Test'); -const Factory = use('Factory'); - -let dummyProject = null; - -let dummySprint = null; - -let dummyUser = null; - -let dummyTeam = null; +const {before, test, trait} = Suite; trait('Test/ApiClient'); -test('Add sprint to a existing project', async ({ client }) => { - - // mock an object in the memory - const project = await Factory.model('App/Models/Project') - .make(); - - // create a dummy user - const user = await Factory.model('App/Models/User') - .make(); - - // create an team - const team = await Factory.model('App/Models/Team') - .make(); +trait('Auth/Client'); - // response for the create user - const responseUser = await client.post('/api/v1/users') - .send(user.$attributes) - .end(); - - // parse the dummyUser - dummyUser = (JSON.parse(responseUser.text)).data; - - // add the client_id to the attributes - project.$attributes['_client_id'] = dummyUser._id; - - // hit the API tp save the team - const responseTeam = await client.post('/api/v1/teams') - .send(team.$attributes) - .end(); - - // parse to JSON - dummyTeam = (JSON.parse(responseTeam.text)).data; - - // append to the project attributes - project.$attributes['_team_id'] = dummyTeam._id; - - // using it's data, we send a request to the API server. - const response = await client.post('/api/v1/projects') - .send(project.$attributes) - .end(); - - dummyProject = (JSON.parse(response.text)).data; - - // status of 201 will be return upon success - response.assertStatus(201); - - // check if the returned data matches the prev mocked object - response.assertJSONSubset({ - status: "OK", // check if we have a status of "OK" - data: { - name: project.$attributes.name - } - }); +const Factory = use('Factory'); - // mock the object in the memory - const sprint = await Factory.model('App/Models/Sprint').make(); +let authUser = null; - // create the actual instance visa the API. ( sprint ) - const responseSprint = await client.post('/api/v1/projects/' + dummyProject._id + '/sprints') - .send(sprint.$attributes) - .end(); +let dummyProject = null; - dummySprint = (JSON.parse(responseSprint.text)).data; +let dummySprint = null; - responseSprint.assertStatus(201); +let dummyTeam = null; - responseSprint.assertJSONSubset({ - status: "OK" - }); +before(async () => +{ + // get an authenticated user. + authUser = await Factory.model('App/Models/User') + .create({type: "admin"}); }); -test('Create sprint can be fetched via the API for a given project', async ({ client }) => { - - const response = await client.get('/api/v1/projects/' + dummyProject._id + '/sprints') - .end(); - - response.assertStatus(200); - - response.assertJSONSubset({ - data: [{ - name: dummySprint.name - }] - }); - +test('Add sprint to a existing project', async ({client}) => +{ + + // mock an object in the memory + const project = await Factory.model('App/Models/Project') + .make(); + + // create an team + const team = await Factory.model('App/Models/Team') + .make(); + + // add the client_id to the attributes + project.$attributes['_client_id'] = authUser._id; + project.$attributes['_manager_id'] = authUser._id; + + // hit the API tp save the team + const responseTeam = await client.post('/api/v1/teams') + .loginVia(authUser, 'jwt') + .send(team.$attributes) + .end(); + + // parse to JSON + dummyTeam = (JSON.parse(responseTeam.text)).data; + + // append to the project attributes + project.$attributes['_team_id'] = dummyTeam._id; + + // using it's data, we send a request to the API server. + const response = await client.post('/api/v1/projects') + .loginVia(authUser, 'jwt') + .send(project.$attributes) + .end(); + + dummyProject = (JSON.parse(response.text)).data; + + // status of 201 will be return upon success + response.assertStatus(201); + + // check if the returned data matches the prev mocked object + response.assertJSONSubset({ + status: "OK", // check if we have a status of "OK" + data: { + name: project.$attributes.name + } + }); + + // mock the object in the memory + const sprint = await Factory.model('App/Models/Sprint') + .make(); + + sprint.$attributes._project_id = dummyProject._id; + + // create the actual instance visa the API. ( sprint ) + const responseSprint = await client.post('/api/v1/projects/' + dummyProject._id + '/sprints') + .loginVia(authUser, 'jwt') + .send(sprint.$attributes) + .end(); + + dummySprint = (JSON.parse(responseSprint.text)).data; + + responseSprint.assertStatus(201); + + responseSprint.assertJSONSubset({ + status: "OK" + }); + }); -test("A Sprint can be updated via the API.", async ({ client }) => { - - dummySprint.name = "Some updated sprint name"; - - const response = await client.patch('/api/v1/projects/' + dummyProject._id + '/sprints/' + dummySprint._id) - .send(dummySprint) - .end(); - - response.assertStatus(200); - - response.assertJSONSubset({ - data: { - name: dummySprint.name - } - }); - +test('Create sprint can be fetched via the API for a given project', async ({client}) => +{ + + const response = await client.get('/api/v1/projects/' + dummyProject._id + '/sprints') + .loginVia(authUser, 'jwt') + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + data: [{ + name: dummySprint.name + }] + }); + }); -test('A Sprint can be soft deleted deleted via the API', async ({ client }) => { - - const response = await client.delete('/api/v1/projects/' + dummyProject._id + '/sprints/' + dummySprint._id) - .end(); - - response.assertStatus(200); - - response.assertJSONSubset({ - status: "OK" - }); - +test("A Sprint can be updated via the API.", async ({client}) => +{ + + dummySprint.name = "Some updated sprint name"; + + const response = await client.patch('/api/v1/projects/' + dummyProject._id + '/sprints/' + dummySprint._id) + .loginVia(authUser, 'jwt') + .send(dummySprint) + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + data: { + name: dummySprint.name + } + }); + }); -test('A Sprint can be force deleted via the API', async({ client }) => { - - const response = await client.delete('/api/v1/projects/' + dummyProject._id + '/sprints/' + dummySprint._id + '/?forceDestroy=true') - .end(); - - response.assertStatus(200); - - response.assertJSONSubset({ - status: "OK" - }); - - const responseProject = await client.delete('/api/v1/projects/' + dummyProject._id + '/?forceDestroy=true') - .end(); - - responseProject.assertStatus(200); +test('A Sprint can be soft deleted deleted via the API', async ({client}) => +{ + + const response = await client.delete('/api/v1/projects/' + dummyProject._id + '/sprints/' + dummySprint._id) + .loginVia(authUser, 'jwt') + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + status: "OK" + }); + +}); - responseProject.assertJSONSubset({ - status: "OK" - }); +test('A Sprint can be force deleted via the API', async ({client}) => +{ + + const response = await client.delete('/api/v1/projects/' + dummyProject._id + '/sprints/' + dummySprint._id + '/?forceDestroy=true') + .loginVia(authUser, 'jwt') + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + status: "OK" + }); + + const responseProject = await client.delete('/api/v1/projects/' + dummyProject._id + '/?forceDestroy=true') + .loginVia(authUser, 'jwt') + .end(); + + responseProject.assertStatus(200); + + responseProject.assertJSONSubset({ + status: "OK" + }); + }); diff --git a/test/unit/tickets-rest-test.spec.js b/test/unit/tickets-rest-test.spec.js index 72f898c..d097d60 100644 --- a/test/unit/tickets-rest-test.spec.js +++ b/test/unit/tickets-rest-test.spec.js @@ -1,205 +1,225 @@ 'use strict'; -const {test, trait} = use('Test/Suite')('Tickets R.E.S.T Test'); +const Suite = use('Test/Suite')('Tickets R.E.S.T Test'); -const Factory = use('Factory'); - -let project = null; - -let dummyRelease = null; - -let dummySprint = null; - -let dummyTicket = null; - -let dummyUser = null; - -let dummyTeam = null; +const {before, test, trait} = Suite; trait('Test/ApiClient'); -test('A ticket can be created for a project', async ({client}) => { - - // create the project - project = await Factory.model('App/Models/Project') - .make(); - - // create a dummy user - const user = await Factory.model('App/Models/User') - .make(); - - // create an team - const team = await Factory.model('App/Models/Team') - .make(); - - // response for the create user - const responseUser = await client.post('/api/v1/users') - .send(user.$attributes) - .end(); +trait('Auth/Client'); - // parse the dummyUser - dummyUser = (JSON.parse(responseUser.text)).data; - - // add the client_id to the attributes - project.$attributes['_client_id'] = dummyUser._id; - - // hit the API tp save the team - const responseTeam = await client.post('/api/v1/teams') - .send(team.$attributes) - .end(); - - // parse to JSON - dummyTeam = (JSON.parse(responseTeam.text)).data; - - // append to the project attributes - project.$attributes['_team_id'] = dummyTeam._id; - - const projectResponse = await client.post('/api/v1/projects') - .send(project.$attributes) - .end(); - - project = (JSON.parse(projectResponse.text)).data; - - projectResponse.assertStatus(201); - - projectResponse.assertJSONSubset({ - status: "OK", - data: { - name: project.name - } - }); - - // create version for the release - // dummyRelease = await Factory.model('App/Models/Release') - // .make(); - // - // const response = await client.post('/api/v1/projects/' + project._id + '/releases') - // .send(dummyRelease.$attributes) - // .end(); - // - // dummyRelease = (JSON.parse(response.text)).data; - // - // response.assertStatus(201); - // - // response.assertJSONSubset({ - // status: "OK" - // }); - - // create the sprint - - dummySprint = await Factory.model('App/Models/Sprint') - .make(); - - const responseSprint = await client.post('/api/v1/projects/' + project._id + '/sprints') - .send(dummySprint.$attributes) - .end(); - - dummySprint = (JSON.parse(responseSprint.text)).data; +const Factory = use('Factory'); - responseSprint.assertStatus(201); +let project = null; - responseSprint.assertJSONSubset({ - status: "OK" - }); +let dummySprint = null; - dummyTicket = await Factory.model('App/Models/Ticket') - .make(); +let authUser = null; - dummyTicket.$attributes._sprint_id = dummySprint._id; - dummyTicket.$attributes.ticket_type = "BUG"; - dummyTicket.$attributes.route_cause_analysis = "Some Cause"; - dummyTicket.$attributes.bug_sub_type = "Some Type"; - dummyTicket.$attributes.description_of_fix = "Some fix description"; +let dummyTicket = null; - const responseTicket = await client.post('/api/v1/projects/' - + project._id + '/sprints/' + dummySprint._id + '/tickets') - .send(dummyTicket.$attributes) - .end(); +let dummyRelease = null; - dummyTicket = (JSON.parse(responseTicket.text)).data; +let dummyTeam = null; - responseTicket.assertStatus(201); +before(async () => +{ + // get an authenticated user. + authUser = await Factory.model('App/Models/User') + .create({type: "admin"}); +}); - responseTicket.assertJSONSubset({ - status: "OK" - }); +test('A ticket can be created for a project', async ({client}) => +{ + + // create the project + project = await Factory.model('App/Models/Project') + .make(); + + // create an team + const team = await Factory.model('App/Models/Team') + .make(); + + // add the client_id to the attributes + project.$attributes['_client_id'] = authUser._id; + project.$attributes['_manager_id'] = authUser._id; + + // hit the API tp save the team + const responseTeam = await client.post('/api/v1/teams') + .loginVia(authUser, 'jwt') + .send(team.$attributes) + .end(); + + // parse to JSON + dummyTeam = (JSON.parse(responseTeam.text)).data; + + // append to the project attributes + project.$attributes['_team_id'] = dummyTeam._id; + + const projectResponse = await client.post('/api/v1/projects') + .loginVia(authUser, 'jwt') + .send(project.$attributes) + .end(); + + project = (JSON.parse(projectResponse.text)).data; + + projectResponse.assertStatus(201); + + projectResponse.assertJSONSubset({ + status: "OK", + data: { + name: project.name + } + }); + + // create version for the release + dummyRelease = await Factory.model('App/Models/Release') + .make(); + + dummyRelease.$attributes._project_id = project._id; + + const response = await client.post('/api/v1/projects/' + project._id + '/releases') + .loginVia(authUser, 'jwt') + .send(dummyRelease.$attributes) + .end(); + + dummyRelease = (JSON.parse(response.text)).data; + + response.assertStatus(201); + + response.assertJSONSubset({ + status: "OK" + }); + + // create the sprint + dummySprint = await Factory.model('App/Models/Sprint') + .make(); + + dummySprint.$attributes._project_id = project._id; + + const responseSprint = await client.post('/api/v1/projects/' + project._id + '/sprints') + .loginVia(authUser, 'jwt') + .send(dummySprint.$attributes) + .end(); + + dummySprint = (JSON.parse(responseSprint.text)).data; + + responseSprint.assertStatus(201); + + responseSprint.assertJSONSubset({ + status: "OK" + }); + + dummyTicket = await Factory.model('App/Models/Ticket') + .make(); + + dummyTicket.$attributes._sprint_id = dummySprint._id; + dummyTicket.$attributes._assignee_id = authUser._id; + dummyTicket.$attributes._release_id = dummyRelease._id; + dummyTicket.$attributes.ticket_type = "BUG"; + dummyTicket.$attributes.route_cause_analysis = "Some Cause"; + dummyTicket.$attributes.bug_sub_type = "Some Type"; + dummyTicket.$attributes.description_of_fix = "Some fix description"; + + const responseTicket = await client.post('/api/v1/projects/' + + project._id + '/sprints/' + dummySprint._id + '/tickets') + .loginVia(authUser, 'jwt') + .send(dummyTicket.$attributes) + .end(); + + dummyTicket = (JSON.parse(responseTicket.text)).data; + + responseTicket.assertStatus(201); + + responseTicket.assertJSONSubset({ + status: "OK" + }); + }); -test("A ticket exists on the given sprint", async({ client }) => +test("A ticket exists on the given sprint", async ({client}) => { - - const response = await client.get('/api/v1/projects/' + project._id + '/sprints/' + dummySprint._id) - .end(); - - response.assertStatus(200); - - response.assertJSONSubset({ - status: "OK", - data: { - name: dummySprint.name - } - }) - + + const response = await client.get('/api/v1/projects/' + project._id + '/sprints/' + dummySprint._id) + .loginVia(authUser, 'jwt') + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + status: "OK", + data: { + name: dummySprint.name + } + }) + }); -test("A Ticket can be updated when a sprint is given", async({ client }) => { - - - dummyTicket.name = "Updating Ticket name"; - - const response = await client.patch('/api/v1/projects/' + project._id + '/sprints/' + dummySprint._id + '/tickets/' + dummyTicket._id) - .send(dummyTicket) - .end(); - - dummyTicket = (JSON.parse(response.text)).data; - - response.assertStatus(200); - - response.assertJSONSubset({ - status: "OK", - data: { - name: dummyTicket.name - } - }) - +test("A Ticket can be updated when a sprint is given", async ({client}) => +{ + + + dummyTicket.name = "Updating Ticket name"; + + const response = await client.patch('/api/v1/projects/' + project._id + '/sprints/' + dummySprint._id + '/tickets/' + dummyTicket._id) + .loginVia(authUser, 'jwt') + .send(dummyTicket) + .end(); + + dummyTicket = (JSON.parse(response.text)).data; + + response.assertStatus(200); + + response.assertJSONSubset({ + status: "OK", + data: { + name: dummyTicket.name + } + }) + }); -test("A ticket can be soft deleted when a sprint is given", async({ client }) => { - - const response = await client.delete('/api/v1/projects/' + project._id + '/sprints/' + dummySprint._id + '/tickets/' + dummyTicket._id) - .send(dummyTicket) - .end(); - - response.assertStatus(200); - - response.assertJSONSubset({ - status: "OK" - }); - +test("A ticket can be soft deleted when a sprint is given", async ({client}) => +{ + + const response = await client.delete('/api/v1/projects/' + project._id + '/sprints/' + dummySprint._id + '/tickets/' + dummyTicket._id) + .loginVia(authUser, 'jwt') + .send(dummyTicket) + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + status: "OK" + }); + }); -test("A ticket can be force deleted when a sprint is given", async({ client }) => { - - const response = await client.delete('/api/v1/projects/' + project._id + '/sprints/' + dummySprint._id + '/tickets/' + dummyTicket._id + '/?forceDestroy=true') - .send(dummyTicket) - .end(); - - response.assertStatus(200); - - response.assertJSONSubset({ - status: "OK" - }); - - const responseProject = await client.delete('/api/v1/projects/' + project._id + '/?forceDestroy=true') - .send(dummyTicket) - .end(); - - responseProject.assertStatus(200); - - responseProject.assertJSONSubset({ - status: "OK" - }); - +test("A ticket can be force deleted when a sprint is given", async ({client}) => +{ + + const response = await client.delete('/api/v1/projects/' + project._id + '/sprints/' + dummySprint._id + '/tickets/' + dummyTicket._id + '/?forceDestroy=true') + .loginVia(authUser, 'jwt') + .send(dummyTicket) + .end(); + + response.assertStatus(200); + + response.assertJSONSubset({ + status: "OK" + }); + + const responseProject = await client.delete('/api/v1/projects/' + project._id + '/?forceDestroy=true') + .loginVia(authUser, 'jwt') + .send(dummyTicket) + .end(); + + responseProject.assertStatus(200); + + responseProject.assertJSONSubset({ + status: "OK" + }); + }); \ No newline at end of file diff --git a/test/unit/user-rest-test.spec.js b/test/unit/user-rest-test.spec.js index d6f30a8..851fafc 100644 --- a/test/unit/user-rest-test.spec.js +++ b/test/unit/user-rest-test.spec.js @@ -1,116 +1,140 @@ 'use strict'; -const { test, trait } = use('Test/Suite')('User R.E.S.T Tests'); +const Suite = use('Test/Suite')('Tickets R.E.S.T Test'); -const Factory = use('Factory'); +const {before, test, trait} = Suite; trait('Test/ApiClient'); -let dummyUser = null; - -test('New user can be created via the API', async ( { client, assert }) => { - - // create a user temporary using the seeders - const user = await Factory.model('App/Models/User') - .make(); - - // assign the user - dummyUser = user; - - // check if the user is undefined. - assert.notEqual(user, undefined); - - // send the request to the API to create the user from the API - const response = await client.post('/api/v1/users') - .send(user.$attributes) - .end(); - - // parse the json object - let dataObject = JSON.parse(response._res.text); +trait('Auth/Client'); - // get the userId - dummyUser._id = dataObject.data._id; - - // check if we have the proper response from the API - response.assertStatus(201); +const Factory = use('Factory'); - // from the response, explicitly access the data object and see if received an "OK" for the response. - response.assertJSONSubset({status: "OK"}); +let dummyUser = null, + authUser = null; +before(async () => +{ + // get an authenticated user. + authUser = await Factory.model('App/Models/User') + .create({type: "admin"}); }); -test('Users can be fetched via the API', async ({ client }) => { - - // get all the users from the database - const response = await client.get('/api/v1/users').end(); - - // assert the response - response.assertStatus(200); - - // assert for the JSON Object - response.assertJSONSubset({ - data: [ - { - name: dummyUser.$attributes.name - } - ] - }) +test('New user can be created via the API', async ({client, assert}) => +{ + + // create a user temporary using the seeders + const user = await Factory.model('App/Models/User') + .make(); + + // assign the user + dummyUser = user; + + // check if the user is undefined. + assert.notEqual(user, undefined); + + // send the request to the API to create the user from the API + const response = await client.post('/api/v1/users') + .loginVia(authUser, 'jwt') + .send(user.$attributes) + .end(); + + // parse the json object + let dataObject = JSON.parse(response._res.text); + + // get the userId + dummyUser._id = dataObject.data._id; + + // check if we have the proper response from the API + response.assertStatus(201); + + // from the response, explicitly access the data object and see if received an "OK" for the response. + response.assertJSONSubset({status: "OK"}); + }); -test('A user can be updated via the API', async ({ client }) => { - - // set the updating name - let updatingName = "Updated Name"; - - // assign it - dummyUser.$attributes.name = updatingName; - - // make the request to the API - const response = await client.patch('/api/v1/users/'+dummyUser.$attributes._id) - .send(dummyUser.$attributes) - .end(); - - // check for the return status - response.assertStatus(200); - - // check if the API return "OK" for the status - response.assertJSONSubset({ - data: { - name: updatingName - } - }); - +test('Users can be fetched via the API', async ({client}) => +{ + + // get all the users from the database + const response = await client.get('/api/v1/users') + .loginVia(authUser, 'jwt') + .end(); + + // assert the response + response.assertStatus(200); + + // assert for the JSON Object + response.assertJSONSubset({ + data: [ + { + name: dummyUser.$attributes.name + } + ] + }) + }); -test('A user can be soft deleted via the API', async ({ client }) => { - - // send the response to the server with correctly attached ids - const response = await client.delete('/api/v1/users/' + dummyUser.$attributes._id + '?forceDestroy=false') - .end(); - - // check for the response - response.assertStatus(200); - - // check if the API returned "OK" for the status - response.assertJSONSubset({ - status: "OK" - }); - +test('A user can be updated via the API', async ({client}) => +{ + + // set the updating name + let updatingName = "Updated Name"; + + // assign it + dummyUser.$attributes.name = updatingName; + + // make the request to the API + const response = await client.patch('/api/v1/users/' + dummyUser.$attributes._id) + .loginVia(authUser, 'jwt') + .send(dummyUser.$attributes) + .end(); + + // check for the return status + response.assertStatus(200); + + // check if the API return "OK" for the status + response.assertJSONSubset({ + data: { + name: updatingName + } + }); + }); -test('A user can be force deleted via the API', async ({ client }) => { - - // send the response to the server with correctly attached ids - const response = await client.delete('/api/v1/users/' + dummyUser.$attributes._id + '?forceDestroy=true') - .end(); - - // check for the response - response.assertStatus(200); - - // check if the API returned "OK" for the status - response.assertJSONSubset({ - status: "OK" - }); +test('A user can be soft deleted via the API', async ({client}) => +{ + + // send the response to the server with correctly attached ids + const response = await client.delete('/api/v1/users/' + dummyUser.$attributes._id + '?forceDestroy=false') + .loginVia(authUser, 'jwt') + .end(); + + // check for the response + response.assertStatus(200); + + // check if the API returned "OK" for the status + response.assertJSONSubset({ + status: "OK" + }); + +}); +test('A user can be force deleted via the API', async ({client}) => +{ + + // send the response to the server with correctly attached ids + const response = await client.delete('/api/v1/users/' + dummyUser.$attributes._id + '?forceDestroy=true') + .loginVia(authUser, 'jwt') + .end(); + + // check for the response + response.assertStatus(200); + + // check if the API returned "OK" for the status + response.assertJSONSubset({ + status: "OK" + }); + });