any_login 1.6.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/any_login/application_controller.rb +9 -0
  3. data/app/views/any_login/_any_login.html.erb +1 -0
  4. data/app/views/any_login/assets/_css.html.erb +73 -1
  5. data/app/views/any_login/assets/_js.html.erb +205 -1
  6. data/config/routes.rb +1 -0
  7. data/lib/any_login/helpers.rb +31 -4
  8. data/lib/any_login/version.rb +1 -1
  9. data/lib/any_login.rb +24 -0
  10. data/lib/generators/any_login/templates/any_login.rb +3 -0
  11. data/test/authlogic/integration/navigation_test.rb +1 -0
  12. data/test/clearance/integration/navigation_test.rb +1 -0
  13. data/test/devise/integration/navigation_test.rb +1 -0
  14. data/test/devise/integration/search_test.rb +169 -0
  15. data/test/devise/unit/collection_test.rb +2 -2
  16. data/test/devise/unit/helpers_test.rb +14 -0
  17. data/test/devise/unit/search_test.rb +52 -0
  18. data/test/rails_apps/authlogic/db/schema.rb +3 -5
  19. data/test/rails_apps/clearance/db/schema.rb +10 -12
  20. data/test/rails_apps/devise/db/schema.rb +7 -9
  21. data/test/rails_apps/devise/db/test.sqlite3 +0 -0
  22. data/test/rails_apps/devise/log/test.log +519 -1999
  23. data/test/rails_apps/devise/tmp/local_secret.txt +1 -1
  24. data/test/rails_apps/sorcery/db/schema.rb +3 -5
  25. data/test/sorcery/integration/navigation_test.rb +1 -0
  26. metadata +27 -68
  27. data/test/rails_apps/authlogic/db/development.sqlite3 +0 -0
  28. data/test/rails_apps/authlogic/db/test.sqlite3 +0 -0
  29. data/test/rails_apps/authlogic/db/test.sqlite3-shm +0 -0
  30. data/test/rails_apps/authlogic/db/test.sqlite3-wal +0 -0
  31. data/test/rails_apps/authlogic/log/development.log +0 -438
  32. data/test/rails_apps/authlogic/log/test.log +0 -1144
  33. data/test/rails_apps/authlogic/tmp/development_secret.txt +0 -1
  34. data/test/rails_apps/authlogic/tmp/local_secret.txt +0 -1
  35. data/test/rails_apps/clearance/db/development.sqlite3 +0 -0
  36. data/test/rails_apps/clearance/db/test.sqlite3 +0 -0
  37. data/test/rails_apps/clearance/db/test.sqlite3-shm +0 -0
  38. data/test/rails_apps/clearance/db/test.sqlite3-wal +0 -0
  39. data/test/rails_apps/clearance/log/development.log +0 -388
  40. data/test/rails_apps/clearance/log/test.log +0 -1110
  41. data/test/rails_apps/clearance/tmp/development_secret.txt +0 -1
  42. data/test/rails_apps/clearance/tmp/local_secret.txt +0 -1
  43. data/test/rails_apps/devise/db/development.sqlite3 +0 -0
  44. data/test/rails_apps/devise/db/test.sqlite3-shm +0 -0
  45. data/test/rails_apps/devise/db/test.sqlite3-wal +0 -0
  46. data/test/rails_apps/devise/log/development.log +0 -1081
  47. data/test/rails_apps/devise/tmp/development_secret.txt +0 -1
  48. data/test/rails_apps/sorcery/db/development.sqlite3 +0 -0
  49. data/test/rails_apps/sorcery/db/test.sqlite3 +0 -0
  50. data/test/rails_apps/sorcery/db/test.sqlite3-shm +0 -0
  51. data/test/rails_apps/sorcery/db/test.sqlite3-wal +0 -0
  52. data/test/rails_apps/sorcery/log/development.log +0 -247
  53. data/test/rails_apps/sorcery/log/test.log +0 -532
  54. data/test/rails_apps/sorcery/tmp/development_secret.txt +0 -1
  55. data/test/rails_apps/sorcery/tmp/local_secret.txt +0 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bfc947837944c5fd9470108cd402ac191433e9b79d1f49820ff9f7709e5c6d0a
