Skip to content

Commit 9a13c77

Browse files
committed
Merge pull request coderwall#234 from YaroSpace/join_team_error_#391
Join team error #391
2 parents 6302b77 + 2d58319 commit 9a13c77

17 files changed

+220
-34
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
.idea
1212
.sass-cache
1313
.vagrant
14+
.rspec
1415
.yardoc
1516
/.bundle
1617
/config/application.yml
@@ -56,3 +57,4 @@ git_stats
5657
vcr_cassettes
5758
dump
5859
BACKUP
60+
Guardfile

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ group :test do
169169
# gem 'rspec-its'
170170
gem 'capybara'
171171
gem 'capybara-screenshot'
172+
gem 'rack_session_access' # allows to set session from within Capybara
173+
gem 'poltergeist' # headless js driver for Capybara that uses phantomJs
172174
gem 'codeclimate-test-reporter', require: false
173175
gem 'database_cleaner'
174176
gem 'fuubar', '2.0.0.rc1'

Gemfile.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ GEM
140140
choice (0.1.6)
141141
chronic (0.10.2)
142142
chunky_png (1.3.1)
143+
cliver (0.3.2)
143144
clockwork (0.7.7)
144145
activesupport
145146
tzinfo
@@ -431,6 +432,11 @@ GEM
431432
slop (~> 3.4, >= 3.4.5)
432433
pg (0.17.1)
433434
pg_array_parser (0.0.9)
435+
poltergeist (1.5.1)
436+
capybara (~> 2.1)
437+
cliver (~> 0.3.1)
438+
multi_json (~> 1.0)
439+
websocket-driver (>= 0.2.0)
434440
polyamorous (0.5.0)
435441
activerecord (~> 3.0)
436442
polyglot (0.3.5)
@@ -485,6 +491,9 @@ GEM
485491
rack
486492
rack-test (0.6.2)
487493
rack (>= 1.0)
494+
rack_session_access (0.1.1)
495+
builder (>= 2.0.0)
496+
rack (>= 1.0.0)
488497
rails (3.2.19)
489498
actionmailer (= 3.2.19)
490499
actionpack (= 3.2.19)
@@ -700,6 +709,7 @@ GEM
700709
addressable (>= 2.2.7)
701710
crack (>= 0.3.2)
702711
websocket (1.2.0)
712+
websocket-driver (0.3.5)
703713
xpath (2.0.0)
704714
nokogiri (~> 1.3)
705715
yard (0.8.7.4)
@@ -776,13 +786,15 @@ DEPENDENCIES
776786
omniauth-linkedin (~> 0.0.6)
777787
omniauth-twitter (~> 0.0.16)
778788
pg
789+
poltergeist
779790
postgres_ext
780791
pry-byebug
781792
pry-rescue
782793
pubnub (= 0.1.9)
783794
puma
784795
querystring
785796
quiet_assets
797+
rack_session_access
786798
rails (~> 3.2)
787799
rails-assets-font-awesome
788800
rails-assets-jquery-cookie (= 1.4.0)

app/assets/stylesheets/product_description.css.scss

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,17 @@
211211
margin: 0 auto 3% auto;
212212
}
213213

214+
.notice {
215+
font-size: 1.4em;
216+
background: $green;
217+
color: #fff;
218+
padding: 1%;
219+
text-align: center;
220+
display: block;
221+
width: 50%;
222+
margin: 0 auto 3% auto;
223+
}
224+
214225
.errors {
215226
width: 90%;
216227
background: $red;
@@ -423,6 +434,10 @@
423434
color: #5f5f5f;
424435
font-family: "MuseoSans-500";
425436
}
437+
438+
@media screen and (min-width: 768px) {
439+
min-height: 55px;
440+
}
426441
}
427442

428443
.selected {
@@ -1166,4 +1181,4 @@
11661181

11671182
}
11681183

1169-
//body end
1184+
//body end

app/controllers/teams_controller.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,30 +78,29 @@ def new
7878
end
7979

8080
def create
81-
team_params = params.require(:team).permit(:selected, :slug, :name)
82-
selected = team_params.fetch(:selected, nil)
81+
team_params = params.require(:team).permit(:name, :slug, :show_similar, :join_team)
82+
team_name = team_params.fetch(:name, '')
8383

84-
@teams = Team.with_similar_names(team_params[:name])
85-
86-
unless selected == 'false' || @teams.empty?
87-
@new_team_name = team_params[:name]
84+
@teams = Team.with_similar_names(team_name)
85+
if team_params[:show_similar] == 'true' && !@teams.empty?
86+
@new_team_name = team_name
8887
render 'similar_teams' and return
8988
end
9089

91-
if selected == 'true'
90+
if team_params[:join_team] == 'true'
9291
@team = Team.where(slug: team_params[:slug]).first
9392
render :create and return
9493
end
9594

96-
@team = Team.new(name: team_params[:name])
95+
@team = Team.new(name: team_name)
9796
if @team.save
9897
record_event('created team')
9998
@team.add_user(current_user)
10099

