]> git.openstreetmap.org Git - rails.git/blob - test/system/create_note_test.rb
Merge remote-tracking branch 'upstream/pull/7190'
[rails.git] / test / system / create_note_test.rb
1 # frozen_string_literal: true
2
3 require "application_system_test_case"
4
5 class CreateNoteTest < ApplicationSystemTestCase
6   include ActionMailer::TestHelper
7
8   def setup
9     OmniAuth.config.test_mode = true
10
11     stub_request(:get, /.*gravatar.com.*d=404/).to_return(:status => 404)
12   end
13
14   def teardown
15     OmniAuth.config.mock_auth[:google] = nil
16     OmniAuth.config.test_mode = false
17   end
18
19   test "can create note" do
20     visit new_note_path(:anchor => "map=18/0/0")
21
22     within_sidebar do
23       assert_button "Add Note", :disabled => true
24
25       fill_in "text", :with => "Some newly added note description"
26       click_on "Add Note"
27
28       assert_content "Unresolved note #"
29       assert_content "Some newly added note description"
30     end
31   end
32
33   test "cannot create new note when zoomed out" do
34     visit new_note_path(:anchor => "map=12/0/0")
35
36     within_sidebar do
37       assert_no_content "Zoom in to add a note"
38       assert_button "Add Note", :disabled => true
39
40       fill_in "text", :with => "Some newly added note description"
41
42       assert_no_content "Zoom in to add a note"
43       assert_button "Add Note", :disabled => false
44     end
45
46     within "#map" do
47       click_on "Zoom Out"
48     end
49
50     within_sidebar do
51       assert_content "Zoom in to add a note"
52       assert_button "Add Note", :disabled => true
53     end
54
55     within "#map" do
56       click_on "Zoom In"
57     end
58
59     within_sidebar do
60       assert_no_content "Zoom in to add a note"
61       assert_button "Add Note", :disabled => false
62
63       click_on "Add Note"
64
65       assert_content "Unresolved note #"
66       assert_content "Some newly added note description"
67     end
68   end
69
70   test "can open new note page when zoomed out" do
71     visit new_note_path(:anchor => "map=11/0/0")
72
73     within_sidebar do
74       assert_content "Zoom in to add a note"
75       assert_button "Add Note", :disabled => true
76
77       fill_in "text", :with => "Some newly added note description"
78
79       assert_content "Zoom in to add a note"
80       assert_button "Add Note", :disabled => true
81     end
82
83     within "#map" do
84       click_on "Zoom In"
85     end
86
87     within_sidebar do
88       assert_no_content "Zoom in to add a note"
89       assert_button "Add Note", :disabled => false
90     end
91   end
92
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")
96
97       within_sidebar do
98         assert_no_button "Add Note", :disabled => true
99       end
100     end
101   end
102
103   test "encouragement to contribute appears after 5 created notes and disappears after login" do
104     check_encouragement_while_creating_notes(5)
105
106     sign_in_as(create(:user))
107
108     check_no_encouragement_while_logging_out
109   end
110
111   test "encouragement to contribute appears after 5 created notes and disappears after email signup" do
112     check_encouragement_while_creating_notes(5)
113
114     sign_up_with_email
115
116     check_no_encouragement_while_logging_out
117   end
118
119   test "encouragement to contribute appears after 5 created notes and disappears after google signup" do
120     check_encouragement_while_creating_notes(5)
121
122     sign_up_with_google
123
124     check_no_encouragement_while_logging_out
125   end
126
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)
129
130     sign_in_as(create(:user))
131
132     check_no_encouragement_while_logging_out
133   end
134
135   test "shows a message when there is an error creating a note" do
136     visit new_note_path(:anchor => "map=18/0/0")
137
138     execute_script <<~SCRIPT
139       window.fetch = (...args) => {
140         return Promise.reject("The test injected a forced error");
141       }
142     SCRIPT
143
144     within_sidebar do
145       fill_in "text", :with => "Some newly added note description"
146       click_on "Add Note"
147
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
152     end
153   end
154
155   private
156
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}")
160
161       within_sidebar do
162         assert_no_content(/already posted at least \d+ anonymous note/)
163
164         fill_in "text", :with => "new note ##{n + 1}"
165         click_on "Add Note"
166
167         assert_content "new note ##{n + 1}"
168       end
169     end
170
171     visit new_note_path(:anchor => "map=16/0/#{0.001 * encouragement_threshold}")
172
173     within_sidebar do
174       assert_content(/already posted at least #{encouragement_threshold} anonymous note/)
175     end
176   end
177
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}")
181
182       within_sidebar do
183         if n < encouragement_threshold
184           assert_no_content(/already posted at least \d+ anonymous note/)
185         else
186           assert_content(/already posted at least \d+ anonymous note/)
187         end
188
189         fill_in "text", :with => "new note ##{n + 1}"
190         click_on "Add Note"
191
192         assert_content "new note ##{n + 1}"
193       end
194     end
195
196     visit new_note_path(:anchor => "map=16/0/#{0.001 * strict_encouragement_threshold}")
197
198     within_sidebar do
199       assert_content(/already posted at least #{strict_encouragement_threshold} anonymous note/)
200       assert_no_button "Add Note"
201     end
202   end
203
204   def check_no_encouragement_while_logging_out
205     visit new_note_path(:anchor => "map=16/0/0")
206
207     within_sidebar do
208       assert_no_content(/already posted at least \d+ anonymous note/)
209     end
210
211     sign_out
212     visit new_note_path(:anchor => "map=16/0/0")
213
214     within_sidebar do
215       assert_no_content(/already posted at least \d+ anonymous note/)
216     end
217   end
218
219   def sign_up_with_email
220     click_on "Sign Up"
221
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"
227
228       assert_emails 1 do
229         click_on "Sign Up"
230       end
231     end
232
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)
236     assert_not_nil match
237
238     visit match[0]
239
240     assert_content "Welcome!"
241   end
242
243   def sign_up_with_google
244     OmniAuth.config.add_mock(:google,
245                              :uid => "123454321",
246                              :extra => { :id_info => { :openid_id => "http://localhost:1123/new.tester" } },
247                              :info => { :email => "google_user_account@example.com", :name => "google_user_account" })
248
249     click_on "Sign Up"
250
251     within_content_body do
252       click_on "Log in with Google"
253       click_on "Sign Up"
254     end
255   end
256 end