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,348 @@
|
|
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 User do
|
12
|
+
let(:user) { User.new }
|
13
|
+
|
14
|
+
before {
|
15
|
+
developer = double(:role, name: :developer)
|
16
|
+
admin = double(:admin, admin: true)
|
17
|
+
allow(RecordSchema).to receive(:default_role) { developer }
|
18
|
+
allow(RecordSchema).to receive(:roles) { { admin: admin, developer: developer } }
|
19
|
+
}
|
20
|
+
|
21
|
+
describe '#role' do
|
22
|
+
it 'should set the default value of the role from the Schema' do
|
23
|
+
expect(user.role).to eq 'developer'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should generate a authentication_token after creating a user' do
|
28
|
+
user.save
|
29
|
+
user.reload
|
30
|
+
expect(user.authentication_token).to_not be_nil
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "user_sets" do
|
34
|
+
describe "custom_find" do
|
35
|
+
it "should lookup the UserSet by Mongo ID" do
|
36
|
+
expect(user.user_sets).to receive(:find).with('503a95b112575773920005f4')
|
37
|
+
user.user_sets.custom_find('503a95b112575773920005f4')
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should lookup the UserSet by URL if param not an ID" do
|
41
|
+
expect(user.user_sets).to receive(:where).with(url: 'http://google.com') { [] }
|
42
|
+
user.user_sets.custom_find('http://google.com')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#sets=" do
|
48
|
+
it "creates a new set for the user" do
|
49
|
+
user.sets = [{name: "Favourites", privacy: "hidden", priority: 0}]
|
50
|
+
expect(user.user_sets.size).to eq 1
|
51
|
+
expect(user.user_sets.first.name).to eq "Favourites"
|
52
|
+
expect(user.user_sets.first.privacy).to eq "hidden"
|
53
|
+
expect(user.user_sets.first.priority).to eq 0
|
54
|
+
end
|
55
|
+
|
56
|
+
it "doesn't create a new set when the sets are nil" do
|
57
|
+
user.sets = nil
|
58
|
+
expect(user.user_sets.size).to eq 0
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '#name' do
|
63
|
+
it "returns the user's name" do
|
64
|
+
expect(User.new(name: 'John').name).to eq 'John'
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'returns the username when the name is empty' do
|
68
|
+
expect(User.new(username: 'ben').name).to eq 'ben'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#updated_today?' do
|
73
|
+
it 'should return true if the user was last updated today' do
|
74
|
+
user.updated_at = Time.now.beginning_of_day + 10.seconds
|
75
|
+
expect(user.updated_today?).to be_truthy
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should return false when the user was last updated yesterday' do
|
79
|
+
user.updated_at = Time.now - 1.day
|
80
|
+
expect(user.updated_today?).to be_falsey
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '#check_daily_requests' do
|
85
|
+
it "should reset daily requests if it wasn't updated today" do
|
86
|
+
user.attributes = { updated_at: Time.now-1.day, daily_requests: 100 }
|
87
|
+
user.check_daily_requests
|
88
|
+
expect(user.daily_requests).to eq 1
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should increment daily requests if it was updated today' do
|
92
|
+
user.attributes = { updated_at: Time.now, daily_requests: 100 }
|
93
|
+
user.check_daily_requests
|
94
|
+
expect(user.daily_requests).to eq 101
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
context 'api limit notifications' do
|
99
|
+
before {
|
100
|
+
@email = double
|
101
|
+
allow(SupplejackApi::RequestLimitMailer).to receive(:at90percent) { @email }
|
102
|
+
}
|
103
|
+
|
104
|
+
it 'should send an email if the user is at 90% daily requests' do
|
105
|
+
expect(@email).to receive(:deliver)
|
106
|
+
user.attributes = {daily_requests: 89, updated_at: Time.now, max_requests: 100}
|
107
|
+
expect(SupplejackApi::RequestLimitMailer).to receive(:at90percent).with(user) {@email}
|
108
|
+
user.check_daily_requests
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'should not send an email if the user has past 90%' do
|
112
|
+
user.attributes = {daily_requests: 90, updated_at: Time.now, max_requests: 100}
|
113
|
+
expect(SupplejackApi::RequestLimitMailer).to_not receive(:at90percent).with(user) {@email}
|
114
|
+
user.check_daily_requests
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'should send an email if the user has reached 100%' do
|
118
|
+
expect(@email).to receive(:deliver)
|
119
|
+
user.attributes = {daily_requests: 99, updated_at: Time.now, max_requests: 100}
|
120
|
+
expect(SupplejackApi::RequestLimitMailer).to receive(:at100percent).with(user) {@email}
|
121
|
+
user.check_daily_requests
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should not send an email when the user has past 100%' do
|
125
|
+
user.attributes = {daily_requests: 100, updated_at: Time.now, max_requests: 100}
|
126
|
+
expect(SupplejackApi::RequestLimitMailer).to_not receive(:at100percent).with(user) {@email}
|
127
|
+
user.check_daily_requests
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
describe '#increment_daily_requests' do
|
133
|
+
it 'should increment daily_requests by 1' do
|
134
|
+
user.attributes = { updated_at: Time.now, daily_requests: 100 }
|
135
|
+
user.increment_daily_requests
|
136
|
+
expect(user.daily_requests).to eq 101
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'increments daily requests when is nil' do
|
140
|
+
user.attributes = { updated_at: Time.now, daily_requests: nil }
|
141
|
+
user.increment_daily_requests
|
142
|
+
expect(user.daily_requests).to eq 1
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
describe '#update_daily_activity' do
|
147
|
+
let(:request) { double(:request, params: { action: ', controller: ' }).as_null_object }
|
148
|
+
|
149
|
+
it 'sets the daily_activity_stored flag to false' do
|
150
|
+
allow(request).to receive(:params) { { action: 'index', controller: 'records' } }
|
151
|
+
user.update_daily_activity(request)
|
152
|
+
expect(user.daily_activity_stored).to be_falsey
|
153
|
+
end
|
154
|
+
|
155
|
+
context 'records and search requests' do
|
156
|
+
it 'updates the search requests' do
|
157
|
+
allow(request).to receive(:params) { { action: 'index', controller: 'records' } }
|
158
|
+
user.update_daily_activity(request)
|
159
|
+
expect(user.daily_activity['search']['records']).to eq 1
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'increases the amount of requests for the day' do
|
163
|
+
allow(request).to receive(:params) { {action: 'index', controller: 'records'} }
|
164
|
+
user.update_daily_activity(request)
|
165
|
+
expect(user.daily_activity['search']['records']).to eq 1
|
166
|
+
|
167
|
+
user.update_daily_activity(request)
|
168
|
+
expect(user.daily_activity['search']['records']).to eq 2
|
169
|
+
end
|
170
|
+
|
171
|
+
it 'updates the requests for the record details' do
|
172
|
+
allow(request).to receive(:params) { {action: 'show', controller: 'records'} }
|
173
|
+
user.update_daily_activity(request)
|
174
|
+
expect(user.daily_activity['records']['show']).to eq 1
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'updates the requests for multiple records' do
|
178
|
+
allow(request).to receive(:params) { {action: 'multiple', controller: 'records'} }
|
179
|
+
user.update_daily_activity(request)
|
180
|
+
expect(user.daily_activity['records']['multiple']).to eq 1
|
181
|
+
end
|
182
|
+
|
183
|
+
it 'updates the requests for the source redirect' do
|
184
|
+
allow(request).to receive(:params) { {action: 'source', controller: 'records'} }
|
185
|
+
user.update_daily_activity(request)
|
186
|
+
expect(user.daily_activity['records']['source']).to eq 1
|
187
|
+
end
|
188
|
+
|
189
|
+
it 'updates the requests for search through a custom search' do
|
190
|
+
allow(request).to receive(:params) { {action: 'records', controller: 'custom_searches'} }
|
191
|
+
user.update_daily_activity(request)
|
192
|
+
expect(user.daily_activity['search']['custom_search']).to eq 1
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
describe '#reset_daily_activity' do
|
198
|
+
it 'nullifies the daily_activity' do
|
199
|
+
user.reset_daily_activity
|
200
|
+
expect(user.daily_activity).to be_nil
|
201
|
+
end
|
202
|
+
|
203
|
+
it 'sets the daily_activity_stored flag to true' do
|
204
|
+
user.daily_activity_stored = false
|
205
|
+
user.reset_daily_activity
|
206
|
+
expect(user.daily_activity_stored).to be_truthy
|
207
|
+
end
|
208
|
+
|
209
|
+
it 'resets the daily requests count' do
|
210
|
+
user.daily_requests = 100
|
211
|
+
user.reset_daily_activity
|
212
|
+
expect(user.daily_requests).to eq 0
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe '#over_limit?' do
|
217
|
+
context 'user was updated today' do
|
218
|
+
before(:each) do
|
219
|
+
user.updated_at = Time.now
|
220
|
+
end
|
221
|
+
|
222
|
+
it 'should return true when daily requests is greater than max requests' do
|
223
|
+
user.attributes = {daily_requests: 100, max_requests: 99}
|
224
|
+
expect(user.over_limit?).to be_truthy
|
225
|
+
end
|
226
|
+
|
227
|
+
it 'should return false when daily requests is less than max requests' do
|
228
|
+
user.attributes = {daily_requests: 100, max_requests: 110}
|
229
|
+
expect(user.over_limit?).to be_falsey
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
context "user wasn't updated today" do
|
234
|
+
it 'should always return false' do
|
235
|
+
user.attributes = {updated_at: Time.now-1.day, daily_requests: 100, max_requests: 99}
|
236
|
+
expect(user.over_limit?).to be_falsey
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
describe '#calculate_last_30_days_requests' do
|
242
|
+
let!(:user) { FactoryGirl.create(:user) }
|
243
|
+
let!(:user_activity) { FactoryGirl.create(:user_activity, user_id: user.id, total: 5, created_at: Time.now) }
|
244
|
+
|
245
|
+
it 'adds up the totals of the last 30 days' do
|
246
|
+
FactoryGirl.create(:user_activity, user_id: user.id, total: 2, created_at: Time.now - 5.days)
|
247
|
+
expect(user.calculate_last_30_days_requests).to eq 7
|
248
|
+
end
|
249
|
+
|
250
|
+
it 'ignores requests older than 30 days' do
|
251
|
+
FactoryGirl.create(:user_activity, user_id: user.id, total: 2, created_at: Time.now - 31.days)
|
252
|
+
expect(user.calculate_last_30_days_requests).to eq 5
|
253
|
+
end
|
254
|
+
|
255
|
+
it 'stores the requests in monthly_requests field' do
|
256
|
+
user.calculate_last_30_days_requests
|
257
|
+
expect(user.monthly_requests).to eq 5
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
describe '#requests_per_day' do
|
262
|
+
let!(:user) { FactoryGirl.create(:user) }
|
263
|
+
|
264
|
+
before do
|
265
|
+
FactoryGirl.create(:user_activity, user_id: user.id, total: 5, created_at: Time.now - 1.day)
|
266
|
+
FactoryGirl.create(:user_activity, user_id: user.id, total: 2, created_at: Time.now)
|
267
|
+
end
|
268
|
+
|
269
|
+
it 'returns an array with the total requests per day' do
|
270
|
+
expect(user.requests_per_day(2)).to eq [5, 2]
|
271
|
+
end
|
272
|
+
|
273
|
+
it "returns 0 for days when there isn't any activity" do
|
274
|
+
FactoryGirl.create(:user_activity, user_id: user.id, total: 1, created_at: Time.now - 3.day)
|
275
|
+
expect(user.requests_per_day(4)).to eq [1, 0, 5, 2]
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
describe '#name_or_user' do
|
280
|
+
it 'should return the name' do
|
281
|
+
user.name = 'Federico'
|
282
|
+
expect(user.name_or_user).to eq('Federico')
|
283
|
+
end
|
284
|
+
|
285
|
+
it 'should return the username' do
|
286
|
+
user.name = ''
|
287
|
+
user.username = 'fedegl'
|
288
|
+
expect(user.name_or_user).to eq('fedegl')
|
289
|
+
end
|
290
|
+
|
291
|
+
it 'should return the first part of the email address from name if email' do
|
292
|
+
user.name = 'chris.mcdowall@dia.govt.nz'
|
293
|
+
expect(user.name_or_user).to eq('chris.mcdowall')
|
294
|
+
end
|
295
|
+
|
296
|
+
it 'should return the first part of the email address from username if email' do
|
297
|
+
user.name = ''
|
298
|
+
user.username = 'chris.mcdowall@dia.govt.nz'
|
299
|
+
expect(user.name_or_user).to eq('chris.mcdowall')
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
describe '#find_by_api_key' do
|
304
|
+
it 'searches for a user by its api key' do
|
305
|
+
expect(User).to receive(:where).with(authentication_token: '1234').and_return([double(:record)])
|
306
|
+
User.find_by_api_key('1234')
|
307
|
+
end
|
308
|
+
|
309
|
+
it 'returns nil when user not found' do
|
310
|
+
allow(User).to receive(:where).and_return([])
|
311
|
+
expect(User.find_by_api_key('1234')).to be_nil
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
describe '#custom_find' do
|
316
|
+
let(:user) { FactoryGirl.create(:user) }
|
317
|
+
|
318
|
+
it 'finds the user by the api_key' do
|
319
|
+
expect(User.custom_find(user.api_key)).to eq user
|
320
|
+
end
|
321
|
+
|
322
|
+
it 'should raise a error when a record is not found' do
|
323
|
+
expect { User.custom_find('sfsdfsdf') }.to raise_error(Mongoid::Errors::DocumentNotFound)
|
324
|
+
end
|
325
|
+
|
326
|
+
it 'finds the user by the id' do
|
327
|
+
expect(User.custom_find(user.id)).to eq user
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
describe "can_change_featured_sets?" do
|
332
|
+
context "admin user" do
|
333
|
+
before { user.role = 'admin' }
|
334
|
+
|
335
|
+
it "should return true" do
|
336
|
+
expect(user.can_change_featured_sets?).to be_truthy
|
337
|
+
end
|
338
|
+
end
|
339
|
+
|
340
|
+
context "user" do
|
341
|
+
it "should return false" do
|
342
|
+
expect(user.can_change_featured_sets?).to be_falsey
|
343
|
+
end
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
end
|
348
|
+
end
|
@@ -0,0 +1,22 @@
|
|
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 'Concepts route', type: :routing do
|
12
|
+
routes { SupplejackApi::Engine.routes }
|
13
|
+
|
14
|
+
it 'routes /concepts/1.format to concepts#index' do
|
15
|
+
expect(get '/concepts/1.json').to route_to(controller: 'supplejack_api/concepts', action: 'show', id: '1', format: 'json', version: nil)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'routes /concepts/1/records.format to records#index' do
|
19
|
+
expect(get '/concepts/1/records.json').to route_to(controller: 'supplejack_api/records', action: 'index', concept_id: '1', format: 'json', version: nil)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,66 @@
|
|
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 HarvesterConstraint do
|
12
|
+
let(:constraint) { HarvesterConstraint.new }
|
13
|
+
|
14
|
+
describe "#forwarded_ips" do
|
15
|
+
it "returns a single ip address in the X-FORWARDED-FOR header" do
|
16
|
+
request = double(:request, :env => {"HTTP_X_FORWARDED_FOR" => "192.122.171.246"})
|
17
|
+
expect(constraint.forwarded_ips(request)).to eq ["192.122.171.246"]
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns multiple ip addresses in the X-FORWARDED-FOR header" do
|
21
|
+
request = double(:request, :env => {"HTTP_X_FORWARDED_FOR" => "192.122.171.246, 192.122.171.234"})
|
22
|
+
expect(constraint.forwarded_ips(request)).to eq ["192.122.171.246", "192.122.171.234"]
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns multiple ip addresses in the X-FORWARDED-FOR header without spaces" do
|
26
|
+
request = double(:request, :env => {"HTTP_X_FORWARDED_FOR" => "192.122.171.246,192.122.171.234"})
|
27
|
+
expect(constraint.forwarded_ips(request)).to eq ["192.122.171.246", "192.122.171.234"]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#matches?" do
|
32
|
+
context "forwarded_ips is empty" do
|
33
|
+
it "returns true when the remote_ip is one of the allowed ips" do
|
34
|
+
request = double(:request, :env => {"HTTP_X_FORWARDED_FOR" => nil}, :remote_ip => "127.0.0.1")
|
35
|
+
expect(constraint.matches?(request)).to be_truthy
|
36
|
+
end
|
37
|
+
|
38
|
+
it "returns false when the remote_ip is not allowed" do
|
39
|
+
request = double(:request, :env => {"HTTP_X_FORWARDED_FOR" => nil}, :remote_ip => "192.1.1.1")
|
40
|
+
expect(constraint.matches?(request)).to be_falsey
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "with some forwarded_ips" do
|
45
|
+
it "returns true when the remote_ip and the forwarded_ips are allowed" do
|
46
|
+
request = double(:request, :env => {"HTTP_X_FORWARDED_FOR" => "192.1.1.0"}, :remote_ip => "193.1.1.0")
|
47
|
+
constraint.instance_variable_set("@ips", ["192.1.1.0", "193.1.1.0"])
|
48
|
+
expect(constraint.matches?(request)).to be_truthy
|
49
|
+
end
|
50
|
+
|
51
|
+
it "returns false when the remote_ip is not allowed" do
|
52
|
+
request = double(:request, :env => {"HTTP_X_FORWARDED_FOR" => "192.1.1.0"}, :remote_ip => "222.1.1.0")
|
53
|
+
constraint.instance_variable_set("@ips", ["192.1.1.0", "193.1.1.0"])
|
54
|
+
expect(constraint.matches?(request)).to be_falsey
|
55
|
+
end
|
56
|
+
|
57
|
+
it "returns false when any of the forwarded_ips are not allowed" do
|
58
|
+
request = double(:request, :env => {"HTTP_X_FORWARDED_FOR" => "192.1.1.0, 222.1.1.0"}, :remote_ip => "193.1.1.0")
|
59
|
+
constraint.instance_variable_set("@ips", ["192.1.1.0", "193.1.1.0", "194.1.1.0"])
|
60
|
+
expect(constraint.matches?(request)).to be_falsey
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,51 @@
|
|
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 routes", :type => :routing do
|
12
|
+
routes { SupplejackApi::Engine.routes }
|
13
|
+
|
14
|
+
before(:each) do
|
15
|
+
allow_any_instance_of(HarvesterConstraint).to receive(:matches?).and_return(true)
|
16
|
+
end
|
17
|
+
|
18
|
+
context "Record routes" do
|
19
|
+
it "routes /harvester/records to harvester/records#create" do
|
20
|
+
expect(post('/harvester/records.json')).to route_to(controller: 'supplejack_api/harvester/records', action: 'create', format: 'json')
|
21
|
+
end
|
22
|
+
|
23
|
+
it "routes /harvester/records/1 to harvester/records#show" do
|
24
|
+
expect(get('/harvester/records/1.json')).to route_to(controller: 'supplejack_api/harvester/records', action: 'show', id: '1', format: 'json')
|
25
|
+
end
|
26
|
+
|
27
|
+
it "routes /harvester/records/1 to harvester/records#update" do
|
28
|
+
expect(put('/harvester/records/1.json')).to route_to(controller: 'supplejack_api/harvester/records', action: 'update', id: '1', format: 'json')
|
29
|
+
end
|
30
|
+
|
31
|
+
it "routes /harvester/records/flush to harvester/records#flush" do
|
32
|
+
expect(post('/harvester/records/flush')).to route_to('supplejack_api/harvester/records#flush')
|
33
|
+
end
|
34
|
+
|
35
|
+
it "routes /harvester/records/abc123 to harvester/records#delete" do
|
36
|
+
expect(put('/harvester/records/delete')).to route_to('supplejack_api/harvester/records#delete')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "Fragment routes" do
|
41
|
+
it "routes /harvester/records/1/fragments.json to harvester/fragments#create" do
|
42
|
+
expect(post('/harvester/records/1/fragments.json')).to route_to('supplejack_api/harvester/fragments#create', record_id: '1', format: 'json')
|
43
|
+
end
|
44
|
+
|
45
|
+
it "routes /harvester/fragments/1.json to harvester/fragments#destroy" do
|
46
|
+
expect(delete('/harvester/fragments/1.json')).to route_to('supplejack_api/harvester/fragments#destroy', id: '1', format: 'json')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|