supplejack_api 1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +80 -0
- data/Rakefile +42 -0
- data/app/assets/javascripts/supplejack_api/application.js +26 -0
- data/app/assets/stylesheets/supplejack_api/application.css +30 -0
- data/app/controllers/supplejack_api/admin/base_controller.rb +24 -0
- data/app/controllers/supplejack_api/admin/sessions_controller.rb +25 -0
- data/app/controllers/supplejack_api/admin/site_activities_controller.rb +21 -0
- data/app/controllers/supplejack_api/admin/users_controller.rb +55 -0
- data/app/controllers/supplejack_api/application_controller.rb +70 -0
- data/app/controllers/supplejack_api/concepts_controller.rb +52 -0
- data/app/controllers/supplejack_api/concerns/ignore_metrics.rb +14 -0
- data/app/controllers/supplejack_api/concerns/records_controller_metrics.rb +26 -0
- data/app/controllers/supplejack_api/concerns/set_items_controller_metrics.rb +20 -0
- data/app/controllers/supplejack_api/concerns/user_sets_controller_metrics.rb +26 -0
- data/app/controllers/supplejack_api/harvester/concepts_controller.rb +41 -0
- data/app/controllers/supplejack_api/harvester/fragments_controller.rb +30 -0
- data/app/controllers/supplejack_api/harvester/records_controller.rb +68 -0
- data/app/controllers/supplejack_api/metrics_api_controller.rb +44 -0
- data/app/controllers/supplejack_api/partners_controller.rb +39 -0
- data/app/controllers/supplejack_api/records_controller.rb +71 -0
- data/app/controllers/supplejack_api/schema_controller.rb +19 -0
- data/app/controllers/supplejack_api/set_items_controller.rb +38 -0
- data/app/controllers/supplejack_api/sources_controller.rb +67 -0
- data/app/controllers/supplejack_api/status_controller.rb +67 -0
- data/app/controllers/supplejack_api/user_sets_controller.rb +97 -0
- data/app/controllers/supplejack_api/users_controller.rb +49 -0
- data/app/helpers/supplejack_api/application_helper.rb +39 -0
- data/app/helpers/supplejack_api/facets_helper.rb +30 -0
- data/app/mailers/supplejack_api/request_limit_mailer.rb +43 -0
- data/app/models/supplejack_api/api_concept/concept_fragment.rb +31 -0
- data/app/models/supplejack_api/api_record/record_fragment.rb +15 -0
- data/app/models/supplejack_api/concept.rb +46 -0
- data/app/models/supplejack_api/concept_search.rb +86 -0
- data/app/models/supplejack_api/concerns/queryable_by_date.rb +31 -0
- data/app/models/supplejack_api/concerns/record.rb +108 -0
- data/app/models/supplejack_api/concerns/record_fragmentable.rb +24 -0
- data/app/models/supplejack_api/concerns/searchable.rb +430 -0
- data/app/models/supplejack_api/concerns/user_set.rb +219 -0
- data/app/models/supplejack_api/daily_metrics.rb +20 -0
- data/app/models/supplejack_api/faceted_metrics.rb +55 -0
- data/app/models/supplejack_api/fragment.rb +82 -0
- data/app/models/supplejack_api/interaction_models/record.rb +60 -0
- data/app/models/supplejack_api/interaction_models/set.rb +13 -0
- data/app/models/supplejack_api/interaction_updaters/all_usage_metric.rb +41 -0
- data/app/models/supplejack_api/interaction_updaters/set_metrics.rb +32 -0
- data/app/models/supplejack_api/interaction_updaters/usage_metrics.rb +73 -0
- data/app/models/supplejack_api/partner.rb +24 -0
- data/app/models/supplejack_api/preview_record.rb +30 -0
- data/app/models/supplejack_api/record.rb +18 -0
- data/app/models/supplejack_api/record_search.rb +12 -0
- data/app/models/supplejack_api/schema_definition.rb +147 -0
- data/app/models/supplejack_api/search.rb +14 -0
- data/app/models/supplejack_api/set_item.rb +57 -0
- data/app/models/supplejack_api/site_activity.rb +59 -0
- data/app/models/supplejack_api/source.rb +28 -0
- data/app/models/supplejack_api/source_activity.rb +36 -0
- data/app/models/supplejack_api/source_authority.rb +42 -0
- data/app/models/supplejack_api/supplejack_schema.rb +37 -0
- data/app/models/supplejack_api/support/concept/searchable.rb +79 -0
- data/app/models/supplejack_api/support/concept/storable.rb +59 -0
- data/app/models/supplejack_api/support/fragment_helpers.rb +96 -0
- data/app/models/supplejack_api/support/harvestable.rb +105 -0
- data/app/models/supplejack_api/support/searchable.rb +115 -0
- data/app/models/supplejack_api/support/status_logger.rb +23 -0
- data/app/models/supplejack_api/support/storable.rb +49 -0
- data/app/models/supplejack_api/support/validation_logger.rb +23 -0
- data/app/models/supplejack_api/usage_metrics.rb +25 -0
- data/app/models/supplejack_api/user.rb +211 -0
- data/app/models/supplejack_api/user_activity.rb +65 -0
- data/app/models/supplejack_api/user_set.rb +17 -0
- data/app/serializers/supplejack_api/application_serializer.rb +12 -0
- data/app/serializers/supplejack_api/concept_record_serializer.rb +23 -0
- data/app/serializers/supplejack_api/concept_search_serializer.rb +12 -0
- data/app/serializers/supplejack_api/concept_serializer.rb +100 -0
- data/app/serializers/supplejack_api/concerns/record_serializable.rb +119 -0
- data/app/serializers/supplejack_api/concerns/search_serializable.rb +24 -0
- data/app/serializers/supplejack_api/record_search_serializer.rb +64 -0
- data/app/serializers/supplejack_api/record_serializer.rb +13 -0
- data/app/serializers/supplejack_api/search_serializer.rb +13 -0
- data/app/serializers/supplejack_api/source_authority_serializer.rb +23 -0
- data/app/serializers/supplejack_api/user_serializer.rb +13 -0
- data/app/serializers/supplejack_api/user_set_record_serializer.rb +14 -0
- data/app/serializers/supplejack_api/user_set_serializer.rb +77 -0
- data/app/services/metrics_api/v3/api.rb +30 -0
- data/app/services/metrics_api/v3/endpoints/facets.rb +23 -0
- data/app/services/metrics_api/v3/endpoints/global.rb +26 -0
- data/app/services/metrics_api/v3/endpoints/helpers.rb +14 -0
- data/app/services/metrics_api/v3/endpoints/root.rb +97 -0
- data/app/services/metrics_api/v3/presenters/daily_metric.rb +23 -0
- data/app/services/metrics_api/v3/presenters/extended_metadata.rb +41 -0
- data/app/services/metrics_api/v3/presenters/record.rb +26 -0
- data/app/services/metrics_api/v3/presenters/view.rb +28 -0
- data/app/views/layouts/supplejack_api/_head.html.erb +17 -0
- data/app/views/layouts/supplejack_api/_top_nav.html.erb +33 -0
- data/app/views/layouts/supplejack_api/application.html.erb +33 -0
- data/app/views/supplejack_api/admin/sessions/new.html.erb +25 -0
- data/app/views/supplejack_api/admin/shared/_links.erb +34 -0
- data/app/views/supplejack_api/admin/site_activities/index.csv.erb +22 -0
- data/app/views/supplejack_api/admin/site_activities/index.html.erb +54 -0
- data/app/views/supplejack_api/admin/users/edit.html.erb +16 -0
- data/app/views/supplejack_api/admin/users/index.csv.erb +22 -0
- data/app/views/supplejack_api/admin/users/index.html.erb +57 -0
- data/app/views/supplejack_api/admin/users/show.html.erb +33 -0
- data/app/views/supplejack_api/request_limit_mailer/at100percent.text.erb +13 -0
- data/app/views/supplejack_api/request_limit_mailer/at100percent_admin.text.erb +1 -0
- data/app/views/supplejack_api/request_limit_mailer/at90percent.text.erb +13 -0
- data/app/views/supplejack_api/request_limit_mailer/at90percent_admin.text.erb +1 -0
- data/app/workers/supplejack_api/daily_metrics_worker.rb +112 -0
- data/app/workers/supplejack_api/interaction_metrics_worker.rb +45 -0
- data/config/cucumber.yml +16 -0
- data/config/initializers/force_eagerload.rb +11 -0
- data/config/initializers/interaction_updaters.rb +7 -0
- data/config/routes.rb +81 -0
- data/db/binding_records.development.json +7 -0
- data/db/binding_records.staging.json +7 -0
- data/db/concepts.json +33 -0
- data/db/concepts_data.json +569745 -0
- data/db/source_authorities.json +119 -0
- data/lib/generators/supplejack_api/install_generator.rb +124 -0
- data/lib/mongoid/paperclip.rb +53 -0
- data/lib/mongoid/string.rb +20 -0
- data/lib/sunspot/mongoid.rb +51 -0
- data/lib/sunspot/resque_session_proxy.rb +72 -0
- data/lib/sunspot/sunspot_spellcheck.rb +98 -0
- data/lib/supplejack_api.rb +13 -0
- data/lib/supplejack_api/admin/sortable.rb +40 -0
- data/lib/supplejack_api/engine.rb +46 -0
- data/lib/supplejack_api/harvester_constraint.rb +28 -0
- data/lib/supplejack_api/stylesheets.rb +36 -0
- data/lib/supplejack_api/utils.rb +75 -0
- data/lib/supplejack_api/version.rb +11 -0
- data/lib/tasks/resque.rake +22 -0
- data/lib/url_validator.rb +38 -0
- data/spec/controllers/supplejack_api/admin/base_controller_spec.rb +55 -0
- data/spec/controllers/supplejack_api/admin/site_activities_controller_spec.rb +43 -0
- data/spec/controllers/supplejack_api/admin/users_controller_spec.rb +79 -0
- data/spec/controllers/supplejack_api/application_controller_spec.rb +133 -0
- data/spec/controllers/supplejack_api/concepts_controller_spec.rb +83 -0
- data/spec/controllers/supplejack_api/concerns/records_controller_metrics_spec.rb +60 -0
- data/spec/controllers/supplejack_api/harvester/concepts_controller_spec.rb +57 -0
- data/spec/controllers/supplejack_api/harvester/records_controller_spec.rb +138 -0
- data/spec/controllers/supplejack_api/metrics_api_controller_spec.rb +71 -0
- data/spec/controllers/supplejack_api/partners_controller_spec.rb +77 -0
- data/spec/controllers/supplejack_api/records_controller_spec.rb +143 -0
- data/spec/controllers/supplejack_api/set_items_controller_spec.rb +78 -0
- data/spec/controllers/supplejack_api/sources_controller_spec.rb +161 -0
- data/spec/controllers/supplejack_api/status_controller_spec.rb +120 -0
- data/spec/controllers/supplejack_api/user_sets_controller_spec.rb +233 -0
- data/spec/controllers/supplejack_api/users_controller_spec.rb +61 -0
- data/spec/dummy/README.rdoc +268 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +23 -0
- data/spec/dummy/app/assets/stylesheets/application.css +22 -0
- data/spec/dummy/app/controllers/application_controller.rb +10 -0
- data/spec/dummy/app/helpers/application_helper.rb +9 -0
- data/spec/dummy/app/supplejack_api/concept_schema.rb +83 -0
- data/spec/dummy/app/supplejack_api/record_schema.rb +64 -0
- data/spec/dummy/app/supplejack_api/record_schema.txt +66 -0
- data/spec/dummy/app/views/layouts/application.html.erb +23 -0
- data/spec/dummy/app/workers/supplejack_api/clear_index_buffer.rb +32 -0
- data/spec/dummy/app/workers/supplejack_api/flush_old_records_worker.rb +17 -0
- data/spec/dummy/app/workers/supplejack_api/index_buffer.rb +49 -0
- data/spec/dummy/app/workers/supplejack_api/index_source_worker.rb +39 -0
- data/spec/dummy/app/workers/supplejack_api/index_worker.rb +82 -0
- data/spec/dummy/app/workers/supplejack_api/store_user_activity_worker.rb +24 -0
- data/spec/dummy/config.ru +11 -0
- data/spec/dummy/config/application.rb +76 -0
- data/spec/dummy/config/application.yml +31 -0
- data/spec/dummy/config/application.yml.example +36 -0
- data/spec/dummy/config/boot.rb +17 -0
- data/spec/dummy/config/environment.rb +12 -0
- data/spec/dummy/config/environments/development.rb +42 -0
- data/spec/dummy/config/environments/production.rb +76 -0
- data/spec/dummy/config/environments/test.rb +46 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +14 -0
- data/spec/dummy/config/initializers/devise.rb +214 -0
- data/spec/dummy/config/initializers/inflections.rb +22 -0
- data/spec/dummy/config/initializers/kaminari_config.rb +17 -0
- data/spec/dummy/config/initializers/mime_types.rb +12 -0
- data/spec/dummy/config/initializers/mongoid.rb +9 -0
- data/spec/dummy/config/initializers/quiet_logger.rb +23 -0
- data/spec/dummy/config/initializers/resque.rb +18 -0
- data/spec/dummy/config/initializers/secret_token.rb +14 -0
- data/spec/dummy/config/initializers/session_store.rb +15 -0
- data/spec/dummy/config/initializers/simple_form.rb +149 -0
- data/spec/dummy/config/initializers/simple_form_foundation.rb +33 -0
- data/spec/dummy/config/initializers/state_machine.rb +8 -0
- data/spec/dummy/config/initializers/sunspot.rb +32 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +17 -0
- data/spec/dummy/config/locales/devise.en.yml +63 -0
- data/spec/dummy/config/locales/en.yml +66 -0
- data/spec/dummy/config/locales/simple_form.en.yml +33 -0
- data/spec/dummy/config/mongoid.travis.yml +34 -0
- data/spec/dummy/config/mongoid.yml +36 -0
- data/spec/dummy/config/resque-pool.yml +18 -0
- data/spec/dummy/config/resque_schedule.yml +12 -0
- data/spec/dummy/config/routes.rb +11 -0
- data/spec/dummy/config/secrets.yml +8 -0
- data/spec/dummy/config/sunspot.yml +38 -0
- data/spec/dummy/db/concepts.json +37 -0
- data/spec/dummy/db/source_authorities.json +116 -0
- data/spec/dummy/public/404.html +35 -0
- data/spec/dummy/public/422.html +35 -0
- data/spec/dummy/public/500.html +34 -0
- data/spec/dummy/public/favicon.ico +8 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/solr/collection1/conf/elevate.xml +36 -0
- data/spec/dummy/solr/collection1/conf/mapping-FoldToASCII.txt +3813 -0
- data/spec/dummy/solr/collection1/conf/schema.xml +292 -0
- data/spec/dummy/solr/collection1/conf/solrconfig.xml +1780 -0
- data/spec/dummy/solr/collection1/conf/spellings.txt +2 -0
- data/spec/dummy/solr/collection1/conf/stopwords.txt +58 -0
- data/spec/dummy/solr/collection1/conf/synonyms.txt +31 -0
- data/spec/factories/concepts.rb +16 -0
- data/spec/factories/daily_metrics.rb +10 -0
- data/spec/factories/faceted_metrics.rb +25 -0
- data/spec/factories/fragment.rb +14 -0
- data/spec/factories/partners.rb +14 -0
- data/spec/factories/record_interaction.rb +17 -0
- data/spec/factories/records.rb +47 -0
- data/spec/factories/set_interaction.rb +15 -0
- data/spec/factories/source_authorities.rb +19 -0
- data/spec/factories/sources.rb +16 -0
- data/spec/factories/usage_metrics.rb +20 -0
- data/spec/factories/user_activities.rb +14 -0
- data/spec/factories/user_sets.rb +27 -0
- data/spec/factories/users.rb +23 -0
- data/spec/helpers/application_helper_spec.rb +35 -0
- data/spec/models/concept_spec.rb +163 -0
- data/spec/models/fragment_spec.rb +15 -0
- data/spec/models/site_activity_spec.rb +103 -0
- data/spec/models/source_activity_spec.rb +56 -0
- data/spec/models/source_authority_spec.rb +42 -0
- data/spec/models/supplejack_api/api_concept/concept_fragment_spec.rb +189 -0
- data/spec/models/supplejack_api/api_record/record_fragment_spec.rb +189 -0
- data/spec/models/supplejack_api/concept_spec.rb +0 -0
- data/spec/models/supplejack_api/interaction_updaters/all_usage_metric_spec.rb +41 -0
- data/spec/models/supplejack_api/interaction_updaters/set_metrics_spec.rb +32 -0
- data/spec/models/supplejack_api/interaction_updaters/usage_metrics_spec.rb +44 -0
- data/spec/models/supplejack_api/partner_spec.rb +20 -0
- data/spec/models/supplejack_api/record_search_spec.rb +429 -0
- data/spec/models/supplejack_api/record_spec.rb +280 -0
- data/spec/models/supplejack_api/schema_definition_spec.rb +272 -0
- data/spec/models/supplejack_api/search_spec.rb +243 -0
- data/spec/models/supplejack_api/set_item_spec.rb +123 -0
- data/spec/models/supplejack_api/source_spec.rb +22 -0
- data/spec/models/supplejack_api/support/fragment_helpers_spec.rb +216 -0
- data/spec/models/supplejack_api/support/harvestable_spec.rb +177 -0
- data/spec/models/supplejack_api/support/searchable_spec.rb +85 -0
- data/spec/models/supplejack_api/support/storable_spec.rb +18 -0
- data/spec/models/supplejack_api/user_activity_spec.rb +88 -0
- data/spec/models/supplejack_api/user_set_spec.rb +559 -0
- data/spec/models/supplejack_api/user_spec.rb +348 -0
- data/spec/routing/concepts_routing_spec.rb +22 -0
- data/spec/routing/harvester_constraint_spec.rb +66 -0
- data/spec/routing/harvester_routing_spec.rb +51 -0
- data/spec/routing/partner_routing_spec.rb +34 -0
- data/spec/routing/records_routing_spec.rb +26 -0
- data/spec/routing/sets_routing_spec.rb +44 -0
- data/spec/routing/source_routing_spec.rb +38 -0
- data/spec/routing/users_routing_spec.rb +18 -0
- data/spec/serializers/concept_record_serializer_spec.rb +30 -0
- data/spec/serializers/supplejack_api/application_serializer_spec.rb +14 -0
- data/spec/serializers/supplejack_api/concept_search_serializer_spec.rb +34 -0
- data/spec/serializers/supplejack_api/concept_serializer_spec.rb +92 -0
- data/spec/serializers/supplejack_api/record_search_serializer_spec.rb +34 -0
- data/spec/serializers/supplejack_api/record_serializer_spec.rb +271 -0
- data/spec/serializers/supplejack_api/search_serializer_spec.rb +90 -0
- data/spec/serializers/supplejack_api/user_serializer_spec.rb +22 -0
- data/spec/serializers/supplejack_api/user_set_serializer_spec.rb +96 -0
- data/spec/services/metrics_api/v3/api_spec.rb +23 -0
- data/spec/services/metrics_api/v3/endpoints/facets_spec.rb +25 -0
- data/spec/services/metrics_api/v3/endpoints/global_spec.rb +22 -0
- data/spec/services/metrics_api/v3/endpoints/root_spec.rb +49 -0
- data/spec/services/metrics_api/v3/presenters/extended_metadata_spec.rb +49 -0
- data/spec/spec_helper.rb +70 -0
- data/spec/support/api/schemas/metrics/daily_metrics_metadata.json +24 -0
- data/spec/support/api/schemas/metrics/extended_metrics.json +26 -0
- data/spec/support/api/schemas/metrics/extended_response.json +6 -0
- data/spec/support/api/schemas/metrics/facets_response.json +6 -0
- data/spec/support/api/schemas/metrics/record_metadata.json +33 -0
- data/spec/support/api/schemas/metrics/top_level_response.json +6 -0
- data/spec/support/api/schemas/metrics/view_metadata.json +33 -0
- data/spec/support/api_schema_matcher.rb +14 -0
- data/spec/support/devise.rb +10 -0
- data/spec/workers/supplejack_api/daily_metrics_worker_spec.rb +140 -0
- data/spec/workers/supplejack_api/flush_old_records_worker_spec.rb +20 -0
- data/spec/workers/supplejack_api/index_worker_spec.rb +52 -0
- data/spec/workers/supplejack_api/source_index_worker_spec.rb +61 -0
- metadata +1231 -0
@@ -0,0 +1,133 @@
|
|
1
|
+
# The majority of the Supplejack API code is Crown copyright (C) 2014, New Zealand Government,
|
2
|
+
# and is licensed under the GNU General Public License, version 3.
|
3
|
+
# One component is a third party component. See https://github.com/DigitalNZ/supplejack_api for details.
|
4
|
+
#
|
5
|
+
# Supplejack was created by DigitalNZ at the National Library of NZ and
|
6
|
+
# the Department of Internal Affairs. http://digitalnz.org/supplejack
|
7
|
+
|
8
|
+
require 'spec_helper'
|
9
|
+
|
10
|
+
module SupplejackApi
|
11
|
+
describe ApplicationController, type: :controller do
|
12
|
+
routes { SupplejackApi::Engine.routes }
|
13
|
+
|
14
|
+
before(:each) do
|
15
|
+
@controller = ApplicationController.new
|
16
|
+
allow(@controller).to receive(:render) { nil }
|
17
|
+
allow(RecordSchema).to receive(:roles) { { admin: double(:admin, admin: true) } }
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#authenticate_user!' do
|
21
|
+
before(:each) do
|
22
|
+
allow(@controller).to receive(:params) { { api_key: '12345' } }
|
23
|
+
allow(@controller).to receive(:request) { double(:request, ip: '1.1.1.1', format: :json) }
|
24
|
+
@user = FactoryGirl.create(:user)
|
25
|
+
allow(@user).to receive(:update_daily_activity) { nil }
|
26
|
+
allow(@controller).to receive(:current_user) { @user }
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should set the current_user' do
|
30
|
+
@controller.authenticate_user!
|
31
|
+
expect(@controller.current_user).to eq @user
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'updates the tracked fields for the user' do
|
35
|
+
expect(@user).to receive(:update_tracked_fields)
|
36
|
+
@controller.authenticate_user!
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'updates the daily activity for the user' do
|
40
|
+
expect(@user).to receive(:update_daily_activity)
|
41
|
+
@controller.authenticate_user!
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'verifies the user limits' do
|
45
|
+
expect(@user).to receive(:check_daily_requests)
|
46
|
+
@controller.authenticate_user!
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'saves the user' do
|
50
|
+
expect(@user).to receive(:save)
|
51
|
+
@controller.authenticate_user!
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'user over daily requests limit' do
|
55
|
+
it 'returns a error message' do
|
56
|
+
allow(@user).to receive(:over_limit?) { true }
|
57
|
+
expect(@controller).to receive(:render).with({ json: { errors: 'You have reached your maximum number of api requests today' }, status: :forbidden })
|
58
|
+
@controller.authenticate_user!
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'api_key not found' do
|
63
|
+
it 'returns a error message' do
|
64
|
+
allow(@controller).to receive(:current_user) { nil }
|
65
|
+
expect(@controller).to receive(:render).with({ json: { errors: 'Invalid API Key' }, status: :forbidden})
|
66
|
+
@controller.authenticate_user!
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'api key not provided' do
|
71
|
+
it 'returns a error message' do
|
72
|
+
allow(@controller).to receive(:params) { { api_key: ''} }
|
73
|
+
expect(@controller).to receive(:render).with({ json: { errors: 'Please provide a API Key' }, status: :forbidden })
|
74
|
+
@controller.authenticate_user!
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'wrong format' do
|
79
|
+
it 'returns a error message in the default format' do
|
80
|
+
allow(@controller).to receive(:params) { {} }
|
81
|
+
allow(@controller).to receive(:request) { double(:request, format: :css).as_null_object }
|
82
|
+
expect(@controller).to receive(:render).with({ json: { errors: 'Please provide a API Key' }, status: :forbidden })
|
83
|
+
@controller.authenticate_user!
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "#authenticate_admin!" do
|
89
|
+
before {
|
90
|
+
allow(@controller).to receive(:request) { double(:request, :ip => "1.1.1.1", :format => :json)}
|
91
|
+
}
|
92
|
+
|
93
|
+
it "returns true when the admin authentication was successful" do
|
94
|
+
allow(@controller).to receive(:current_user) { double(:user, admin?: true, role: 'admin') }
|
95
|
+
expect(@controller.authenticate_admin!).to be_truthy
|
96
|
+
end
|
97
|
+
|
98
|
+
it "returns false when the admin authentication was not successful" do
|
99
|
+
allow(@controller).to receive(:current_user) { double(:user, admin?: false, role: 'developer') }
|
100
|
+
expect(@controller.authenticate_admin!).to be_falsey
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "#find_user_set" do
|
105
|
+
context "current_user is a admin" do
|
106
|
+
before :each do
|
107
|
+
@user_set = double(:set).as_null_object
|
108
|
+
allow(@controller).to receive(:current_user) { double(:user, admin?: true, role: 'admin').as_null_object }
|
109
|
+
allow(@controller).to receive(:params) { {:id => "12345"} }
|
110
|
+
end
|
111
|
+
|
112
|
+
it "finds the set even if it's not owned by the current_user" do
|
113
|
+
expect(UserSet).to receive(:custom_find).with("12345") { @user_set }
|
114
|
+
@controller.find_user_set
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# TODO: Move this to the app
|
119
|
+
# context "current_user has dnz role" do
|
120
|
+
# before :each do
|
121
|
+
# @user_set = double(:set).as_null_object
|
122
|
+
# allow(@controller).to receive(:current_user) { double(:user, dnz?: true).as_null_object }
|
123
|
+
# allow(@controller).to receive(:params) { {:id => "12345"} }
|
124
|
+
# end
|
125
|
+
|
126
|
+
# it "finds the set even if it's not owned by the current_user" do
|
127
|
+
# expect(UserSet).to receive(:custom_find).with("12345") { @user_set }
|
128
|
+
# @controller.find_user_set
|
129
|
+
# end
|
130
|
+
# end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# The majority of the Supplejack API code is Crown copyright (C) 2014, New Zealand Government,
|
2
|
+
# and is licensed under the GNU General Public License, version 3.
|
3
|
+
# One component is a third party component. See https://github.com/DigitalNZ/supplejack_api for details.
|
4
|
+
#
|
5
|
+
# Supplejack was created by DigitalNZ at the National Library of NZ and
|
6
|
+
# the Department of Internal Affairs. http://digitalnz.org/supplejack
|
7
|
+
|
8
|
+
require 'spec_helper'
|
9
|
+
|
10
|
+
module SupplejackApi
|
11
|
+
describe ConceptsController, type: :controller do
|
12
|
+
routes { SupplejackApi::Engine.routes }
|
13
|
+
|
14
|
+
before { @user = FactoryGirl.create(:user, authentication_token: 'apikey', role: 'developer') }
|
15
|
+
|
16
|
+
describe 'GET show' do
|
17
|
+
before {
|
18
|
+
@concept = FactoryGirl.create(:concept)
|
19
|
+
allow(controller).to receive(:current_user) { @user }
|
20
|
+
}
|
21
|
+
|
22
|
+
it 'should find the concept and assign it' do
|
23
|
+
expect(Concept).to receive(:custom_find).with('123', @user, {}).and_return(@concept)
|
24
|
+
get :show, id: 123, search: {}, api_key: 'abc123', format: "json"
|
25
|
+
expect(assigns(:concept)).to eq(@concept)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'renders a error when records is not found' do
|
29
|
+
allow(Concept).to receive(:custom_find).and_raise(Mongoid::Errors::DocumentNotFound.new(Concept, ['123'], ['123']))
|
30
|
+
get :show, id: 123, search: {}, api_key: 'abc123', :format => 'json'
|
31
|
+
expect(response.body).to eq({:errors => 'Concept with ID 123 was not found'}.to_json)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# describe 'GET index' do
|
36
|
+
# before {
|
37
|
+
# @search = ConceptSearch.new
|
38
|
+
# allow(@search).to receive(:valid?) { false }
|
39
|
+
# allow(@search).to receive(:new) { @search }
|
40
|
+
# }
|
41
|
+
|
42
|
+
# it 'initializes a new search instance' do
|
43
|
+
# expect(ConceptSearch).to receive(:new).with(hash_including(text: 'dogs')).and_return(@search)
|
44
|
+
# get :index, api_key: 'apikey', text: 'dogs'
|
45
|
+
# expect(assigns(:search)).to eq(@search)
|
46
|
+
# end
|
47
|
+
|
48
|
+
# it 'sets the request url on search object' do
|
49
|
+
# allow_any_instance_of(ConceptSearch).to receive(:valid?) { false }
|
50
|
+
# allow(controller.request).to receive(:original_url).and_return('http://foo.com/blah')
|
51
|
+
# get :index, api_key: 'apikey', format: "json"
|
52
|
+
# expect(assigns(:search).request_url).to eq 'http://foo.com/blah'
|
53
|
+
# end
|
54
|
+
|
55
|
+
# it 'should set the current_user on the search' do
|
56
|
+
# allow_any_instance_of(ConceptSearch).to receive(:valid?) { false }
|
57
|
+
# get :index, api_key: 'apikey', format: "json"
|
58
|
+
# expect(assigns(:search).scope).to eq @user
|
59
|
+
# end
|
60
|
+
|
61
|
+
# it 'renders a the solr error when the query is invalid' do
|
62
|
+
# allow(ConceptSearchSerializer).to receive(:new).and_raise(RSolr::Error::Http.new({}, {}))
|
63
|
+
# allow(controller).to receive(:solr_error_message).and_return('Error')
|
64
|
+
# get :index, api_key: 'apikey', format: 'json'
|
65
|
+
# expect(response.body).to eq({errors: 'Error'}.to_json)
|
66
|
+
# end
|
67
|
+
|
68
|
+
# it 'renders a error when the requested field doesn\'t exist' do
|
69
|
+
# allow(ConceptSearchSerializer).to receive(:new).and_raise(Sunspot::UnrecognizedFieldError.new('No field configured for Concept with name "something"'))
|
70
|
+
# get :index, api_key: 'apikey', format: 'json', and: { something: true }
|
71
|
+
# expect(response.body).to eq({ errors: 'No field configured for Concept with name "something"' }.to_json)
|
72
|
+
# end
|
73
|
+
|
74
|
+
# it 'should return an error if the search request is invalid' do
|
75
|
+
# allow_any_instance_of(ConceptSearch).to receive(:valid?) { false }
|
76
|
+
# allow_any_instance_of(ConceptSearch).to receive(:errors) { ['The page parameter can not exceed 100,000'] }
|
77
|
+
# get :index, api_key: 'apikey', page: 100001, format: 'json'
|
78
|
+
# expect(response.body).to eq({ errors: ['The page parameter can not exceed 100,000'] }.to_json)
|
79
|
+
# end
|
80
|
+
# end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ApplicationController, type: :controller do
|
4
|
+
|
5
|
+
class DummySearch < BasicObject
|
6
|
+
def initialize(results)
|
7
|
+
@results = results
|
8
|
+
end
|
9
|
+
|
10
|
+
def results
|
11
|
+
@results
|
12
|
+
end
|
13
|
+
|
14
|
+
def valid?
|
15
|
+
true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
controller do
|
20
|
+
include SupplejackApi::Concerns::RecordsControllerMetrics
|
21
|
+
|
22
|
+
def index
|
23
|
+
@search = DummySearch.new(SupplejackApi::Record.all.to_a)
|
24
|
+
render nothing: true
|
25
|
+
end
|
26
|
+
|
27
|
+
def show
|
28
|
+
@record = SupplejackApi::Record.first
|
29
|
+
render nothing: true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
before do
|
34
|
+
create(:record_with_fragment)
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'GET#index' do
|
38
|
+
it 'creates an interation model when ignore_metrics is not set' do
|
39
|
+
get :index
|
40
|
+
expect(SupplejackApi::InteractionModels::Record.first.request_type).to eq "search"
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'does not create an interaction model when ignore_metrics :true' do
|
44
|
+
get :index, ignore_metrics: true
|
45
|
+
expect(SupplejackApi::InteractionModels::Record.count).to eq 0
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'GET#show' do
|
50
|
+
it 'creates an interaction model when ignore_metrics is not set' do
|
51
|
+
get :show, id: 1
|
52
|
+
expect(SupplejackApi::InteractionModels::Record.first.request_type).to eq "get"
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'does not create an interaction model when ignore_metrics :true' do
|
56
|
+
get :show, id: 1, ignore_metrics: true
|
57
|
+
expect(SupplejackApi::InteractionModels::Record.count).to eq 0
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# The majority of the Supplejack API code is Crown copyright (C) 2014, New Zealand Government,
|
2
|
+
# and is licensed under the GNU General Public License, version 3.
|
3
|
+
# One component is a third party component. See https://github.com/DigitalNZ/supplejack_api for details.
|
4
|
+
#
|
5
|
+
# Supplejack was created by DigitalNZ at the National Library of NZ and
|
6
|
+
# the Department of Internal Affairs. http://digitalnz.org/supplejack
|
7
|
+
|
8
|
+
require "spec_helper"
|
9
|
+
|
10
|
+
module SupplejackApi
|
11
|
+
describe Harvester::ConceptsController, type: :controller do
|
12
|
+
# routes { SupplejackApi::Engine.routes }
|
13
|
+
|
14
|
+
# let(:concept) { FactoryGirl.build(:concept) }
|
15
|
+
|
16
|
+
# describe "POST create" do
|
17
|
+
# before(:each) do
|
18
|
+
# allow(Concept).to receive(:find_or_initialize_by_identifier) { concept }
|
19
|
+
# end
|
20
|
+
|
21
|
+
# context "preview is false" do
|
22
|
+
# it "finds or initializes a concept by identifier" do
|
23
|
+
# expect(Concept).to receive(:find_or_initialize_by_identifier).with("internal_identifier" => "1234") { concept }
|
24
|
+
# post :create, concept: {internal_identifier: "1234"}
|
25
|
+
# expect(assigns(:concept)).to eq concept
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
|
29
|
+
# context "preview is true" do
|
30
|
+
# it "finds or initializes a preview record by identifier" do
|
31
|
+
# expect(PreviewRecord).to receive(:find_or_initialize_by_identifier).with("internal_identifier" => "1234") { concept }
|
32
|
+
# post :create, concept: {internal_identifier: "1234"}, preview: true
|
33
|
+
# expect(assigns(:concept)).to eq concept
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
|
38
|
+
# describe "PUT update" do
|
39
|
+
# let(:concept) { double(:concept).as_null_object }
|
40
|
+
|
41
|
+
# before do
|
42
|
+
# allow(Concept).to receive(:custom_find) { concept }
|
43
|
+
# end
|
44
|
+
|
45
|
+
# it 'finds the record and assigns it' do
|
46
|
+
# expect(Concept).to receive(:custom_find).with('123', nil, {status: :all}) { concept }
|
47
|
+
# put :update, id: 123, concept: { status: 'supressed' }, format: :json
|
48
|
+
# expect(assigns(:concept)).to eq(concept)
|
49
|
+
# end
|
50
|
+
|
51
|
+
# it "updates the status of the record" do
|
52
|
+
# expect(concept).to receive(:update_attribute).with(:status, 'supressed')
|
53
|
+
# put :update, id: 123, concept: { status: 'supressed' }, format: :json
|
54
|
+
# end
|
55
|
+
# end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
# The majority of the Supplejack API code is Crown copyright (C) 2014, New Zealand Government,
|
2
|
+
# and is licensed under the GNU General Public License, version 3.
|
3
|
+
# One component is a third party component. See https://github.com/DigitalNZ/supplejack_api for details.
|
4
|
+
#
|
5
|
+
# Supplejack was created by DigitalNZ at the National Library of NZ and
|
6
|
+
# the Department of Internal Affairs. http://digitalnz.org/supplejack
|
7
|
+
|
8
|
+
require "spec_helper"
|
9
|
+
|
10
|
+
module SupplejackApi
|
11
|
+
describe Harvester::RecordsController, type: :controller do
|
12
|
+
routes { SupplejackApi::Engine.routes }
|
13
|
+
|
14
|
+
let(:record) { FactoryGirl.build(:record) }
|
15
|
+
|
16
|
+
describe "POST create" do
|
17
|
+
before(:each) do
|
18
|
+
allow(Record).to receive(:find_or_initialize_by_identifier) { record }
|
19
|
+
end
|
20
|
+
|
21
|
+
context "preview is false" do
|
22
|
+
it "finds or initializes a record by identifier" do
|
23
|
+
expect(Record).to receive(:find_or_initialize_by_identifier).with("internal_identifier" => "1234") { record }
|
24
|
+
post :create, record: {internal_identifier: "1234"}
|
25
|
+
expect(assigns(:record)).to eq record
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "preview is true" do
|
30
|
+
it "finds or initializes a preview record by identifier" do
|
31
|
+
expect(PreviewRecord).to receive(:find_or_initialize_by_identifier).with("internal_identifier" => "1234") { record }
|
32
|
+
post :create, record: {internal_identifier: "1234"}, preview: true
|
33
|
+
expect(assigns(:record)).to eq record
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "record has a priority other then 0" do
|
38
|
+
it "creates or updates the fragment" do
|
39
|
+
rec = {
|
40
|
+
"internal_identifier" => "1234",
|
41
|
+
"title" => "Hi",
|
42
|
+
"priority" => "10"
|
43
|
+
}
|
44
|
+
expect(record).to receive(:create_or_update_fragment).with(rec)
|
45
|
+
post :create, record: rec
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it "sets the status based on the required fragments" do
|
50
|
+
expect(record).to receive(:set_status).with(['ndha_rights'])
|
51
|
+
post :create, record: {"internal_identifier" => "1234"}, required_fragments: ['ndha_rights']
|
52
|
+
end
|
53
|
+
|
54
|
+
it "saves the record" do
|
55
|
+
expect(record).to receive(:save)
|
56
|
+
post :create, record: {"internal_identifier" => "1234"}
|
57
|
+
end
|
58
|
+
|
59
|
+
it "unsets null fields" do
|
60
|
+
expect(record).to receive(:unset_null_fields)
|
61
|
+
post :create, record: {"internal_identifier" => "1234"}
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "PUT delete" do
|
66
|
+
it "should find the record by internal_identifier" do
|
67
|
+
expect(Record).to receive(:where).with({internal_identifier: "abc123"}) { [record] }
|
68
|
+
put :delete, id: "abc123"
|
69
|
+
expect(assigns(:record)).to eq record
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should update the records status attribute to deleted" do
|
73
|
+
allow(Record).to receive(:where) { [record] }
|
74
|
+
expect(record).to receive(:update_attribute).with(:status, "deleted")
|
75
|
+
put :delete, id: "abc123"
|
76
|
+
end
|
77
|
+
|
78
|
+
it "handles a nil record" do
|
79
|
+
allow(Record).to receive(:where) { [] }
|
80
|
+
expect { put :delete, id: "abc123" }.to_not raise_exception
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "DELETE flush" do
|
85
|
+
before do
|
86
|
+
allow(Record).to receive(:flush_old_records)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "calls flush_old_records" do
|
90
|
+
expect(Resque).to receive(:enqueue).with(FlushOldRecordsWorker, 'tapuhi', 'abc123')
|
91
|
+
delete :flush, source_id: 'tapuhi', job_id: 'abc123'
|
92
|
+
end
|
93
|
+
|
94
|
+
it "returns a 204" do
|
95
|
+
delete :flush, source_id: 'tapuhi', job_id: 'abc123'
|
96
|
+
expect(response.code).to eq '204'
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe 'GET #show' do
|
101
|
+
it 'should find the record by internal_identifier' do
|
102
|
+
expect(Record).to receive(:where).with({ record_id: 'abc123' }) { [record] }
|
103
|
+
get :show, id: 'abc123'
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should assign the record to @record' do
|
107
|
+
allow(Record).to receive(:where) { [record] }
|
108
|
+
get :show, id: 'abc123'
|
109
|
+
expect(assigns(:record)).to eq record
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'should handle a nil record' do
|
113
|
+
allow(Record).to receive(:where) { [] }
|
114
|
+
expect { get :show, id: 'abc123' }.to_not raise_exception
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "PUT update" do
|
119
|
+
let(:record) { double(:record).as_null_object }
|
120
|
+
|
121
|
+
before do
|
122
|
+
allow(controller).to receive(:authenticate_user!) { true }
|
123
|
+
allow(Record).to receive(:custom_find) { record }
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'finds the record and asigns it' do
|
127
|
+
expect(Record).to receive(:custom_find).with('123', nil, {status: :all}) { record }
|
128
|
+
put :update, id: 123, record: { status: 'supressed' }, format: :json
|
129
|
+
expect(assigns(:record)).to eq(record)
|
130
|
+
end
|
131
|
+
|
132
|
+
it "updates the status of the record" do
|
133
|
+
expect(record).to receive(:update_attribute).with(:status, 'supressed')
|
134
|
+
put :update, id: 123, record: { status: 'supressed' }, format: :json
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|