1 # frozen_string_literal: true
 
   3 require "application_system_test_case"
 
   5 class CreateNoteTest < ApplicationSystemTestCase
 
   6   include ActionMailer::TestHelper
 
   9     OmniAuth.config.test_mode = true
 
  11     stub_request(:get, /.*gravatar.com.*d=404/).to_return(:status => 404)
 
  15     OmniAuth.config.mock_auth[:google] = nil
 
  16     OmniAuth.config.test_mode = false
 
  19   test "can create note" do
 
  20     visit new_note_path(:anchor => "map=18/0/0")
 
  23       assert_button "Add Note", :disabled => true
 
  25       fill_in "text", :with => "Some newly added note description"
 
  28       assert_content "Unresolved note #"
 
  29       assert_content "Some newly added note description"
 
  33   test "cannot create new note when zoomed out" do
 
  34     visit new_note_path(:anchor => "map=12/0/0")
 
  37       assert_no_content "Zoom in to add a note"
 
  38       assert_button "Add Note", :disabled => true
 
  40       fill_in "text", :with => "Some newly added note description"
 
  42       assert_no_content "Zoom in to add a note"
 
  43       assert_button "Add Note", :disabled => false
 
  51       assert_content "Zoom in to add a note"
 
  52       assert_button "Add Note", :disabled => true
 
  60       assert_no_content "Zoom in to add a note"
 
  61       assert_button "Add Note", :disabled => false
 
  65       assert_content "Unresolved note #"
 
  66       assert_content "Some newly added note description"
 
  70   test "can open new note page when zoomed out" do
 
  71     visit new_note_path(:anchor => "map=11/0/0")
 
  74       assert_content "Zoom in to add a note"
 
  75       assert_button "Add Note", :disabled => true
 
  77       fill_in "text", :with => "Some newly added note description"
 
  79       assert_content "Zoom in to add a note"
 
  80       assert_button "Add Note", :disabled => true
 
  88       assert_no_content "Zoom in to add a note"
 
  89       assert_button "Add Note", :disabled => false
 
  93   test "cannot create note when api is readonly" do
 
  94     with_settings(:status => "api_readonly") do
 
  95       visit new_note_path(:anchor => "map=18/0/0")
 
  98         assert_no_button "Add Note", :disabled => true
 
 103   test "encouragement to contribute appears after 10 created notes and disappears after login" do
 
 104     check_encouragement_while_creating_notes(10)
 
 106     sign_in_as(create(:user))
 
 108     check_no_encouragement_while_logging_out
 
 111   test "encouragement to contribute appears after 10 created notes and disappears after email signup" do
 
 112     check_encouragement_while_creating_notes(10)
 
 116     check_no_encouragement_while_logging_out
 
 119   test "encouragement to contribute appears after 10 created notes and disappears after google signup" do
 
 120     check_encouragement_while_creating_notes(10)
 
 124     check_no_encouragement_while_logging_out
 
 129   def check_encouragement_while_creating_notes(encouragement_threshold)
 
 130     encouragement_threshold.times do |n|
 
 131       visit new_note_path(:anchor => "map=16/0/#{0.001 * n}")
 
 134         assert_no_content(/already posted at least \d+ anonymous note/)
 
 136         fill_in "text", :with => "new note ##{n + 1}"
 
 139         assert_content "new note ##{n + 1}"
 
 143     visit new_note_path(:anchor => "map=16/0/#{0.001 * encouragement_threshold}")
 
 146       assert_content(/already posted at least #{encouragement_threshold} anonymous note/)
 
 150   def check_no_encouragement_while_logging_out
 
 151     visit new_note_path(:anchor => "map=16/0/0")
 
 154       assert_no_content(/already posted at least \d+ anonymous note/)
 
 158     visit new_note_path(:anchor => "map=16/0/0")
 
 161       assert_no_content(/already posted at least \d+ anonymous note/)
 
 165   def sign_up_with_email
 
 168     within_content_body do
 
 169       fill_in "Email", :with => "new_user_account@example.com"
 
 170       fill_in "Display Name", :with => "new_user_account"
 
 171       fill_in "Password", :with => "new_user_password"
 
 172       fill_in "Confirm Password", :with => "new_user_password"
 
 179     email = ActionMailer::Base.deliveries.first
 
 180     email_text = email.parts[0].parts[0].decoded
 
 181     match = %r{/user/new_user_account/confirm\?confirm_string=\S+}.match(email_text)
 
 186     assert_content "Welcome!"
 
 189   def sign_up_with_google
 
 190     OmniAuth.config.add_mock(:google,
 
 192                              :extra => { :id_info => { :openid_id => "http://localhost:1123/new.tester" } },
 
 193                              :info => { :email => "google_user_account@example.com", :name => "google_user_account" })
 
 197     within_content_body do
 
 198       click_on "Log in with Google"