Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for duration when creating new viewing party and tests #12

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions app/controllers/viewing_parties_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ def create
@user = User.find(params[:user_id])
@users = User.where("id != #{params[:user_id]}")
@movie = facade.movie_details(params[:movid_id])
@party = @user.parties.new(movie_id: @movie[:id], movie_title: @movie[:title], duration: params[:duration], date: params[:date], start_time: params[:start_time])
if @party.save
@user.user_parties.create!(user_id: @user.id, party_id: @party.id, is_host: true)
@users.each do |other_user|
if params[:"#{other_user.id}"].present?
other_user.user_parties.create!(party_id: @party.id)
if @movie[:runtime].to_i > params[:duration].to_i
flash[:alert] = "Error: duration cannot be less that movie runtime"
redirect_to "/users/#{@user.id}/movies/#{@movie[:id]}/viewing-party/new"
else
@party = @user.parties.new(movie_id: @movie[:id], movie_title: @movie[:title], duration: params[:duration], date: params[:date], start_time: params[:start_time])
if @party.save
@user.user_parties.create!(user_id: @user.id, party_id: @party.id, is_host: true)
@users.each do |other_user|
if params[:"#{other_user.id}"].present?
other_user.user_parties.create!(party_id: @party.id)
end
end
redirect_to user_path(@user.id)
else
flash[:alert] = "Error: something is wrong with credentials"
redirect_to "/users/#{@user.id}/movies/#{@movie[:id]}/viewing-party/new"
end
redirect_to user_path(@user.id)
else
flash[:alert] = "Error: something is wrong with credentials"
redirect_to "/users/#{@user.id}/movies/#{@movie.id}/viewing-party/new"
end
end
end
50 changes: 43 additions & 7 deletions spec/features/users/movies/viewing-party/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
RSpec.describe 'New Viewing Party Page' do
before(:each) do
load_test_data
end

it 'When I visit the new viewing party page it contains movie title, duration of party, when date, start time, checkboxes next to each user' do
specific_movie_response = File.read('spec/fixtures/specific_movie.json')
starwars_response = File.read('spec/fixtures/starwars_collection.json')
lotr_response = File.read('spec/fixtures/lotr_collection.json')
Expand Down Expand Up @@ -36,7 +33,9 @@
'User-Agent'=>'Faraday v2.7.12'
}).
to_return(status: 200, body: specific_movie_response, headers: {})
end

it 'When I visit the new viewing party page it contains movie title, duration of party, when date, start time, checkboxes next to each user' do
visit "/users/#{@user1.id}/movies/268/viewing-party/new"
expect(page).to have_field(:duration, with:126)
expect(page).to have_field(:date)
Expand All @@ -55,10 +54,47 @@
click_button 'Create Party'

expect(current_path).to eq(user_path(@user1.id))
# save_and_open_page
# expect(page).to have_content('Batman')
end

it 'sad path for creating a party: Empty duration' do
visit "/users/#{@user1.id}/movies/268/viewing-party/new"
expect(page).to have_field(:duration, with:126)
expect(page).to have_field(:date)
expect(page).to have_field(:start_time)
expect(page).to have_button('Create Party')
expect(page).to have_content('Batman')

expect(@user2.user_parties.count).to eq(3)
expect(@user3.user_parties.count).to eq(2)

fill_in :duration, with: ""
fill_in :start_time, with: '10:00'
fill_in :date, with: '2023/08/01'
check "#{@user2.name} (#{@user2.email})"
check "#{@user3.name} (#{@user3.email})"
click_button 'Create Party'

expect(current_path).to eq("/users/#{@user1.id}/movies/268/viewing-party/new")
end

it 'sad path for creating a party: Bad duration' do
visit "/users/#{@user1.id}/movies/268/viewing-party/new"
expect(page).to have_field(:duration, with:126)
expect(page).to have_field(:date)
expect(page).to have_field(:start_time)
expect(page).to have_button('Create Party')
expect(page).to have_content('Batman')

expect(@user2.user_parties.count).to eq(3)
expect(@user3.user_parties.count).to eq(2)

fill_in :duration, with: "2"
fill_in :start_time, with: '10:00'
fill_in :date, with: '2023/08/01'
check "#{@user2.name} (#{@user2.email})"
check "#{@user3.name} (#{@user3.email})"
click_button 'Create Party'

# expect(@user2.user_parties.count).to eq(4)
# expect(@user3.user_parties.count).to eq(3)
expect(current_path).to eq("/users/#{@user1.id}/movies/268/viewing-party/new")
end
end