101100
flash.now[:notice] = "Successfully created a team #{@team.name}"
102-
render :create
103101
else
104-
flash[:error] = "There was an error in creating a team #{@team.name}"
102+
message = @team.errors.full_messages.join("\n")
103+
flash[:error] = "There was an error in creating a team #{@team.name}\n#{message}"
105104
end
106105
end
107106

app/controllers/usernames_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
class UsernamesController < ApplicationController
22
skip_before_action :require_registration
33

4+
def index
5+
# returns nothing if validation is run agains empty params[:id]
6+
render nothing: true
7+
end
8+
49
def show
510
# allow validation to pass if it's the user's username that they're trying to validate (for edit username)
611
if signed_in? && current_user.username.downcase == params[:id].downcase
@@ -11,4 +16,4 @@ def show
1116
head :ok
1217
end
1318
end
14-
end
19+
end

app/views/teams/_form.html.haml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
= f.text_field :name
88

99
-if @team.new_record?
10+
= hidden_field_tag('team[show_similar]', true)
1011
.save
1112
%input.button{ type: 'submit', value: 'Next' }

app/views/teams/_payment.html.haml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
.intro
22
- if @team.can_upgrade?
33
%h2.plans-heading Select a plan and enter payment details to get started
4+
- unless flash[:notice].blank?
5+
%span.notice#notice
6+
-flash[:notice].split("\n").each do |notice|
7+
= notice
8+
%br
49
- unless flash[:error].blank?
510
%span.error#error
611
-flash[:error].split("\n").each do |error|

app/views/teams/_similar_teams.html.haml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
.team-avatar
88
=image_tag(team.avatar_url)
99
%h4= link_to team.name, teamname_path(:slug => team.slug), :target => :new
10-
= link_to 'Select', teams_path(Team.new, :team => { :slug=> team.slug, selected: true }), :remote => true, :method => :post, :class => "select"
10+
= link_to 'Select', teams_path(:team => { :slug=> team.slug, join_team: true }), :remote => true, :method => :post, :class => "select"
1111

1212
- unless exact_team_exists?(@teams, @new_team_name)
1313
.just-create-team
1414
%h3 None of the above are my team
15-
= link_to "Create team #{@new_team_name}", teams_path(Team.new, :team => { :name => @new_team_name, selected: false }), :remote => true, :method => :post, :class => "create-team"
15+
= link_to "Create team #{@new_team_name}", teams_path(:team => { :name => @new_team_name }), :remote => true, :method => :post, :class => "create-team"

app/views/teams/create.js.erb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
var notificationBar = $('#main-content').find('.notification-bar');
2-
notificationBar.find('.notification-bar-inside').find('p').html("<%= flash[:error] || flash[:notice] %>");
3-
notificationBar.show();
4-
51
$('section.feature:not(.payment)').hide();
62
$('section.feature.payment').append('<%= escape_javascript(render :partial => "payment", :locals => {:account => @team.account || @team.build_account, :plan => @team.account.try(:current_plan)}) %>');
73
$('section.feature.payment').show();

config/environments/development.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@
3434
Rails.application.config.sass.cache_location = "/tmp/codewall-cache/sass/"
3535

3636
BetterErrors::Middleware.allow_ip! ENV['TRUSTED_IP'] if ENV['TRUSTED_IP']
37+
Rails.logger = Logger.new(STDOUT)
38+
Rails.logger.level = Logger::DEBUG
3739
end

config/environments/test.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
config.cache_classes = false
44
config.whiny_nils = true
55
config.consider_all_requests_local = true
6-
config.action_dispatch.show_exceptions = false
6+
config.action_dispatch.show_exceptions = true
77
config.action_controller.allow_forgery_protection = false
88
config.action_mailer.delivery_method = :test
99
config.active_support.deprecation = :stderr
@@ -13,4 +13,9 @@
1313

1414
# Allow pass debug_assets=true as a query parameter to load pages with unpackaged assets
1515
config.assets.allow_debugging = true
16+
17+
config.middleware.use RackSessionAccess::Middleware # alloes to set session from within Capybara
18+
19+
Rails.logger = Logger.new(STDOUT)
20+
Rails.logger.level = Logger::DEBUG # provides more verbose output when testing with headless browsers in case of errors
1621
end

spec/controllers/teams_controller_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@
4747
end
4848

4949
context 'a team is selected from a list of similar teams' do
50-
it 'renders a template with a choice of tariff plans when user picks a name from existing names' do
50+
it 'renders a template with a choice of tariff plans when user joins and existing team' do
5151
allow(Team).to receive(:where).and_return(['team_1', 'team_2'])
52-
post :create, :team => { selected: 'true', slug: 'team_name' }, format: :js
52+
post :create, :team => { join_team: 'true', slug: 'team_name' }, format: :js
5353

5454
expect(assigns[:team]).to eq('team_1')
5555
expect(response).to render_template('create')
5656
end
5757

