]> git.openstreetmap.org Git - rails.git/blob - test/integration/user_terms_seen_test.rb
d4d7450fb545eeb994203917741cb70091d623b8
[rails.git] / test / integration / user_terms_seen_test.rb
1 require "test_helper"
2
3 class UserTermsSeenTest < ActionDispatch::IntegrationTest
4   fixtures :users
5
6   setup do
7     stub_signup_requests
8   end
9
10   def test_api_blocked
11     with_terms_seen(true) do
12       user = users(:terms_not_seen_user)
13
14       get "/api/#{API_VERSION}/user/preferences", nil, auth_header(user.display_name, "test")
15       assert_response :forbidden
16
17       # touch it so that the user has seen the terms
18       user.terms_seen = true
19       user.save
20
21       get "/api/#{API_VERSION}/user/preferences", nil, auth_header(user.display_name, "test")
22       assert_response :success
23     end
24   end
25
26   def test_terms_presented_at_login
27     with_terms_seen(true) do
28       user = users(:terms_not_seen_user)
29
30       # try to log in
31       get_via_redirect "/login"
32       assert_response :success
33       assert_template "user/login"
34       post "/login", :username => user.email, :password => "test", :referer => "/diary/new"
35       assert_response :redirect
36       # but now we need to look at the terms
37       assert_redirected_to :controller => :user, :action => :terms, :referer => "/diary/new"
38       follow_redirect!
39       assert_response :success
40
41       # don't agree to the terms, but hit decline
42       post "/user/save", :decline => true, :referer => "/diary/new"
43       assert_redirected_to "/diary/new"
44       follow_redirect!
45
46       # should be carried through to a normal login with a message
47       assert_response :success
48       assert !flash[:notice].nil?
49     end
50   end
51
52   def test_terms_cant_be_circumvented
53     with_terms_seen(true) do
54       user = users(:terms_not_seen_user)
55
56       # try to log in
57       get_via_redirect "/login"
58       assert_response :success
59       assert_template "user/login"
60       post "/login", :username => user.email, :password => "test", :referer => "/diary/new"
61       assert_response :redirect
62       # but now we need to look at the terms
63       assert_redirected_to :controller => :user, :action => :terms, :referer => "/diary/new"
64
65       # check that if we go somewhere else now, it redirects
66       # back to the terms page.
67       get "/traces/mine"
68       assert_redirected_to :controller => :user, :action => :terms, :referer => "/traces/mine"
69       get "/traces/mine", :referer => "/diary/new"
70       assert_redirected_to :controller => :user, :action => :terms, :referer => "/diary/new"
71     end
72   end
73
74   private
75
76   def auth_header(user, pass)
77     { "HTTP_AUTHORIZATION" => format("Basic %s", Base64.encode64("#{user}:#{pass}")) }
78   end
79
80   def with_terms_seen(value)
81     require_terms_seen = Object.send("remove_const", "REQUIRE_TERMS_SEEN")
82     Object.const_set("REQUIRE_TERMS_SEEN", value)
83
84     yield
85
86     Object.send("remove_const", "REQUIRE_TERMS_SEEN")
87     Object.const_set("REQUIRE_TERMS_SEEN", require_terms_seen)
88   end
89 end