openstax_utilities 3.0.0 → 4.0.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 (62) hide show
  1. checksums.yaml +8 -8
  2. data/Rakefile +4 -3
  3. data/app/handlers/openstax/utilities/abstract_keyword_search_handler.rb +92 -0
  4. data/app/routines/openstax/utilities/abstract_keyword_search_routine.rb +149 -0
  5. data/lib/openstax/utilities/delegate_access_control.rb +5 -0
  6. data/lib/openstax/utilities/engine.rb +4 -8
  7. data/lib/openstax/utilities/helpers/datetime.rb +4 -4
  8. data/{app/helpers → lib}/openstax/utilities/osu_helper.rb +9 -1
  9. data/lib/openstax/utilities/version.rb +1 -1
  10. data/lib/openstax_utilities.rb +1 -7
  11. data/spec/dummy/README.md +1 -1
  12. data/spec/dummy/Rakefile +1 -2
  13. data/spec/dummy/app/assets/javascripts/application.js +3 -5
  14. data/spec/dummy/app/assets/stylesheets/application.css +5 -3
  15. data/spec/dummy/app/controllers/application_controller.rb +3 -3
  16. data/spec/dummy/app/handlers/users_search.rb +11 -0
  17. data/spec/dummy/app/routines/search_users.rb +22 -0
  18. data/spec/dummy/app/views/layouts/application.html.erb +2 -2
  19. data/spec/dummy/bin/bundle +3 -0
  20. data/spec/dummy/bin/rails +4 -0
  21. data/spec/dummy/bin/rake +4 -0
  22. data/spec/dummy/config.ru +1 -1
  23. data/spec/dummy/config/application.rb +1 -37
  24. data/spec/dummy/config/boot.rb +4 -9
  25. data/spec/dummy/config/database.yml +8 -8
  26. data/spec/dummy/config/environment.rb +3 -3
  27. data/spec/dummy/config/environments/development.rb +19 -19
  28. data/spec/dummy/config/environments/production.rb +41 -30
  29. data/spec/dummy/config/environments/test.rb +17 -15
  30. data/spec/dummy/config/initializers/assets.rb +8 -0
  31. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  32. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  33. data/spec/dummy/config/initializers/inflections.rb +6 -5
  34. data/spec/dummy/config/initializers/mime_types.rb +0 -1
  35. data/spec/dummy/config/initializers/session_store.rb +1 -6
  36. data/spec/dummy/config/initializers/wrap_parameters.rb +6 -6
  37. data/spec/dummy/config/locales/en.yml +20 -2
  38. data/spec/dummy/config/routes.rb +0 -3
  39. data/spec/dummy/config/secrets.yml +22 -0
  40. data/spec/dummy/db/migrate/0_create_users.rb +8 -2
  41. data/spec/dummy/db/schema.rb +13 -7
  42. data/spec/dummy/public/404.html +54 -13
  43. data/spec/dummy/public/422.html +54 -13
  44. data/spec/dummy/public/500.html +53 -12
  45. data/spec/factories/user.rb +8 -0
  46. data/spec/handlers/openstax/utilities/abstract_keyword_search_handler_spec.rb +93 -0
  47. data/spec/lib/openstax/utilities/access_policy_spec.rb +2 -2
  48. data/spec/rails_helper.rb +54 -0
  49. data/spec/routines/openstax/utilities/abstract_keyword_search_routine_spec.rb +114 -0
  50. data/spec/spec_helper.rb +80 -11
  51. metadata +102 -24
  52. data/app/assets/javascripts/openstax_utilities.js +0 -0
  53. data/app/assets/stylesheets/openstax_utilities.css +0 -4
  54. data/spec/dummy/app/assets/stylesheets/scaffold.css +0 -56
  55. data/spec/dummy/app/controllers/users_controller.rb +0 -87
  56. data/spec/dummy/app/views/users/_form.html.erb +0 -17
  57. data/spec/dummy/app/views/users/edit.html.erb +0 -6
  58. data/spec/dummy/app/views/users/index.html.erb +0 -21
  59. data/spec/dummy/app/views/users/new.html.erb +0 -5
  60. data/spec/dummy/app/views/users/show.html.erb +0 -5
  61. data/spec/dummy/config/initializers/secret_token.rb +0 -7
  62. data/spec/dummy/script/rails +0 -6
