diff --git a/spec/controllers/client_api/teams/teams_controller_spec.rb b/spec/controllers/client_api/teams/teams_controller_spec.rb index 609d01e11..2ffd1484b 100644 --- a/spec/controllers/client_api/teams/teams_controller_spec.rb +++ b/spec/controllers/client_api/teams/teams_controller_spec.rb @@ -7,7 +7,9 @@ describe ClientApi::Teams::TeamsController, type: :controller do @user_one = User.first @user_two = FactoryGirl.create :user, email: 'sec_user@asdf.com' @team_one = FactoryGirl.create :team, created_by: @user_one - @team_two = FactoryGirl.create :team, name: 'Team two', created_by: @user_two + @team_two = FactoryGirl.create :team, + name: 'Team two', + created_by: @user_two FactoryGirl.create :user_team, team: @team_one, user: @user_one, role: 2 end @@ -33,7 +35,7 @@ describe ClientApi::Teams::TeamsController, type: :controller do it 'should return HTTP unprocessable_entity response if name too short' do @team_one.update_attribute( :name, - "#{'a' * (Constants::NAME_MIN_LENGTH - 1)}" + ('a' * (Constants::NAME_MIN_LENGTH - 1)).to_s ) post :create, params: { team: @team_one }, as: :json expect(response).to have_http_status(:unprocessable_entity) @@ -42,7 +44,7 @@ describe ClientApi::Teams::TeamsController, type: :controller do it 'should return HTTP unprocessable_entity response if name too long' do @team_one.update_attribute( :name, - "#{'a' * (Constants::NAME_MAX_LENGTH + 1)}" + ('a' * (Constants::NAME_MAX_LENGTH + 1)).to_s ) post :create, params: { team: @team_one }, as: :json expect(response).to have_http_status(:unprocessable_entity) @@ -51,7 +53,7 @@ describe ClientApi::Teams::TeamsController, type: :controller do it 'should return HTTP unprocessable_entity response if description too long' do @team_one.update_attribute( :description, - "#{'a' * (Constants::TEXT_MAX_LENGTH + 1)}" + ('a' * (Constants::TEXT_MAX_LENGTH + 1)).to_s ) post :create, params: { team: @team_one }, as: :json expect(response).to have_http_status(:unprocessable_entity) diff --git a/spec/services/client_api/teams/create_service_spec.rb b/spec/services/client_api/teams/create_service_spec.rb new file mode 100644 index 000000000..55c083736 --- /dev/null +++ b/spec/services/client_api/teams/create_service_spec.rb @@ -0,0 +1,70 @@ +require 'rails_helper' + +include ClientApi::Teams + +describe ClientApi::Teams::CreateService do + + let(:user) { create :user, email: 'user@asdf.com' } + let(:team) { build :team, + name: 'My Team', + description: 'My Description' } + + it 'should raise a StandardError if current_user is not assigned' do + expect { CreateService.new }.to raise_error(StandardError) + end + + it 'should create a new team' do + service = CreateService.new( + current_user: user, + params: { name: team.name, description: team.description } + ) + result = service.execute + expect(result[:status]).to eq :success + + team_n = Team.order(created_at: :desc).first + expect(team_n.name).to eq team.name + expect(team_n.description).to eq team.description + expect(team_n.created_by).to eq user + expect(team_n.users.count).to eq 1 + expect(team_n.users.take).to eq user + end + + it 'should return error response if not all params are present' do + service = CreateService.new(current_user: user, params: {}) + result = service.execute + expect(result[:status]).to eq :error + + service = CreateService.new( + current_user: user, + params: { description: team.description }) + result = service.execute + expect(result[:status]).to eq :error + end + + it 'should return error response if name too short' do + team.name = ('a' * (Constants::NAME_MIN_LENGTH - 1)).to_s + service = CreateService.new( + current_user: user, + params: { name: team.name, description: team.description }) + result = service.execute + expect(result[:status]).to eq :error + end + + it 'should return error response if name too long' do + team.name = ('a' * (Constants::NAME_MAX_LENGTH + 1)).to_s + service = CreateService.new( + current_user: user, + params: { name: team.name, description: team.description }) + result = service.execute + expect(result[:status]).to eq :error + end + + it 'should return error response if description too long' do + team.description = ('a' * (Constants::TEXT_MAX_LENGTH + 1)).to_s + service = CreateService.new( + current_user: user, + params: { name: team.name, description: team.description }) + result = service.execute + expect(result[:status]).to eq :error + end +end