58-
it 'renders a template with a choice of tariff plans if user picks his own team name' do
59-
post :create, :team => { name: 'team_name', selected: 'false' }, format: :js
58+
it 'renders a template with a choice of tariff plans if user picks supplied team name' do
59+
post :create, :team => { name: 'team_name' }, format: :js
6060
expect(response).to render_template('create')
6161
end
6262
end
@@ -88,13 +88,13 @@
8888
it 'renders template with option to join' do
8989
expect(response).to be_success
9090
expect(response).to render_template('create')
91-
expect(flash[:notice]).to eq("Successfully created a team team_name")
91+
expect(flash[:notice]).to include("Successfully created a team team_name")
9292
end
9393

9494
it 'renders failure notice' do
9595
allow(team).to receive(:save).and_return(false)
9696
response
97-
expect(flash[:error]).to eq("There was an error in creating a team team_name")
97+
expect(flash[:error]).to include("There was an error in creating a team team_name")
9898
end
9999
end
100100

@@ -105,7 +105,7 @@
105105
end
106106

107107
it 'renders a template with a list of similar teams' do
108-
post :create, :team => { name: 'team_name' }, format: :js
108+
post :create, :team => { name: 'team_name', show_similar: 'true' }, format: :js
109109

110110
expect(assigns[:new_team_name]).to eq('team_name')
111111
expect(response).to render_template('similar_teams')
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
require "rails_helper"
2+
3+
feature "Teams management", js: true do
4+
5+
background do
6+
login_as(username: 'alice', bypass_ui_login: true)
7+
end
8+
9+
context 'creating a team with no similar names in db' do
10+
scenario 'create a new team' do
11+
create_team('TEST_TEAM')
12+
expect(page).to have_content('Successfully created a team TEST_TEAM')
13+
end
14+
15+
scenario 'add user to the newly created team' do
16+
create_team('TEST_TEAM')
17+
18+
find('section.feature.payment') # ensures that we wait until the create_team action completes
19+
visit '/alice'
20+
21+
expect(page).to have_content('TEST_TEAM')
22+
end
23+
24+
scenario 'show payment plans selection' do
25+
create_team('TEST_TEAM')
26+
27+
expect(page).to have_content('Select a plan and enter payment details to get started')
28+
expect(page).to have_content('FREE')
29+
expect(page).to have_content('MONTHLY')
30+
expect(page).to have_content('ANALYTICS')
31+
end
32+
33+
scenario 'redirect to team profile page when user selects FREE plan' do
34+
create_team('TEST_TEAM')
35+
find('section.feature.payment').find('.plans .plan.free').click_link 'Select plan'
36+
37+
team_id = Team.any_of({name: 'TEST_TEAM'}).first.id
38+
expect(current_path).to eq(team_path(team_id))
39+
end
40+
end
41+
42+
context 'create a team with similar names already in db' do
43+
let!(:team) { Team.create(name: 'EXISTING_TEAM') }
44+
45+
scenario 'create a new team' do
46+
create_team('TEAM')
47+
48+
expect(page).to have_content('We found some matching teams')
49+
expect(page).to have_content('EXISTING_TEAM')
50+
expect(page).to have_content('Select')
51+
expect(page).to have_content('None of the above are my team')
52+
expect(page).to have_content('Create team TEAM')
53+
end
54+
55+
scenario 'create a new team with originally supplied name' do
56+
create_team('TEAM')
57+
find('.just-create-team').click_link('Create team TEAM')
58+
expect(page).to have_content('Successfully created a team TEAM')
59+
end
60+
61+
scenario 'attempt to create a team with exact name already in db' do
62+
create_team('EXISTING_TEAM')
63+
find('.just-create-team').click_link('Create team EXISTING_TEAM')
64+
expect(page).to have_content('There was an error in creating a team EXISTING_TEAM')
65+
expect(page).to have_content('Name is already taken')
66+
end
67+
end
68+
69+
context 'join a team with a similar name' do
70+
let!(:team) { Team.create(name: 'EXISTING_TEAM') }
71+
72+
scenario 'join an existing team' do
73+
create_team('TEAM')
74+
75+
find('.results-list').click_link('Select')
76+
77+
expect(page).to have_content('Select a plan and enter payment details to get started')
78+
expect(page).to have_content('I work at EXISTING_TEAM and just want to join the team')
79+
expect(page).to have_content('Request to join team')
80+
end
81+
82+
scenario 'request to join a team' do
83+
create_team('TEAM')
84+
85+
find('.results-list').click_link('Select')
86+
find('section.feature.payment').click_link 'Request to join team'
87+
88+
expect(current_path).to eq(teamname_path(team.slug))
89+
expect(page).to have_content('We have submitted your join request to the team admin to approve')
90+
end
91+
end
92+
93+
94+
end

spec/rails_helper.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
require 'spec_helper.rb'
1+
require 'spec_helper.rb'
2+
require 'capybara/poltergeist'
3+
require 'rack_session_access/capybara'
4+
5+
Capybara.javascript_driver = :poltergeist
6+
Capybara.default_wait_time = 5
7+
8+
RSpec.configure do |config|
9+
config.before(:example, js: :true) do
10+
DatabaseCleaner.strategy = :truncation
11+
ActiveRecord::Base.establish_connection
12+
end
13+
14+
config.include Features::GeneralHelpers, type: :feature
15+
end

0 commit comments

Comments
 (0)