Skip to content

Commit

Permalink
Ajouter un point d'accès correspondant à l'action "index"
Browse files Browse the repository at this point in the history
  • Loading branch information
Yalaeddin committed May 13, 2024
1 parent 1e9fdd9 commit ea54231
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 10 deletions.
9 changes: 8 additions & 1 deletion app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,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
Expand Down
28 changes: 28 additions & 0 deletions app/views/organizations/index.api.rsb
Original file line number Diff line number Diff line change
@@ -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
84 changes: 75 additions & 9 deletions spec/controllers/organizations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,16 @@
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)
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
Expand All @@ -214,6 +215,71 @@
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
Expand Down

0 comments on commit ea54231

Please sign in to comment.