]> git.openstreetmap.org Git - rails.git/blob - test/system/create_note_test.rb
Merge pull request #6394 from openstreetmap/dependabot/github_actions/ruby/setup...
[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 10 created notes and disappears after login" do
104     check_encouragement_while_creating_notes(10)
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 10 created notes and disappears after email signup" do
112     check_encouragement_while_creating_notes(10)
113
114     sign_up_with_email
115
116     check_no_encouragement_while_logging_out
117   end
118
119   test "encouragement to contribute appears after 10 created notes and disappears after google signup" do
120     check_encouragement_while_creating_notes(10)
121
122     sign_up_with_google
123
124     check_no_encouragement_while_logging_out
125   end
126
127   private
128
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}")
132
133       within_sidebar do
134         assert_no_content(/already posted at least \d+ anonymous note/)
135
136         fill_in "text", :with => "new note ##{n + 1}"
137         click_on "Add Note"
138
139         assert_content "new note ##{n + 1}"
140       end
141     end
142
143     visit new_note_path(:anchor => "map=16/0/#{0.001 * encouragement_threshold}")
144
145     within_sidebar do
146       assert_content(/already posted at least #{encouragement_threshold} anonymous note/)
147     end
148   end
149
150   def check_no_encouragement_while_logging_out
151     visit new_note_path(:anchor => "map=16/0/0")
152
153     within_sidebar do
154       assert_no_content(/already posted at least \d+ anonymous note/)
155     end
156
157     sign_out
158     visit new_note_path(:anchor => "map=16/0/0")
159
160     within_sidebar do
161       assert_no_content(/already posted at least \d+ anonymous note/)
162     end
163   end
164
165   def sign_up_with_email
166     click_on "Sign Up"
167
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"
173
174       assert_emails 1 do
175         click_on "Sign Up"
176       end
177     end
178
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)
182     assert_not_nil match
183
184     visit match[0]
185
186     assert_content "Welcome!"
187   end
188
189   def sign_up_with_google
190     OmniAuth.config.add_mock(:google,
191                              :uid => "123454321",
192                              :extra => { :id_info => { :openid_id => "http://localhost:1123/new.tester" } },
193                              :info => { :email => "google_user_account@example.com", :name => "google_user_account" })
194
195     click_on "Sign Up"
196
197     within_content_body do
198       click_on "Log in with Google"
199       click_on "Sign Up"
200     end
201   end
202 end