OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
add time entries create form and schema
ulferts614373828faa
diff --git a/app/contracts/concerns/unchanged_project.rb b/app/contracts/concerns/unchanged_project.rbnew file mode 100644index 00000000000..f754c9d4a50--- /dev/null+++ b/app/contracts/concerns/unchanged_project.rb@@ -0,0 +1,51 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module Concerns::UnchangedProject+ extend ActiveSupport::Concern++ included do+ def with_unchanged_project_id+ if model.project_id_changed?+ current_project_id = model.project_id++ model.project_id = model.project_id_was++ return_value = yield++ model.project_id = current_project_id++ return_value+ else+ yield+ end+ end+ end+enddiff --git a/app/contracts/model_contract.rb b/app/contracts/model_contract.rbindex fda51c3212f..b680a0d4d98 100644--- a/app/contracts/model_contract.rb+++ b/app/contracts/model_contract.rb@@ -180,7 +180,7 @@ class ModelContract < Reform::Contractinvalid_changes = attributes_changed_by_user - writable_attributesinvalid_changes.each do |attribute|- outside_attribute = collect_ancestor_attributes(:attribute_aliases)[attribute] || attribute+ outside_attribute = collect_ancestor_attribute_aliases[attribute] || attributeerrors.add outside_attribute, :error_readonlyend@@ -204,6 +204,10 @@ class ModelContract < Reform::Contractcollect_ancestor_attributes(:attribute_validations)end+ def collect_ancestor_attribute_aliases+ @ancestor_attribute_aliases ||= collect_ancestor_attributes(:attribute_aliases)+ end+# Traverse ancestor hierarchy to collect contract information.# This allows to define attributes on a common base class of two or more contracts.def collect_ancestor_attributes(attribute_to_collect)@@ -239,6 +243,12 @@ class ModelContract < Reform::Contractdef collect_writable_attributeswritable = collect_ancestor_attributes(:writable_attributes)+ writable.each do |attribute|+ if collect_ancestor_attribute_aliases[attribute]+ writable << collect_ancestor_attribute_aliases[attribute].to_s+ end+ end+if model.respond_to?(:available_custom_fields)writable += model.available_custom_fields.map { |cf| "custom_field_#{cf.id}" }enddiff --git a/app/contracts/time_entries/base_contract.rb b/app/contracts/time_entries/base_contract.rbindex ea5c968d9a9..accbf143695 100644--- a/app/contracts/time_entries/base_contract.rb+++ b/app/contracts/time_entries/base_contract.rb@@ -32,6 +32,15 @@ require 'model_contract'module TimeEntriesclass BaseContract < ::ModelContract+ include Concerns::AssignableValuesContract+ include Concerns::AssignableCustomFieldValues++ delegate :work_package,+ :project,+ :available_custom_fields,+ :new_record?,+ to: :model+def self.modelTimeEntryend@@ -51,11 +60,21 @@ module TimeEntriesendattribute :hoursattribute :comments+ attribute_alias :comments, :comment+attribute :spent_onattribute :tyearattribute :tmonthattribute :tweek+ def assignable_activities+ if !model.project+ TimeEntryActivity.where('1=0')+ else+ TimeEntryActivity::Scopes::ActiveInProject.fetch(model.project)+ end+ end+privatedef validate_work_package@@ -76,7 +95,7 @@ module TimeEntriesenddef validate_activity_active- errors.add :activity_id, :inclusion if model.activity && !model.activity.active_in_project?(model.project)+ errors.add :activity_id, :inclusion if model.activity_id && !assignable_activities.exists?(model.activity_id)enddef work_package_invisible?diff --git a/app/contracts/time_entries/create_contract.rb b/app/contracts/time_entries/create_contract.rbindex 1a69c3f6372..fdbc4b33673 100644--- a/app/contracts/time_entries/create_contract.rb+++ b/app/contracts/time_entries/create_contract.rb@@ -30,12 +30,9 @@module TimeEntriesclass CreateContract < BaseContract- attribute :user_id do- errors.add :user_id, :invalid if model.user != user- end-def validateuser_allowed_to_add+ validate_user_current_usersuperend@@ -47,5 +44,9 @@ module TimeEntrieserrors.add :base, :error_unauthorizedendend++ def validate_user_current_user+ errors.add :user_id, :invalid if model.user != user+ endendenddiff --git a/app/contracts/time_entries/update_contract.rb b/app/contracts/time_entries/update_contract.rbindex 2bf0084d12b..be56bd57644 100644--- a/app/contracts/time_entries/update_contract.rb+++ b/app/contracts/time_entries/update_contract.rb@@ -30,6 +30,8 @@module TimeEntriesclass UpdateContract < BaseContract+ include Concerns::UnchangedProject+def validateunless user_allowed_to_update?errors.add :base, :error_unauthorized@@ -41,18 +43,18 @@ module TimeEntriesprivate##- # Users may update time entries IFF+ # Users may update time entries IF# they have the :edit_time_entries or# user == editing user and :edit_own_time_entriesdef user_allowed_to_update?- edit_all = user.allowed_to?(:edit_time_entries, model.project)- edit_own = user.allowed_to?(:edit_own_time_entries, model.project)+ with_unchanged_project_id do+ user_allowed_to_update_in?(model.project)+ end && user_allowed_to_update_in?(model.project)+ end- if model.user == user- edit_own || edit_all- else- edit_all- end+ def user_allowed_to_update_in?(project)+ user.allowed_to?(:edit_time_entries, project) ||+ (model.user == user && user.allowed_to?(:edit_own_time_entries, project))endendenddiff --git a/app/contracts/work_packages/update_contract.rb b/app/contracts/work_packages/update_contract.rbindex 259c0e55b59..e2e8274585f 100644--- a/app/contracts/work_packages/update_contract.rb+++ b/app/contracts/work_packages/update_contract.rb@@ -32,6 +32,8 @@ require 'work_packages/base_contract'module WorkPackagesclass UpdateContract < BaseContract+ include Concerns::UnchangedProject+attribute :lock_version,permission: %i[edit_work_packages assign_versions manage_subtasks move] doif model.lock_version.nil? || model.lock_version_changed?@@ -68,20 +70,6 @@ module WorkPackagesendend- def with_unchanged_project_id- if model.project_id_changed?- current_project_id = model.project_id-- model.project_id = model.project_id_was-- yield-- model.project_id = current_project_id- else- yield- end- end-def allowed_journal_addition?model.changes.empty? && model.journal_notes && can.allowed?(model, :comment)enddiff --git a/app/helpers/timelog_helper.rb b/app/helpers/timelog_helper.rbindex 075390e19e0..088d2478238 100644--- a/app/helpers/timelog_helper.rb+++ b/app/helpers/timelog_helper.rb@@ -39,7 +39,7 @@ module TimelogHelperif project.nil?TimeEntryActivity.all.activeelse- TimeEntryActivity.in_project(project)+ TimeEntryActivity::Scopes::ActiveInProject.fetch(project)endactivities.map do |a|diff --git a/app/models/project/activity.rb b/app/models/project/activity.rbindex 3ae763c8f75..10be45f8062 100644--- a/app/models/project/activity.rb+++ b/app/models/project/activity.rb@@ -31,10 +31,10 @@ require Rails.root.join('config/constants/project_activity')module Project::Activitydef self.included(base)- base.send :extend, Scopes+ base.send :extend, ActivityScopesend- module Scopes+ module ActivityScopesdef register_latest_project_activity(on:, chain: [], attribute:)Constants::ProjectActivity.register(on: on,chain: chain,diff --git a/app/models/project/scopes/activated_time_activity.rb b/app/models/project/scopes/activated_time_activity.rbnew file mode 100644index 00000000000..3ba47126bf4--- /dev/null+++ b/app/models/project/scopes/activated_time_activity.rb@@ -0,0 +1,52 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module Project::Scopes+ class ActivatedTimeActivity+ def self.fetch(time_entry_activity)+ join_condition = <<-SQL+ LEFT OUTER JOIN time_entry_activities_projects+ ON projects.id = time_entry_activities_projects.project_id+ AND time_entry_activities_projects.activity_id = #{time_entry_activity.id}+ SQL++ join_scope = Project.joins(join_condition)++ result_scope = join_scope.where(time_entry_activities_projects: { active: true })++ if time_entry_activity.active?+ result_scope+ .or(join_scope.where(time_entry_activities_projects: { project_id: nil }))+ else+ result_scope+ end+ end+ end+enddiff --git a/app/models/project/scopes/visible_with_activated_time_activity.rb b/app/models/project/scopes/visible_with_activated_time_activity.rbnew file mode 100644index 00000000000..cb5332a9a50--- /dev/null+++ b/app/models/project/scopes/visible_with_activated_time_activity.rb@@ -0,0 +1,52 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module Project::Scopes+ class VisibleWithActivatedTimeActivity+ class << self+ def fetch(activity)+ allowed_scope+ .where(id: activated_projects(activity).select(:id))+ end++ private++ def activated_projects(activity)+ Project::Scopes::ActivatedTimeActivity.fetch(activity)+ end++ def allowed_scope+ Project+ .where(id: Project.allowed_to(User.current, :view_time_entries).select(:id))+ .or(Project.where(id: Project.allowed_to(User.current, :view_own_time_entries).select(:id)))+ end+ end+ end+enddiff --git a/app/models/time_entry.rb b/app/models/time_entry.rbindex f775a2708e4..bed7a18eb96 100644--- a/app/models/time_entry.rb+++ b/app/models/time_entry.rb@@ -93,12 +93,4 @@ class TimeEntry < ActiveRecord::Basescope = scope.where(project_id: project.hierarchy.map(&:id)) if projectscope.includes(:project).maximum(:spent_on)end-- def authoritativ_activity- if activity.shared?- activity- else- activity.root- end- endenddiff --git a/app/models/time_entry_activity.rb b/app/models/time_entry_activity.rbindex e2527e789c7..ecd73351339 100644--- a/app/models/time_entry_activity.rb+++ b/app/models/time_entry_activity.rb@@ -34,15 +34,6 @@ class TimeEntryActivity < Enumerationvalidates :parent, absence: true- def self.in_project(project)- scope = includes(time_entry_activities_projects: :activity)-- scope- .where(time_entry_activities_projects: { project_id: project.id, active: true })- .or(scope.where.not(time_entry_activities_projects: { project_id: project.id }).where(enumerations: { active: true }))- .or(scope.where(time_entry_activities_projects: { project_id: nil }, enumerations: { active: true }))- end-OptionName = :enumeration_activitiesdef option_name@@ -63,21 +54,6 @@ class TimeEntryActivity < Enumerationenddef activated_projects- join_condition = <<-SQL- LEFT OUTER JOIN time_entry_activities_projects- ON projects.id = time_entry_activities_projects.project_id- AND time_entry_activities_projects.activity_id = #{id}- SQL-- join_scope = Project.joins(join_condition)-- result_scope = join_scope.where(time_entry_activities_projects: { active: true })-- if active?- result_scope- .or(join_scope.where(time_entry_activities_projects: { project_id: nil }))- else- result_scope- end+ Project::Scopes::ActivatedTimeActivity.fetch(self)endenddiff --git a/app/models/time_entry_activity/scopes/active_in_project.rb b/app/models/time_entry_activity/scopes/active_in_project.rbnew file mode 100644index 00000000000..7245d890278--- /dev/null+++ b/app/models/time_entry_activity/scopes/active_in_project.rb@@ -0,0 +1,42 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module TimeEntryActivity::Scopes+ class ActiveInProject+ def self.fetch(project)+ scope = TimeEntryActivity.includes(time_entry_activities_projects: :activity)++ scope+ .where(time_entry_activities_projects: { project_id: project.id, active: true })+ .or(scope.where.not(time_entry_activities_projects: { project_id: project.id }).where(enumerations: { active: true }))+ .or(scope.where(time_entry_activities_projects: { project_id: nil }, enumerations: { active: true }))+ end+ end+enddiff --git a/app/services/time_entries/set_attributes_service.rb b/app/services/time_entries/set_attributes_service.rbindex 170c55b9600..ddf6b857b5c 100644--- a/app/services/time_entries/set_attributes_service.rb+++ b/app/services/time_entries/set_attributes_service.rb@@ -49,7 +49,9 @@ module TimeEntriesenddef set_default_user- model.user ||= user+ change_by_system do+ model.user = user+ endenddef set_default_activitydiff --git a/docs/api/apiv3/endpoints/time_entries.apib b/docs/api/apiv3/endpoints/time_entries.apibindex 28d6beaa8bd..af8c0d94b2f 100644--- a/docs/api/apiv3/endpoints/time_entries.apib+++ b/docs/api/apiv3/endpoints/time_entries.apib@@ -11,20 +11,22 @@| :-----------: | -------------------------------------------------------------- | ------------- | --------------------- | -------------------- | ----------------------------------------- || self | This time entry | TimeEntry | not null | READ | || project | The project the time entry is bundled in. The project might be different from the work package's project once the workPackage is moved. | Project | not null | READ / WRITE | |-| workPackage | The work package the time entry is created on | WorkPackage | not null | READ | |-| user | The user the time entry tracks expenditures for | User | not null | READ | |-| activity | The time entry activity the time entry is categorized as | TimeEntriesActivity | not null | READ | |+| workPackage | The work package the time entry is created on | WorkPackage | | READ / WRITE | |+| user | The user the time entry tracks expenditures for | User | not null | READ | |+| activity | The time entry activity the time entry is categorized as | TimeEntriesActivity | not null | READ / WRITE | |Depending on custom fields defined for time entries, additional links might exist.+Time entries are either linked to a work package or to a project. If they are linked to a project, the work package reference is empty. If they are linked to a work package, the project reference is filled up automatically to point to the work package's project.+## Local Properties| Property | Description | Type | Constraints | Supported operations | Condition || :----------: | --------------------------------------------------------- | -------- | ---------------------------------------------------- | -------------------- | ----------------------------------------------------------- || id | Time entries' id | Integer | x > 0 | READ | |-| comment | A text provided by the user detailing the time entry | String | max 255 characters | READ | |-| spentOn | The date the expenditure is booked for | Date | | READ | |-| hours | The time quantifiying the expenditure | Time | | READ | |+| comment | A text provided by the user detailing the time entry | String | max 255 characters | READ / WRITE | |+| spentOn | The date the expenditure is booked for | Date | | READ / WRITE | |+| hours | The time quantifiying the expenditure | Time | | READ / WRITE | || createdAt | The time the time entry was created | DateTime | | READ | || updatedAt | The time the time entry was last updated | DateTime | | READ | |@@ -263,6 +265,14 @@ Permanently deletes the specified time entry.},"nextByOffset": {"href": "/api/v3/time_entries?offset=2&pageSize=2"+ },+ "createTimeEntry": {+ "href": "/api/v3/time_entries/form",+ "method": "post"+ },+ "createTimeEntryImmediately": {+ "href": "/api/v3/time_entries",+ "method": "post"}}}@@ -338,7 +348,7 @@ There is no form and schema resource for time entries, yet."href": "/api/v3/projects/34"},"activity": {- "href": "/api/v3/time_entries/activities/18",+ "href": "/api/v3/time_entries/activities/18"},"workPackage": {"href": "/api/v3/work_packages/5"@@ -352,11 +362,11 @@ There is no form and schema resource for time entries, yet.},"hours": 'PT5H',"comment": {- "raw": "Some comment",+ "raw": "Some comment"},"spentOn": "2017-07-28","customField1": {- raw: 'some text custom field value'+ "raw": "some text custom field value"},"customField8": 5}@@ -423,7 +433,7 @@ There is no form and schema resource for time entries, yet.{"_links": {"activity": {- "href": "/api/v3/time_entries/activities/18",+ "href": "/api/v3/time_entries/activities/18"},"workPackage": {"href": "/api/v3/work_packages/5"@@ -435,13 +445,13 @@ There is no form and schema resource for time entries, yet."href": "/api/v3/custom_options/11"}},- "hours": 'PT5H',+ "hours": "PT5H","comment": {- "raw": "Some comment",+ "raw": "Some comment"},"spentOn": "2017-07-28","customField1": {- raw: 'some text custom field value'+ "raw": "some text custom field value"},"customField8": 5}@@ -495,7 +505,448 @@ There is no form and schema resource for time entries, yet.}}+## Time entry schema [/api/v3/time_entries/schemas]+++ Model+ + Body++ {+ "_type": "Schema",+ "_dependencies": [],+ "id": {+ "type": "Integer",+ "name": "ID",+ "required": true,+ "hasDefault": false,+ "writable": false,+ "options": {}+ },+ "createdAt": {+ "type": "DateTime",+ "name": "Created on",+ "required": true,+ "hasDefault": false,+ "writable": false,+ "options": {}+ },+ "updatedAt": {+ "type": "DateTime",+ "name": "Updated on",+ "required": true,+ "hasDefault": false,+ "writable": false,+ "options": {}+ },+ "spentOn": {+ "type": "Date",+ "name": "Date",+ "required": true,+ "hasDefault": false,+ "writable": true,+ "options": {}+ },+ "hours": {+ "type": "Duration",+ "name": "Hours",+ "required": true,+ "hasDefault": false,+ "writable": true,+ "options": {}+ },+ "user": {+ "type": "User",+ "name": "User",+ "required": true,+ "hasDefault": false,+ "writable": false,+ "options": {}+ },+ "workPackage": {+ "type": "WorkPackage",+ "name": "Work package",+ "required": false,+ "hasDefault": false,+ "writable": true,+ "_links": {}+ },+ "project": {+ "type": "Project",+ "name": "Project",+ "required": false,+ "hasDefault": false,+ "writable": true,+ "_links": {}+ },+ "activity": {+ "type": "TimeEntriesActivity",+ "name": "Activity",+ "required": true,+ "hasDefault": true,+ "writable": true,+ "_links": {}+ },+ "customField29": {+ "type": "String",+ "name": "sfsdfsdfsdfsdfdsf",+ "required": false,+ "hasDefault": false,+ "writable": true,+ "options": {+ "rtl": null+ }+ },+ "_links": {+ "self": {+ "href": "/api/v3/time_entries/schema"+ }+ }+ }++## View time entry schema [GET]+++ Response 200 (application/hal+json)++ [Time entry schema][]+++ Response 403 (application/hal+json)++ Returned if the client does not have sufficient permissions to see the schema.++ **Required permission:** *log time* or *view time entries* or *edit time entries* or *edit own time entries* on any project++ + Body++ {+ "_type": "Error",+ "errorIdentifier": "urn:openproject-org:api:v3:errors:MissingPermission",+ "message": "You are not authorized to access this resource."+ }++## Time entry create form [/api/v3/time_entries/form]++This endpoint returns a form allowing a guided creation of new time entries.++For more details and all possible responses see the general specification of [Forms](/api/forms).++## Time entry create form [POST]+++ Request Create time entry form++ + Body++ {+ "_links": {+ "activity": {+ "href": "/api/v3/time_entries/activities/18"+ },+ "workPackage": {+ "href": "/api/v3/work_packages/5"+ }+ },+ "hours": "PT5H",+ "comment": {+ "raw": "Some comment"+ },+ "spentOn": "2017-07-28",+ "customField1": {+ "raw": "some text custom field value"+ },+ "customField8": 5+ }+++ Response 200 (application/hal+json)++ + Body++ {+ "_type": "Form",+ "_embedded": {+ "payload": {+ {+ "_links": {+ "project": {+ "href": "/api/v3/projects/1",+ "title": "Some project"+ },+ "activity": {+ "href": "/api/v3/time_entries/activities/18",+ "title": "Management"+ },+ "workPackage": {+ "href": "/api/v3/work_packages/5",+ "title": "non sed quis id vel quia culpa ipsam § hkjhkhj"+ }+ },+ "hours": "PT5H",+ "comment": {+ "format": "plain",+ "raw": "Some comment",+ "html": "<p>Some comment</p>"+ },+ "spentOn": "2017-07-28",+ "customField1": {+ "format": "markdown",+ "raw": "some text custom field value"+ "html": "<p>some text custom field value</p>"+ },+ "customField8": 5+ }+ },+ "schema": {+ "_type": "Schema",+ "_dependencies": [],+ "id": {+ "type": "Integer",+ "name": "ID",+ "required": true,+ "hasDefault": false,+ "writable": false,+ "options": {}+ },+ "createdAt": {+ "type": "DateTime",+ "name": "Created on",+ "required": true,+ "hasDefault": false,+ "writable": false,+ "options": {}+ },+ "updatedAt": {+ "type": "DateTime",+ "name": "Updated on",+ "required": true,+ "hasDefault": false,+ "writable": false,+ "options": {}+ },+ "spentOn": {+ "type": "Date",+ "name": "Date",+ "required": true,+ "hasDefault": false,+ "writable": true,+ "options": {}+ },+ "hours": {+ "type": "Duration",+ "name": "Hours",+ "required": true,+ "hasDefault": false,+ "writable": true,+ "options": {}+ },+ "user": {+ "type": "User",+ "name": "User",+ "required": true,+ "hasDefault": false,+ "writable": false,+ "options": {}+ },+ "workPackage": {+ "type": "WorkPackage",+ "name": "Work package",+ "required": false,+ "hasDefault": false,+ "writable": true,+ "_links": {+ "allowedValues": {+ "href": "/api/v3/work_packages?filters=%5B%7B%22project%22%3A%7B%22operator%22%3A%22%3D%22%2C%22values%22%3A%5B%221%22%5D%7D%7D%5D"+ }+ }+ },+ "project": {+ "type": "Project",+ "name": "Project",+ "required": false,+ "hasDefault": false,+ "writable": true,+ "_links": {+ "allowedValues": {+ "href": "/api/v3/time_entries/available_projects"+ }+ }+ },+ "activity": {+ "type": "TimeEntriesActivity",+ "name": "Activity",+ "required": true,+ "hasDefault": true,+ "writable": true,+ "_embedded": {+ "allowedValues": [+ {+ "_type": "TimeEntriesActivity",+ "id": 18,+ "name": "Management",+ "position": 1,+ "default": true,+ "_links": {+ "self": {+ "href": "/api/v3/time_entries/activities/18",+ "title": "Management"+ },+ "projects": [+ {+ "href": "/api/v3/projects/some-project",+ "title": "Some project"+ },+ {+ "href": "/api/v3/projects/some-other-project",+ "title": "Some other project"+ }+ ]+ }+ },+ {+ "_type": "TimeEntriesActivity",+ "id": 23,+ "name": "Implementation",+ "position": 1,+ "default": true,+ "_links": {+ "self": {+ "href": "/api/v3/time_entries/activities/23",+ "title": "Implementation"+ },+ "projects": [+ {+ "href": "/api/v3/projects/some-project",+ "title": "Some project"+ }+ ]+ }+ }+ ]+ },+ "_links": {+ "allowedValues": [+ {+ "href": "/api/v3/time_entries/activities/18",+ "title": "Management"+ },+ {+ "href": "/api/v3/time_entries/activities/23",+ "title": "Implementation"+ }+ ]+ }+ },+ "customField1": {+ "type": "Formattable",+ "name": "Some formattable",+ "required": false,+ "hasDefault": false,+ "writable": true,+ "options": {+ "rtl": null+ }+ },+ "customField8": {+ "type": "Integer",+ "name": "Some integer",+ "required": false,+ "hasDefault": false,+ "writable": true,+ "options": {+ "rtl": null+ }+ },+ "_links": {}+ },+ "validationErrors": {}+ },+ "_links": {+ "self": {+ "href": "/api/v3/time_entries/form",+ "method": "post"+ },+ "validate": {+ "href": "/api/v3/time_entries/form",+ "method": "post"+ },+ "commit": {+ "href": "/api/v3/time_entries",+ "method": "post"+ }+ }+ }+++ Response 400 (application/hal+json)++ Occurs when the client did not send a valid JSON object in the request body.++ + Body++ {+ "_type": "Error",+ "errorIdentifier": "urn:openproject-org:api:v3:errors:InvalidRequestBody",+ "message": "The request body was not a single JSON object."+ }+++ Response 403 (application/hal+json)++ Returned if the client does not have sufficient permissions.++ **Required permission:** *log time* in any project++ + Body++ {+ "_type": "Error",+ "errorIdentifier": "urn:openproject-org:api:v3:errors:MissingPermission",+ "message": "You are not authorized to access this resource."+ }++## Available projects for time entries [/api/v3/time_entries/available_projects]+++ Model+ + Body++ {+ "_links": {+ "self": {+ "href": "/api/v3/time_entries/available_projects"+ }+ },+ "_type": "Collection",+ "total": 2,+ "count": 2,+ "_embedded": {+ "elements": [+ {+ "_type": "Project",+ << omitted for brevity >>+ },+ {+ "_type": "Project",+ << omitted for brevity >>+ }+ ]+ }+ }++## Available projects for time entries [GET]++Gets a list of projects in which a time entry can be created in or be assigned to on update. The list contains all projects in which the user issuing the request has the necessary permissions.+++ Response 200 (application/hal+json)++ [Available projects for time entries][]+++ Response 403 (application/hal+json)++ Returned if the client does not have sufficient permissions.++ **Required permission:** *log time*, *edit time entries* or *edit own time entries* in any project+ + Body++ {+ "_type": "Error",+ "errorIdentifier": "urn:openproject-org:api:v3:errors:MissingPermission",+ "message": "You are not authorized to access this resource."+ }# Group Time Entry Activities@@ -555,6 +1006,7 @@ None]}}+## View time entries activity[GET]+ Parametersdiff --git a/docs/api/apiv3/endpoints/work-packages.apib b/docs/api/apiv3/endpoints/work-packages.apibindex b330c93d2c6..3261b65c45d 100644--- a/docs/api/apiv3/endpoints/work-packages.apib+++ b/docs/api/apiv3/endpoints/work-packages.apib@@ -2005,7 +2005,7 @@ Only linked revisions from repositories are shown if the user has the view chang+ Response 200 (application/hal+json)-Returns the activity resource+ [Work Package activities][]+ Response 404 (application/hal+json)diff --git a/lib/api/decorators/create_form.rb b/lib/api/decorators/create_form.rbindex e575e88cb2b..9cc5d135f50 100644--- a/lib/api/decorators/create_form.rb+++ b/lib/api/decorators/create_form.rb@@ -50,7 +50,7 @@ module APIprivatedef downcase_model_name- model_name.downcase+ model_name.underscoreendendenddiff --git a/lib/api/v3/time_entries/available_projects_api.rb b/lib/api/v3/time_entries/available_projects_api.rbnew file mode 100644index 00000000000..f8b051c7670--- /dev/null+++ b/lib/api/v3/time_entries/available_projects_api.rb@@ -0,0 +1,52 @@+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module API+ module V3+ module TimeEntries+ class AvailableProjectsAPI < ::API::OpenProjectAPI+ after_validation do+ authorize_any %i[log_time edit_time_entries edit_own_time_entries],+ global: true+ end++ resources :available_projects do+ get &::API::V3::Utilities::Endpoints::Index+ .new(model: Project,+ scope: -> {+ Project+ .where(id: Project.allowed_to(User.current, :log_time))+ .or(Project.where(id: Project.allowed_to(User.current, :edit_time_entries)))+ .or(Project.where(id: Project.allowed_to(User.current, :edit_own_time_entries)))+ })+ .mount+ end+ end+ end+ end+enddiff --git a/lib/api/v3/time_entries/create_form_api.rb b/lib/api/v3/time_entries/create_form_api.rbnew file mode 100644index 00000000000..eccb8bcbd93--- /dev/null+++ b/lib/api/v3/time_entries/create_form_api.rb@@ -0,0 +1,45 @@+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module API+ module V3+ module TimeEntries+ class CreateFormAPI < ::API::OpenProjectAPI+ resource :form do+ after_validation do+ authorize :log_time, global: true+ end++ post &::API::V3::Utilities::Endpoints::CreateForm+ .new(model: TimeEntry)+ .mount+ end+ end+ end+ end+enddiff --git a/lib/api/v3/time_entries/create_form_representer.rb b/lib/api/v3/time_entries/create_form_representer.rbnew file mode 100644index 00000000000..613b0caa0bc--- /dev/null+++ b/lib/api/v3/time_entries/create_form_representer.rb@@ -0,0 +1,39 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module API+ module V3+ module TimeEntries+ class CreateFormRepresenter < FormRepresenter+ include API::Decorators::CreateForm+ end+ end+ end+enddiff --git a/lib/api/v3/time_entries/form_representer.rb b/lib/api/v3/time_entries/form_representer.rbnew file mode 100644index 00000000000..1ff0beaa4f5--- /dev/null+++ b/lib/api/v3/time_entries/form_representer.rb@@ -0,0 +1,41 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module API+ module V3+ module TimeEntries+ class FormRepresenter < ::API::Decorators::SimpleForm+ def model+ TimeEntry+ end+ end+ end+ end+enddiff --git a/lib/api/v3/time_entries/schemas/time_entry_schema_api.rb b/lib/api/v3/time_entries/schemas/time_entry_schema_api.rbnew file mode 100644index 00000000000..a3bea0afea6--- /dev/null+++ b/lib/api/v3/time_entries/schemas/time_entry_schema_api.rb@@ -0,0 +1,51 @@+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module API+ module V3+ module TimeEntries+ module Schemas+ class TimeEntrySchemaAPI < ::API::OpenProjectAPI+ resources :schema do+ after_validation do+ authorize_any %i[log_time+ view_time_entries+ edit_time_entries+ edit_own_time_entries],+ global: true+ end++ get &::API::V3::Utilities::Endpoints::Schema+ .new(model: TimeEntry)+ .mount+ end+ end+ end+ end+ end+enddiff --git a/lib/api/v3/time_entries/schemas/time_entry_schema_representer.rb b/lib/api/v3/time_entries/schemas/time_entry_schema_representer.rbnew file mode 100644index 00000000000..3834529cec4--- /dev/null+++ b/lib/api/v3/time_entries/schemas/time_entry_schema_representer.rb@@ -0,0 +1,102 @@+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module API+ module V3+ module TimeEntries+ module Schemas+ class TimeEntrySchemaRepresenter < ::API::Decorators::SchemaRepresenter+ extend ::API::V3::Utilities::CustomFieldInjector::RepresenterClass+ custom_field_injector type: :schema_representer++ def self.represented_class+ TimeEntry+ end++ schema :id,+ type: 'Integer'++ schema :created_at,+ type: 'DateTime'++ schema :updated_at,+ type: 'DateTime'++ schema :spent_on,+ type: 'Date'++ schema :hours,+ type: 'Duration'++ schema :user,+ type: 'User'++ schema_with_allowed_link :work_package,+ has_default: false,+ required: false,+ href_callback: ->(*) {+ allowed_work_package_href+ }++ schema_with_allowed_link :project,+ has_default: false,+ required: false,+ href_callback: ->(*) {+ allowed_projects_href+ }++ schema_with_allowed_collection :activity,+ type: 'TimeEntriesActivity',+ value_representer: TimeEntriesActivityRepresenter,+ has_default: true,+ required: true,+ link_factory: ->(value) {+ {+ href: api_v3_paths.time_entries_activity(value.id),+ title: value.name+ }+ }++ def allowed_work_package_href+ api_v3_paths.path_for(:work_packages, filters: allowed_work_packages_filters)+ end++ def allowed_work_packages_filters+ return unless represented.project++ [{ project: { operator: '=', values: [represented.project.id.to_s] } }]+ end++ def allowed_projects_href+ api_v3_paths.time_entries_available_projects+ end+ end+ end+ end+ end+enddiff --git a/lib/api/v3/time_entries/time_entries_activity_representer.rb b/lib/api/v3/time_entries/time_entries_activity_representer.rbindex 2cebbd8c912..ffe2d2c3c81 100644--- a/lib/api/v3/time_entries/time_entries_activity_representer.rb+++ b/lib/api/v3/time_entries/time_entries_activity_representer.rb@@ -65,10 +65,7 @@ module APIenddef active_projects- # TODO: Move into contract- Project- .allowed_to(current_user, :view_time_entries)- .where(id: represented.activated_projects.select(:id))+ Project::Scopes::VisibleWithActivatedTimeActivity.fetch(represented)endendenddiff --git a/lib/api/v3/time_entries/time_entries_api.rb b/lib/api/v3/time_entries/time_entries_api.rbindex cb14ed1e0a5..da881c4c1ba 100644--- a/lib/api/v3/time_entries/time_entries_api.rb+++ b/lib/api/v3/time_entries/time_entries_api.rb@@ -36,6 +36,10 @@ module APIget &::API::V3::Utilities::Endpoints::Index.new(model: TimeEntry).mountpost &::API::V3::Utilities::Endpoints::Create.new(model: TimeEntry).mount+ mount ::API::V3::TimeEntries::CreateFormAPI+ mount ::API::V3::TimeEntries::Schemas::TimeEntrySchemaAPI+ mount ::API::V3::TimeEntries::AvailableProjectsAPI+route_param :id, type: Integer, desc: 'Time entry ID' doafter_validation do@time_entry = TimeEntrydiff --git a/lib/api/v3/time_entries/time_entry_collection_representer.rb b/lib/api/v3/time_entries/time_entry_collection_representer.rbindex ca1d88477fc..5afb5b584c7 100644--- a/lib/api/v3/time_entries/time_entry_collection_representer.rb+++ b/lib/api/v3/time_entries/time_entry_collection_representer.rb@@ -34,6 +34,15 @@ module APIclass TimeEntryCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollectionelement_decorator ::API::V3::TimeEntries::TimeEntryRepresenter+ link :createTimeEntry do+ next unless current_user.allowed_to_globally?(:log_time)++ {+ href: api_v3_paths.create_time_entry_form,+ method: :post+ }+ end+link :createTimeEntryImmediately donext unless current_user.allowed_to_globally?(:log_time)diff --git a/lib/api/v3/time_entries/time_entry_payload_representer.rb b/lib/api/v3/time_entries/time_entry_payload_representer.rbindex 4592021225a..6fa71410486 100644--- a/lib/api/v3/time_entries/time_entry_payload_representer.rb+++ b/lib/api/v3/time_entries/time_entry_payload_representer.rb@@ -30,6 +30,7 @@ module APImodule V3module TimeEntriesclass TimeEntryPayloadRepresenter < TimeEntryRepresenter+ include ::API::Utilities::PayloadRepresenterendendenddiff --git a/lib/api/v3/time_entries/time_entry_representer.rb b/lib/api/v3/time_entries/time_entry_representer.rbindex 04acd9c3aed..7b2e15b45a5 100644--- a/lib/api/v3/time_entries/time_entry_representer.rb+++ b/lib/api/v3/time_entries/time_entry_representer.rb@@ -70,7 +70,7 @@ module APIproperty :hours,exec_context: :decorator,getter: ->(*) do- datetime_formatter.format_duration_from_hours(represented.hours)+ datetime_formatter.format_duration_from_hours(represented.hours) if represented.hoursend,setter: ->(fragment:, **) dorepresented.hours = datetime_formatter.parse_duration_to_hours(fragment,@@ -92,14 +92,6 @@ module APIassociated_resource :activity,representer: TimeEntriesActivityRepresenter,v3_path: :time_entries_activity,- getter: associated_resource_default_getter(:authoritativ_activity, TimeEntriesActivityRepresenter),- link: ->(*) {- activity = represented.authoritativ_activity- {- href: api_v3_paths.time_entries_activity(activity.id),- title: activity.name- }- },setter: ->(fragment:, **) {::API::Decorators::LinkObject.new(represented,diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rbindex fa3087b0d28..61e6ce4c862 100644--- a/lib/api/v3/utilities/path_helper.rb+++ b/lib/api/v3/utilities/path_helper.rb@@ -323,13 +323,16 @@ module API"#{root}/string_objects?value=#{val}"end- index :time_entry- show :time_entry+ resources :time_entrydef self.time_entries_activity(activity_id)"#{root}/time_entries/activities/#{activity_id}"end+ def self.time_entries_available_projects+ "#{time_entries}/available_projects"+ end+index :typeshow :typediff --git a/spec/contracts/time_entries/create_contract_spec.rb b/spec/contracts/time_entries/create_contract_spec.rbindex 74f1e6860bb..289c3f42189 100644--- a/spec/contracts/time_entries/create_contract_spec.rb+++ b/spec/contracts/time_entries/create_contract_spec.rb@@ -43,10 +43,13 @@ describe TimeEntries::CreateContract doendlet(:permissions) { %i(log_time) }let(:other_user) { FactoryBot.build_stubbed(:user) }+ let(:changed_by_system) { %w(user_id) }- subject(:contract) { described_class.new(time_entry, current_user) }+ subject(:contract) do+ described_class.new(time_entry, current_user, options: { changed_by_system: changed_by_system })+ end- context 'when user is not allowed to log time' do+ context 'if user is not allowed to log time' dolet(:permissions) { [] }it 'is invalid' do@@ -54,7 +57,7 @@ describe TimeEntries::CreateContract doendend- context 'when time_entry user is not contract user' do+ context 'if time_entry user is not contract user' dolet(:time_entry_user) { other_user }it 'is invalid' do@@ -62,7 +65,16 @@ describe TimeEntries::CreateContract doendend- context 'when the user is nil' do+ context 'if time_entry user was not set by system' do+ let(:time_entry_user) { other_user }+ let(:changed_by_system) { %w() }++ it 'is invalid' do+ expect_valid(false, user_id: %i(invalid error_readonly))+ end+ end++ context 'if the user is nil' dolet(:time_entry_user) { nil }it 'is invalid' dodiff --git a/spec/contracts/time_entries/shared_contract_examples.rb b/spec/contracts/time_entries/shared_contract_examples.rbindex f60292d9dbc..48b296eff97 100644--- a/spec/contracts/time_entries/shared_contract_examples.rb+++ b/spec/contracts/time_entries/shared_contract_examples.rb@@ -47,24 +47,42 @@ shared_examples_for 'time entry contract' dolet(:time_entry_project) { FactoryBot.build_stubbed(:project) }let(:time_entry_user) { current_user }let(:time_entry_activity) do- FactoryBot.build_stubbed(:time_entry_activity).tap do |activity|- allow(activity)- .to receive(:active_in_project?)- .with(time_entry_project)- .and_return(time_entry_activity_active)- end+ FactoryBot.build_stubbed(:time_entry_activity)endlet(:time_entry_activity_active) { true }let(:time_entry_spent_on) { Date.today }let(:time_entry_hours) { 5 }let(:time_entry_comments) { "A comment" }let(:work_package_visible) { true }+ let(:activities_scope) do+ scope = double('activities')++ if (time_entry_activity)+ allow(scope)+ .to receive(:exists?)+ .with(time_entry_activity.id)+ .and_return(time_entry_activity_active)+ end++ scope+ endbefore do- allow(time_entry_work_package)- .to receive(:visible?)- .with(current_user)- .and_return(work_package_visible)+ if time_entry_work_package+ allow(time_entry_work_package)+ .to receive(:visible?)+ .with(current_user)+ .and_return(work_package_visible)+ end++ allow(TimeEntryActivity::Scopes::ActiveInProject)+ .to receive(:fetch)+ .and_return(TimeEntryActivity.where('1=0'))++ allow(TimeEntryActivity::Scopes::ActiveInProject)+ .to receive(:fetch)+ .with(time_entry_project)+ .and_return(activities_scope)enddef expect_valid(valid, symbols = {})@@ -91,6 +109,12 @@ shared_examples_for 'time entry contract' doendend+ context 'when the work_package is nil' do+ let(:time_entry_work_package) { nil }++ it_behaves_like 'is valid'+ end+context 'when the project is nil' dolet(:time_entry_project) { nil }@@ -160,4 +184,22 @@ shared_examples_for 'time entry contract' doit_behaves_like 'is valid'end++ describe 'assignable_activities' do+ context 'if no project is set' do+ let(:time_entry_project) { nil }++ it 'is empty' do+ expect(contract.assignable_activities)+ .to be_empty+ end+ end++ context 'if a project is set' do+ it 'returns all activities active in the project' do+ expect(contract.assignable_activities)+ .to eql activities_scope+ end+ end+ endenddiff --git a/spec/contracts/time_entries/update_contract_spec.rb b/spec/contracts/time_entries/update_contract_spec.rbindex 6ed1c41f7d9..d346fed66e8 100644--- a/spec/contracts/time_entries/update_contract_spec.rb+++ b/spec/contracts/time_entries/update_contract_spec.rb@@ -45,7 +45,7 @@ describe TimeEntries::UpdateContract dosubject(:contract) { described_class.new(time_entry, current_user) }let(:permissions) { %i(edit_time_entries) }- context 'when user is not allowed to edit time entries' do+ context 'if user is not allowed to edit time entries' dolet(:permissions) { [] }it 'is invalid' do@@ -53,7 +53,65 @@ describe TimeEntries::UpdateContract doendend- context 'when the user is nil' do+ context 'if project changed' do+ let(:new_project) do+ FactoryBot.build_stubbed(:project).tap do |p|+ allow(TimeEntryActivity::Scopes::ActiveInProject)+ .to receive(:fetch)+ .with(p)+ .and_return(activities_scope)++ allow(time_entry)+ .to receive(:project) do+ case time_entry.project_id+ when p.id+ p+ when time_entry_project.id+ time_entry_project+ end+ end++ time_entry_work_package.project = p++ allow(current_user)+ .to receive(:allowed_to?) do |permission, permission_project|+ new_project_permissions.include?(permission) && p == permission_project ||+ permissions.include?(permission) && time_entry_project == permission_project+ end+ end+ end++ before do+ time_entry.project = new_project+ end++ context 'if user is not allowed to edit time entries in old project but in new' do+ let(:permissions) { [] }+ let(:new_project_permissions) { %i(edit_time_entries) }++ it 'is invalid' do+ expect_valid(false, base: %i(error_unauthorized))+ end+ end++ context 'if user is allowed to edit time entries in old project but not in new' do+ let(:new_project_permissions) { [] }++ it 'is invalid' do+ expect_valid(false, base: %i(error_unauthorized))+ end+ end++ context 'if user is allowed to edit time entries in both projects' do+ let(:new_project_permissions) { %i(edit_time_entries) }++ it 'is valid' do+ expect_valid(true)+ end+ end+ end++ context 'if the user is nil' dolet(:time_entry_user) { nil }it 'is invalid' do@@ -61,17 +119,17 @@ describe TimeEntries::UpdateContract doendend- context 'when the user is changed' do+ context 'if the user is changed' doit 'is invalid' dotime_entry.user = other_userexpect_valid(false, user_id: %i(error_readonly))endend- context 'when time_entry user is not contract user' do+ context 'if time_entry user is not contract user' dolet(:time_entry_user) { other_user }- context 'when has permission' do+ context 'if has permission' dolet(:permissions) { %i[edit_time_entries] }it 'is valid' do@@ -79,7 +137,7 @@ describe TimeEntries::UpdateContract doendend- context 'when has no permission' do+ context 'if has no permission' dolet(:permissions) { %i[edit_own_time_entries] }it 'is invalid' doexpect_valid(false, base: %i(error_unauthorized))diff --git a/spec/lib/api/v3/time_entries/schemas/time_entry_schema_representer_spec.rb b/spec/lib/api/v3/time_entries/schemas/time_entry_schema_representer_spec.rbnew file mode 100644index 00000000000..45382013fec--- /dev/null+++ b/spec/lib/api/v3/time_entries/schemas/time_entry_schema_representer_spec.rb@@ -0,0 +1,264 @@+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'++describe ::API::V3::TimeEntries::Schemas::TimeEntrySchemaRepresenter do+ include API::V3::Utilities::PathHelper++ let(:current_user) { FactoryBot.build_stubbed(:user) }++ let(:self_link) { '/a/self/link' }+ let(:embedded) { true }+ let(:new_record) { true }+ let(:project) { FactoryBot.build_stubbed(:project) }+ let(:user) { FactoryBot.build_stubbed(:user) }+ let(:assigned_project) { nil }+ let(:activity) { FactoryBot.build_stubbed(:time_entry_activity) }++ let(:contract) do+ contract = double('contract',+ new_record?: new_record,+ project: assigned_project)++ allow(contract)+ .to receive(:writable?) do |attribute|+ %w(spent_on hours project work_package activity).include?(attribute.to_s)+ end++ allow(contract)+ .to receive(:available_custom_fields)+ .and_return([])++ allow(contract)+ .to receive(:assignable_values) do |attribute, _user|+ case attribute+ when :activity+ [activity]+ end+ end++ contract+ end+ let(:representer) do+ described_class.create(contract,+ self_link,+ form_embedded: embedded,+ current_user: current_user)+ end++ context 'generation' do+ subject(:generated) { representer.to_json }++ describe '_type' do+ it 'is indicated as Schema' do+ is_expected.to be_json_eql('Schema'.to_json).at_path('_type')+ end+ end++ describe 'id' do+ let(:path) { 'id' }++ it_behaves_like 'has basic schema properties' do+ let(:type) { 'Integer' }+ let(:name) { I18n.t('attributes.id') }+ let(:required) { true }+ let(:writable) { false }+ end+ end++ describe 'spentOn' do+ let(:path) { 'spentOn' }++ it_behaves_like 'has basic schema properties' do+ let(:type) { 'Date' }+ let(:name) { TimeEntry.human_attribute_name('spent_on') }+ let(:required) { true }+ let(:writable) { true }+ end+ end++ describe 'hours' do+ let(:path) { 'hours' }++ it_behaves_like 'has basic schema properties' do+ let(:type) { 'Duration' }+ let(:name) { TimeEntry.human_attribute_name('hours') }+ let(:required) { true }+ let(:writable) { true }+ end+ end++ describe 'createdAt' do+ let(:path) { 'createdAt' }++ it_behaves_like 'has basic schema properties' do+ let(:type) { 'DateTime' }+ let(:name) { TimeEntry.human_attribute_name('created_at') }+ let(:required) { true }+ let(:writable) { false }+ end+ end++ describe 'updatedAt' do+ let(:path) { 'updatedAt' }++ it_behaves_like 'has basic schema properties' do+ let(:type) { 'DateTime' }+ let(:name) { TimeEntry.human_attribute_name('updated_at') }+ let(:required) { true }+ let(:writable) { false }+ end+ end++ describe 'user' do+ let(:path) { 'user' }++ it_behaves_like 'has basic schema properties' do+ let(:type) { 'User' }+ let(:name) { TimeEntry.human_attribute_name('user') }+ let(:required) { true }+ let(:writable) { false }+ end+ end++ describe 'work_package' do+ let(:path) { 'workPackage' }++ it_behaves_like 'has basic schema properties' do+ let(:type) { 'WorkPackage' }+ let(:name) { TimeEntry.human_attribute_name('work_package') }+ let(:required) { false }+ let(:writable) { true }+ end++ context 'if embedding' do+ let(:embedded) { true }++ context 'if having no project' do+ it_behaves_like 'links to allowed values via collection link' do+ let(:href) do+ api_v3_paths.work_packages+ end+ end+ end++ context 'if having a project' do+ let(:assigned_project) { project }++ it_behaves_like 'links to allowed values via collection link' do+ let(:href) do+ api_v3_paths.path_for(:work_packages, filters: [{ project: { operator: '=', values: [project.id.to_s] } }])+ end+ end+ end+ end++ describe 'project' do+ let(:path) { 'project' }++ it_behaves_like 'has basic schema properties' do+ let(:type) { 'Project' }+ let(:name) { TimeEntry.human_attribute_name('project') }+ let(:required) { false }+ let(:writable) { true }+ end++ context 'if embedding' do+ let(:embedded) { true }++ it_behaves_like 'links to allowed values via collection link' do+ let(:href) do+ api_v3_paths.time_entries_available_projects+ end+ end+ end+ end+ end++ describe 'activity' do+ let(:path) { 'activity' }++ it_behaves_like 'has basic schema properties' do+ let(:type) { 'TimeEntriesActivity' }+ let(:name) { TimeEntry.human_attribute_name('activity') }+ let(:has_default) { true }+ let(:required) { true }+ let(:writable) { true }+ end++ context 'if embedding' do+ let(:embedded) { true }++ it_behaves_like 'links to and embeds allowed values directly' do+ let(:hrefs) { [activity].map { |value| "/api/v3/time_entries/activities/#{value.id}" } }+ end+ end++ describe 'project' do+ let(:path) { 'project' }++ it_behaves_like 'has basic schema properties' do+ let(:type) { 'Project' }+ let(:name) { TimeEntry.human_attribute_name('project') }+ let(:required) { false }+ let(:writable) { true }+ end++ context 'if embedding' do+ let(:embedded) { true }++ it_behaves_like 'links to allowed values via collection link' do+ let(:href) do+ api_v3_paths.time_entries_available_projects+ end+ end+ end+ end+ end++ context 'custom value' do+ let(:custom_field) { FactoryBot.build_stubbed(:time_entry_custom_field) }++ before do+ allow(contract)+ .to receive(:available_custom_fields)+ .and_return([custom_field])+ end++ let(:path) { "customField#{custom_field.id}" }++ it_behaves_like 'has basic schema properties' do+ let(:type) { 'Formattable' }+ let(:name) { custom_field.name }+ let(:required) { false }+ let(:writable) { true }+ end+ end+ end+enddiff --git a/spec/lib/api/v3/time_entries/time_entries_activity_representer_rendering_spec.rb b/spec/lib/api/v3/time_entries/time_entries_activity_representer_rendering_spec.rbindex cc399c61ffc..71356552fa8 100644--- a/spec/lib/api/v3/time_entries/time_entries_activity_representer_rendering_spec.rb+++ b/spec/lib/api/v3/time_entries/time_entries_activity_representer_rendering_spec.rb@@ -54,15 +54,9 @@ describe ::API::V3::TimeEntries::TimeEntriesActivityRepresenter, 'rendering' dolet(:project2) { FactoryBot.build_stubbed(:project) }before do- scope = double('scope')-- allow(activity)- .to receive(:activated_projects)- .and_return(scope)-- allow(scope)- .to receive(:allowed_to)- .with(user, :view_time_entries)+ allow(::Project::Scopes::VisibleWithActivatedTimeActivity)+ .to receive(:fetch)+ .with(activity).and_return([project1,project2])enddiff --git a/spec/lib/api/v3/time_entries/time_entry_representer_rendering_spec.rb b/spec/lib/api/v3/time_entries/time_entry_representer_rendering_spec.rbindex ca7bc387797..532459690a4 100644--- a/spec/lib/api/v3/time_entries/time_entry_representer_rendering_spec.rb+++ b/spec/lib/api/v3/time_entries/time_entry_representer_rendering_spec.rb@@ -37,7 +37,7 @@ describe ::API::V3::TimeEntries::TimeEntryRepresenter, 'rendering' dospent_on: Date.today,created_on: DateTime.now - 6.hours,updated_on: DateTime.now - 3.hours,- hours: 5,+ hours: hours,activity: activity,project: project,user: user)@@ -47,6 +47,7 @@ describe ::API::V3::TimeEntries::TimeEntryRepresenter, 'rendering' dolet(:activity) { FactoryBot.build_stubbed(:time_entry_activity) }let(:user) { FactoryBot.build_stubbed(:user) }let(:current_user) { user }+ let(:hours) { 5 }let(:permissions) do[:edit_time_entries]end@@ -98,28 +99,6 @@ describe ::API::V3::TimeEntries::TimeEntryRepresenter, 'rendering' dolet(:title) { activity.name }end- context 'for a non shared (project specific) activity' do- let(:activity) do- activity = FactoryBot.build_stubbed(:time_entry_activity,- project: project,- parent: parent_activity)- allow(activity)- .to receive(:root)- .and_return(parent_activity)-- activity- end- let(:parent_activity) do- FactoryBot.build_stubbed(:time_entry_activity)- end-- it_behaves_like 'has a titled link' do- let(:link) { 'activity' }- let(:href) { api_v3_paths.time_entries_activity parent_activity.id }- let(:title) { parent_activity.name }- end- end-context 'custom value' dolet(:custom_field) doFactoryBot.build_stubbed(:time_entry_custom_field, field_format: 'user')@@ -234,8 +213,18 @@ describe ::API::V3::TimeEntries::TimeEntryRepresenter, 'rendering' dolet(:value) { time_entry.spent_on }end- it_behaves_like 'property', :hours do- let(:value) { 'PT5H' }+ context 'hours' do+ it_behaves_like 'property', :hours do+ let(:value) { 'PT5H' }+ end++ context 'if hours are nil' do+ let(:hours) { nil }++ it_behaves_like 'property', :hours do+ let(:value) { nil }+ end+ endendit_behaves_like 'datetime property', :createdAt dodiff --git a/spec/lib/api/v3/utilities/path_helper_spec.rb b/spec/lib/api/v3/utilities/path_helper_spec.rbindex 1302168d59d..d8962893e21 100644--- a/spec/lib/api/v3/utilities/path_helper_spec.rb+++ b/spec/lib/api/v3/utilities/path_helper_spec.rb@@ -388,14 +388,19 @@ describe ::API::V3::Utilities::PathHelper doendcontext 'time_entry paths' do- it_behaves_like 'index', :time_entry- it_behaves_like 'show', :time_entry+ it_behaves_like 'resource', :time_entry- describe '.time_entries_activity' do+ describe '#time_entries_activity' dosubject { helper.time_entries_activity 42 }it_behaves_like 'api v3 path', '/time_entries/activities/42'end++ describe '#time_entries_available_projects' do+ subject { helper.time_entries_available_projects }++ it_behaves_like 'api v3 path', '/time_entries/available_projects'+ endenddescribe 'types paths' dodiff --git a/spec/models/project/scopes/activated_time_activity_spec.rb b/spec/models/project/scopes/activated_time_activity_spec.rbnew file mode 100644index 00000000000..caec9cd617f--- /dev/null+++ b/spec/models/project/scopes/activated_time_activity_spec.rb@@ -0,0 +1,86 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'++describe Project::Scopes::ActivatedTimeActivity, type: :model do+ let!(:activity) { FactoryBot.create(:time_entry_activity) }+ let!(:project) { FactoryBot.create(:project) }+ let!(:other_project) { FactoryBot.create(:project) }++ describe '.fetch' do+ subject { described_class.fetch(activity) }++ context 'without project specific overrides' do+ context 'and being active' do+ it 'returns all projects' do+ is_expected+ .to match_array [project, other_project]+ end+ end++ context 'and not being active' do+ before do+ activity.update_attribute(:active, false)+ end++ it 'returns no projects' do+ is_expected+ .to be_empty+ end+ end+ end++ context 'with project specific overrides' do+ before do+ TimeEntryActivitiesProject.insert(activity_id: activity.id, project_id: project.id, active: true)+ TimeEntryActivitiesProject.insert(activity_id: activity.id, project_id: other_project.id, active: false)+ end++ context 'and being active' do+ it 'returns the project the activity is activated in' do+ is_expected+ .to match_array [project]+ end+ end++ context 'and not being active' do+ before do+ activity.update_attribute(:active, false)+ end++ it 'returns only the projects the activity is activated in' do+ is_expected+ .to match_array [project]+ end+ end+ end+ end+enddiff --git a/spec/models/project/scopes/visible_with_activated_time_activity_spec.rb b/spec/models/project/scopes/visible_with_activated_time_activity_spec.rbnew file mode 100644index 00000000000..0b7e6f150f2--- /dev/null+++ b/spec/models/project/scopes/visible_with_activated_time_activity_spec.rb@@ -0,0 +1,125 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'++describe Project::Scopes::VisibleWithActivatedTimeActivity, type: :model do+ let!(:activity) { FactoryBot.create(:time_entry_activity) }+ let!(:project) { FactoryBot.create(:project) }+ let!(:other_project) { FactoryBot.create(:project) }+ let(:project_permissions) { [:view_time_entries] }+ let(:other_project_permissions) { [:view_time_entries] }+ let(:current_user) do+ FactoryBot.create(:user).tap do |u|+ FactoryBot.create(:member,+ project: project,+ principal: u,+ roles: [FactoryBot.create(:role, permissions: project_permissions)])++ FactoryBot.create(:member,+ project: other_project,+ principal: u,+ roles: [FactoryBot.create(:role, permissions: other_project_permissions)])+ end+ end++ before do+ login_as(current_user)+ end++ describe '.fetch' do+ subject { described_class.fetch(activity) }++ context 'without project specific overrides' do+ context 'and being active' do+ it 'returns all projects' do+ is_expected+ .to match_array [project, other_project]+ end+ end++ context 'and not being active' do+ before do+ activity.update_attribute(:active, false)+ end++ it 'returns no projects' do+ is_expected+ .to be_empty+ end+ end++ context 'and having only view_own_time_entries_permission' do+ let(:project_permissions) { [:view_own_time_entries] }+ let(:other_project_permissions) { [:view_own_time_entries] }++ it 'returns all projects' do+ is_expected+ .to match_array [project, other_project]+ end+ end++ context 'and having no view permission' do+ let(:project_permissions) { [] }+ let(:other_project_permissions) { [] }++ it 'returns all projects' do+ is_expected+ .to be_empty+ end+ end+ end++ context 'with project specific overrides' do+ before do+ TimeEntryActivitiesProject.insert(activity_id: activity.id, project_id: project.id, active: true)+ TimeEntryActivitiesProject.insert(activity_id: activity.id, project_id: other_project.id, active: false)+ end++ context 'and being active' do+ it 'returns the project the activity is activated in' do+ is_expected+ .to match_array [project]+ end+ end++ context 'and not being active' do+ before do+ activity.update_attribute(:active, false)+ end++ it 'returns only the projects the activity is activated in' do+ is_expected+ .to match_array [project]+ end+ end+ end+ end+enddiff --git a/spec/models/time_entry_activity_spec.rb b/spec/models/time_entry_activity/scopes/active_in_project_spec.rbsimilarity index 61%rename from spec/models/time_entry_activity_spec.rbrename to spec/models/time_entry_activity/scopes/active_in_project_spec.rbindex 0e2b61e4ede..75dcc733a3f 100644--- a/spec/models/time_entry_activity_spec.rb+++ b/spec/models/time_entry_activity/scopes/active_in_project_spec.rb@@ -1,4 +1,5 @@#-- encoding: UTF-8+#-- copyright# OpenProject is a project management system.# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)@@ -29,16 +30,18 @@require 'spec_helper'-describe TimeEntryActivity, type: :model do+describe TimeEntryActivity::Scopes::ActiveInProject, type: :model dolet(:activity) { FactoryBot.create(:time_entry_activity) }let(:project) { FactoryBot.create(:project) }let(:other_project) { FactoryBot.create(:project) }- describe '.in_project' do+ describe '.fetch' do+ subject { described_class.fetch(project) }+context 'without a project configuration' docontext 'with the activity being active' doit 'includes the activity' do- expect(TimeEntryActivity.in_project(project))+ is_expected.to match_array [activity]endend@@ -49,7 +52,7 @@ describe TimeEntryActivity, type: :model doendit 'excludes the activity' do- expect(TimeEntryActivity.in_project(project))+ is_expected.to be_emptyendend@@ -61,7 +64,7 @@ describe TimeEntryActivity, type: :model doendit 'includes the activity' do- expect(TimeEntryActivity.in_project(project))+ is_expected.to match_array [activity]end@@ -71,7 +74,7 @@ describe TimeEntryActivity, type: :model doendit 'includes the activity' do- expect(TimeEntryActivity.in_project(project))+ is_expected.to match_array [activity]endend@@ -83,7 +86,7 @@ describe TimeEntryActivity, type: :model doendit 'includes the activity' do- expect(TimeEntryActivity.in_project(project))+ is_expected.to match_array [activity]endend@@ -94,61 +97,9 @@ describe TimeEntryActivity, type: :model doendit 'excludes the activity' do- expect(TimeEntryActivity.in_project(project))+ is_expected.to be_emptyendendend-- describe '#activated_projects' do- before do- project- other_project- end-- context 'without project specific overrides' do- context 'and being active' do- it 'returns all projects' do- expect(activity.activated_projects)- .to match_array [project, other_project]- end- end-- context 'and not being active' do- before do- activity.update_attribute(:active, false)- end-- it 'returns no projects' do- expect(activity.activated_projects)- .to be_empty- end- end- end-- context 'with project specific overrides' do- before do- TimeEntryActivitiesProject.insert(activity_id: activity.id, project_id: project.id, active: true)- TimeEntryActivitiesProject.insert(activity_id: activity.id, project_id: other_project.id, active: false)- end-- context 'and being active' do- it 'returns the project the activity is activated in' do- expect(activity.activated_projects)- .to match_array [project]- end- end-- context 'and not being active' do- before do- activity.update_attribute(:active, false)- end-- it 'returns only the projects the activity is activated in' do- expect(activity.activated_projects)- .to match_array [project]- end- end- end- endenddiff --git a/spec/requests/api/v3/time_entries/available_projects_resource_spec.rb b/spec/requests/api/v3/time_entries/available_projects_resource_spec.rbnew file mode 100644index 00000000000..8ef3d0ab403--- /dev/null+++ b/spec/requests/api/v3/time_entries/available_projects_resource_spec.rb@@ -0,0 +1,146 @@+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'+require 'rack/test'++describe 'API v3 time entries available projects resource', type: :request do+ include Rack::Test::Methods+ include API::V3::Utilities::PathHelper++ let(:current_user) do+ FactoryBot.create(:user)+ end+ let(:other_user) do+ FactoryBot.create(:user)+ end+ let(:project_with_log_permission) do+ FactoryBot.create(:project).tap do |p|+ FactoryBot.create(:member,+ roles: [FactoryBot.create(:role, permissions: [:log_time])],+ project: p,+ user: current_user)+ end+ end+ let(:project_with_edit_permission) do+ FactoryBot.create(:project).tap do |p|+ FactoryBot.create(:member,+ roles: [FactoryBot.create(:role, permissions: [:edit_time_entries])],+ project: p,+ user: current_user)+ end+ end+ let(:project_with_edit_own_permission) do+ FactoryBot.create(:project).tap do |p|+ FactoryBot.create(:member,+ roles: [FactoryBot.create(:role, permissions: [:edit_own_time_entries])],+ project: p,+ user: current_user)+ end+ end+ let(:project_with_view_permission) do+ FactoryBot.create(:project).tap do |p|+ FactoryBot.create(:member,+ roles: [FactoryBot.create(:role, permissions: [:view_time_entries])],+ project: p,+ user: current_user)+ end+ end+ let(:project_without_permission) do+ FactoryBot.create(:project).tap do |p|+ FactoryBot.create(:member,+ roles: [FactoryBot.create(:role, permissions: [])],+ project: p,+ user: current_user)+ end+ end+ let(:project_without_membership) do+ FactoryBot.create(:project)+ end++ subject(:response) { last_response }++ describe 'GET api/v3/memberships/available_projects' do+ let(:projects) do+ [project_with_log_permission,+ project_with_edit_permission,+ project_with_edit_own_permission,+ project_with_view_permission,+ project_without_permission,+ project_without_membership]+ end++ before do+ projects+ login_as(current_user)++ get path+ end++ let(:path) { api_v3_paths.path_for(:time_entries_available_projects, sort_by: [%w(id asc)]) }++ it 'responds 200 OK' do+ expect(subject.status).to eq(200)+ end++ it 'returns a collection of projects containing only the ones for which the user has the necessary permissions' do+ expect(subject.body)+ .to be_json_eql('Collection'.to_json)+ .at_path('_type')++ expect(subject.body)+ .to be_json_eql('3')+ .at_path('total')++ expect(subject.body)+ .to be_json_eql(project_with_log_permission.id.to_json)+ .at_path('_embedded/elements/0/id')++ expect(subject.body)+ .to be_json_eql(project_with_edit_permission.id.to_json)+ .at_path('_embedded/elements/1/id')++ expect(subject.body)+ .to be_json_eql(project_with_edit_own_permission.id.to_json)+ .at_path('_embedded/elements/2/id')+ end++ context 'without permissions' do+ let(:projects) do+ [project_with_view_permission,+ project_without_permission,+ project_without_membership]+ end++ it 'returns a 403' do+ expect(subject.status)+ .to eq(403)+ end+ end+ end+enddiff --git a/spec/requests/api/v3/time_entries/create_form_resource_spec.rb b/spec/requests/api/v3/time_entries/create_form_resource_spec.rbnew file mode 100644index 00000000000..7aedf98fbe8--- /dev/null+++ b/spec/requests/api/v3/time_entries/create_form_resource_spec.rb@@ -0,0 +1,205 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2015 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See doc/COPYRIGHT.rdoc for more details.++require 'spec_helper'+require 'rack/test'++describe ::API::V3::TimeEntries::CreateFormAPI, content_type: :json do+ include Rack::Test::Methods+ include API::V3::Utilities::PathHelper++ let(:project) { FactoryBot.create(:project) }+ let(:active_activity) { FactoryBot.create(:time_entry_activity) }+ let(:in_project_inactive_activity) do+ FactoryBot.create(:time_entry_activity).tap do |tea|+ TimeEntryActivitiesProject.insert(project_id: project.id, activity_id: tea.id, active: false)+ end+ end+ let(:custom_field) { FactoryBot.create(:time_entry_custom_field) }+ let(:user) do+ FactoryBot.create(:user,+ member_in_project: project,+ member_with_permissions: permissions)+ end+ let(:work_package) do+ FactoryBot.create(:work_package, project: project)+ end+ let(:other_user) { FactoryBot.create(:user) }+ let(:permissions) { %i[log_time view_work_packages] }++ let(:path) { api_v3_paths.create_time_entry_form }+ let(:parameters) { {} }++ before do+ login_as(user)++ post path, parameters.to_json+ end++ subject(:response) { last_response }++ describe '#POST /api/v3/time_entries/form' do+ it 'returns 200 OK' do+ expect(response.status).to eq(200)+ end++ it 'returns a form' do+ expect(response.body)+ .to be_json_eql('Form'.to_json)+ .at_path('_type')+ end++ it 'does not create a time_entry' do+ expect(TimeEntry.count)+ .to eql 0+ end++ context 'with empty parameters' do+ it 'has 4 validation errors' do+ # There are 4 validation errors instead of 3 with two duplicating each other+ expect(subject.body).to have_json_size(4).at_path('_embedded/validationErrors')+ end++ it 'has a validation error on activity' do+ expect(subject.body).to have_json_path('_embedded/validationErrors/activity')+ end++ it 'has a validation error on project' do+ expect(subject.body).to have_json_path('_embedded/validationErrors/project')+ end++ it 'has a validation error on spentOn' do+ expect(subject.body).to have_json_path('_embedded/validationErrors/spentOn')+ end++ it 'has a validation error on hours' do+ expect(subject.body).to have_json_path('_embedded/validationErrors/hours')+ end++ it 'has no commit link' do+ expect(subject.body)+ .not_to have_json_path('_links/commit')+ end+ end++ context 'with all parameters' do+ let(:parameters) do+ {+ _links: {+ workPackage: {+ href: api_v3_paths.work_package(work_package.id)+ },+ project: {+ href: api_v3_paths.project(project.id)+ },+ activity: {+ href: api_v3_paths.time_entries_activity(active_activity.id)+ }+ },+ spentOn: Date.today.to_s,+ hours: 'PT5H',+ "comment": {+ raw: "some comment"+ },+ "customField#{custom_field.id}": {+ raw: 'some cf text'+ }+ }+ end++ it 'has 0 validation errors' do+ expect(subject.body).to have_json_size(0).at_path('_embedded/validationErrors')+ end++ it 'has the values prefilled in the payload' do+ body = subject.body++ expect(body)+ .to be_json_eql(api_v3_paths.project(project.id).to_json)+ .at_path('_embedded/payload/_links/project/href')++ expect(body)+ .to be_json_eql(api_v3_paths.work_package(work_package.id).to_json)+ .at_path('_embedded/payload/_links/workPackage/href')++ expect(body)+ .to be_json_eql(api_v3_paths.time_entries_activity(active_activity.id).to_json)+ .at_path('_embedded/payload/_links/activity/href')++ expect(body)+ .to be_json_eql("some comment".to_json)+ .at_path("_embedded/payload/comment/raw")++ expect(body)+ .to be_json_eql(Date.today.to_s.to_json)+ .at_path('_embedded/payload/spentOn')++ expect(body)+ .to be_json_eql("PT5H".to_json)+ .at_path('_embedded/payload/hours')++ expect(body)+ .to be_json_eql("some cf text".to_json)+ .at_path("_embedded/payload/customField#{custom_field.id}/raw")++ # As the user is always the current user, it is not part of the payload+ expect(body)+ .not_to have_json_path('_embedded/payload/_links/user')+ end++ it 'has the available values listed in the schema' do+ body = subject.body++ wp_path = api_v3_paths.path_for(:work_packages, filters: [{ project: { operator: '=', values: [project.id.to_s] } }])++ expect(body)+ .to be_json_eql(wp_path.to_json)+ .at_path('_embedded/schema/workPackage/_links/allowedValues/href')++ expect(body)+ .to be_json_eql(api_v3_paths.time_entries_available_projects.to_json)+ .at_path('_embedded/schema/project/_links/allowedValues/href')+ end++ it 'has a commit link' do+ expect(subject.body)+ .to be_json_eql(api_v3_paths.time_entries.to_json)+ .at_path('_links/commit/href')+ end+ end++ context 'without the necessary permission' do+ let(:permissions) { [] }++ it 'returns 403 Not Authorized' do+ expect(response.status).to eq(403)+ end+ end+ end+enddiff --git a/spec/requests/api/v3/time_entries/schemas/time_entry_schema_resource_spec.rb b/spec/requests/api/v3/time_entries/schemas/time_entry_schema_resource_spec.rbnew file mode 100644index 00000000000..700d0b8db14--- /dev/null+++ b/spec/requests/api/v3/time_entries/schemas/time_entry_schema_resource_spec.rb@@ -0,0 +1,81 @@+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++require 'spec_helper'+require 'rack/test'++describe 'API v3 Time entry schema resource', type: :request, content_type: :json do+ include Rack::Test::Methods+ include API::V3::Utilities::PathHelper++ let(:project) { FactoryBot.create(:project) }+ let(:current_user) do+ FactoryBot.create(:user,+ member_in_project: project,+ member_with_permissions: permissions)+ end++ let(:permissions) { [:view_time_entries] }++ let(:path) { api_v3_paths.time_entry_schema }++ before do+ login_as(current_user)+ end++ subject(:response) { last_response }++ describe '#GET /time_entries/schema' do+ before do+ get path+ end++ it 'responds with 200 OK' do+ expect(subject.status).to eq(200)+ end++ it 'returns a schema' do+ expect(subject.body)+ .to be_json_eql('Schema'.to_json)+ .at_path '_type'+ end++ it 'does not embed' do+ expect(subject.body)+ .not_to have_json_path('project/_links/allowedValues')+ end++ context 'if lacking permissions' do+ let(:permissions) { [] }++ it 'responds with 403' do+ expect(subject.status).to eq(403)+ end+ end+ end+enddiff --git a/spec/services/time_entries/set_attributes_service_spec.rb b/spec/services/time_entries/set_attributes_service_spec.rbindex 05890c69ccd..28f6ee5ae5d 100644--- a/spec/services/time_entries/set_attributes_service_spec.rb+++ b/spec/services/time_entries/set_attributes_service_spec.rb@@ -61,7 +61,7 @@ describe TimeEntries::SetAttributesService, type: :model dolet(:contract_class) doallow(TimeEntries::CreateContract).to receive(:new)- .with(anything, user, options: { changed_by_system: [] })+ .with(anything, user, options: { changed_by_system: ["user_id"] }).and_return(contract_instance)TimeEntries::CreateContract@@ -94,6 +94,13 @@ describe TimeEntries::SetAttributesService, type: :model do.to eql userend+ it 'notes the user to be system changed' do+ subject++ expect(instance.changed_by_system)+ .to include('user_id')+ end+it 'assigns the default TimeEntryActivity' doallow(TimeEntryActivity).to receive(:default)@@ -106,10 +113,8 @@ describe TimeEntries::SetAttributesService, type: :model doendcontext 'with params' do- let(:user2) { FactoryBot.build_stubbed(:user) }let(:params) do{- user: user2,work_package: work_package,project: project,activity: activity,@@ -121,7 +126,7 @@ describe TimeEntries::SetAttributesService, type: :model dolet(:expected) do{- user_id: user2.id,+ user_id: user.id,work_package_id: work_package.id,project_id: project.id,activity_id: activity.id,