diff --git a/.github/workflows/4_2_11.yml b/.github/workflows/4_2_11.yml index 96977e3..4db9ff8 100644 --- a/.github/workflows/4_2_11.yml +++ b/.github/workflows/4_2_11.yml @@ -88,6 +88,8 @@ jobs: - name: Prepare Redmine source working-directory: redmine run: | + # TODO Remove the following line when https://www.redmine.org/issues/40551 is fixed + sed -i -e 's/.*mocha.*/ gem "mocha", "2.1.0"/' Gemfile # Fix core tests not compatible with Mocha 2.2.0 sed -i '/rubocop/d' Gemfile rm -f .rubocop* cp plugins/redmine_base_rspec/spec/support/database-${{ matrix.db }}.yml config/database.yml diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index fedc8eb..309e03b 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -4,7 +4,8 @@ class OrganizationsController < ApplicationController before_action :require_admin_or_manager, :except => [:index, :show, :autocomplete_users, :fetch_users_by_orga] before_action :require_login, :only => [:index, :show, :autocomplete_users] before_action :find_project_by_project_id, :only => [:autocomplete_users] - after_action :update_fullname_and_identifier_of_children, only: [:update] + after_action :update_fullname_and_identifier_of_children, only: [:update] + accept_api_auth :index, :show layout 'admin' @@ -13,7 +14,14 @@ def index @managers_by_organization = @organizations.map { |o| [o.id, o.managers.map(&:name)] }.to_h @team_leaders_by_organization = @organizations.map { |o| [o.id, o.team_leaders.map(&:name)] }.to_h @managed_organizations = Organization.managed_by(user: User.current) - render :layout => (User.current.admin? ? 'admin' : 'base') + + respond_to do |format| + format.html do + render :layout => (User.current.admin? ? 'admin' : 'base') + end + format.api + end + end def show @@ -40,8 +48,10 @@ def show project_ids = Member.joins(:user).where('users.status = ? AND users.organization_id IN (?)', User::STATUS_ACTIVE, organization_ids).map(&:project_id).uniq @issues = Issue.open.visible.on_active_project.where(project_id: project_ids).joins(:priority).order("enumerations.position desc").limit(50) - render :layout => 'base' - + respond_to do |format| + format.html + format.api + end rescue ActiveRecord::RecordNotFound render_404 end diff --git a/app/views/organizations/index.api.rsb b/app/views/organizations/index.api.rsb new file mode 100644 index 0000000..7ccf2a8 --- /dev/null +++ b/app/views/organizations/index.api.rsb @@ -0,0 +1,28 @@ +api.array :organizations do + @organizations.each do |organization| + api.organization do + api.id organization.id + api.name organization.name + api.description organization.description + api.parent(:id => organization.parent.id, :name => organization.parent.name) if organization.parent + api.mail organization.mail + api.direction organization.direction + api.name_with_parents organization.name_with_parents + + api.top_department_in_ldap organization.top_department_in_ldap + api.created_at organization.created_at + api.updated_at organization.updated_at + + api.array :users do + organization.users.each do |user| + api.users do + api.id user.id + api.name user.name + api.manager organization.managers.include?(user) + api.team_leader organization.team_leaders.include?(user) + end + end + end if include_in_api_response?('users') + end + end +end diff --git a/app/views/organizations/show.api.rsb b/app/views/organizations/show.api.rsb new file mode 100644 index 0000000..b913cf3 --- /dev/null +++ b/app/views/organizations/show.api.rsb @@ -0,0 +1,24 @@ +api.organization do + api.id @organization.id + api.name @organization.name + api.description @organization.description + api.parent(:id => @organization.parent.id, :name => @organization.parent.name) if @organization.parent + api.mail @organization.mail + api.direction @organization.direction + api.name_with_parents @organization.name_with_parents + + api.top_department_in_ldap @organization.top_department_in_ldap + api.created_at @organization.created_at + api.updated_at @organization.updated_at + + api.array :users do + @organization.users.each do |user| + api.users do + api.id user.id + api.name user.name + api.manager @organization.managers.include?(user) + api.team_leader @organization.team_leaders.include?(user) + end + end + end if include_in_api_response?('users') +end diff --git a/spec/controllers/organizations_controller_spec.rb b/spec/controllers/organizations_controller_spec.rb index 9f15f23..ce43765 100644 --- a/spec/controllers/organizations_controller_spec.rb +++ b/spec/controllers/organizations_controller_spec.rb @@ -1,8 +1,9 @@ +# frozen_string_literal: true + require "spec_helper" require "active_support/testing/assertions" describe OrganizationsController, :type => :controller do - fixtures :organizations, :organization_managers, :users, :organization_team_leaders, :members, :member_roles, :roles @@ -88,7 +89,7 @@ it "Changing name of parent organization should update full_name and identifier of its children" do org = Organization.find(1) new_name = "name_test" - #Fill in the name_with_parents of the children of organization, because they are not filled in by the fixture + # Fill in the name_with_parents of the children of organization, because they are not filled in by the fixture org.children.each do |child| child.name_with_parents = org.name + Organization::SEPARATOR + child.name child.save @@ -105,7 +106,6 @@ end describe "Manager actions" do - before do @request.session[:user_id] = 2 end @@ -133,7 +133,6 @@ end describe "add_users method" do - before do @request.session[:user_id] = 1 end @@ -150,8 +149,138 @@ end end - describe "memberships methods" do + describe "GET #show/api" do + let(:organization_1) { Organization.find(1) } + let(:organization_2) { Organization.find(2) } + + before do + Setting.rest_api_enabled = '1' + request.headers['Authorization'] = ActionController::HttpAuthentication::Basic.encode_credentials("admin", "admin") + User.find(1).update_attribute('organization_id', 1) + User.find(4).update_attribute('organization_id', 1) + User.find(2).update_attribute('organization_id', 2) + User.find(7).update_attribute('organization_id', 2) + end + + it "returns a success response" do + get :show, params: {:id => organization_1.to_param, :format => :json } + expect(response).to be_successful + expect(response).to have_http_status(200) + end + + it "renders the show view" do + get :show, params: { id: organization_1.to_param, format: :json } + expect(response).to render_template(:show) + end + + it "returns organization details in JSON format" do + get :show, params: { id: organization_2.to_param, format: :json } + expect(response).to have_http_status(:success) + + parent_id = organization_2.parent_id + json_response = JSON.parse(response.body) + + json_organization = json_response["organization"] + expect(json_organization['id']).to eq(organization_2.id) + expect(json_organization['name']).to eq(organization_2.name) + expect(json_organization['description']).to eq(organization_2.description) + expect(json_organization['parent']['id']).to eq(parent_id) + expect(json_organization['parent']['name']).to eq(Organization.find(parent_id).fullname) + expect(json_organization['mail']).to eq(organization_2.mail) + expect(json_organization['direction']).to eq(organization_2.direction) + expect(json_organization['name_with_parents']).to eq(organization_2.name_with_parents) + expect(json_organization['top_department_in_ldap']).to eq(organization_2.top_department_in_ldap) + end + + it "returns organization users in JSON format" do + get :show, params: {:id => organization_1.to_param, :include => ["users"], :format => 'json' } + expect(response).to have_http_status(:success) + json_response = JSON.parse(response.body) + + expect(json_response["organization"]['users'].count).to eq(2) + + users_in_response = json_response["organization"]['users'] + user_1 = users_in_response.find { |user| user['id'] == 1 } + expect(user_1["manager"]).to eq(true) + expect(user_1["team_leader"]).to eq(true) + + user_2 = users_in_response.find { |user| user['id'] == 4 } + expect(user_2["manager"]).to eq(false) + expect(user_2["team_leader"]).to eq(false) + end + + it "returns a 404 error when the organization does not exist" do + get :show, params: { id: 80 } + expect(response).to have_http_status(:not_found) + end + end + + describe "GET #index/api" do + let(:organizations) { Organization.all } + before do + Setting.rest_api_enabled = '1' + request.headers['Authorization'] = ActionController::HttpAuthentication::Basic.encode_credentials("admin", "admin") + User.find(1).update_attribute('organization_id', 1) + User.find(4).update_attribute('organization_id', 1) + User.find(2).update_attribute('organization_id', 2) + User.find(7).update_attribute('organization_id', 2) + end + + it "returns a success response" do + get :index, params: { :format => :json } + expect(response).to be_successful + expect(response).to have_http_status(200) + end + + it "renders the index view" do + get :index, params: { format: :json } + expect(response).to render_template(:index) + end + + it "returns organizations details in JSON format" do + get :index, params: { format: :json } + expect(response).to have_http_status(:success) + + json_response = JSON.parse(response.body) + expect(json_response["organizations"].count).to eq(Organization.count) + + organizations.each do |organization| + json_organization = json_response["organizations"].find { |org| org["id"] == organization.id } + + expect(json_organization["name"]).to eq(organization.name) + expect(json_organization["description"]).to eq(organization.description) + expect(json_organization['parent']['id']).to eq(organization.parent_id) if json_organization['parent'].present? + expect(json_organization["mail"]).to eq(organization.mail) + expect(json_organization["direction"]).to eq(organization.direction) + expect(json_organization["name_with_parents"]).to eq(organization.name_with_parents) + expect(json_organization["top_department_in_ldap"]).to eq(organization.top_department_in_ldap) + + end + end + + it "returns organizations users in JSON format" do + get :index, params: { include: ["users"], format: 'json' } + + expect(response).to have_http_status(:success) + json_response = JSON.parse(response.body) + + json_response["organizations"].each_with_index do |json_organization, index| + organization = organizations[index] + + json_users = json_organization["users"] + expect(json_users.count).to eq(organization.users.count) + + organization.users.each do |user| + json_user = json_users.find { |u| u["id"] == user.id } + expect(json_user).to_not be_nil + end + end + + end + end + + describe "memberships methods" do before do @request.session[:user_id] = 1 members = Member.where("project_id = ?", 2)