@@ -2,4 +2,3 @@
2
2
 
3
3
  # Add new mime types for use in respond_to blocks:
4
4
  # Mime::Type.register "text/richtext", :rtf
5
- # Mime::Type.register_alias "text/html", :iphone
@@ -1,8 +1,3 @@
1
1
  # Be sure to restart your server when you modify this file.
2
2
 
3
- Dummy::Application.config.session_store :cookie_store, key: '_dummy_session'
4
-
5
- # Use the database for sessions instead of the cookie-based default,
6
- # which shouldn't be used to store highly confidential information
7
- # (create the session table with "rails generate session_migration")
8
- # Dummy::Application.config.session_store :active_record_store
3
+ Rails.application.config.session_store :cookie_store, key: '_dummy_session'
@@ -1,14 +1,14 @@
1
1
  # Be sure to restart your server when you modify this file.
2
- #
2
+
3
3
  # This file contains settings for ActionController::ParamsWrapper which
4
4
  # is enabled by default.
5
5
 
6
6
  # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
7
  ActiveSupport.on_load(:action_controller) do
8
- wrap_parameters format: [:json]
8
+ wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
9
9
  end
10
10
 
11
- # Disable root element in JSON by default.
12
- ActiveSupport.on_load(:active_record) do
13
- self.include_root_in_json = false
14
- end
11
+ # To enable root element in JSON for ActiveRecord objects.
12
+ # ActiveSupport.on_load(:active_record) do
13
+ # self.include_root_in_json = true
14
+ # end
@@ -1,5 +1,23 @@
1
- # Sample localization file for English. Add more files in this directory for other locales.
2
- # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
1
+ # Files in the config/locales directory are used for internationalization
2
+ # and are automatically loaded by Rails. If you want to use locales other
3
+ # than English, add the necessary files in this directory.
4
+ #
5
+ # To use the locales, use `I18n.t`:
6
+ #
7
+ # I18n.t 'hello'
8
+ #
9
+ # In views, this is aliased to just `t`:
10
+ #
11
+ # <%= t('hello') %>
12
+ #
13
+ # To use a different locale, set it with `I18n.locale`:
14
+ #
15
+ # I18n.locale = :es
16
+ #
17
+ # This would use the information in config/locales/es.yml.
18
+ #
19
+ # To learn more, please read the Rails Internationalization guide
20
+ # available at http://guides.rubyonrails.org/i18n.html.
3
21
 
4
22
  en:
5
23
  hello: "Hello world"
@@ -1,5 +1,2 @@
1
1
  Rails.application.routes.draw do
2
- resources :users
3
-
4
- mount OpenStax::Utilities::Engine => "/openstax_utilities"
5
2
  end
@@ -0,0 +1,22 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key is used for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+
6
+ # Make sure the secret is at least 30 characters and all random,
7
+ # no regular words or you'll be exposed to dictionary attacks.
8
+ # You can use `rake secret` to generate a secure secret key.
9
+
10
+ # Make sure the secrets in this file are kept private
11
+ # if you're sharing your code publicly.
12
+
13
+ development:
14
+ secret_key_base: 29e54ca73023838b463db707b1229ec66e43c4fb9a6302b136f4a09ba9f14fcfe39bf238f139a8475a6840e9a197abfbef5034ff1e227551cd914bac2000d958
15
+
16
+ test:
17
+ secret_key_base: e96d972e503764aa27f184c2c9e1e14598be32d4471d93ffbeed148df0c8af572e7e9e60dfb32d21176118ace6a5de4075be4671d03937e6aa15527ea1800383
18
+
19
+ # Do not keep production secrets in the repository,
20
+ # instead read values from the environment.
21
+ production:
22
+ secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
@@ -1,10 +1,16 @@
1
1
  class CreateUsers < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :users do |t|
