openstax_api 8.3.1 → 8.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openstax/api/version.rb +1 -1
  3. metadata +2 -120
  4. data/spec/controllers/openstax/api/v1/api_controller_spec.rb +0 -176
  5. data/spec/dummy/README.md +0 -1
  6. data/spec/dummy/Rakefile +0 -6
  7. data/spec/dummy/app/assets/javascripts/application.js +0 -13
  8. data/spec/dummy/app/assets/stylesheets/application.css +0 -15
  9. data/spec/dummy/app/controllers/api/v1/dummy_controller.rb +0 -23
  10. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  11. data/spec/dummy/app/models/user.rb +0 -2
  12. data/spec/dummy/app/representers/user_representer.rb +0 -13
  13. data/spec/dummy/app/representers/user_search_representer.rb +0 -5
  14. data/spec/dummy/app/routines/search_users.rb +0 -42
  15. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  16. data/spec/dummy/bin/bundle +0 -3
  17. data/spec/dummy/bin/rails +0 -4
  18. data/spec/dummy/bin/rake +0 -4
  19. data/spec/dummy/config.ru +0 -4
  20. data/spec/dummy/config/application.rb +0 -23
  21. data/spec/dummy/config/boot.rb +0 -5
  22. data/spec/dummy/config/database.yml +0 -25
  23. data/spec/dummy/config/environment.rb +0 -7
  24. data/spec/dummy/config/environments/development.rb +0 -37
  25. data/spec/dummy/config/environments/production.rb +0 -78
  26. data/spec/dummy/config/environments/test.rb +0 -39
  27. data/spec/dummy/config/initializers/assets.rb +0 -8
  28. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  29. data/spec/dummy/config/initializers/cookies_serializer.rb +0 -3
  30. data/spec/dummy/config/initializers/doorkeeper.rb +0 -75
  31. data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  32. data/spec/dummy/config/initializers/inflections.rb +0 -16
  33. data/spec/dummy/config/initializers/mime_types.rb +0 -4
  34. data/spec/dummy/config/initializers/openstax_api.rb +0 -4
  35. data/spec/dummy/config/initializers/session_store.rb +0 -3
  36. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  37. data/spec/dummy/config/locales/en.yml +0 -23
  38. data/spec/dummy/config/routes.rb +0 -6
  39. data/spec/dummy/config/secrets.yml +0 -22
  40. data/spec/dummy/db/development.sqlite3 +0 -0
  41. data/spec/dummy/db/migrate/0_create_doorkeeper_tables.rb +0 -42
  42. data/spec/dummy/db/migrate/1_create_users.rb +0 -16
  43. data/spec/dummy/db/schema.rb +0 -68
  44. data/spec/dummy/db/test.sqlite3 +0 -0
  45. data/spec/dummy/lib/controller_includes.rb +0 -3
  46. data/spec/dummy/public/404.html +0 -67
  47. data/spec/dummy/public/422.html +0 -67
  48. data/spec/dummy/public/500.html +0 -66
  49. data/spec/dummy/public/favicon.ico +0 -0
  50. data/spec/factories/user.rb +0 -8
  51. data/spec/lib/openstax/api/apipie_spec.rb +0 -15
  52. data/spec/lib/openstax/api/constraints_spec.rb +0 -84
  53. data/spec/lib/openstax/api/doorkeeper_application_includes_spec.rb +0 -17
  54. data/spec/lib/openstax/api/params_spec.rb +0 -78
  55. data/spec/lib/openstax/api/representable_schema_printer_spec.rb +0 -27
  56. data/spec/lib/openstax/api/roar_spec.rb +0 -45
  57. data/spec/lib/openstax/api/routing_mapper_includes_spec.rb +0 -20
  58. data/spec/lib/openstax_api_spec.rb +0 -19
  59. data/spec/models/openstax/api/api_user_spec.rb +0 -47
  60. data/spec/rails_helper.rb +0 -54
  61. data/spec/representers/openstax/api/v1/abstract_search_representer_spec.rb +0 -144
  62. data/spec/spec_helper.rb +0 -86
