Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/4_2_11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 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 All @@ -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
Expand All @@ -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
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
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
139 changes: 134 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,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)
Expand Down