diff --git a/app/controllers/client_api/users/user_teams_controller.rb b/app/controllers/client_api/users/user_teams_controller.rb index 419cb8f2d..6fe8f56c5 100644 --- a/app/controllers/client_api/users/user_teams_controller.rb +++ b/app/controllers/client_api/users/user_teams_controller.rb @@ -29,6 +29,7 @@ module ClientApi end def user_cant_leave + return unless @user_team && @team @user_team.admin? && @team.user_teams.where(role: 2).count <= 1 end diff --git a/app/javascript/packs/src/settings/components/team/components/TeamsDataTable.jsx b/app/javascript/packs/src/settings/components/team/components/TeamsDataTable.jsx index d2e4586ca..e638f9516 100644 --- a/app/javascript/packs/src/settings/components/team/components/TeamsDataTable.jsx +++ b/app/javascript/packs/src/settings/components/team/components/TeamsDataTable.jsx @@ -38,12 +38,11 @@ class TeamsDataTable extends Component { render() { const options = { - defaultSortName: "name", // default sort column name - defaultSortOrder: "desc", // default sort order + defaultSortName: "name", + defaultSortOrder: "desc", sizePerPageList: [10, 25, 50, 100], paginationPosition: "top", - alwaysShowAllBtns: false, - ignoreSinglePage: true + alwaysShowAllBtns: false }; const columns = [ { diff --git a/spec/controllers/client_api/users/user_teams_controller.rb b/spec/controllers/client_api/users/user_teams_controller.rb new file mode 100644 index 000000000..d1e0604bd --- /dev/null +++ b/spec/controllers/client_api/users/user_teams_controller.rb @@ -0,0 +1,32 @@ +require 'rails_helper' + +describe ClientApi::Users::UserTeamsController, type: :controller do + describe 'DELETE #leave_team' do + login_user + before do + @user_one = User.first + @user_two = FactoryGirl.create(:user, email: 'sec_user@asdf.com') + @team = FactoryGirl.create :team + FactoryGirl.create :user_team, team: @team, user: @user_one, role: 2 + end + + it 'Returns HTTP success if user can leave the team' do + FactoryGirl.create :user_team, team: @team, user: @user_two, role: 2 + delete :leave_team, params: { team: @team.id }, format: :json + expect(response).to be_success + expect(response).to have_http_status(200) + end + + it 'Returns HTTP unprocessable_entity if user can\'t leave the team' do + delete :leave_team, params: { team: @team.id }, format: :json + expect(response).to_not be_success + expect(response).to have_http_status(:unprocessable_entity) + end + + it 'Returns HTTP unprocessable_entity if no params given' do + delete :leave_team, format: :json + expect(response).to_not be_success + expect(response).to have_http_status(:unprocessable_entity) + end + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 516a6be2b..42ede5b3e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -165,7 +165,8 @@ describe User, type: :model do name: team.name, members: 1, role: 2, - current_team: true + current_team: true, + can_be_leaved: false } user_one.teams_data.first.each do |k, v| @@ -181,7 +182,8 @@ describe User, type: :model do name: team.name, members: 2, role: 2, - current_team: true + current_team: true, + can_be_leaved: true } user_one.teams_data.first.each do |k, v| diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 32c2525a0..3d521b498 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -2,6 +2,8 @@ require 'spec_helper' require 'shoulda-matchers' require 'database_cleaner' +require 'devise' +require_relative 'support/controller_macros' ENV['RAILS_ENV'] = 'test' require File.expand_path('../../config/environment', __FILE__) # Prevent database truncation if the environment is production @@ -78,6 +80,9 @@ RSpec.configure do |config| # includes FactoryGirl in rspec config.include FactoryGirl::Syntax::Methods + # Devise + config.include Devise::Test::ControllerHelpers, type: :controller + config.extend ControllerMacros, :type => :controller end # config shoulda matchers to work with rspec diff --git a/spec/support/controller_macros.rb b/spec/support/controller_macros.rb new file mode 100644 index 000000000..5900d6ecd --- /dev/null +++ b/spec/support/controller_macros.rb @@ -0,0 +1,10 @@ +module ControllerMacros + def login_user + before(:each) do + @request.env["devise.mapping"] = Devise.mappings[:user] + user = FactoryGirl.create(:user) + user.confirm + sign_in user + end + end +end