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 5 created notes and disappears after login" do
104 check_encouragement_while_creating_notes(5)
106 sign_in_as(create(:user))
108 check_no_encouragement_while_logging_out
111 test "encouragement to contribute appears after 5 created notes and disappears after email signup" do
112 check_encouragement_while_creating_notes(5)
116 check_no_encouragement_while_logging_out
119 test "encouragement to contribute appears after 5 created notes and disappears after google signup" do
120 check_encouragement_while_creating_notes(5)
124 check_no_encouragement_while_logging_out
127 test "strict encouragement to contribute appears after 10 created notes and disappears after login" do
128 check_strict_encouragement_while_creating_notes(5, 10)
130 sign_in_as(create(:user))
132 check_no_encouragement_while_logging_out
135 test "shows a message when there is an error creating a note" do
136 visit new_note_path(:anchor => "map=18/0/0")
138 execute_script <<~SCRIPT
139 window.fetch = (...args) => {
140 return Promise.reject("The test injected a forced error");
145 fill_in "text", :with => "Some newly added note description"
148 assert_content "There was an error when creating the note"
149 find(".new-note-error summary").click
150 assert_content "The test injected a forced error"
151 assert_button "Add Note", :disabled => false
157 def check_encouragement_while_creating_notes(encouragement_threshold)
158 encouragement_threshold.times do |n|
159 visit new_note_path(:anchor => "map=16/0/#{0.001 * n}")
162 assert_no_content(/already posted at least \d+ anonymous note/)
164 fill_in "text", :with => "new note ##{n + 1}"
167 assert_content "new note ##{n + 1}"
171 visit new_note_path(:anchor => "map=16/0/#{0.001 * encouragement_threshold}")
174 assert_content(/already posted at least #{encouragement_threshold} anonymous note/)
178 def check_strict_encouragement_while_creating_notes(encouragement_threshold, strict_encouragement_threshold)
179 strict_encouragement_threshold.times do |n|
180 visit new_note_path(:anchor => "map=16/0/#{0.001 * n}")
183 if n < encouragement_threshold
184 assert_no_content(/already posted at least \d+ anonymous note/)
186 assert_content(/already posted at least \d+ anonymous note/)
189 fill_in "text", :with => "new note ##{n + 1}"
192 assert_content "new note ##{n + 1}"
196 visit new_note_path(:anchor => "map=16/0/#{0.001 * strict_encouragement_threshold}")
199 assert_content(/already posted at least #{strict_encouragement_threshold} anonymous note/)
200 assert_no_button "Add Note"
204 def check_no_encouragement_while_logging_out
205 visit new_note_path(:anchor => "map=16/0/0")
208 assert_no_content(/already posted at least \d+ anonymous note/)
212 visit new_note_path(:anchor => "map=16/0/0")
215 assert_no_content(/already posted at least \d+ anonymous note/)
219 def sign_up_with_email
222 within_content_body do
223 fill_in "Email", :with => "new_user_account@example.com"
224 fill_in "Display Name", :with => "new_user_account"
225 fill_in "Password", :with => "new_user_password"
226 fill_in "Confirm Password", :with => "new_user_password"
233 email = ActionMailer::Base.deliveries.first
234 email_text = email.parts[0].parts[0].decoded
235 match = %r{/user/new_user_account/confirm\?confirm_string=\S+}.match(email_text)
240 assert_content "Welcome!"
243 def sign_up_with_google
244 OmniAuth.config.add_mock(:google,
246 :extra => { :id_info => { :openid_id => "http://localhost:1123/new.tester" } },
247 :info => { :email => "google_user_account@example.com", :name => "google_user_account" })
251 within_content_body do
252 click_on "Log in with Google"