4
- data.tar.gz: 13dceae2e364d6a13a694855b1d2539216b1e4c5d7fb93d550a7df8a17ff2ba7
3
+ metadata.gz: 81976672ef109beee1a632ce9db286ea3a3db35a0a8f9fd3f0508d6f271c36a2
4
+ data.tar.gz: 94fb1b32e590ac671b6038d20f8a0f84734a39f138d7ae820523aff05e424477
5
5
  SHA512:
6
- metadata.gz: beca6cddab932f7c230a1d5ec979789d72ec54f1b7cea40bfdc56949680641534a0d2c8fe29b8664fa5073abf2324cc9f89d3eb76e6e7d56eef5a248fa96d557
7
- data.tar.gz: 9ab1c3b8fb682bc8037e1caa9e741a1d4933676dcc76f3f68746d80a9c1472fca7fd67a73de3734ce7530f6079887c4c90b18265913806c64a81b7c93d1e0141
6
+ metadata.gz: 0a481e25f5ab908ac79d3095199988fa39a938f460e71d8cb85016b1db62aad3c6370a9b645cb114243a6b864c3372a45776450ef43782c054bfc86fab9482eb
7
+ data.tar.gz: 5ad2495bff1bbafbf07da03014f8cb3b06d83d498d8948e5ee6a7d7e40ef25e7bcb0fd61e3891e5cf79e3d7aae707446f26cf0106a902967578bc068a47f7e99
@@ -19,6 +19,15 @@ module AnyLogin
19
19
  AnyLogin.provider.constantize::Controller.instance_method(:any_login_sign_in).bind(self).call
20
20
  end
21
21
 
22
+ def search
23
+ return head(:forbidden) unless AnyLogin.verify_access_proc.call(self)
24
+ return head(:not_found) unless AnyLogin.search_enabled?
25
+
26
+ query = params[:q].to_s.strip
27
+ users = query.empty? ? [] : AnyLogin.search(query)
28
+ render json: users.map { |user| { id: user.id, label: AnyLogin.label_for(user) } }
29
+ end
30
+
22
31
  private
23
32
 
24
33
  def try_not_to_leak_any_login_is_installed
@@ -8,6 +8,7 @@
8
8
  </label>
9
9
  <input type="checkbox" id="any_login_form_toggle" <%= AnyLogin.auto_show ? "checked" : "" %>>
10
10
  <span id='any_login_box'>
11
+ <%= any_login_search_input %>
11
12
  <%= any_login_select %>
12
13
  or by
13
14
  <%= any_login_id_input %>
@@ -1,4 +1,4 @@
1
- <style>
1
+ <style nonce="<%= content_security_policy_nonce %>">
2
2
  #any_login {
3
3
  position: fixed;
4
4
  opacity: 0.5;
@@ -93,6 +93,78 @@
93
93
  display: inline;
94
94
  }
95
95
 
