From 1325732b0c791320be442632ef6d96a83197941b Mon Sep 17 00:00:00 2001 From: zmagod Date: Wed, 30 Aug 2017 16:18:21 +0200 Subject: [PATCH] adds specs for team controller actions --- .../client_api/teams/teams_controller.rb | 10 +-- .../packs/shared/actions/TeamsActions.js | 2 +- app/services/client_api/teams_service.rb | 18 ++++-- .../client_api/teams/details.json.jbuilder | 19 +++--- config/environments/test.rb | 6 ++ .../client_api/teams/teams_controller_spec.rb | 62 ++++++++++++++++++ .../client_api/users/user_teams_controller.rb | 8 +-- .../services/client_api/teams_service_spec.rb | 64 +++++++++++++++++++ spec/spec_helper.rb | 13 ++++ 9 files changed, 177 insertions(+), 25 deletions(-) create mode 100644 spec/controllers/client_api/teams/teams_controller_spec.rb create mode 100644 spec/services/client_api/teams_service_spec.rb diff --git a/app/controllers/client_api/teams/teams_controller.rb b/app/controllers/client_api/teams/teams_controller.rb index 4e2999d1c..6d3aca54e 100644 --- a/app/controllers/client_api/teams/teams_controller.rb +++ b/app/controllers/client_api/teams/teams_controller.rb @@ -9,20 +9,20 @@ module ClientApi end def details - team_service = ClientApi::TeamsService.new(id: params[:team_id], + team_service = ClientApi::TeamsService.new(team_id: params[:team_id], current_user: current_user) success_response('/client_api/teams/details', team_service.team_page_details_data) - rescue MissingTeamError + rescue ClientApi::CustomTeamError error_response end def change_team - team_service = ClientApi::TeamsService.new(id: params[:team_id], + team_service = ClientApi::TeamsService.new(team_id: params[:team_id], current_user: current_user) team_service.change_current_team! success_response('/client_api/teams/index', team_service.teams_data) - rescue MissingTeamError + rescue ClientApi::CustomTeamError error_response end @@ -41,7 +41,7 @@ module ClientApi def error_response respond_to do |format| format.json do - render json: { message: 'Bad boy!' }, status: :bad_request + render json: { message: 'Bad boy!' }, status: :unprocessable_entity end end end diff --git a/app/javascript/packs/shared/actions/TeamsActions.js b/app/javascript/packs/shared/actions/TeamsActions.js index 40f4b7295..a2fad8da1 100644 --- a/app/javascript/packs/shared/actions/TeamsActions.js +++ b/app/javascript/packs/shared/actions/TeamsActions.js @@ -36,7 +36,7 @@ export function getTeamsList() { export function changeTeam(teamId) { return dispatch => { axios - .post(CHANGE_TEAM_PATH, { teamId }, { withCredentials: true }) + .post(CHANGE_TEAM_PATH, { team_id: 44 }, { withCredentials: true }) .then(response => { const teams = response.data.teams.collection; dispatch(addTeamsData(teams)); diff --git a/app/services/client_api/teams_service.rb b/app/services/client_api/teams_service.rb index 35da8baa9..484dd02d7 100644 --- a/app/services/client_api/teams_service.rb +++ b/app/services/client_api/teams_service.rb @@ -1,23 +1,27 @@ module ClientApi class TeamsService - MissingTeamError = Class.new(StandardError) def initialize(arg) - team_id = arg.fetch(:team_id) { raise MissingTeamError } + team_id = arg.fetch(:team_id) { raise ClientApi::CustomTeamError } @team = Team.find_by_id(team_id) - @current_user = arg.fetch(:current_user) { raise MissingTeamError } - raise MissingTeamError unless @current_user.teams.include? @team + @user = arg.fetch(:current_user) { raise ClientApi::CustomTeamError } + raise ClientApi::CustomTeamError unless @user.teams.include? @team end def change_current_team! - @current_user.update_attribute(:current_team_id, @team.id) + @user.update_attribute(:current_team_id, @team.id) end def team_page_details_data - { team: @team, users: @team.users.eager_load(:user_teams) } + team_users = UserTeam.includes(:user) + .references(:user) + .where(team: @team) + .distinct + { team: @team, team_users: team_users } end def teams_data - { teams: @current_user.teams_data } + { teams: @user.teams_data } end end + CustomTeamError = Class.new(StandardError) end diff --git a/app/views/client_api/teams/details.json.jbuilder b/app/views/client_api/teams/details.json.jbuilder index 2a495034f..967c9cbe1 100644 --- a/app/views/client_api/teams/details.json.jbuilder +++ b/app/views/client_api/teams/details.json.jbuilder @@ -1,15 +1,18 @@ json.team_details do json.id team.id json.created_at team.created_at - json.created_by team.created_by + json.created_by "#{team.created_by.full_name} (#{team.created_by.email})" json.space_taken team.space_taken json.description team.description - json.users users do |user| - # json.id team.fetch('id') - # json.name team.fetch('name') - # json.members team.fetch('members') - # json.role retrive_role_name(team.fetch('role') { nil }) - # json.current_team team.fetch('current_team') - # json.can_be_leaved team.fetch('can_be_leaved') + json.users team_users do |team_user| + json.id team_user.user.id + json.email team_user.user.full_name + json.role team_user.role_str + json.created_at I18n.l(team_user.created_at, format: :full_date) + json.status team_user.user.active_status_str + json.actions do + json.current_role team_user.role_str + json.team_user_id team_user.id + end end end diff --git a/config/environments/test.rb b/config/environments/test.rb index 8562e03ce..c35f9ba75 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -1,5 +1,11 @@ Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + config.after_initialize do + Bullet.enable = true + Bullet.bullet_logger = true + Bullet.raise = true # raise an error if n+1 query occurs + end # Configure public file server for tests with Cache-Control for performance. config.public_file_server.enabled = true diff --git a/spec/controllers/client_api/teams/teams_controller_spec.rb b/spec/controllers/client_api/teams/teams_controller_spec.rb new file mode 100644 index 000000000..a086e5413 --- /dev/null +++ b/spec/controllers/client_api/teams/teams_controller_spec.rb @@ -0,0 +1,62 @@ +require 'rails_helper' + +describe ClientApi::Teams::TeamsController, type: :controller do + login_user + + before do + @user_one = User.first + @user_two = FactoryGirl.create(:user, email: 'sec_user@asdf.com') + @team_one = FactoryGirl.create :team + @team_two = FactoryGirl.create :team, name: 'Team two' + FactoryGirl.create :user_team, team: @team_one, user: @user_one, role: 2 + end + + describe 'GET #index' do + it 'should return HTTP success response' do + get :index, format: :json + expect(response).to be_success + expect(response).to have_http_status(:ok) + end + end + + describe 'POST #change_team' do + it 'should return HTTP success response' do + FactoryGirl.create :user_team, team: @team_two, user: @user_one, role: 2 + @user_one.update_attribute(:current_team_id, @team_one.id) + post :change_team, params: { team_id: @team_two.id }, as: :json + expect(response).to have_http_status(:ok) + end + + it 'should return HTTP unprocessable_entity response if user not in team' do + @user_one.update_attribute(:current_team_id, @team_one.id) + post :change_team, params: { team_id: @team_two.id }, as: :json + expect(response).to have_http_status(:unprocessable_entity) + end + + it 'should return HTTP success response if same team as current' do + @user_one.update_attribute(:current_team_id, @team_one.id) + post :change_team, params: { team_id: @team_one.id }, as: :json + expect(response).to have_http_status(:ok) + end + end + + describe 'GET #details' do + it 'should return HTTP success response' do + FactoryGirl.create :user_team, team: @team_two, user: @user_one, role: 2 + @user_one.update_attribute(:current_team_id, @team_one.id) + get :details, params: { team_id: @team_two.id }, as: :json + expect(response).to have_http_status(:ok) + end + + it 'should return HTTP unprocessable_entity response if user not in team' do + @user_one.update_attribute(:current_team_id, @team_one.id) + get :details, params: { team_id: @team_two.id }, as: :json + expect(response).to have_http_status(:unprocessable_entity) + end + + it 'should return HTTP unprocessable_entity response if team_id not valid' do + get :details, params: { team_id: 'banana' }, as: :json + expect(response).to have_http_status(:unprocessable_entity) + end + end +end diff --git a/spec/controllers/client_api/users/user_teams_controller.rb b/spec/controllers/client_api/users/user_teams_controller.rb index d1e0604bd..10b683b2d 100644 --- a/spec/controllers/client_api/users/user_teams_controller.rb +++ b/spec/controllers/client_api/users/user_teams_controller.rb @@ -10,20 +10,20 @@ describe ClientApi::Users::UserTeamsController, type: :controller do FactoryGirl.create :user_team, team: @team, user: @user_one, role: 2 end - it 'Returns HTTP success if user can leave the team' do + it 'should return 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) + expect(response).to have_http_status(:ok) end - it 'Returns HTTP unprocessable_entity if user can\'t leave the team' do + it 'should return 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 + it 'should return 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) diff --git a/spec/services/client_api/teams_service_spec.rb b/spec/services/client_api/teams_service_spec.rb new file mode 100644 index 000000000..e0481ce76 --- /dev/null +++ b/spec/services/client_api/teams_service_spec.rb @@ -0,0 +1,64 @@ +require 'rails_helper' + +describe ClientApi::TeamsService do + let(:team_one) { create :team } + let(:user_one) { create :user } + + it 'should raise an ClientApi::CustomTeamError if user is not assigned' do + expect { + ClientApi::TeamsService.new(team_id: team_one.id) + }.to raise_error(ClientApi::CustomTeamError) + end + + it 'should raise an ClientApi::CustomTeamError if team is not assigned' do + expect { + ClientApi::TeamsService.new(current_user: user_one) + }.to raise_error(ClientApi::CustomTeamError) + end + + it 'should raise an ClientApi::CustomTeamError if team is not user team' do + expect { + ClientApi::TeamsService.new(current_user: user_one, team_id: team_one.id) + }.to raise_error(ClientApi::CustomTeamError) + end + + describe '#change_current_team!' do + let(:team_two) { create :team, name: 'team two' } + let(:user_two) do + create :user, current_team_id: team_one.id, email: 'user_two@test.com' + end + + it 'should change user current team' do + create :user_team, user: user_two, team: team_two + teams_service = ClientApi::TeamsService.new(current_user: user_two, + team_id: team_two.id) + teams_service.change_current_team! + expect(user_two.current_team_id).to eq team_two.id + end + end + + describe '#team_page_details_data' do + let(:team_service) do + ClientApi::TeamsService.new(current_user: user_one, team_id: team_one.id) + end + + it 'should return team page data' do + user_team = create :user_team, user: user_one, team: team_one + data = team_service.team_page_details_data + expect(data.fetch(:team).name).to eq team_one.name + expect(data.fetch(:team_users).first).to eq user_team + end + end + + describe '#teams_data' do + let(:team_service) do + ClientApi::TeamsService.new(current_user: user_one, team_id: team_one.id) + end + + it 'should return user teams' do + create :user_team, user: user_one, team: team_one + data = team_service.teams_data.fetch(:teams) + expect(data.first.fetch('name')).to eq team_one.name + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2b2309f8c..b24b6926a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,6 +16,8 @@ require 'capybara/rspec' require 'simplecov' require 'faker' +require 'active_record' +require 'bullet' RSpec.configure do |config| # rspec-expectations config goes here. You can use an alternate @@ -97,4 +99,15 @@ RSpec.configure do |config| # as the one that triggered the failure. Kernel.srand config.seed =end + # Enable bullet gem in tests + if Bullet.enable? + config.before(:each) do + Bullet.start_request + end + + config.after(:each) do + Bullet.perform_out_of_channel_notifications if Bullet.notification? + Bullet.end_request + end + end end