From ee9685195179a31d591954350973adb59e3ecd50 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Thu, 21 Sep 2017 13:46:36 +0200 Subject: [PATCH 1/5] Add tests for create new team page Closes SCI-1620. --- .../client_api/teams/teams_controller_spec.rb | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/spec/controllers/client_api/teams/teams_controller_spec.rb b/spec/controllers/client_api/teams/teams_controller_spec.rb index 9ec6ba221..609d01e11 100644 --- a/spec/controllers/client_api/teams/teams_controller_spec.rb +++ b/spec/controllers/client_api/teams/teams_controller_spec.rb @@ -6,8 +6,8 @@ describe ClientApi::Teams::TeamsController, type: :controller do 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' + @team_one = FactoryGirl.create :team, created_by: @user_one + @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 @@ -19,6 +19,45 @@ describe ClientApi::Teams::TeamsController, type: :controller do end end + describe 'POST #create' do + before do + @team_one.update_attribute(:name, 'My Team') + @team_one.update_attribute(:description, 'Lorem ipsum ipsum') + end + + it 'should return HTTP success response' do + post :create, params: { team: @team_one }, as: :json + expect(response).to have_http_status(:ok) + end + + it 'should return HTTP unprocessable_entity response if name too short' do + @team_one.update_attribute( + :name, + "#{'a' * (Constants::NAME_MIN_LENGTH - 1)}" + ) + post :create, params: { team: @team_one }, as: :json + expect(response).to have_http_status(:unprocessable_entity) + end + + it 'should return HTTP unprocessable_entity response if name too long' do + @team_one.update_attribute( + :name, + "#{'a' * (Constants::NAME_MAX_LENGTH + 1)}" + ) + post :create, params: { team: @team_one }, as: :json + expect(response).to have_http_status(:unprocessable_entity) + end + + it 'should return HTTP unprocessable_entity response if description too long' do + @team_one.update_attribute( + :description, + "#{'a' * (Constants::TEXT_MAX_LENGTH + 1)}" + ) + post :create, params: { team: @team_one }, as: :json + expect(response).to have_http_status(:unprocessable_entity) + 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 From a14457cba0d1b009c33314e2b2ed78e5830a8591 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Thu, 21 Sep 2017 19:12:47 +0200 Subject: [PATCH 2/5] Add tests for create team service --- .../client_api/teams/teams_controller_spec.rb | 10 +-- .../client_api/teams/create_service_spec.rb | 70 +++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 spec/services/client_api/teams/create_service_spec.rb 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 From e0a6b1360da99a6b4ceef672ad9230c55044baa2 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Thu, 21 Sep 2017 19:18:39 +0200 Subject: [PATCH 3/5] Hound is love, Hound is life --- .../client_api/teams/create_service_spec.rb | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/spec/services/client_api/teams/create_service_spec.rb b/spec/services/client_api/teams/create_service_spec.rb index 55c083736..954a57b16 100644 --- a/spec/services/client_api/teams/create_service_spec.rb +++ b/spec/services/client_api/teams/create_service_spec.rb @@ -3,11 +3,10 @@ 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' } + let(:team) do + build :team, name: 'My Team', description: 'My Description' + end it 'should raise a StandardError if current_user is not assigned' do expect { CreateService.new }.to raise_error(StandardError) @@ -36,7 +35,8 @@ describe ClientApi::Teams::CreateService do service = CreateService.new( current_user: user, - params: { description: team.description }) + params: { description: team.description } + ) result = service.execute expect(result[:status]).to eq :error end @@ -45,7 +45,8 @@ describe ClientApi::Teams::CreateService do team.name = ('a' * (Constants::NAME_MIN_LENGTH - 1)).to_s service = CreateService.new( current_user: user, - params: { name: team.name, description: team.description }) + params: { name: team.name, description: team.description } + ) result = service.execute expect(result[:status]).to eq :error end @@ -54,7 +55,8 @@ describe ClientApi::Teams::CreateService do team.name = ('a' * (Constants::NAME_MAX_LENGTH + 1)).to_s service = CreateService.new( current_user: user, - params: { name: team.name, description: team.description }) + params: { name: team.name, description: team.description } + ) result = service.execute expect(result[:status]).to eq :error end @@ -63,7 +65,8 @@ describe ClientApi::Teams::CreateService do team.description = ('a' * (Constants::TEXT_MAX_LENGTH + 1)).to_s service = CreateService.new( current_user: user, - params: { name: team.name, description: team.description }) + params: { name: team.name, description: team.description } + ) result = service.execute expect(result[:status]).to eq :error end From 6e082ccc24649da462242d848da690b61042000d Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Thu, 21 Sep 2017 19:20:19 +0200 Subject: [PATCH 4/5] Hound is love, Hound is life #2 --- spec/services/client_api/teams/create_service_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/services/client_api/teams/create_service_spec.rb b/spec/services/client_api/teams/create_service_spec.rb index 954a57b16..d5648adf6 100644 --- a/spec/services/client_api/teams/create_service_spec.rb +++ b/spec/services/client_api/teams/create_service_spec.rb @@ -5,7 +5,7 @@ include ClientApi::Teams describe ClientApi::Teams::CreateService do let(:user) { create :user, email: 'user@asdf.com' } let(:team) do - build :team, name: 'My Team', description: 'My Description' + build :team, name: 'My Team', description: 'My Description' end it 'should raise a StandardError if current_user is not assigned' do From fbddf4da797bcff5c9a9e13d7930e7236b712d1c Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Mon, 2 Oct 2017 11:37:58 +0200 Subject: [PATCH 5/5] Update Gemfile for correct devise version, minorly fix services spec --- Gemfile | 4 +++- Gemfile.lock | 12 +++++++++--- .../services/client_api/teams/create_service_spec.rb | 4 +++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index d487bbb7b..1d8d05d80 100644 --- a/Gemfile +++ b/Gemfile @@ -70,7 +70,9 @@ gem 'paperclip', '~> 5.1' # File attachment, image attachment library gem 'aws-sdk', '~> 2' gem 'delayed_job_active_record' -gem 'devise-async' +gem 'devise-async', + git: 'https://github.com/mhfs/devise-async.git', + branch: 'devise-4.x' gem 'ruby-graphviz', '~> 1.2' # Graphviz for rails gem 'tinymce-rails', '~> 4.6.4' # Rich text editor diff --git a/Gemfile.lock b/Gemfile.lock index a5f40ce5a..ec7e69440 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -21,6 +21,14 @@ GIT activejob (>= 4.2) paperclip (>= 3.3) +GIT + remote: https://github.com/mhfs/devise-async.git + revision: 177f6363a002f7ff28f1d289c8cab7ad8d9cb8c5 + branch: devise-4.x + specs: + devise-async (0.10.2) + devise (>= 4.0) + GIT remote: https://github.com/phatworx/devise_security_extension.git revision: b2ee978af7d49f0fb0e7271c6ac074dfb4d39353 @@ -175,8 +183,6 @@ GEM railties (>= 4.1.0, < 5.2) responders warden (~> 1.2.3) - devise-async (0.7.0) - devise (>= 2.2) devise_invitable (1.7.2) actionmailer (>= 4.1.0) devise (>= 4.0.0) @@ -487,7 +493,7 @@ DEPENDENCIES delayed_job_active_record delayed_paperclip! devise (~> 4.3.0) - devise-async + devise-async! devise_invitable devise_security_extension! factory_girl_rails diff --git a/spec/services/client_api/teams/create_service_spec.rb b/spec/services/client_api/teams/create_service_spec.rb index d5648adf6..57207bc1d 100644 --- a/spec/services/client_api/teams/create_service_spec.rb +++ b/spec/services/client_api/teams/create_service_spec.rb @@ -28,11 +28,13 @@ describe ClientApi::Teams::CreateService do expect(team_n.users.take).to eq user end - it 'should return error response if not all params are present' do + it 'should return error response if params = {}' do service = CreateService.new(current_user: user, params: {}) result = service.execute expect(result[:status]).to eq :error + end + it 'should return error response if params are missing :name attribute' do service = CreateService.new( current_user: user, params: { description: team.description }