4
- t.string :username
5
- t.string :password_hash
4
+ t.string :username, null: false
5
+ t.string :password_hash, null: false
6
+ t.string :name, null: false
7
+ t.string :email, null: false
6
8
 
7
9
  t.timestamps
8
10
  end
11
+
12
+ add_index :users, :username
13
+ add_index :users, :name
14
+ add_index :users, :email
9
15
  end
10
16
  end
@@ -9,15 +9,21 @@
9
9
  # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
10
  # you'll amass, the slower it'll run and the greater likelihood for issues).
11
11
  #
12
- # It's strongly recommended to check this file into your version control system.
12
+ # It's strongly recommended that you check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(:version => 0) do
14
+ ActiveRecord::Schema.define(version: 0) do
15
15
 
16
- create_table "users", :force => true do |t|
17
- t.string "username"
18
- t.string "password_hash"
19
- t.datetime "created_at", :null => false
20
- t.datetime "updated_at", :null => false
16
+ create_table "users", force: true do |t|
17
+ t.string "username", null: false
18
+ t.string "password_hash", null: false
19
+ t.string "name", null: false
20
+ t.string "email", null: false
21
+ t.datetime "created_at"
22
+ t.datetime "updated_at"
21
23
  end
22
24
 
25
+ add_index "users", ["email"], name: "index_users_on_email"
26
+ add_index "users", ["name"], name: "index_users_on_name"
27
+ add_index "users", ["username"], name: "index_users_on_username"
28
+
23
29
  end
@@ -2,25 +2,66 @@
2
2
  <html>
3
3
  <head>
4
4
  <title>The page you were looking for doesn't exist (404)</title>
5
- <style type="text/css">
6
- body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
- div.dialog {
8
- width: 25em;
9
- padding: 0 4em;
10
- margin: 4em auto 0 auto;
11
- border: 1px solid #ccc;
12
- border-right-color: #999;
13
- border-bottom-color: #999;
14
- }
15
- h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
16
54
  </style>
17
55
  </head>
18
56
 
19
57
  <body>
20
58
  <!-- This file lives in public/404.html -->
21
59
  <div class="dialog">
22
- <h1>The page you were looking for doesn't exist.</h1>
23
- <p>You may have mistyped the address or the page may have moved.</p>
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
24
65
  </div>
25
66
  </body>
26
67
  </html>
@@ -2,25 +2,66 @@
2
2
  <html>
3
3
  <head>
4
4
  <title>The change you wanted was rejected (422)</title>
5
- <style type="text/css">
6
- body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
- div.dialog {
8
- width: 25em;
9
- padding: 0 4em;
10
- margin: 4em auto 0 auto;
11
- border: 1px solid #ccc;
12
- border-right-color: #999;
13
- border-bottom-color: #999;
14
- }
15
- h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
16
54
  </style>
17
55
  </head>
18
56
 
19
57
  <body>
20
58
  <!-- This file lives in public/422.html -->
21
59
  <div class="dialog">
22
- <h1>The change you wanted was rejected.</h1>
23
- <p>Maybe you tried to change something you didn't have access to.</p>
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
24
65
  </div>
25
66
  </body>
26
67
  </html>
@@ -2,24 +2,65 @@
2
2
  <html>
3
3
  <head>
4
4
  <title>We're sorry, but something went wrong (500)</title>
5
- <style type="text/css">
6
- body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
- div.dialog {
8
- width: 25em;
9
- padding: 0 4em;
10
- margin: 4em auto 0 auto;
11
- border: 1px solid #ccc;
12
- border-right-color: #999;
13
- border-bottom-color: #999;
14
- }
15
- h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
16
54
  </style>
17
55
  </head>
18
56
 
19
57
  <body>
20
58
  <!-- This file lives in public/500.html -->
21
59
  <div class="dialog">
22
- <h1>We're sorry, but something went wrong.</h1>
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
23
64
  </div>
24
65
  </body>
25
66
  </html>
