diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index fedc8eb..83a0302 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' @@ -40,8 +41,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/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..5b64023 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,72 @@ 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) + + expect(json_response["organization"]['id']).to eq(organization_2.id) + expect(json_response["organization"]['name']).to eq(organization_2.name) + expect(json_response["organization"]['description']).to eq(organization_2.description) + expect(json_response["organization"]['parent']['id']).to eq(parent_id) + expect(json_response["organization"]['parent']['name']).to eq(Organization.find(parent_id).fullname) + expect(json_response["organization"]['mail']).to eq(organization_2.mail) + expect(json_response["organization"]['direction']).to eq(organization_2.direction) + expect(json_response["organization"]['name_with_parents']).to eq(organization_2.name_with_parents) + expect(json_response["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 "memberships methods" do before do @request.session[:user_id] = 1 members = Member.where("project_id = ?", 2)