diff --git a/Gemfile b/Gemfile index 405a659d..4d8e47df 100644 --- a/Gemfile +++ b/Gemfile @@ -61,6 +61,7 @@ group :test do gem 'guard', '2.14.0' gem 'guard-minitest', '2.4.6' gem 'minitest-reporters', '1.1.14' + gem 'webmock', '~> 3.3' gem 'simplecov', :require => false end diff --git a/Gemfile.lock b/Gemfile.lock index ffbf3aa7..1e8b9ac5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -98,6 +98,8 @@ GEM country_select (3.1.1) countries (~> 2.0) sort_alphabetical (~> 1.0) + crack (0.4.3) + safe_yaml (~> 1.0.0) crass (1.0.3) descendants_tracker (0.0.4) thread_safe (~> 0.3, >= 0.3.1) @@ -282,6 +284,7 @@ GEM guard-minitest (2.4.6) guard-compat (~> 1.2) minitest (>= 3.0) + hashdiff (0.3.7) http_parser.rb (0.6.0) i18n (0.9.5) concurrent-ruby (~> 1.0) @@ -413,6 +416,7 @@ GEM ruby-graphviz (1.2.3) ruby-progressbar (1.9.0) ruby_dep (1.5.0) + safe_yaml (1.0.4) sass (3.5.6) sass-listen (~> 4.0.0) sass-listen (4.0.0) @@ -475,6 +479,10 @@ GEM activemodel (>= 5.0) bindex (>= 0.4.0) railties (>= 5.0) + webmock (3.3.0) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.3) @@ -531,6 +539,7 @@ DEPENDENCIES tzinfo-data uglifier (>= 1.3.0) web-console (>= 3.3.0) + webmock (~> 3.3) will_paginate (~> 3.1, >= 3.1.6) RUBY VERSION diff --git a/test/controllers/events_controller_test.rb b/test/controllers/events_controller_test.rb index 1677e3e1..501baae6 100644 --- a/test/controllers/events_controller_test.rb +++ b/test/controllers/events_controller_test.rb @@ -62,7 +62,7 @@ class EventsControllerTest < ActionDispatch::IntegrationTest sign_in(@user) patch group_event_url(@group, @event), - params: { event: { image: sample_image } } + params: { event: { image: upload_valid_image } } assert_redirected_to group_event_url(@group, @event) end @@ -87,12 +87,8 @@ def event_params website: "www.event.com", start_date: Time.zone.now + 6.days, end_date: Time.zone.now + 1.week, - image: sample_image + image: upload_valid_image } } end - - def sample_image - fixture_file_upload("test/fixtures/files/sample.jpeg", "image/jpeg") - end end diff --git a/test/controllers/groups_controller_test.rb b/test/controllers/groups_controller_test.rb index cf904d13..1c544e08 100644 --- a/test/controllers/groups_controller_test.rb +++ b/test/controllers/groups_controller_test.rb @@ -59,10 +59,6 @@ class GroupsControllerTest < ActionDispatch::IntegrationTest private - def upload_valid_image - fixture_file_upload("test/fixtures/files/sample.jpeg", "image/jpeg") - end - def group_params { name: "Test group", diff --git a/test/decorators/event_decorator_test.rb b/test/decorators/event_decorator_test.rb index 1e511d35..faf731a3 100644 --- a/test/decorators/event_decorator_test.rb +++ b/test/decorators/event_decorator_test.rb @@ -1,15 +1,19 @@ require 'test_helper' class EventDecoratorTest < ActiveSupport::TestCase + def setup + stub_requests_to_googleapis + end + test "#full_address returns full address without country" do - event = fake_event + event = fake_event_decorator full_address = "Obento, Matsubara-dori, 8, Kyoto, 6050856" assert_equal event.full_address, full_address end test "#short_address returns place name and city" do - event = fake_event + event = fake_event_decorator short_address = "Obento, Kyoto" assert_equal event.short_address, short_address @@ -17,33 +21,7 @@ class EventDecoratorTest < ActiveSupport::TestCase private - def fake_event(params = {}) - @fake_event ||= EventDecorator.new(Event.new( - group: groups(:two), - title: params[:title] || "Test event", - description: params[:description] || Faker::Lorem.paragraph, - website: params[:website] || "", - start_date: params[:start_date] || Time.zone.now + 6.days, - end_date: params[:end_date] || Time.zone.now + 1.week, - image: params[:image] || image, - organizer: users(:phil), - address_attributes: address(params) - )) - end - - def image - File.open(Rails.root.join('test/fixtures/files/sample.jpeg')) - end - - def address(params = {}) - { - place_name: params[:place_name] || "Obento", - street1: params[:street1] || "Matsubara-dori, 8", - street2: params[:street2] || "", - city: params[:city] || "Kyoto", - state: params[:state] || "", - post_code: params[:post_code] || "6050856", - country: params[:country] || "Japan" - } + def fake_event_decorator(params = {}) + @fake_event_decorator ||= EventDecorator.new(fake_event(params)) end end diff --git a/test/integration/events_creation_test.rb b/test/integration/events_creation_test.rb index 4160d0e2..25d0c509 100644 --- a/test/integration/events_creation_test.rb +++ b/test/integration/events_creation_test.rb @@ -24,10 +24,10 @@ def setup click_on "Create event" end - fill_in_valid_information + fill_in_valid_information(@event.description) fill_in_valid_address fill_in_valid_dates - attach_valid_image + attach_valid_image_for "event_image" click_on_create_event assert_valid @@ -44,11 +44,11 @@ def setup end fill_in "Title", with: "T" - fill_in_description + fill_in_description(@event.description) fill_in_valid_address fill_in_valid_dates - attach_valid_image + attach_valid_image_for "event_image" click_on_create_event assert_invalid @@ -67,7 +67,7 @@ def setup fill_in_valid_address fill_in_valid_dates - attach_valid_image + attach_valid_image_for "event_image" click_on_create_event assert_invalid @@ -81,7 +81,7 @@ def setup click_on "Create event" end - fill_in_valid_information + fill_in_valid_information(@event.description) fill_in "Address 1", with: "" fill_in "Address 2", with: @event.street2 @@ -91,7 +91,7 @@ def setup select "Spain", from: "Country" fill_in_valid_dates - attach_valid_image + attach_valid_image_for "event_image" click_on_create_event assert_invalid @@ -105,7 +105,7 @@ def setup click_on "Create event" end - fill_in_valid_information + fill_in_valid_information(@event.description) fill_in "Address 1", with: @event.street1 fill_in "Address 2", with: @event.street2 @@ -115,7 +115,7 @@ def setup select "Spain", from: "Country" fill_in_valid_dates - attach_valid_image + attach_valid_image_for "event_image" click_on_create_event assert_invalid @@ -129,7 +129,7 @@ def setup click_on "Create event" end - fill_in_valid_information + fill_in_valid_information(@event.description) fill_in "Address 1", with: @event.street1 fill_in "Address 2", with: @event.street2 @@ -139,7 +139,7 @@ def setup select "Spain", from: "Country" fill_in_valid_dates - attach_valid_image + attach_valid_image_for "event_image" click_on_create_event assert_invalid @@ -153,7 +153,7 @@ def setup click_on "Create event" end - fill_in_valid_information + fill_in_valid_information(@event.description) fill_in "Address 1", with: @event.street1 fill_in "Address 2", with: @event.street2 @@ -162,7 +162,7 @@ def setup select "", from: "Country" fill_in_valid_dates - attach_valid_image + attach_valid_image_for "event_image" click_on_create_event assert_invalid @@ -176,13 +176,13 @@ def setup click_on "Create event" end - fill_in_valid_information + fill_in_valid_information(@event.description) fill_in_valid_address select_date_and_time 1.week.ago, from: "event_start_date" select_date_and_time @event.end_date, from: "event_end_date" - attach_valid_image + attach_valid_image_for "event_image" click_on_create_event assert_invalid @@ -196,13 +196,13 @@ def setup click_on "Create event" end - fill_in_valid_information + fill_in_valid_information(@event.description) fill_in_valid_address select_date_and_time @event.start_date, from: "event_start_date" select_date_and_time 1.week.ago, from: "event_end_date" - attach_valid_image + attach_valid_image_for "event_image" click_on_create_event assert_invalid @@ -216,13 +216,13 @@ def setup click_on "Create event" end - fill_in_valid_information + fill_in_valid_information(@event.description) fill_in_valid_address select_date_and_time nil, from: "event_start_date" select_date_and_time @event.end_date, from: "event_end_date" - attach_valid_image + attach_valid_image_for "event_image" click_on_create_event assert_invalid @@ -236,13 +236,13 @@ def setup click_on "Create event" end - fill_in_valid_information + fill_in_valid_information(@event.description) fill_in_valid_address select_date_and_time @event.start_date, from: "event_start_date" select_date_and_time nil, from: "event_end_date" - attach_valid_image + attach_valid_image_for "event_image" click_on_create_event assert_invalid @@ -256,7 +256,7 @@ def setup click_on "Create event" end - fill_in_valid_information + fill_in_valid_information(@event.description) fill_in_valid_address fill_in_valid_dates @@ -266,13 +266,9 @@ def setup private - def fill_in_valid_information + def fill_in_valid_information(event_description) fill_in "Title", with: @event.title - fill_in_description - end - - def fill_in_description(description = @event.description) - find("trix-editor").click.set(description) + fill_in_description(event_description) end def fill_in_valid_address @@ -289,10 +285,6 @@ def fill_in_valid_dates select_date_and_time @event.end_date, from: "event_end_date" end - def attach_valid_image - attach_file "event_image", "test/fixtures/files/sample.jpeg" - end - def click_on_create_event within "form" do click_on "Create event" diff --git a/test/integration/groups_breadcrumbs_test.rb b/test/integration/groups_breadcrumbs_test.rb index 088a00e7..afcf5e35 100644 --- a/test/integration/groups_breadcrumbs_test.rb +++ b/test/integration/groups_breadcrumbs_test.rb @@ -2,15 +2,13 @@ class GroupsBreadcrumbsTest < ActionDispatch::IntegrationTest def setup - @group = groups(:one) - @event = @group.events.first - @phil = users(:phil) - @penny = users(:penny) - @woodell = users(:woodell) - @stranger = users(:stranger) - - add_group_owner_to_organizers - add_to_members @penny + @group = groups(:one) + @event = @group.events.first + @phil = users(:phil) + @penny = users(:penny) + + add_group_owner_to_organizers @group + add_members_to_group @group, @penny end test "user visits group organizer from group 'show' view" do @@ -22,13 +20,9 @@ def setup click_on @phil.name end - assert page.has_css? ".breadcrumb" + assert_organizer_and_members_breadcrumbs_for @group, @phil within ".breadcrumb" do - assert page.has_link? @group.name - assert page.has_link? "Organizers & Members" - assert page.has_content? @phil.name - click_on @group.name end @@ -54,13 +48,9 @@ def setup click_on @penny.name end - assert page.has_css? ".breadcrumb" + assert_organizer_and_members_breadcrumbs_for @group, @penny within ".breadcrumb" do - assert page.has_link? @group.name - assert page.has_link? "Organizers & Members" - assert page.has_content? @penny.name - click_on @group.name end @@ -86,13 +76,9 @@ def setup click_on @phil.name end - assert page.has_css? ".breadcrumb" + assert_organizer_and_members_breadcrumbs_for @group, @phil within ".breadcrumb" do - assert page.has_link? @group.name - assert page.has_link? "Organizers & Members" - assert page.has_content? @phil.name - click_on "Organizers & Members" end @@ -108,13 +94,9 @@ def setup click_on @penny.name end - assert page.has_css? ".breadcrumb" + assert_organizer_and_members_breadcrumbs_for @group, @penny within ".breadcrumb" do - assert page.has_link? @group.name - assert page.has_link? "Organizers & Members" - assert page.has_content? @penny.name - click_on "Organizers & Members" end @@ -128,8 +110,6 @@ def setup click_on @event.title - assert page.has_css? ".breadcrumb" - within ".breadcrumb" do assert page.has_link? @group.name assert page.has_content? @event.title @@ -149,14 +129,12 @@ def setup click_on @phil.name end - assert page.has_css? ".breadcrumb" - - within ".breadcrumb" do - assert page.has_link? @group.name - assert page.has_link? @event.title + assert_event_breadcrumbs_for @group, @event do assert page.has_content? "Organizer" assert page.has_content? @phil.name + end + within ".breadcrumb" do click_on @event.title end @@ -164,24 +142,23 @@ def setup end test "user visits event attendee from event 'show' view" do - @event.attendees << @woodell + woodell = users(:woodell) + @event.attendees << woodell log_in_as @phil visit group_event_path @group, @event within ".attendees-preview" do - click_on @woodell.name + click_on woodell.name end - assert page.has_css? ".breadcrumb" - - within ".breadcrumb" do - assert page.has_link? @group.name - assert page.has_link? @event.title + assert_event_breadcrumbs_for @group, @event do assert page.has_content? "Attendees" - assert page.has_content? @woodell.name + assert page.has_content? woodell.name + end + within ".breadcrumb" do click_on @group.name end @@ -189,24 +166,23 @@ def setup end test "user visits event attendee from event attendees 'index' view" do - @event.attendees << @woodell + woodell = users(:woodell) + @event.attendees << woodell log_in_as @phil visit event_attendances_path @event within ".attendees" do - click_on @woodell.name + click_on woodell.name end - assert page.has_css? ".breadcrumb" - - within ".breadcrumb" do - assert page.has_link? @group.name - assert page.has_link? @event.title + assert_event_breadcrumbs_for @group, @event do assert page.has_content? "Attendees" - assert page.has_content? @woodell.name + assert page.has_content? woodell.name + end + within ".breadcrumb" do click_on @group.name end @@ -215,13 +191,22 @@ def setup private - # We need to do this because Rolify doesn't seem to work very well with - # fixtures for scoped roles. - def add_group_owner_to_organizers - @group.owner.add_role(:organizer, @group) + def assert_organizer_and_members_breadcrumbs_for(group, user) + within ".breadcrumb" do + assert page.has_link? group.name + assert page.has_link? "Organizers & Members" + assert page.has_content? user.name + + yield if block_given? + end end - def add_to_members(*users) - users.each { |user| user.add_role(:member, @group) } + def assert_event_breadcrumbs_for(group, event) + within ".breadcrumb" do + assert page.has_link? group.name + assert page.has_link? event.title + + yield if block_given? + end end end diff --git a/test/integration/groups_creation_test.rb b/test/integration/groups_creation_test.rb index bf06e41f..bfd08327 100644 --- a/test/integration/groups_creation_test.rb +++ b/test/integration/groups_creation_test.rb @@ -22,7 +22,7 @@ class GroupsCreationTest < ActionDispatch::IntegrationTest fill_in "Location", with: Faker::Address.city fill_in_description(Faker::Lorem.paragraph) - attach_valid_image + attach_valid_image_for "group_image" choose "group_hidden_false" choose "group_all_members_can_create_events_false" @@ -37,14 +37,4 @@ class GroupsCreationTest < ActionDispatch::IntegrationTest click_on user.name end end - - private - - def fill_in_description(description) - find("trix-editor").click.set(description) - end - - def attach_valid_image - attach_file "group_image", "test/fixtures/files/sample.jpeg" - end end diff --git a/test/integration/groups_edit_test.rb b/test/integration/groups_edit_test.rb index 8f7d07dd..9beca436 100644 --- a/test/integration/groups_edit_test.rb +++ b/test/integration/groups_edit_test.rb @@ -15,7 +15,7 @@ class GroupsEditTest < ActionDispatch::IntegrationTest fill_in "Location", with: Faker::Address.city fill_in_description(Faker::Lorem.paragraph) - attach_valid_image + attach_valid_image_for "group_image" choose "group_hidden_true" choose "group_all_members_can_create_events_true" @@ -34,14 +34,4 @@ class GroupsEditTest < ActionDispatch::IntegrationTest assert page.has_content? "The group was deleted." end - - private - - def fill_in_description(description) - find("trix-editor").click.set(description) - end - - def attach_valid_image - attach_file "group_image", "test/fixtures/files/sample.jpeg" - end end diff --git a/test/integration/groups_roles_test.rb b/test/integration/groups_roles_test.rb index cf8fd737..a832780b 100644 --- a/test/integration/groups_roles_test.rb +++ b/test/integration/groups_roles_test.rb @@ -2,15 +2,15 @@ class GroupsRolesTest < ActionDispatch::IntegrationTest def setup - @phil = users(:phil) - @group = groups(:one) + @phil = users(:phil) + @group = groups(:one) @woodell = users(:woodell) - @penny = users(:penny) + @penny = users(:penny) @pennys_group = groups(:three) end test "group owner allows all members to create events" do - add_members [@phil, @woodell], group: @pennys_group + add_members_to_group(@pennys_group, @phil, @woodell) refute_user_can_create_events @phil, group: @pennys_group refute_user_can_create_events @woodell, group: @pennys_group @@ -27,7 +27,7 @@ def setup end test "group owner disallows all members to create events" do - add_members [@phil, @woodell], group: @pennys_group + add_members_to_group(@pennys_group, @phil, @woodell) log_in_as @penny @@ -144,17 +144,9 @@ def refute_user_can_create_events(user, group:) end def set_all_members_can_create_events_to(option) - attach_valid_image + attach_valid_image_for "group_image" choose "group_all_members_can_create_events_#{option}" click_on "Update group" assert page.has_content? "The group has been updated." end - - def attach_valid_image - attach_file "group_image", "test/fixtures/files/sample.jpeg" - end - - def add_members(users, group:) - [users].flatten.each { |user| user.add_role :member, group } - end end diff --git a/test/integration/groups_search_test.rb b/test/integration/groups_search_test.rb index ac3db3f8..14fdb895 100644 --- a/test/integration/groups_search_test.rb +++ b/test/integration/groups_search_test.rb @@ -5,48 +5,36 @@ def setup @user = users(:penny) end - test "user can search for a group using only the city" do + test "user can search for a group using only the location" do log_in_as(@user) visit root_path - fill_in "Location", with: "Portland" - fill_in "Group", with: "" - click_on "Search" + search_using_only_location "Portland" + assert page.has_content? "6 groups found" - assert page.has_content? "6 groups found" - - fill_in "Location", with: "Kyoto" - fill_in "Group", with: "" - click_on "Search" - - assert page.has_content? "1 group found" + search_using_only_location "Kyoto" + assert page.has_content? "1 group found" end test "user can search for a group using only the group's name" do log_in_as(@user) visit root_path - fill_in "Location", with: "" - fill_in "Group", with: "Nike" - click_on "Search" - + search_using_only_group "Nike" assert page.has_content? "1 group found" - fill_in "Location", with: "" - fill_in "Group", with: "Stranger's Group" - click_on "Search" - + search_using_only_group "Stranger's Group" assert page.has_content? "1 group found" end - test "user can search for a group using the city and group's name" do + test "user can search for a group using the location and group's name" do log_in_as(@user) visit root_path - fill_in "Location", with: "Kyoto" - fill_in "Group", with: "Sakura" - click_on "Search" + search location: "Kyoto", group: "Sakura" + assert page.has_content? "1 group found" + search location: "Portland", group: "Woodell's Group" assert page.has_content? "1 group found" end @@ -54,16 +42,10 @@ def setup log_in_as(@user) visit root_path - fill_in "Location", with: "Port" - fill_in "Group", with: "Gr" - click_on "Search" - + search location: "Port", group: "Gr" assert page.has_content? "5 groups found" - fill_in "Location", with: "Kyo" - fill_in "Group", with: "" - click_on "Search" - + search location: "Kyo", group: "" assert page.has_content? "1 group found" end @@ -71,16 +53,26 @@ def setup log_in_as(@user) visit root_path - fill_in "Location", with: "" - fill_in "Group", with: "Sakura" - click_on "Search" - + search location: "", group: "sakura" assert page.has_content? "1 group found" - fill_in "Location", with: "" - fill_in "Group", with: "Stranger" - click_on "Search" - - assert page.has_content? "1 group found" + search location: "portland", group: "" + assert page.has_content? "6 groups found" end + + private + + def search_using_only_location(location) + search location: location, group: "" + end + + def search_using_only_group(group) + search location: "", group: group + end + + def search(location:, group:) + fill_in "Location", with: location + fill_in "Group", with: group + click_on "Search" + end end diff --git a/test/integration/groups_show_test.rb b/test/integration/groups_show_test.rb index cba33793..deaae9c4 100644 --- a/test/integration/groups_show_test.rb +++ b/test/integration/groups_show_test.rb @@ -6,75 +6,70 @@ def setup @penny = users(:penny) @woodell = users(:woodell) @onitsuka = users(:onitsuka) + @group = groups(:one) + + add_group_owner_to_organizers(@group) end test "group owner visits group" do - @group = groups(:one) - add_group_owner_to_organizers - add_to_members(@penny, @woodell) + add_members_to_group(@group, @penny, @woodell) log_in_as(@phil) visit group_path(@group) - assert_group_info_and_image - assert_organizers - assert_members_preview + assert_group_info_and_image(@group) + assert_organizers(@group) + assert_members_preview(@group) refute_membership assert_upcoming_events - assert_members + assert_members(@group) end test "group member visits group" do - @group = groups(:one) - add_group_owner_to_organizers - add_to_members(@penny, @woodell) + add_members_to_group(@group, @penny, @woodell) log_in_as(@penny) visit group_path(@group) - assert_group_info_and_image - assert_organizers - assert_members_preview + assert_group_info_and_image(@group) + assert_organizers(@group) + assert_members_preview(@group) refute_membership assert_upcoming_events - assert_members + assert_members(@group) end test "logged in user visits group" do - user = users(:stranger) - @group = groups(:one) - add_group_owner_to_organizers - add_to_members(@penny) + stranger = users(:stranger) + add_members_to_group(@group, @penny) - log_in_as(user) + log_in_as(stranger) visit group_path(@group) - assert_group_info_and_image - assert_organizers - assert_members_preview_title - assert_membership "Request membership" + assert_group_info_and_image(@group) + assert_organizers(@group) + assert_members_preview_title(@group) + assert_membership_button "Request membership" refute_upcoming_events refute_members end test "logged out user visits group" do - @group = groups(:one) - add_group_owner_to_organizers - add_to_members(@penny) + add_members_to_group(@group, @penny) visit group_path(@group) - assert_group_info_and_image - assert_organizers - assert_members_preview_title + assert_group_info_and_image(@group) + assert_organizers(@group) + assert_members_preview_title(@group) assert_members_count(1) - assert_membership "Log in to request membership" + assert_membership_button "Log in to request membership" refute_upcoming_events refute_members @@ -82,35 +77,25 @@ def setup private - # We need to do this because Rolify doesn't seem to work very well with - # fixtures for scoped roles. - def add_group_owner_to_organizers - @group.owner.add_role(:organizer, @group) - end - - def add_to_members(*users) - users.each { |user| user.add_role(:member, @group) } - end - - def assert_group_info_and_image + def assert_group_info_and_image(group) assert page.has_css? ".group-image" - assert page.has_content? @group.name - assert page.has_content? @group.location - assert page.has_content? @group.description + assert page.has_content? group.name + assert page.has_content? group.location + assert page.has_content? group.description end - def assert_organizers + def assert_organizers(group) assert page.has_content? "Organizer" - assert page.has_link? @group.owner.name + assert page.has_link? group.owner.name end - def assert_members_preview - assert_members_preview_title - assert_members_preview_shows_members + def assert_members_preview(group) + assert_members_preview_title(group) + assert_members_preview_shows_members(group) end - def assert_members_preview_title - count = @group.members_with_role.count + def assert_members_preview_title(group) + count = group.members_with_role.count within ".members-preview" do if count > 1 @@ -123,9 +108,9 @@ def assert_members_preview_title end end - def assert_members_preview_shows_members + def assert_members_preview_shows_members(group) within ".members-preview" do - last_members_names = @group.members_with_role.last(5).map(&:name) + last_members_names = group.members_with_role.last(5).map(&:name) last_members_names.each do |name| assert page.has_link? name end @@ -138,8 +123,8 @@ def assert_members_count(number) end end - def assert_membership(button_text) - assert page.has_css? ".group-membership" + def assert_membership_button(button_text) + assert page.has_css? ".group-membership" assert page.has_content? button_text end @@ -157,10 +142,10 @@ def refute_upcoming_events refute page.has_css? ".event-box" end - def assert_members + def assert_members(group) within ".members-container" do assert page.has_content? "Members" - last_members_names = @group.members.last(12).map(&:name) + last_members_names = group.members.last(12).map(&:name) last_members_names.each do |name| assert page.has_link? name end diff --git a/test/integration/notifications_test.rb b/test/integration/notifications_test.rb index 2d3a5a70..a52cebf9 100644 --- a/test/integration/notifications_test.rb +++ b/test/integration/notifications_test.rb @@ -172,21 +172,15 @@ def setup visit user_notification_settings_url(@onitsuka) - assert page.has_checked_field? "user_membership_request_emails" - assert page.has_checked_field? "user_group_membership_emails" - assert page.has_checked_field? "user_group_role_emails" + assert_checked_fields - uncheck "user_membership_request_emails" - uncheck "user_group_membership_emails" - uncheck "user_group_role_emails" + uncheck_fields click_on "Update" assert page.has_content? "updated" - assert page.has_no_checked_field? "user_membership_request_emails" - assert page.has_no_checked_field? "user_group_membership_emails" - assert page.has_no_checked_field? "user_group_role_emails" + assert_unchecked_fields end private @@ -256,4 +250,30 @@ def assert_regular_notifications click_on "Notifications" refute page.has_content? "There are no notifications" end + + def assert_checked_fields + checkbox_fields.each do |field| + assert page.has_checked_field? field + end + end + + def uncheck_fields + checkbox_fields.each do |field| + uncheck field + end + end + + def assert_unchecked_fields + checkbox_fields.each do |field| + assert page.has_no_checked_field? field + end + end + + def checkbox_fields + [ + "user_membership_request_emails", + "user_group_membership_emails", + "user_group_role_emails" + ] + end end diff --git a/test/integration/users_signup_test.rb b/test/integration/users_signup_test.rb index f2ca6db3..4a52c976 100644 --- a/test/integration/users_signup_test.rb +++ b/test/integration/users_signup_test.rb @@ -6,16 +6,16 @@ class UserSignupTest < ActionDispatch::IntegrationTest click_on "Sign up" - fill_in "Name", with: "Test" - fill_in "Email", with: "test@test.com" - fill_in "Password", with: "password" - fill_in "Password confirmation", with: "password" + fill_in_correct_name + fill_in_correct_email + fill_in_correct_password within "form" do click_on "Sign up" end - assert page.has_content? "A message with a confirmation link has been sent to your email address." + assert page.has_content? "A message with a confirmation link " \ + "has been sent to your email address." end test "sign up with invalid name" do @@ -23,18 +23,15 @@ class UserSignupTest < ActionDispatch::IntegrationTest click_on "Sign up" - fill_in "Name", with: "T" - fill_in "Email", with: "test@test.com" - fill_in "Password", with: "password" - fill_in "Password confirmation", with: "password" + fill_in "Name", with: "T" + fill_in_correct_email + fill_in_correct_password within "form" do click_on "Sign up" end - assert_invalid do - assert page.has_content? "Name is too short" - end + assert_invalid_with_message "Name is too short" end test "sign up with empty name" do @@ -42,18 +39,16 @@ class UserSignupTest < ActionDispatch::IntegrationTest click_on "Sign up" - fill_in "Name", with: "" - fill_in "Email", with: "test@test.com" - fill_in "Password", with: "password" - fill_in "Password confirmation", with: "password" + fill_in "Name", with: "" + fill_in_correct_email + fill_in_correct_password within "form" do click_on "Sign up" end - assert_invalid do - assert page.has_content? "Name can't be blank" - end + assert_invalid_with_message "Name can't be blank" + end test "sign up with invalid email" do @@ -61,18 +56,15 @@ class UserSignupTest < ActionDispatch::IntegrationTest click_on "Sign up" - fill_in "Name", with: "Test" - fill_in "Email", with: "test.test.com" - fill_in "Password", with: "password" - fill_in "Password confirmation", with: "password" + fill_in_correct_name + fill_in "Email", with: "test.test.com" + fill_in_correct_password within "form" do click_on "Sign up" end - assert_invalid do - assert page.has_content? "Email is invalid" - end + assert_invalid_with_message "Email is invalid" end test "sign up with empty email" do @@ -80,18 +72,15 @@ class UserSignupTest < ActionDispatch::IntegrationTest click_on "Sign up" - fill_in "Name", with: "Test" - fill_in "Email", with: "" - fill_in "Password", with: "password" - fill_in "Password confirmation", with: "password" + fill_in_correct_name + fill_in "Email", with: "" + fill_in_correct_password within "form" do click_on "Sign up" end - assert_invalid do - assert page.has_content? "Email can't be blank" - end + assert_invalid_with_message "Email can't be blank" end test "sign up with invalid password" do @@ -99,8 +88,8 @@ class UserSignupTest < ActionDispatch::IntegrationTest click_on "Sign up" - fill_in "Name", with: "Test" - fill_in "Email", with: "test@test.com" + fill_in_correct_name + fill_in_correct_email fill_in "Password", with: "pass" fill_in "Password confirmation", with: "pass" @@ -108,15 +97,27 @@ class UserSignupTest < ActionDispatch::IntegrationTest click_on "Sign up" end - assert_invalid do - assert page.has_content? "Password is too short" - end + assert_invalid_with_message "Password is too short" + end private - def assert_invalid + def fill_in_correct_name + fill_in "Name", with: "Test" + end + + def fill_in_correct_email + fill_in "Email", with: "test@test.com" + end + + def fill_in_correct_password + fill_in "Password", with: "password" + fill_in "Password confirmation", with: "password" + end + + def assert_invalid_with_message(message) assert page.has_content? "error" - yield if block_given? + assert page.has_content? message end end diff --git a/test/mailers/notification_mailer_test.rb b/test/mailers/notification_mailer_test.rb index 879a646a..5975b62e 100644 --- a/test/mailers/notification_mailer_test.rb +++ b/test/mailers/notification_mailer_test.rb @@ -10,80 +10,104 @@ def setup test "#new_membership_request" do email = NotificationMailer.new_membership_request(@user, @group) - assert_equal [@owner.email], email.to - assert_equal ["notifications@letsmeet.com"], email.from - assert_equal "New membership request from " \ - "#{@user.name} in #{@group.name}", email.subject + assert_email_to(email, @owner) + assert_email_from(email) + assert_email_subject(email, "New membership request from " \ + "#{@user.name} in #{@group.name}") assert_match "Hello #{@owner.name},", email.body.encoded assert_match "You have a new membership request from " \ "#{@user.name} in #{@group.name}.", email.body.encoded - assert_match "Go to notifications", email.body.encoded + + assert_notifications_link(email) end test "#declined_membership_request" do email = NotificationMailer.declined_membership_request(@user, @group) - assert_equal [@user.email], email.to - assert_equal ["notifications@letsmeet.com"], email.from - assert_equal "Membership request declined", email.subject + assert_email_to(email, @user) + assert_email_from(email) + assert_email_subject(email, "Membership request declined") assert_match "Hello #{@user.name},", email.body.encoded assert_match "We're sorry to say that your membership request for " \ "#{@group.name} was declined.", email.body.encoded - assert_match "Go to notifications", email.body.encoded + + assert_notifications_link(email) end test "#new_group_membership" do email = NotificationMailer.new_group_membership(@user, @group) - assert_equal [@user.email], email.to - assert_equal ["notifications@letsmeet.com"], email.from - assert_equal "#{@group.name} membership", email.subject + assert_email_to(email, @user) + assert_email_from(email) + assert_email_subject(email, "#{@group.name} membership") assert_match "Congratulations #{@user.name}!", email.body.encoded assert_match "You have been accepted " \ "as a member of #{@group.name}!", email.body.encoded - assert_match "Go to notifications", email.body.encoded + + assert_notifications_link(email) end test "#deleted_group_membership" do email = NotificationMailer.deleted_group_membership(@user, @group) - assert_equal [@user.email], email.to - assert_equal ["notifications@letsmeet.com"], email.from - assert_equal "Your #{@group.name} membership was cancelled", email.subject + assert_email_to(email, @user) + assert_email_from(email) + assert_email_subject(email, "Your #{@group.name} membership was cancelled") assert_match "Hello #{@user.name},", email.body.encoded assert_match "We're sorry to say that your " \ "#{@group.name} membership was cancelled.", email.body.encoded - assert_match "Go to notifications", email.body.encoded + + assert_notifications_link(email) end test "#added_to_organizers" do email = NotificationMailer.added_to_organizers(@user, @group) - assert_equal [@user.email], email.to - assert_equal ["notifications@letsmeet.com"], email.from - assert_equal "You are now an organizer in #{@group.name}!", email.subject + assert_email_to(email, @user) + assert_email_from(email) + assert_email_subject(email, "You are now an organizer in #{@group.name}!") assert_match "Congratulations #{@user.name}!", email.body.encoded assert_match "You are now an organizer in #{@group.name}!", email.body.encoded - assert_match "Go to notifications", email.body.encoded + + assert_notifications_link(email) end test "#deleted_from_organizers" do email = NotificationMailer.deleted_from_organizers(@user, @group) - assert_equal [@user.email], email.to - assert_equal ["notifications@letsmeet.com"], email.from - assert_equal "You are no longer an organizer in #{@group.name}", - email.subject + assert_email_to(email, @user) + assert_email_from(email) + assert_email_subject(email, + "You are no longer an organizer in #{@group.name}") assert_match "Hello #{@user.name},", email.body.encoded assert_match "We're sorry to say that you are no longer " \ "an organizer in #{@group.name}.", email.body.encoded - assert_match "Go to notifications", email.body.encoded + + assert_notifications_link(email) end + + private + + def assert_email_to(email, user) + assert_equal [user.email], email.to + end + + def assert_email_from(email) + assert_equal ["notifications@letsmeet.com"], email.from + end + + def assert_email_subject(email, message) + assert_equal message, email.subject + end + + def assert_notifications_link(email) + assert_match "Go to notifications", email.body.encoded + end end diff --git a/test/models/event_test.rb b/test/models/event_test.rb index a086000a..7f81ca07 100644 --- a/test/models/event_test.rb +++ b/test/models/event_test.rb @@ -1,6 +1,10 @@ require 'test_helper' class EventTest < ActiveSupport::TestCase + def setup + stub_requests_to_googleapis + end + test "is valid" do event = fake_event assert event.valid? @@ -32,12 +36,12 @@ class EventTest < ActiveSupport::TestCase end test "is invalid with a start date that has already passed" do - event = fake_event(start_date: Time.zone.now - 1.day) + event = fake_event(start_date: 1.day.ago) refute event.valid? end test "is invalid with an end date that has already passed" do - event = fake_event(end_date: Time.zone.now - 1.day) + event = fake_event(end_date: 1.day.ago) refute event.valid? end @@ -86,36 +90,4 @@ class EventTest < ActiveSupport::TestCase assert_equal event.group, group end - - private - - def fake_event(params = {}) - @fake_event ||= Event.new( - group: groups(:two), - title: params[:title] || "Test event", - description: params[:description] || Faker::Lorem.paragraph, - website: params[:website] || "", - start_date: params[:start_date] || Time.zone.now + 6.days, - end_date: params[:end_date] || Time.zone.now + 1.week, - image: params[:image] || image, - organizer: users(:phil), - address_attributes: address(params) - ) - end - - def image - File.open(Rails.root.join('test/fixtures/files/sample.jpeg')) - end - - def address(params = {}) - { - place_name: params[:place_name] || "Obento", - street1: params[:street1] || "Matsubara-dori, 8", - street2: params[:street2] || "", - city: params[:city] || "Kyoto", - state: params[:state] || "", - post_code: params[:post_code] || "6050856", - country: params[:country] || "Japan" - } - end end diff --git a/test/models/group_membership_test.rb b/test/models/group_membership_test.rb deleted file mode 100644 index 0f17dd50..00000000 --- a/test/models/group_membership_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class GroupMembershipTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end diff --git a/test/models/group_test.rb b/test/models/group_test.rb index 6e4f8c01..22cd122c 100644 --- a/test/models/group_test.rb +++ b/test/models/group_test.rb @@ -79,8 +79,4 @@ def fake_group(params = {}) params[:all_members_can_create_events] || true ) end - - def valid_image - File.open(Rails.root.join("test/fixtures/files/sample.jpeg")) - end end diff --git a/test/test_helper.rb b/test/test_helper.rb index e42acff4..e47738dd 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,6 +11,10 @@ require 'minitest/autorun' require 'minitest/reporters' + +require 'webmock/minitest' +WebMock.disable_net_connect!(allow_localhost: true) + require 'sucker_punch/testing/inline' Minitest::Reporters.use! [Minitest::Reporters::ProgressReporter.new, Minitest::Reporters::DefaultReporter.new] @@ -20,7 +24,6 @@ config.allow_url("api.tiles.mapbox.com") end - class ActionDispatch::IntegrationTest include Capybara::DSL include Capybara::Minitest::Assertions @@ -85,8 +88,65 @@ def select_date_and_time(date, **options) select date.strftime("%H"), from: "#{field}_4i" # Hour. select date.strftime("%M"), from: "#{field}_5i" # Minutes. end + + def fill_in_description(description) + find("trix-editor").click.set(description) + end + + def attach_valid_image_for(field) + attach_file field, "test/fixtures/files/sample.jpeg" + end + + def upload_valid_image + fixture_file_upload("test/fixtures/files/sample.jpeg", "image/jpeg") + end + + # We need to do this because Rolify doesn't seem to work very well with + # fixtures for scoped roles. + def add_group_owner_to_organizers(group) + group.owner.add_role(:organizer, group) + end + + def add_members_to_group(group, *users) + users.each { |user| user.add_role(:member, group) } + end end class ActiveSupport::TestCase fixtures :all + + def stub_requests_to_googleapis + WebMock.stub_request(:get, /maps.googleapis.com/) + .to_return(status: 200, body: "", headers: {}) + end + + def fake_event(params = {}) + Event.new( + group: params[:group] || groups(:two), + title: params[:title] || "Test event", + description: params[:description] || Faker::Lorem.paragraph, + website: params[:website] || "", + start_date: params[:start_date] || 6.days.from_now, + end_date: params[:end_date] || 1.week.from_now, + image: params[:image] || valid_image, + organizer: params[:user] || users(:phil), + address_attributes: address(params) + ) + end + + def address(params = {}) + { + place_name: params[:place_name] || "Obento", + street1: params[:street1] || "Matsubara-dori, 8", + street2: params[:street2] || "", + city: params[:city] || "Kyoto", + state: params[:state] || "", + post_code: params[:post_code] || "6050856", + country: params[:country] || "Japan" + } + end + + def valid_image + File.open(Rails.root.join("test/fixtures/files/sample.jpeg")) + end end