96
+ #any_login:focus-within {
97
+ opacity: 1;
98
+ }
99
+
100
+ #any_login #any_login_search_box {
101
+ position: relative;
102
+ display: inline-block;
103
+ }
104
+
105
+ #any_login #any_login_search {
106
+ width: 200px;
107
+ }
108
+
109
+ #any_login #any_login_search_results {
110
+ position: absolute;
111
+ left: 0;
112
+ z-index: 10001;
113
+ min-width: 100%;
114
+ width: max-content;
115
+ max-width: 480px;
116
+ max-height: 280px;
117
+ overflow-y: auto;
118
+ margin: 0;
119
+ padding: 4px 0;
120
+ list-style: none;
121
+ background: #fff;
122
+ color: #222;
123
+ font-size: 14px;
124
+ line-height: 20px;
125
+ border: 1px solid #ccc;
126
+ border-radius: 6px;
127
+ box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
128
+ }
129
+
130
+ #any_login #any_login_search_results[hidden] {
131
+ display: none;
132
+ }
133
+
134
+ #any_login .any_login_search_up #any_login_search_results {
135
+ bottom: calc(100% + 4px);
136
+ }
137
+
138
+ #any_login .any_login_search_down #any_login_search_results {
139
+ top: calc(100% + 4px);
140
+ }
141
+
142
+ #any_login #any_login_search_results li {
143
+ padding: 6px 12px;
144
+ white-space: nowrap;
145
+ cursor: pointer;
146
+ }
147
+
148
+ #any_login #any_login_search_results li[aria-selected="true"] {
149
+ background: #5c4383;
150
+ color: #fff;
151
+ }
152
+
153
+ #any_login #any_login_search_results li.any_login_search_group {
154
+ padding: 8px 12px 2px;
155
+ font-size: 11px;
156
+ font-weight: 600;
157
+ letter-spacing: 0.04em;
158
+ text-transform: uppercase;
159
+ color: #888;
160
+ cursor: default;
161
+ }
162
+
163
+ #any_login #any_login_search_results li.any_login_search_message {
164
+ color: #777;
165
+ cursor: default;
166
+ }
167
+
96
168
  #any_login span#anylogin_back_to_user {
97
169
  padding-left: 15px;
98
170
  color: cyan;
@@ -1,4 +1,4 @@
1
- <script>
1
+ <script nonce="<%= content_security_policy_nonce %>">
2
2
  function submitOnChange() {
3
3
  document.getElementById('any_login_form').submit();
4
4
  }
@@ -14,9 +14,213 @@
14
14
  }
15
15
  }
16
16
 
