]> git.openstreetmap.org Git - rails.git/blob - test/system/create_note_test.rb
Merge remote-tracking branch 'upstream/pull/6798'
[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   private
136
137   def check_encouragement_while_creating_notes(encouragement_threshold)
138     encouragement_threshold.times do |n|
139       visit new_note_path(:anchor => "map=16/0/#{0.001 * n}")
140
141       within_sidebar do
142         assert_no_content(/already posted at least \d+ anonymous note/)
143
144         fill_in "text", :with => "new note ##{n + 1}"
145         click_on "Add Note"
146
147         assert_content "new note ##{n + 1}"
148       end
149     end
150
151     visit new_note_path(:anchor => "map=16/0/#{0.001 * encouragement_threshold}")
152
153     within_sidebar do
154       assert_content(/already posted at least #{encouragement_threshold} anonymous note/)
155     end
156   end
157
158   def check_strict_encouragement_while_creating_notes(encouragement_threshold, strict_encouragement_threshold)
159     strict_encouragement_threshold.times do |n|
160       visit new_note_path(:anchor => "map=16/0/#{0.001 * n}")
161
162       within_sidebar do
163         if n < encouragement_threshold
164           assert_no_content(/already posted at least \d+ anonymous note/)
165         else
166           assert_content(/already posted at least \d+ anonymous note/)
167         end
168
169         fill_in "text", :with => "new note ##{n + 1}"
170         click_on "Add Note"
171
172         assert_content "new note ##{n + 1}"
173       end
174     end
175
176     visit new_note_path(:anchor => "map=16/0/#{0.001 * strict_encouragement_threshold}")
177
178     within_sidebar do
179       assert_content(/already posted at least #{strict_encouragement_threshold} anonymous note/)
180       assert_no_button "Add Note"
181     end
182   end
183
184   def check_no_encouragement_while_logging_out
185     visit new_note_path(:anchor => "map=16/0/0")
186
187     within_sidebar do
188       assert_no_content(/already posted at least \d+ anonymous note/)
189     end
190
191     sign_out
192     visit new_note_path(:anchor => "map=16/0/0")
193
194     within_sidebar do
195       assert_no_content(/already posted at least \d+ anonymous note/)
196     end
197   end
198
199   def sign_up_with_email
200     click_on "Sign Up"
201
202     within_content_body do
203       fill_in "Email", :with => "new_user_account@example.com"
204       fill_in "Display Name", :with => "new_user_account"
205       fill_in "Password", :with => "new_user_password"
206       fill_in "Confirm Password", :with => "new_user_password"
207
208       assert_emails 1 do
209         click_on "Sign Up"
210       end
211     end
212
213     email = ActionMailer::Base.deliveries.first
214     email_text = email.parts[0].parts[0].decoded
215     match = %r{/user/new_user_account/confirm\?confirm_string=\S+}.match(email_text)
216     assert_not_nil match
217
218     visit match[0]
219
220     assert_content "Welcome!"
221   end
222
223   def sign_up_with_google
224     OmniAuth.config.add_mock(:google,
225                              :uid => "123454321",
226                              :extra => { :id_info => { :openid_id => "http://localhost:1123/new.tester" } },
227                              :info => { :email => "google_user_account@example.com", :name => "google_user_account" })
228
229     click_on "Sign Up"
230
231     within_content_body do
232       click_on "Log in with Google"
233       click_on "Sign Up"
234     end
235   end
236 end