3 class UserTermsSeenTest < ActionDispatch::IntegrationTest
9 with_terms_seen(true) do
10 user = create(:user, :terms_seen => false)
12 get "/api/#{API_VERSION}/user/preferences", nil, auth_header(user.display_name, "test")
13 assert_response :forbidden
15 # touch it so that the user has seen the terms
16 user.terms_seen = true
19 get "/api/#{API_VERSION}/user/preferences", nil, auth_header(user.display_name, "test")
20 assert_response :success
24 def test_terms_presented_at_login
25 with_terms_seen(true) do
26 user = create(:user, :terms_seen => false)
29 get_via_redirect "/login"
30 assert_response :success
31 assert_template "user/login"
32 post "/login", :username => user.email, :password => "test", :referer => "/diary/new"
33 assert_response :redirect
34 # but now we need to look at the terms
35 assert_redirected_to :controller => :user, :action => :terms, :referer => "/diary/new"
37 assert_response :success
39 # don't agree to the terms, but hit decline
40 post "/user/save", :decline => true, :referer => "/diary/new"
41 assert_redirected_to "/diary/new"
44 # should be carried through to a normal login with a message
45 assert_response :success
46 assert !flash[:notice].nil?
50 def test_terms_cant_be_circumvented
51 with_terms_seen(true) do
52 user = create(:user, :terms_seen => false)
55 get_via_redirect "/login"
56 assert_response :success
57 assert_template "user/login"
58 post "/login", :username => user.email, :password => "test", :referer => "/diary/new"
59 assert_response :redirect
60 # but now we need to look at the terms
61 assert_redirected_to :controller => :user, :action => :terms, :referer => "/diary/new"
63 # check that if we go somewhere else now, it redirects
64 # back to the terms page.
66 assert_redirected_to :controller => :user, :action => :terms, :referer => "/traces/mine"
67 get "/traces/mine", :referer => "/diary/new"
68 assert_redirected_to :controller => :user, :action => :terms, :referer => "/diary/new"
74 def auth_header(user, pass)
75 { "HTTP_AUTHORIZATION" => format("Basic %s", Base64.encode64("#{user}:#{pass}")) }
78 def with_terms_seen(value)
79 require_terms_seen = Object.send("remove_const", "REQUIRE_TERMS_SEEN")
80 Object.const_set("REQUIRE_TERMS_SEEN", value)
84 Object.send("remove_const", "REQUIRE_TERMS_SEEN")
85 Object.const_set("REQUIRE_TERMS_SEEN", require_terms_seen)