17
+ function setupAnyLoginSearch() {
18
+ let input = document.getElementById('any_login_search');
19
+ let list = document.getElementById('any_login_search_results');
20
+ let select = document.getElementById('selected_id');
21
+ if (!elementExists(input) || !elementExists(list) || !elementExists(select) || input.anyLoginSearchBound) {
22
+ return;
23
+ }
24
+ input.anyLoginSearchBound = true;
25
+
26
+ let timer = null;
27
+ let request = null;
28
+ let initialGroups = JSON.parse(input.dataset.initial || '[]');
29
+ let users = [];
30
+ let options = [];
31
+ let active = -1;
32
+
33
+ function setBusy(busy) {
34
+ input.setAttribute('aria-busy', busy ? 'true' : 'false');
35
+ }
36
+
37
+ function setOpen(open) {
38
+ list.hidden = !open;
39
+ input.setAttribute('aria-expanded', open ? 'true' : 'false');
40
+ }
41
+
42
+ function clearList() {
43
+ users = [];
44
+ options = [];
45
+ active = -1;
46
+ list.innerHTML = '';
47
+ input.removeAttribute('aria-activedescendant');
48
+ }
49
+
50
+ function showMessage(text) {
51
+ clearList();
52
+ let item = document.createElement('li');
53
+ item.className = 'any_login_search_message';
54
+ item.textContent = text;
55
+ list.appendChild(item);
56
+ setOpen(true);
57
+ }
58
+
59
+ function highlight(index) {
60
+ active = index;
61
+ options.forEach(function(item, i) {
62
+ item.setAttribute('aria-selected', i === index ? 'true' : 'false');
63
+ });
64
+ if (index >= 0) {
65
+ input.setAttribute('aria-activedescendant', options[index].id);
66
+ options[index].scrollIntoView({ block: 'nearest' });
67
+ } else {
68
+ input.removeAttribute('aria-activedescendant');
69
+ }
70
+ }
71
+
72
+ function addOption(user) {
73
+ let index = users.length;
74
+ let item = document.createElement('li');
75
+ item.id = 'any_login_search_option_' + index;
76
+ item.setAttribute('role', 'option');
77
+ item.setAttribute('aria-selected', 'false');
78
+ item.textContent = user.label;
79
+ item.addEventListener('mousedown', function(event) {
80
+ event.preventDefault();
81
+ pick(index);
82
+ });
83
+ item.addEventListener('mousemove', function() {
84
+ if (active !== index) {
85
+ highlight(index);
86
+ }
87
+ });
88
+ users.push(user);
89
+ options.push(item);
90
+ list.appendChild(item);
91
+ }
92
+
93
+ function render(groups, activeIndex) {
94
+ clearList();
95
+ groups.forEach(function(group) {
96
+ if (!group.users.length) {
97
+ return;
98
+ }
99
+ if (group.label) {
100
+ let header = document.createElement('li');
101
+ header.className = 'any_login_search_group';
102
+ header.setAttribute('role', 'presentation');
103
+ header.textContent = group.label;
104
+ list.appendChild(header);
105
+ }
106
+ group.users.forEach(addOption);
107
+ });
108
+ setOpen(users.length > 0);
109
+ highlight(users.length ? activeIndex : -1);
110
+ }
111
+
112
+ function showInitial() {
113
+ render(initialGroups, -1);
114
+ }
115
+
116
+ function showUsers(found) {
117
+ setBusy(false);
118
+ if (!found.length) {
119
+ showMessage('No matches');
120
+ return;
121
+ }
122
+ render([{ label: null, users: found }], 0);
123
+ }
124
+
125
+ function pick(index) {
126
+ let user = users[index];
127
+ if (!user) {
128
+ return;
129
+ }
130
+ input.value = user.label;
131
+ setOpen(false);
132
+ let option = new Option(user.label, user.id, true, true);
133
+ select.add(option);
134
+ select.value = String(user.id);
135
+ document.getElementById('any_login_form').submit();
136
+ }
137
+
138
+ input.addEventListener('keydown', function(event) {
139
+ if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
140
+ if (list.hidden) {
141
+ reopen();
142
+ }
143
+ if (!users.length) {
144
+ return;
145
+ }
146
+ event.preventDefault();
147
+ let step = event.key === 'ArrowDown' ? 1 : -1;
148
+ if (active < 0) {
149
+ highlight(step > 0 ? 0 : users.length - 1);
150
+ } else {
151
+ highlight((active + step + users.length) % users.length);
152
+ }
153
+ } else if (event.key === 'Enter') {
154
+ event.preventDefault();
155
+ if (input.getAttribute('aria-busy') !== 'true' && !list.hidden && active >= 0) {
156
+ pick(active);
157
+ }
158
+ } else if (event.key === 'Escape') {
159
+ setOpen(false);
160
+ }
161
+ });
162
+
163
+ input.addEventListener('blur', function() {
164
+ setOpen(false);
165
+ });
166
+
167
+ function reopen() {
168
+ if (input.value.trim().length < 2) {
169
+ showInitial();
170
+ } else if (list.children.length) {
171
+ setOpen(true);
172
+ }
173
+ }
174
+
175
+ input.addEventListener('focus', reopen);
176
+ input.addEventListener('click', function() {
177
+ if (list.hidden) {
178
+ reopen();
179
+ }
180
+ });
181
+
182
+ input.addEventListener('input', function() {
183
+ clearTimeout(timer);
184
+ if (request) {
185
+ request.abort();
186
+ request = null;
187
+ }
188
+ let query = input.value.trim();
189
+ setBusy(query.length >= 2);
190
+ if (query.length < 2) {
191
+ showInitial();
192
+ return;
193
+ }
194
+
195
+ timer = setTimeout(function() {
196
+ showMessage('Searching\u2026');
197
+ request = new AbortController();
198
+ fetch(input.dataset.url + '?q=' + encodeURIComponent(query), {
199
+ headers: { Accept: 'application/json' },
200
+ credentials: 'same-origin',
201
+ signal: request.signal
202
+ })
203
+ .then(function(response) {
204
+ if (!response.ok) {
205
+ throw new Error('AnyLogin search failed: ' + response.status);
206
+ }
207
+ return response.json();
208
+ })
209
+ .then(showUsers)
210
+ .catch(function(error) {
211
+ if (error.name !== 'AbortError') {
212
+ setBusy(false);
213
+ showMessage('Search failed');
214
+ }
215
+ });
216
+ }, 250);
217
+ });
218
+ }
219
+
17
220
  function setupAnyLoginHandlers() {
18
221
  addChangeListenerTo("back_to_previous_id");
19
222
  addChangeListenerTo("selected_id");
223
+ setupAnyLoginSearch();
20
224
  }
