3 class UserTermsSeenTest < ActionDispatch::IntegrationTest
7 with_terms_seen(true) do
8 user = users(:terms_not_seen_user)
10 get "/api/#{API_VERSION}/user/preferences", nil, auth_header(user.display_name, "test")
11 assert_response :forbidden
13 # touch it so that the user has seen the terms
14 user.terms_seen = true
17 get "/api/#{API_VERSION}/user/preferences", nil, auth_header(user.display_name, "test")
18 assert_response :success
22 def test_terms_presented_at_login
23 with_terms_seen(true) do
24 user = users(:terms_not_seen_user)
27 get_via_redirect "/login"
28 assert_response :success
29 assert_template "user/login"
30 post "/login", :username => user.email, :password => "test", :referer => "/diary/new"
31 assert_response :redirect
32 # but now we need to look at the terms
33 assert_redirected_to :controller => :user, :action => :terms, :referer => "/diary/new"
35 assert_response :success
37 # don't agree to the terms, but hit decline
38 post "/user/save", :decline => true, :referer => "/diary/new"
39 assert_redirected_to "/diary/new"
42 # should be carried through to a normal login with a message
43 assert_response :success
44 assert !flash[:notice].nil?
48 def test_terms_cant_be_circumvented
49 with_terms_seen(true) do
50 user = users(:terms_not_seen_user)
53 get_via_redirect "/login"
54 assert_response :success
55 assert_template "user/login"
56 post "/login", :username => user.email, :password => "test", :referer => "/diary/new"
57 assert_response :redirect
58 # but now we need to look at the terms
59 assert_redirected_to :controller => :user, :action => :terms, :referer => "/diary/new"
61 # check that if we go somewhere else now, it redirects
62 # back to the terms page.
64 assert_redirected_to :controller => :user, :action => :terms, :referer => "/traces/mine"
65 get "/traces/mine", :referer => "/diary/new"
66 assert_redirected_to :controller => :user, :action => :terms, :referer => "/diary/new"
72 def auth_header(user, pass)
73 { "HTTP_AUTHORIZATION" => format("Basic %s", Base64.encode64("#{user}:#{pass}")) }
76 def with_terms_seen(value)
77 require_terms_seen = Object.send("remove_const", "REQUIRE_TERMS_SEEN")
78 Object.const_set("REQUIRE_TERMS_SEEN", value)
82 Object.send("remove_const", "REQUIRE_TERMS_SEEN")
83 Object.const_set("REQUIRE_TERMS_SEEN", require_terms_seen)