@@ -0,0 +1,8 @@
1
+ FactoryGirl.define do
2
+ factory :user do
3
+ username { SecureRandom.hex.to_s }
4
+ password_hash { SecureRandom.hex.to_s }
5
+ name { Faker::Name.name }
6
+ email { Faker::Internet.email }
7
+ end
8
+ end
@@ -0,0 +1,93 @@
1
+ require 'rails_helper'
2
+
3
+ module OpenStax
4
+ module Utilities
5
+ describe AbstractKeywordSearchHandler do
6
+
7
+ let!(:users_search) { UsersSearch.new }
8
+
9
+ let!(:john_doe) { FactoryGirl.create :user, name: "John Doe",
10
+ username: "doejohn",
11
+ email: "john@doe.com" }
12
+
13
+ let!(:jane_doe) { FactoryGirl.create :user, name: "Jane Doe",
14
+ username: "doejane",
15
+ email: "jane@doe.com" }
16
+
17
+ let!(:jack_doe) { FactoryGirl.create :user, name: "Jack Doe",
18
+ username: "doejack",
19
+ email: "jack@doe.com" }
20
+
21
+ before(:each) do
22
+ 100.times do
23
+ FactoryGirl.create(:user)
24
+ end
25
+ end
26
+
27
+ it "passes its params to the search routine and sets the total_count output" do
28
+ outputs = users_search.call(params: {q: 'username:dOe'}).outputs
29
+ total_count = outputs[:total_count]
30
+ items = outputs[:items]
31
+ expect(total_count).to eq items.count
32
+ expect(items).to include(john_doe)
33
+ expect(items).to include(jane_doe)
34
+ expect(items).to include(jack_doe)
35
+ john_index = items.index(john_doe)
36
+ jane_index = items.index(jane_doe)
37
+ jack_index = items.index(jack_doe)
38
+ expect(jane_index).to be > john_index
39
+ expect(jack_index).to be > jane_index
40
+ items.each do |item|
41
+ expect(item.username).to match(/\Adoe[\w]*\z/i)
42
+ end
43
+
44
+ outputs = users_search.call(params: {order_by: 'cReAtEd_At DeSc, iD dEsC',
45
+ q: 'username:DoE'}).outputs
46
+ total_count = outputs[:total_count]
47
+ items = outputs[:items]
48
+ expect(total_count).to eq items.count
49
+ expect(items).to include(john_doe)
50
+ expect(items).to include(jane_doe)
51
+ expect(items).to include(jack_doe)
52
+ john_index = items.index(john_doe)
53
+ jane_index = items.index(jane_doe)
54
+ jack_index = items.index(jack_doe)
55
+ expect(jane_index).to be < john_index
56
+ expect(jack_index).to be < jane_index
57
+ items.each do |item|
58
+ expect(item.username).to match(/\Adoe[\w]*\z/i)
59
+ end
60
+ end
61
+
62
+ it "errors out if no query is provided" do
63
+ routine = users_search.call(params: {})
64
+ outputs = routine.outputs
65
+ errors = routine.errors
66
+ expect(outputs).to be_empty
67
+ expect(errors).not_to be_empty
68
+ expect(errors.first.code).to eq :no_query
69
+ end
70
+
71
+ it "errors out if the query is too short" do
72
+ routine = users_search.call(params: {q: 'a'})
73
+ outputs = routine.outputs
74
+ errors = routine.errors
75
+ expect(outputs).to be_empty
76
+ expect(errors).not_to be_empty
77
+ expect(errors.first.code).to eq :query_too_short
78
+ end
79
+
80
+ it "errors out if too many items match" do
81
+ routine = users_search.call(params: {q: 'username:a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,-,_'})
82
+ outputs = routine.outputs
83
+ errors = routine.errors
84
+ expect(outputs).not_to be_empty
85
+ expect(outputs[:total_count]).to eq User.count
86
+ expect(outputs[:items]).to be_nil
87
+ expect(errors).not_to be_empty
88
+ expect(errors.first.code).to eq :too_many_matches
89
+ end
90
+
91
+ end
92
+ end
93
+ end