Skip to content

Commit

Permalink
Ajouter un point d'accès correspondant à l'action "show"
Browse files Browse the repository at this point in the history
  • Loading branch information
Yalaeddin committed May 13, 2024
1 parent 3cb2d83 commit aca5e8d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 8 deletions.
9 changes: 6 additions & 3 deletions app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions app/views/organizations/show.api.rsb
Original file line number Diff line number Diff line change
@@ -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
73 changes: 68 additions & 5 deletions spec/controllers/organizations_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -105,7 +106,6 @@
end

describe "Manager actions" do

before do
@request.session[:user_id] = 2
end
Expand Down Expand Up @@ -133,7 +133,6 @@
end

describe "add_users method" do

before do
@request.session[:user_id] = 1
end
Expand All @@ -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)
Expand Down

0 comments on commit aca5e8d

Please sign in to comment.