diff --git a/app/models/game.rb b/app/models/game.rb index e45a2d7..d0de23b 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -64,7 +64,7 @@ class Game < ActiveRecord::Base end def add_player_from_user(user) - return false unless can_accomodate(user) + return false unless self.joinable?(user) new_player = self.players.create(user: user, soft_token: user.soft_token) end @@ -73,7 +73,7 @@ class Game < ActiveRecord::Base end def already_has?(user) - users.include?(user) + self.players.pluck(:user_id, :soft_token).flatten.compact.to_set.intersect? ([user.id, user.soft_token].to_set) end def full? @@ -88,7 +88,7 @@ class Game < ActiveRecord::Base end def joinable? user - !self.users.include?(user) && !self.full? + !self.already_has?(user) && !self.full? end def next_player_id diff --git a/app/models/player.rb b/app/models/player.rb index 6b24556..b4f406f 100644 --- a/app/models/player.rb +++ b/app/models/player.rb @@ -7,8 +7,8 @@ class Player < ActiveRecord::Base has_many :plays #TODO: soft_user will not have a user_id - validates :user_id, presence: true - validates :game_id, presence: true + validates :user, presence: true + validates :game, presence: true def inventory player_cards.where(play_id: nil).order(:value) diff --git a/app/views/users/registrations/new.html.erb b/app/views/users/registrations/new.html.erb new file mode 100644 index 0000000..a42ec17 --- /dev/null +++ b/app/views/users/registrations/new.html.erb @@ -0,0 +1,30 @@ +

Sign up

+ +<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> + <%= devise_error_messages! %> + +
+ <%= f.hidden_field :soft_token, value: current_user.soft_token %> + <%= f.label :email %>
+ <%= f.email_field :email, autofocus: true %> +
+ +
+ <%= f.label :password %> + <% if @minimum_password_length %> + (<%= @minimum_password_length %> characters minimum) + <% end %>
+ <%= f.password_field :password, autocomplete: "off" %> +
+ +
+ <%= f.label :password_confirmation %>
+ <%= f.password_field :password_confirmation, autocomplete: "off" %> +
+ +
+ <%= f.submit "Sign up" %> +
+<% end %> + +<%= render "devise/shared/links" %> diff --git a/spec/features/users/user_join_game_spec.rb b/spec/features/users/user_join_game_spec.rb new file mode 100644 index 0000000..78d8906 --- /dev/null +++ b/spec/features/users/user_join_game_spec.rb @@ -0,0 +1,51 @@ +require 'rails_helper' +include Warden::Test::Helpers +Warden.test_mode! + +feature 'Join a game', type: :feature, js: true do +# before(:each) do +# @game = setup_game +# @game.reload +# @player = @game.controlling_player +# signin(@player.user.email,'please123') +# #login_as(@player.user, scope: :user) +# visit game_path @game +# end + + after(:each) do + Warden.test_reset! + end + + # Scenario: The user joins a game + # Given: A joinable game and I am a signed in user + # When I click the join game button + # Then I see myself in the game + scenario 'user can join a game' do + @game = FactoryGirl.create :game + @user = FactoryGirl.create :user + login_as(@user, scope: :user) + visit game_path @game + click_button "Join" + #TODO: Fix this asynchronous race condition + visit current_path + expect(page).to have_content @user.email + end + + # Scenario: The user is unable to join a game if they are already joined + # Given: A game I am currently in that is otherwise joinable + # When I visit the game path + # Then I do not see a Join button + scenario 'user can not join a game twice' do + @game = FactoryGirl.create :game + @user = FactoryGirl.create :user + login_as(@user, scope: :user) + visit game_path @game + click_button "Join" + #TODO: Fix this asynchronous race condition + visit current_path + expect(page).to_not have_content "Join" + end + + + +end diff --git a/spec/features/visitors/sign_up_spec.rb b/spec/features/visitors/sign_up_spec.rb index 6f51d68..3d5a825 100644 --- a/spec/features/visitors/sign_up_spec.rb +++ b/spec/features/visitors/sign_up_spec.rb @@ -14,6 +14,30 @@ feature 'Sign Up', :devise do expect(page).to have_content(/.*#{txts[0]}.*|.*#{txts[1]}.*/) end + # Scenario: Visitor joins a game and then signs up + # Given I am not signed in + # When I join a game and then sign up + # Then I should see myself in that game + scenario 'visitor joins a game and then signs up', js: true do + visit new_game_path + fill_in 'game_title', with: 'Test Game' + click_button 'Create Game' + click_button 'Join' + sign_up_with('test@example.com', 'please123', 'please123') + visit games_path + click_link 'Test Game' + expect(page).to have_content('test@example.com') + end + +# # Scenario: Visitor joins a game and then signs in +# # Given I am not signed in +# # When I join a game and then sign in +# # Then I should see myself in that game +# scenario 'visitor joins a game and then signs in' do +# sign_up_with('test@example.com', 'please123', 'mismatch') +# expect(page).to have_content "Password confirmation doesn't match" +# end + # Scenario: Visitor cannot sign up with invalid email address # Given I am not signed in # When I sign up with an invalid email address diff --git a/spec/support/helpers/session_helpers.rb b/spec/support/helpers/session_helpers.rb index 1f6a329..9cd256e 100644 --- a/spec/support/helpers/session_helpers.rb +++ b/spec/support/helpers/session_helpers.rb @@ -3,7 +3,7 @@ module Features def sign_up_with(email, password, confirmation) visit new_user_registration_path fill_in 'Email', with: email - fill_in 'Password', with: password + fill_in 'Password', with: password, match: :prefer_exact fill_in 'Password confirmation', with: confirmation click_button 'Sign up' end