21
225
 
22
226
  ['DOMContentLoaded', 'turbolinks:load', 'turbo:load'].forEach(function(e) {
data/config/routes.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  AnyLogin::Engine.routes.draw do
2
2
  post '/any_login/sign_in' => 'application#any_login', as: :sign_in
3
+ get '/any_login/search' => 'application#search', as: :search
3
4
  end
4
5
 
5
6
  Rails.application.routes.draw do
@@ -9,8 +9,19 @@ module AnyLogin
9
9
  if AnyLogin.enabled
10
10
 
11
11
  def any_login_id_input
12
- def any_login_id_input
13
- text_field_tag :id, '', placeholder: 'ID', id: 'any_login_id_input', required: true
12
+ text_field_tag :id, '', placeholder: 'ID', id: 'any_login_id_input', required: true
13
+ end
14
+
15
+ def any_login_search_input
16
+ return unless AnyLogin.search_enabled?
17
+
18
+ direction = AnyLogin.position.to_s.start_with?('top') ? 'down' : 'up'
19
+ content_tag(:span, id: 'any_login_search_box', class: "any_login_search_#{direction}") do
20
+ search_field_tag(nil, '', placeholder: 'Search or pick a user', id: 'any_login_search', autocomplete: 'off',
21
+ role: 'combobox', 'aria-expanded': 'false', 'aria-busy': 'false', 'aria-autocomplete': 'list',
22
+ 'aria-controls': 'any_login_search_results',
23
+ data: { url: any_login.search_path, initial: any_login_search_initial_groups }) +
24
+ content_tag(:ul, '', id: 'any_login_search_results', role: 'listbox', hidden: true)
14
25
  end
15
26
  end
16
27
 
@@ -18,15 +29,31 @@ module AnyLogin
18
29
  submit_tag AnyLogin.login_button_label
19
30
  end
20
31
 
32
+ def any_login_search_initial_groups
33
+ collection = any_login_collection
34
+ groups = collection.grouped? ? collection.to_a : [[nil, collection.to_a]]
35
+ groups.map do |label, options|
36
+ users = options.map do |option|
37
+ option_label, id = option.is_a?(Array) ? option : [option, option]
38
+ { id: id, label: option_label }
39
+ end
40
+ { label: label && Array(label).first.to_s, users: users }
41
+ end
42
+ end
43
+
21
44
  def any_login_select
22
- collection = AnyLogin.collection
45
+ collection = any_login_collection
23
46
  select_options =
24
47
  if collection.grouped?
25
48
  grouped_options_for_select(collection.to_a)
26
49
  else
27
50
  options_for_select(collection.to_a)
28
51
  end
29
- select_tag :selected_id, select_options, select_html_options
52
+ select_tag :selected_id, select_options, select_html_options.merge(hidden: AnyLogin.search_enabled?)
53
+ end
54
+
55
+ def any_login_collection
56
+ @any_login_collection ||= AnyLogin.collection
30
57
  end
31
58
 
32
59
  def any_login_previous_select
@@ -1,3 +1,3 @@
1
1
  module AnyLogin
2
- VERSION = '1.6.0'
2
+ VERSION = '1.8.0'
3
3
  end
data/lib/any_login.rb CHANGED
@@ -64,6 +64,10 @@ module AnyLogin
64
64
  mattr_accessor :limit
65
65
  @@limit = 10
66
66
 
67
+ # search users as you type: attributes to match (e.g. [:email, :name]) or a proc receiving the query, nil to disable
68
+ mattr_accessor :search_by
69
+ @@search_by = nil
70
+
67
71
  # Previous limit, integer or :none
68
72
  mattr_accessor :previous_limit
69
73
  @@previous_limit = 6
@@ -96,6 +100,26 @@ module AnyLogin
96
100
  @@klass = AnyLogin.klass_name.constantize
97
101
  end
98
102
 
103
+ def self.search_enabled?
104
+ search_by.present?
105
+ end
106
+
107
+ def self.search(query)
108
+ result = if search_by.respond_to?(:call)
109
+ search_by.call(query)
110
+ else
111
+ pattern = "%#{klass.sanitize_sql_like(query)}%"
112
+ klass.where(Array(search_by).map { |attribute| klass.arel_table[attribute].matches(pattern, '\\') }.reduce(:or))
113
+ end
114
+ return result.to_a if limit == :none
115
+
116
+ result.is_a?(ActiveRecord::Relation) ? result.limit(limit).to_a : result.take(limit)
117
+ end
118
+
119
+ def self.label_for(user)
120
+ Array(Collection.new([user]).to_a.first).first
121
+ end
122
+
99
123
  def self.cookie_name
100
124
  module_parent_name = if Rails::VERSION::MAJOR >= 6
101
125
  Rails.application.class.module_parent_name
@@ -35,6 +35,9 @@ AnyLogin.setup do |config|
35
35
  # # limit, integer or :none
36
36
  # config.limit = 10
37
37
 
38
+ # # search users as you type: attributes to match or a proc receiving the query
39
+ # config.search_by = [:email, :name]
40
+
38
41
  # # Enable http basic authentication
39
42
  # config.http_basic_authentication_enabled = false
40
43
 
@@ -17,6 +17,7 @@ class NavigationTest < ActionDispatch::IntegrationTest
17
17
 
18
18
  find("#any_login_form_toggle_label").click
19
19
  find('#selected_id option:last-of-type').select_option
20
+ assert_selector "#back_to_previous_id", visible: :all
20
21
 
21
22
  visit "/about"
22
23
 
@@ -16,6 +16,7 @@ class NavigationTest < ActionDispatch::IntegrationTest
16
16
 
17
17
  find("#any_login_form_toggle_label").click
18
18
  find('#selected_id option:last-of-type').select_option
19
+ assert_selector "#back_to_previous_id", visible: :all
19
20
 
20
21
  visit "/about"
21
22
 
@@ -18,6 +18,7 @@ class NavigationTest < ActionDispatch::IntegrationTest
18
18
  # There is an instability in the test suite where the option on the dropdown sometimes shows
19
19
  # up as `1` and sometimes shows up as `test@test.com`. This works around the issue.
20
20
  find('#selected_id option:last-of-type').select_option
21
+ assert_selector "#back_to_previous_id", visible: :all
21
22
 
22
23
  visit "/about"
23
24
 
@@ -0,0 +1,169 @@
1
+ require_relative "../test_helper_devise"
2
+
3
+ class SearchTest < ActionDispatch::IntegrationTest
4
+ setup do
5
+ @previous_search_by = AnyLogin.search_by
6
+ @previous_name_method = AnyLogin.name_method
7
+ @previous_collection_method = AnyLogin.collection_method
8
+ AnyLogin.name_method = proc { |user| [user.email, user.id] }
9
+ @alice = User.create!(name: "Alice Smith", email: "alice@example.com", password: "password123", role: "user")
10
+ User.create!(name: "Bob Jones", email: "bob@sample.org", password: "password123", role: "user")
11
+ end
12
+
13
+ teardown do
14
+ AnyLogin.search_by = @previous_search_by
15
+ AnyLogin.name_method = @previous_name_method
16
+ AnyLogin.collection_method = @previous_collection_method
17
+ end
18
+
19
+ test "returns matching users as JSON" do
20
+ AnyLogin.search_by = [:email, :name]
21
+
22
+ get "/any_login/any_login/search", params: { q: "smith" }
23
+
24
+ assert_response :success
25
+ assert_equal [{ "id" => @alice.id, "label" => "alice@example.com" }], response.parsed_body
26
+ end
27
+
28
+ test "returns nothing for a blank query" do
29
+ AnyLogin.search_by = [:email]
30
+
31
+ get "/any_login/any_login/search", params: { q: " " }
32
+
33
+ assert_equal [], response.parsed_body
34
+ end
35
+
36
+ test "is not found when search is disabled" do
37
+ AnyLogin.search_by = nil
38
+
39
+ get "/any_login/any_login/search", params: { q: "alice" }
40
+
41
+ assert_response :not_found
42
+ end
43
+
44
+ test "is forbidden when access is denied" do
45
+ AnyLogin.search_by = [:email]
46
+ previous_proc = AnyLogin.verify_access_proc
47
+ AnyLogin.verify_access_proc = proc { false }
48
+
49
+ get "/any_login/any_login/search", params: { q: "alice" }
50
+
51
+ assert_response :forbidden
52
+ ensure
53
+ AnyLogin.verify_access_proc = previous_proc
54
+ end
55
+
56
+ test "signs in a user picked from the search dropdown" do
57
+ AnyLogin.search_by = [:email, :name]
58
+
59
+ visit "/"
60
+ find("#any_login_form_toggle_label").click
61
+ fill_in "any_login_search", with: "smith"
62
+ assert_selector "#any_login_search[aria-busy=false]"
63
+ assert_selector "#any_login_search_results [role=option]", text: "alice@example.com"
64
+ assert_no_selector "#any_login_search_results [role=option]", text: "bob@sample.org"
65
+ find("#any_login_search_results [role=option]", text: "alice@example.com").click
66
+ assert_selector "#back_to_previous_id", visible: :all
67
+
68
+ visit "/about"
69
+
70
+ assert_text("This is secret page available only for logged in users")
71
+ end
72
+
73
+ test "picks a search result with the keyboard" do
74
+ AnyLogin.search_by = [:email, :name]
75
+
76
+ visit "/"
77
+ find("#any_login_form_toggle_label").click
78
+ fill_in "any_login_search", with: "example"
79
+ assert_selector "#any_login_search[aria-busy=false]"
80
+ assert_selector "#any_login_search_results [role=option][aria-selected=true]", text: "alice@example.com"
81
+ find("#any_login_search").send_keys(:enter)
82
+ assert_selector "#back_to_previous_id", visible: :all
83
+
84
+ visit "/about"
85
+
86
+ assert_text("This is secret page available only for logged in users")
87
+ end
88
+
89
+ test "opens the dropdown above the search field when the box is at the bottom" do
90
+ AnyLogin.search_by = [:email, :name]
91
+
92
+ visit "/"
93
+ find("#any_login_form_toggle_label").click
94
+ fill_in "any_login_search", with: "smith"
95
+ assert_selector "#any_login_search[aria-busy=false]"
96
+ assert_selector "#any_login_search_results [role=option]", text: "alice@example.com"
97
+
98
+ list_bottom, input_top = evaluate_script(<<~JS)
99
+ [document.getElementById("any_login_search_results").getBoundingClientRect().bottom,
100
+ document.getElementById("any_login_search").getBoundingClientRect().top]
101
+ JS
102
+ assert_operator list_bottom, :<=, input_top
103
+ end
104
+
105
+ test "shows a message when nothing matches" do
106
+ AnyLogin.search_by = [:email, :name]
107
+
108
+ visit "/"
109
+ find("#any_login_form_toggle_label").click
110
+ fill_in "any_login_search", with: "nobody"
111
+ assert_selector "#any_login_search[aria-busy=false]"
112
+
113
+ assert_selector "#any_login_search_results li", text: "No matches"
114
+ end
115
+
116
+ test "replaces the user select with the search field" do
117
+ AnyLogin.search_by = [:email, :name]
118
+
119
+ visit "/"
120
+ find("#any_login_form_toggle_label").click
121
+
122
+ assert_selector "#any_login_search"
123
+ assert_no_selector "#selected_id"
124
+ end
125
+
126
+ test "lists the default users when the empty search field is focused" do
127
+ AnyLogin.search_by = [:email, :name]
128
+
129
+ visit "/"
130
+ find("#any_login_form_toggle_label").click
131
+ find("#any_login_search").click
132
+
133
+ assert_selector "#any_login_search_results [role=option]", text: "alice@example.com"
134
+ assert_selector "#any_login_search_results [role=option]", text: "bob@sample.org"
135
+ assert_no_selector "#any_login_search_results [aria-selected=true]"
136
+ find("#any_login_search_results [role=option]", text: "bob@sample.org").click
137
+ assert_selector "#back_to_previous_id", visible: :all
138
+
139
+ visit "/about"
140
+
141
+ assert_text("This is secret page available only for logged in users")
142
+ end
143
+
144
+ test "shows group headers for a grouped collection" do
145
+ AnyLogin.search_by = [:email, :name]
146
+ AnyLogin.collection_method = :groped_collection
147
+ User.create!(name: "Carol Admin", email: "carol@example.com", password: "password123", role: "admin")
148
+
149
+ visit "/"
150
+ find("#any_login_form_toggle_label").click
151
+ find("#any_login_search").click
152
+
153
+ assert_selector "#any_login_search_results li.any_login_search_group", text: /admin/i
154
+ assert_selector "#any_login_search_results li.any_login_search_group", text: /user/i
155
+ assert_selector "#any_login_search_results [role=option]", count: 3
156
+ end
157
+
158
+ test "arrow up from the default list highlights the last user" do
159
+ AnyLogin.search_by = [:email, :name]
160
+
161
+ visit "/"
162
+ find("#any_login_form_toggle_label").click
163
+ find("#any_login_search").click
164
+ assert_selector "#any_login_search_results [role=option]", count: 2
165
+ find("#any_login_search").send_keys(:up)
166
+
167
+ assert_selector "#any_login_search_results [role=option]:last-child[aria-selected=true]"
168
+ end
169
+ end
@@ -29,7 +29,7 @@ module AnyLogin
29
29
 
30
30
  AnyLogin.name_method = ->(e) { e.id }
31
31
  assert_equal [1, 2], collection.to_a
32
- rescue
32
+ ensure
33
33
  AnyLogin.name_method = prev
34
34
  end
35
35
  end
@@ -67,7 +67,7 @@ module AnyLogin
67
67
  ["user", [3, 4]]
68
68
  ]
69
69
  assert_equal expected, collection.to_a
70
- rescue
70
+ ensure
71
71
  AnyLogin.name_method = prev
72
72
  end
73
73
  end
@@ -0,0 +1,14 @@
1
+ require_relative "../test_helper_devise"
2
+
3
+ module AnyLogin
4
+ class HelpersTest < ActionView::TestCase
5
+ include AnyLogin::Helpers
6
+
7
+ test "any_login_id_input renders the ID field on the first call" do
8
+ assert_dom_equal(
9
+ '<input type="text" name="id" id="any_login_id_input" value="" placeholder="ID" required="required" />',
10
+ any_login_id_input
11
+ )
12
+ end
13
+ end
14
+ end