@@ -1,17 +0,0 @@
1
- require 'rails_helper'
2
-
3
- module OpenStax
4
- module Api
5
- describe DoorkeeperApplicationIncludes do
6
- it 'must add methods to Doorkeeper::Application' do
7
- application = Doorkeeper::Application.new
8
- expect(application).to respond_to(:is_human?)
9
- expect(application.is_human?).to eq(false)
10
- expect(application).to respond_to(:is_application?)
11
- expect(application.is_application?).to eq(true)
12
- expect(application).to respond_to(:is_admin?)
13
- expect(application.is_admin?).to eq(false)
14
- end
15
- end
16
- end
17
- end
@@ -1,78 +0,0 @@
1
- # coding: utf-8
2
- require 'rails_helper'
3
-
4
- module OpenStax
5
- module Api
6
- describe Params do
7
-
8
- let(:params) { {a: '1', b: 2, c: nil, d: '', e: '♥', f: true} }
9
- let(:signed) { described_class.sign(params: params, secret: 'secret') }
10
-
11
- it 'disallows blank secrets' do
12
- expect{
13
- described_class.sign(params: {}, secret: nil)
14
- }.to raise_error(StandardError)
15
- end
16
-
17
- it 'signs and verifies' do
18
- expect(signed[:timestamp]).not_to be_blank
19
- expect(signed[:signature]).not_to be_blank
20
-
21
- expect(
22
- described_class.signature_and_timestamp_valid?(params: signed, secret: 'secret')
23
- ).to eq true
24
- end
25
-
26
- it 'does not verify is signature does not match' do
27
- signed[:signature] += "a"
28
- expect(
29
- described_class.signature_and_timestamp_valid?(params: signed, secret: 'secret')
30
- ).to eq false
31
- end
32
-
33
- it 'does not verify if signature blank' do
34
- signed[:signature] = " "
35
- expect(
36
- described_class.signature_and_timestamp_valid?(params: signed, secret: 'secret')
37
- ).to eq false
38
- end
39
-
40
- describe "altered params" do
41
-
42
- it 'rejects additions' do
43
- expect(
44
- described_class.signature_and_timestamp_valid?(
45
- params: signed.merge(evil: 'yes'),
46
- secret: 'secret')
47
- ).to eq false
48
- end
49
-
50
- it 'rejects alterations' do
51
- expect(
52
- described_class.signature_and_timestamp_valid?(
53
- params: signed.merge(b: 10000),
54
- secret: 'secret')
55
- ).to eq false
56
- end
57
-
58
- it 'rejects deletions' do
59
- expect(
60
- described_class.signature_and_timestamp_valid?(
61
- params: signed.except(:a),
62
- secret: 'secret')
63
- ).to eq false
64
- end
65
-
66
- end
67
-
68
- it 'does not verify if timestamp too long ago' do
69
- expect(
70
- described_class.signature_and_timestamp_valid?(params: signed,
71
- secret: 'secret',
72
- timestamp_window_width: 0.minutes)
73
- ).to eq false
74
- end
75
-
76
- end
77
- end
78
- end
@@ -1,27 +0,0 @@
1
- require 'rails_helper'
2
-
3
- module OpenStax
4
- module Api
5
- describe RepresentableSchemaPrinter do
6
- it 'must print model schemas' do
7
- schema = RepresentableSchemaPrinter.json(UserRepresenter)
8
- expect(schema).to include('## Schema')
9
- expect(schema).to include('{#')
10
- expect(schema).to include(' .schema}')
11
- expect(schema).to include("<pre class='code'>")
12
- expect(schema).to include('</pre>')
13
- json_schema = schema.match(/<pre class='code'>([^<]*)<\/pre>/)
14
- expect(JSON.parse(json_schema[1])).to eq({
15
- "type" => "object",
16
- "required" => [ "username" ],
17
- "properties" => {
18
- "username" => { "type" => "string" },
19
- "name" => { "type" => "string" },
20
- "email" => { "type" => "string" }
21
- },
22
- "additionalProperties" => false
23
- })
24
- end
25
- end
26
- end
27
- end
@@ -1,45 +0,0 @@
1
- require 'rails_helper'
2
-
3
- module OpenStax
4
- module Api
5
- module V1
6
- describe Roar do
7
- let!(:controller) { OpenStax::Api::V1::ApiController.new }
8
-
9
- it 'adds methods to ApiController instance' do
10
- expect(controller).to respond_to(:standard_index)
11
- expect(controller).to respond_to(:standard_search)
12
- expect(controller).to respond_to(:standard_create)
13
- expect(controller).to respond_to(:standard_nested_create)
14
- expect(controller).to respond_to(:standard_read)
15
- expect(controller).to respond_to(:standard_update)
16
- expect(controller).to respond_to(:standard_destroy)
17
- expect(controller).to respond_to(:standard_restore)
18
- expect(controller).to respond_to(:standard_sort)
19
- expect(controller).to respond_to(:render_api_errors)
20
- end
21
-
22
- context 'render_api_errors' do
23
- it 'returns nil if errors is nil' do
24
- expect(controller).not_to receive(:render)
25
- expect(controller.render_api_errors(nil)).to be_nil
26
- end
27
-
28
- it 'returns nil if errors is empty' do
29
- expect(controller).not_to receive(:render)
30
- expect(controller.render_api_errors([])).to be_nil
31
- expect(controller.render_api_errors({})).to be_nil
32
- end
33
-
34
- it 'returns the rendered response if there are errors' do
35
- expect(controller).to receive(:render) do |options|
36
- expect(options[:json]).to eq({ status: 422, errors: [{code: :error}] })
37
- expect(options[:status]).to eq :unprocessable_entity
38
- end
39
- controller.render_api_errors(code: :error)
40
- end
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,20 +0,0 @@
1
- require 'rails_helper'
2
-
3
- module OpenStax
4
- module Api
5
- describe RoutingMapperIncludes do
6
- it 'must add api method to ActionDispatch::Routing::Mapper' do
7
- mapper = ActionDispatch::Routing::Mapper.new(Rails.application.routes)
8
- expect(mapper).to respond_to(:api)
9
- Rails.application.routes.draw do
10
- api :v1 do
11
- resource :dummy_models
12
- end
13
- end
14
- route_names = Rails.application.routes.named_routes.names
15
- expect(route_names).to include(:api)
16
- expect(route_names).to include(:api_dummy_models)
17
- end
18
- end
19
- end
20
- end
@@ -1,19 +0,0 @@
1
- require 'rails_helper'
2
-
3
- describe OpenStax::Api do
4
- it 'is configurable' do
5
- expect(OpenStax::Api.configuration).to respond_to(:user_class_name)
6
- expect(OpenStax::Api.configuration).to respond_to(:current_user_method)
7
- expect(OpenStax::Api.configuration).to respond_to(:routing_error_app)
8
-
9
- expect(OpenStax::Api.configuration.user_class_name).to eq 'User'
10
-
11
- OpenStax::Api.configure { |config| config.user_class_name = 'Test' }
12
-
13
- expect(OpenStax::Api.configuration.user_class_name).to eq 'Test'
14
-
15
- OpenStax::Api.configure { |config| config.user_class_name = 'User' }
16
-
17
- expect(OpenStax::Api.configuration.user_class_name).to eq 'User'
18
- end
19
- end
@@ -1,47 +0,0 @@
1
- require 'rails_helper'
2
-
3
- module OpenStax
4
- module Api
5
- describe ApiUser do
6
- let(:user) { FactoryBot.create :user }
7
- let(:application) { double('Doorkeeper::Application') }
8
- let(:doorkeeper_token) { double('Doorkeeper::AccessToken') }
9
- let(:non_doorkeeper_user_proc) { lambda { user } }
10
-
11
- context 'human user' do
12
- let(:api_user) { ApiUser.new(nil, non_doorkeeper_user_proc) }
13
-
14
- it 'has a human_user but no application' do
15
- expect(api_user.application).to be_nil
16
- expect(api_user.human_user).to eq(user)
17
- end
18
- end
19
-
20
- context 'application with human user' do
21
- let(:api_user) { ApiUser.new(doorkeeper_token,
22
- non_doorkeeper_user_proc) }
23
-
24
- it 'has a human_user and an application' do
25
- allow(doorkeeper_token).to receive(:application).and_return(application)
26
- allow(doorkeeper_token).to receive(:resource_owner_id).and_return(user.id)
27
-
28
- expect(api_user.application).to eq(application)
29
- expect(api_user.human_user).to eq(user)
30
- end
31
- end
32
-
33
- context 'application only' do
34
- let(:api_user) { ApiUser.new(doorkeeper_token,
35
- non_doorkeeper_user_proc) }
36
-
37
- it 'has an application but no human_user' do
38
- allow(doorkeeper_token).to receive(:application).and_return(application)
39
- allow(doorkeeper_token).to receive(:resource_owner_id).and_return(nil)
40
-
41
- expect(api_user.application).to eq(application)
42
- expect(api_user.human_user).to be_nil
43
- end
44
- end
45
- end
46
- end
47
- end
data/spec/rails_helper.rb DELETED
@@ -1,54 +0,0 @@
1
- ENV["RAILS_ENV"] ||= 'test'
2
-
3
- require 'spec_helper'
4
- require File.expand_path("../dummy/config/environment", __FILE__)
5
- require 'rspec/rails'
6
- require 'factory_bot_rails'
7
- require 'faker'
8
- require 'squeel'
9
-
10
- # Add additional requires below this line. Rails is not loaded until this point!
11
-
12
- # Requires supporting ruby files with custom matchers and macros, etc, in
13
- # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
14
- # run as spec files by default. This means that files in spec/support that end
15
- # in _spec.rb will both be required and run as specs, causing the specs to be
16
- # run twice. It is recommended that you do not name files matching this glob to
17
- # end with _spec.rb. You can configure this pattern with the --pattern
18
- # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
19
- #
20
- # The following line is provided for convenience purposes. It has the downside
21
- # of increasing the boot-up time by auto-requiring all files in the support
22
- # directory. Alternatively, in the individual `*_spec.rb` files, manually
23
- # require only the support files necessary.
24
- #
25
- # Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
26
-
27
- # Checks for pending migrations before tests are run.
28
- # If you are not using ActiveRecord, you can remove this line.
29
- ActiveRecord::Migration.maintain_test_schema!
30
-
31
- RSpec.configure do |config|
32
- # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
33
- # config.fixture_path = "#{::Rails.root}/spec/fixtures"
34
-
35
- # If you're not using ActiveRecord, or you'd prefer not to run each of your
36
- # examples within a transaction, remove the following line or assign false
37
- # instead of true.
38
- # config.use_transactional_fixtures = true
39
-
40
- # RSpec Rails can automatically mix in different behaviours to your tests
41
- # based on their file location, for example enabling you to call `get` and
42
- # `post` in specs under `spec/controllers`.
43
- #
44
- # You can disable this behaviour by removing the line below, and instead
45
- # explicitly tag your specs with their type, e.g.:
46
- #
47
- # RSpec.describe UsersController, :type => :controller do
48
- # # ...
49
- # end
50
- #
51
- # The different available types are documented in the features, such as in
52
- # https://relishapp.com/rspec/rspec-rails/docs
53
- config.infer_spec_type_from_file_location!
54
- end
@@ -1,144 +0,0 @@
1
- require 'rails_helper'
2
-
3
- module OpenStax
4
- module Api
5
- module V1
6
- describe AbstractSearchRepresenter do
7
-
8
- before(:all) do
9
- 100.times do
10
- FactoryBot.create(:user)
11
- end
12
-
13
- User.where{name.like "% doe%"}.delete_all
14
-
15
- john_doe = FactoryBot.create :user, name: "John Doe",
16
- username: "doejohn",
17
- email: "john@doe.com"
18
- jane_doe = FactoryBot.create :user, name: "Jane Doe",
19
- username: "doejane",
20
- email: "jane@doe.com"
21
- jack_doe = FactoryBot.create :user, name: "Jack Doe",
22
- username: "doejack",
23
- email: "jack@doe.com"
24
-
25
- @john_hash = JSON.parse(UserRepresenter.new(john_doe).to_json)
26
- @jane_hash = JSON.parse(UserRepresenter.new(jane_doe).to_json)
27
- @jack_hash = JSON.parse(UserRepresenter.new(jack_doe).to_json)
28
- end
29
-
30
- it "represents search results" do
31
- outputs = SearchUsers.call(query: 'last_name:dOe').outputs
32
- response = JSON.parse(UserSearchRepresenter.new(outputs).to_json)
33
- total_count = response['total_count']
34
- items = response['items']
35
-
36
- expect(total_count).to eq 3
37
- expect(items).to include(@john_hash)
38
- expect(items).to include(@jane_hash)
39
- expect(items).to include(@jack_hash)
40
-
41
- outputs = SearchUsers.call(query: 'first_name:jOhN last_name:DoE')
42
- .outputs
43
- response = JSON.parse(UserSearchRepresenter.new(outputs).to_json)
44
- total_count = response['total_count']
45
- items = response['items']
46
-
47
- expect(total_count).to eq 1
48
- expect(items).to include(@john_hash)
49
- expect(items).not_to include(@jane_hash)
50
- expect(items).not_to include(@jack_hash)
51
-
52
- outputs = SearchUsers.call(
53
- query: 'first_name:JoHn,JaNe last_name:dOe'
54
- ).outputs
55
- response = JSON.parse(UserSearchRepresenter.new(outputs).to_json)
56
- total_count = response['total_count']
57
- items = response['items']
58
-
59
- expect(total_count).to eq 2
60
- expect(items).to include(@john_hash)
61
- expect(items).to include(@jane_hash)
62
- expect(items).not_to include(@jack_hash)
63
- end
64
-
65
- it "represents ordered results" do
66
- outputs = SearchUsers.call(query: 'username:DoE',
67
- order_by: 'cReAtEd_At AsC, iD')
68
- .outputs
69
- response = JSON.parse(UserSearchRepresenter.new(outputs).to_json)
70
- total_count = response['total_count']
71
- items = response['items']
72
-
73
- expect(total_count).to eq 3
74
- expect(items[0]).to eq @john_hash
75
- expect(items[1]).to eq @jane_hash
76
- expect(items[2]).to eq @jack_hash
77
-
78
- outputs = SearchUsers.call(query: 'username:dOe',
79
- order_by: 'CrEaTeD_aT dEsC, Id DeSc')
80
- .outputs
81
- response = JSON.parse(UserSearchRepresenter.new(outputs).to_json)
82
- total_count = response['total_count']
83
- items = response['items']
84
-
85
- expect(total_count).to eq 3
86
- expect(items[0]).to eq @jack_hash
87
- expect(items[1]).to eq @jane_hash
88
- expect(items[2]).to eq @john_hash
89
- end
90
-
91
- it "represents paginated results" do
92
- user_count = User.count
93
-
94
- outputs = SearchUsers.call(query: '').outputs
95
- response = JSON.parse(UserSearchRepresenter.new(outputs).to_json)
96
- total_count = response['total_count']
97
- items = response['items']
98
-
99
- expect(total_count).to eq user_count
100
- expect(items.count).to eq user_count
101
-
102
- outputs = SearchUsers.call(query: '', per_page: 20).outputs
103
- response = JSON.parse(UserSearchRepresenter.new(outputs).to_json)
104
- total_count = response['total_count']
105
- items = response['items']
106
-
107
- expect(total_count).to eq user_count
108
- expect(items.count).to eq 20
109
-
110
- for page in 1..5
111
- outputs = SearchUsers.call(query: '',
112
- page: page, per_page: 20).outputs
113
- response = JSON.parse(UserSearchRepresenter.new(outputs).to_json)
114
- total_count = response['total_count']
115
- items = response['items']
116
-
117
- expect(total_count).to eq user_count
118
- expect(items.count).to eq 20
119
- end
120
-
121
- outputs = SearchUsers.call(query: '',
122
- page: 1000, per_page: 20).outputs
123
- response = JSON.parse(UserSearchRepresenter.new(outputs).to_json)
124
- total_count = response['total_count']
125
- items = response['items']
126
-
127
- expect(total_count).to eq user_count
128
- expect(items.count).to eq 0
129
- end
130
-
131
- it "removes duplicate search results" do
132
- outputs = Hashie::Mash.new(items: [@john_hash, @john_hash, @jane_hash])
133
- response = JSON.parse(UserSearchRepresenter.new(outputs).to_json)
134
- total_count = response['total_count']
135
- items = response['items']
136
-
137
- expect(total_count).to eq 2
138
- expect(items.count).to eq 2
139
- end
140
-
141
- end
142
- end
143
- end
144
- end