2659: add text message "You are viewing public data" to top nav when anonymous user...
[arvados.git] / apps / workbench / test / integration / anonymous_access_test.rb
1 require 'integration_helper'
2
3 class AnonymousAccessTest < ActionDispatch::IntegrationTest
4   # These tests don't do state-changing API calls. Save some time by
5   # skipping the database reset.
6   reset_api_fixtures :after_each_test, false
7   reset_api_fixtures :after_suite, true
8
9   setup do
10     need_javascript
11   end
12
13   def verify_homepage_anonymous_enabled user, is_active, has_profile
14     if user
15       if user['is_active']
16         if has_profile
17           assert_text 'Unrestricted public data'
18           assert_selector 'a', text: 'Projects'
19         else
20           assert_text 'All required fields must be completed before you can proceed'
21         end
22       else
23         assert_text 'indicate that you have read and accepted the user agreement'
24       end
25       within('.navbar-fixed-top') do
26         assert_no_text 'You are viewing public data'
27         assert_selector 'a', text: "#{user['email']}"
28         find('a', text: "#{user['email']}").click
29         within('.dropdown-menu') do
30           assert_selector 'a', text: 'Log out'
31         end
32       end
33     else
34       assert_text 'Unrestricted public data'
35       within('.navbar-fixed-top') do
36         assert_text 'You are viewing public data'
37         anonymous_user = api_fixture('users')['anonymous']
38         assert_selector 'a', "#{anonymous_user['email']}"
39         find('a', text: "#{anonymous_user['email']}").click
40         within('.dropdown-menu') do
41           assert_selector 'a', text: 'Log out'
42           #assert_selector 'a', 'Log in'
43         end
44       end
45     end
46   end
47
48   [
49     [nil, nil, false, false],
50     ['inactive', api_fixture('users')['inactive'], false, false],
51     ['active', api_fixture('users')['active'], true, true],
52     ['active_no_prefs_profile', api_fixture('users')['active_no_prefs_profile'], true, false],
53     ['admin', api_fixture('users')['admin'], true, true],
54   ].each do |token, user, is_active, has_profile|
55     test "visit public project as user #{token} when anonymous browsing is enabled" do
56       Rails.configuration.anonymous_user_token = api_fixture('api_client_authorizations')['anonymous']['api_token']
57
58       path = "/projects/#{api_fixture('groups')['anonymously_accessible_project']['uuid']}/?public_data=true"
59
60       if !token
61         visit path
62       else
63         visit page_with_token(token, path)
64       end
65       verify_homepage_anonymous_enabled user, is_active, has_profile
66     end
67   end
68
69   [
70     [nil, nil],
71     ['active', api_fixture('users')['active']],
72   ].each do |token, user, is_active|
73     test "visit public project as user #{token} when anonymous browsing is not enabled" do
74       Rails.configuration.anonymous_user_token = false
75
76       path = "/projects/#{api_fixture('groups')['anonymously_accessible_project']['uuid']}/?public_data=true"
77       if !token
78         visit path
79       else
80         visit page_with_token(token, path)
81       end
82
83       if user
84         assert_text 'Unrestricted public data'
85       else
86         assert_text 'Please log in'
87       end
88     end
89   end
90
91   test "visit non-public project as anonymous when anonymous browsing is enabled and expect page not found" do
92     Rails.configuration.anonymous_user_token = api_fixture('api_client_authorizations')['anonymous']['api_token']
93     visit "/projects/#{api_fixture('groups')['aproject']['uuid']}/?public_data=true"
94     assert_text 'Not Found'
95   end
96 end