From 614ba53fff0c24871d118a5c9532a7bbda057bed Mon Sep 17 00:00:00 2001 From: James Fowler Date: Fri, 15 Dec 2017 13:36:50 -0800 Subject: [PATCH 01/35] Updated dev-envt config file --- config/environments/development.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/config/environments/development.rb b/config/environments/development.rb index f2b97861..c5fb47f9 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -9,7 +9,13 @@ config.cache_classes = false # asset host - config.action_controller.asset_host = 'http://' + Socket.ip_address_list[1].ip_address + ':3000' + # There are two cases for dev asset_host to be aware of: + # + # "localhost"/127.0.0.1 ... when developing locally + # remote domainname/IP ... when developing on a remote server + this_server = Socket.ip_address_list[1] + this_server_hostname = (this_server.getnameinfo[0] == 'localhost') ? 'localhost' : this_server.ip_address + config.action_controller.asset_host = 'http://' + this_server_hostname + ':3000' config.action_mailer.asset_host = config.action_controller.asset_host # Do not eager load code on boot. From 2274f4d84a9fd8593aa28946a146c06672f6e921 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 1 May 2017 12:47:17 -0700 Subject: [PATCH 02/35] Create Adopted controller for API, and wrote initial auth/authorization code for API using basic auth --- app/assets/javascripts/adopted.js | 2 ++ app/assets/stylesheets/adopted.scss | 3 +++ app/controllers/adopted_controller.rb | 24 +++++++++++++++++++++ test/controllers/adopted_controller_test.rb | 7 ++++++ 4 files changed, 36 insertions(+) create mode 100644 app/assets/javascripts/adopted.js create mode 100644 app/assets/stylesheets/adopted.scss create mode 100644 app/controllers/adopted_controller.rb create mode 100644 test/controllers/adopted_controller_test.rb diff --git a/app/assets/javascripts/adopted.js b/app/assets/javascripts/adopted.js new file mode 100644 index 00000000..dee720fa --- /dev/null +++ b/app/assets/javascripts/adopted.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/adopted.scss b/app/assets/stylesheets/adopted.scss new file mode 100644 index 00000000..ed5ba29e --- /dev/null +++ b/app/assets/stylesheets/adopted.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Adopted controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb new file mode 100644 index 00000000..51d8aa1a --- /dev/null +++ b/app/controllers/adopted_controller.rb @@ -0,0 +1,24 @@ +class AdoptedController < ApplicationController + before_filter :authenticate, :admin? + + def index + render html: "
You made it!!!
" + end + + private + def authenticate + authenticate_or_request_with_http_basic('Administration') do |username, password| + user = User.find_by(email: username) + if user && user.valid_password?(password) + sign_in :user, user + end + end + end + + def admin? + if user_signed_in? + render html: "
You must be an admin to access this page
".html_safe unless current_user.admin? + end + end + +end diff --git a/test/controllers/adopted_controller_test.rb b/test/controllers/adopted_controller_test.rb new file mode 100644 index 00000000..69e75d47 --- /dev/null +++ b/test/controllers/adopted_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class AdoptedControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end From c007d1c3fdc8df00a00315dcb1687212ca78b3a2 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 1 May 2017 12:47:47 -0700 Subject: [PATCH 03/35] Add API route, and setting to allow for basic_auth --- config/initializers/devise.rb | 2 +- config/routes.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index e4135f28..a3242eaf 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -59,7 +59,7 @@ # given strategies, for example, `config.http_authenticatable = [:database]` will # enable it only for database authentication. The supported strategies are: # :database = Support basic authentication with authentication key + password - # config.http_authenticatable = false + config.http_authenticatable = false # If http headers should be returned for AJAX requests. True by default. # config.http_authenticatable_on_xhr = true diff --git a/config/routes.rb b/config/routes.rb index f881c6b7..5ce41893 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,4 +19,13 @@ resource :things mount RailsAdmin::Engine => '/admin', :as => 'rails_admin' root to: 'main#index' + + # API + scope '/api' do + scope '/v1' do + scope '/drains' do + get '/adopted' => 'adopted#index' + end + end + end end From e1c2f2432c81ac9ed50ac39f554d06d95ac41025 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 1 May 2017 12:55:21 -0700 Subject: [PATCH 04/35] Forgot to add the helper file --- app/helpers/adopted_helper.rb | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 app/helpers/adopted_helper.rb diff --git a/app/helpers/adopted_helper.rb b/app/helpers/adopted_helper.rb new file mode 100644 index 00000000..7c667ef2 --- /dev/null +++ b/app/helpers/adopted_helper.rb @@ -0,0 +1,2 @@ +module AdoptedHelper +end From 8b188b56c2e84abb1121d1193fc97897e3bdf99b Mon Sep 17 00:00:00 2001 From: root Date: Mon, 1 May 2017 17:26:30 -0700 Subject: [PATCH 05/35] Edited authentication controller so that a session isn't created on the back-end, Added admin check, Render json for adopted drains --- app/controllers/adopted_controller.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb index 51d8aa1a..5a263d8d 100644 --- a/app/controllers/adopted_controller.rb +++ b/app/controllers/adopted_controller.rb @@ -1,8 +1,9 @@ class AdoptedController < ApplicationController - before_filter :authenticate, :admin? + before_filter :authenticate def index - render html: "
You made it!!!
" + @things = Thing.where.not(:user_id => !nil) + render json: @things end private @@ -10,15 +11,14 @@ def authenticate authenticate_or_request_with_http_basic('Administration') do |username, password| user = User.find_by(email: username) if user && user.valid_password?(password) - sign_in :user, user + if user.admin? + return true + else + render html: "
You must be an admin to access this page
".html_safe + end end end end - def admin? - if user_signed_in? - render html: "
You must be an admin to access this page
".html_safe unless current_user.admin? - end - end end From d16cbb3dbb77460bb364b24d6d3c69e2f9a79936 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 1 May 2017 21:40:54 -0700 Subject: [PATCH 06/35] Adopted Drains API controller test --- test/controllers/adopted_controller_test.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/test/controllers/adopted_controller_test.rb b/test/controllers/adopted_controller_test.rb index 69e75d47..038551c6 100644 --- a/test/controllers/adopted_controller_test.rb +++ b/test/controllers/adopted_controller_test.rb @@ -1,7 +1,15 @@ require 'test_helper' class AdoptedControllerTest < ActionController::TestCase - # test "the truth" do - # assert true - # end + setup do + request.env['devise.mapping'] = Devise.mappings[:user] + @user = users(:erik) + end + + test "should get index" do + @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@user.email, "correct") + + get :index + assert_response :success + end end From 6615a92ac1f56389b3a073302c4fd80c97673deb Mon Sep 17 00:00:00 2001 From: root Date: Mon, 1 May 2017 22:03:03 -0700 Subject: [PATCH 07/35] Trying to fix Travis CI errors --- app/controllers/adopted_controller.rb | 26 +++++++++------------ config/routes.rb | 2 +- test/controllers/adopted_controller_test.rb | 6 ++--- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb index 5a263d8d..712a8a64 100644 --- a/app/controllers/adopted_controller.rb +++ b/app/controllers/adopted_controller.rb @@ -1,24 +1,20 @@ class AdoptedController < ApplicationController - before_filter :authenticate + before_action :authenticate def index - @things = Thing.where.not(:user_id => !nil) + @things = Thing.where.not(user_id: !nil) render json: @things end - private - def authenticate - authenticate_or_request_with_http_basic('Administration') do |username, password| - user = User.find_by(email: username) - if user && user.valid_password?(password) - if user.admin? - return true - else - render html: "
You must be an admin to access this page
".html_safe - end - end +private + + def authenticate + authenticate_or_request_with_http_basic('Administration') do |username, password| + user = User.find_by(email: username) + if user && user.valid_password?(password) + return true if user.admin? + render html: '
You must be an admin to access this page
'.html_safe end end - - + end end diff --git a/config/routes.rb b/config/routes.rb index 5ce41893..3f4ce123 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -26,6 +26,6 @@ scope '/drains' do get '/adopted' => 'adopted#index' end - end + end end end diff --git a/test/controllers/adopted_controller_test.rb b/test/controllers/adopted_controller_test.rb index 038551c6..5da285d9 100644 --- a/test/controllers/adopted_controller_test.rb +++ b/test/controllers/adopted_controller_test.rb @@ -6,10 +6,10 @@ class AdoptedControllerTest < ActionController::TestCase @user = users(:erik) end - test "should get index" do - @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@user.email, "correct") + test 'should get index' do + @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@user.email, 'correct') - get :index + get :index assert_response :success end end From ac6c3f7481566436c02313340c985a2203b8bb4b Mon Sep 17 00:00:00 2001 From: root Date: Tue, 2 May 2017 12:25:43 -0700 Subject: [PATCH 08/35] Added formatting for different content types, and edited fields to be output --- app/controllers/adopted_controller.rb | 16 +++++++++++++++- app/controllers/application_controller.rb | 2 ++ app/views/adopted/index.csv.erb | 5 +++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 app/views/adopted/index.csv.erb diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb index 712a8a64..4fe1953c 100644 --- a/app/controllers/adopted_controller.rb +++ b/app/controllers/adopted_controller.rb @@ -3,7 +3,21 @@ class AdoptedController < ApplicationController def index @things = Thing.where.not(user_id: !nil) - render json: @things + respond_to do |format| + format.csv do + headers['Content-Type'] ||= 'text/csv' + headers['Content-Disposition'] = "attachment; filename=\"adopted_drains.csv\"" + end + format.xml do + render xml: @things.map { |thing| {latitude: thing.lat, longitude: thing.lng, city_id: 'N-' + thing.city_id.to_s} } + end + format.json do + render json: @things.map { |thing| {latitude: thing.lat, longitude: thing.lng, city_id: 'N-' + thing.city_id.to_s} } + end + format.all do + render json: @things.map { |thing| {latitude: thing.lat, longitude: thing.lng, city_id: 'N-' + thing.city_id.to_s} } + end + end end private diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c2c7448f..bb50b213 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,5 @@ +require 'csv' + class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. diff --git a/app/views/adopted/index.csv.erb b/app/views/adopted/index.csv.erb new file mode 100644 index 00000000..bab64908 --- /dev/null +++ b/app/views/adopted/index.csv.erb @@ -0,0 +1,5 @@ +<%- headers = ['Lat', 'Lng', 'City ID'] -%> +<%= CSV.generate_line headers %> +<%- @things.each do |thing| -%> +<%= CSV.generate_line([thing.lat, thing.lng, "N-" + thing.city_id.to_s]) -%> +<%- end -%> From b7d863a52189339dbc12fc9f5ad557e5d11573b7 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 2 May 2017 12:44:22 -0700 Subject: [PATCH 09/35] (Finally realized I can run Rubocop on my local machine) --- app/controllers/adopted_controller.rb | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb index 4fe1953c..8424a21b 100644 --- a/app/controllers/adopted_controller.rb +++ b/app/controllers/adopted_controller.rb @@ -3,24 +3,25 @@ class AdoptedController < ApplicationController def index @things = Thing.where.not(user_id: !nil) + render_types + end + +private + + def render_types respond_to do |format| format.csv do headers['Content-Type'] ||= 'text/csv' - headers['Content-Disposition'] = "attachment; filename=\"adopted_drains.csv\"" - end - format.xml do - render xml: @things.map { |thing| {latitude: thing.lat, longitude: thing.lng, city_id: 'N-' + thing.city_id.to_s} } - end - format.json do - render json: @things.map { |thing| {latitude: thing.lat, longitude: thing.lng, city_id: 'N-' + thing.city_id.to_s} } - end - format.all do - render json: @things.map { |thing| {latitude: thing.lat, longitude: thing.lng, city_id: 'N-' + thing.city_id.to_s} } + headers['Content-Disposition'] = 'attachment; filename=\'adopted_drains.csv\'' end + format.xml { render xml: format_fields(@things) } + format.all { render json: format_fields(@things) } end end -private + def format_fields(obj) + obj.map { |thing| {latitude: thing.lat, longitude: thing.lng, city_id: 'N-' + thing.city_id.to_s} } + end def authenticate authenticate_or_request_with_http_basic('Administration') do |username, password| From e2052d03904eef106c0a538edb4236f1641f55ec Mon Sep 17 00:00:00 2001 From: root Date: Tue, 2 May 2017 15:26:41 -0700 Subject: [PATCH 10/35] More controller tests --- test/controllers/adopted_controller_test.rb | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/controllers/adopted_controller_test.rb b/test/controllers/adopted_controller_test.rb index 5da285d9..f670225f 100644 --- a/test/controllers/adopted_controller_test.rb +++ b/test/controllers/adopted_controller_test.rb @@ -4,6 +4,7 @@ class AdoptedControllerTest < ActionController::TestCase setup do request.env['devise.mapping'] = Devise.mappings[:user] @user = users(:erik) + @admin = users(:admin) end test 'should get index' do @@ -12,4 +13,32 @@ class AdoptedControllerTest < ActionController::TestCase get :index assert_response :success end + + test 'should get json' do + @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@admin.email, 'correct') + + get :index, format: :json + assert_equal 'application/json', @response.content_type + end + + test 'should get xml' do + @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@admin.email, 'correct') + + get :index, format: :xml + assert_equal 'application/xml', @response.content_type + end + + test 'should get csv' do + @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@admin.email, 'correct') + + get :index, format: :csv + assert_equal 'text/csv', @response.content_type + end + + test 'only admins get access' do + @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@user.email, 'correct') + + get :index + assert_equal 'text/html', @response.content_type # If user were an admin, content_type would be JSON, since that is default + end end From ffbe684b5fa314280abc381244d4f3f9bb828b19 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 20 Jun 2017 14:39:35 -0700 Subject: [PATCH 11/35] Add Faker gem in order to make sample users --- Gemfile | 1 + Gemfile.lock | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 2762eb45..13655796 100644 --- a/Gemfile +++ b/Gemfile @@ -10,6 +10,7 @@ gem 'local_time', '~> 2.0' gem 'obscenity', '~> 1.0', '>= 1.0.2' gem 'pg' gem 'rails', '~> 4.2.10' +gem 'faker', '1.7.3' gem 'rails_admin', '~> 1.0' gem 'validates_formatting_of', '~> 0.9.0' diff --git a/Gemfile.lock b/Gemfile.lock index a0bb4694..00122903 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -76,6 +76,8 @@ GEM railties (>= 3.2, < 5.2) erubis (2.7.0) execjs (2.7.0) + faker (1.7.3) + i18n (~> 0.5) ffi (1.9.18) font-awesome-rails (4.7.0.2) railties (>= 3.2, < 5.2) @@ -256,6 +258,7 @@ DEPENDENCIES coveralls devise (~> 3.0) dotenv-rails + faker (= 1.7.3) geokit (~> 1.0) haml (~> 5.0) http_accept_language (~> 2.0) @@ -281,4 +284,4 @@ RUBY VERSION ruby 2.2.3p173 BUNDLED WITH - 1.15.4 + 1.16.1 From cfee6da388709ad3943f37872a8a1c100db73ef2 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 20 Jun 2017 15:02:50 -0700 Subject: [PATCH 12/35] Adding some user seeds --- db/seeds.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/db/seeds.rb b/db/seeds.rb index d933bfe7..93905333 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -15,6 +15,7 @@ r = Random.new +=begin 500.times do |i| Thing.where(city_id: i).first_or_initialize.tap do |thing| thing.name = "Some Drain #{i}" @@ -24,3 +25,18 @@ thing.save! end end + +=end + + +1000.times do |i| + first_name = Faker::Name.first_name + last_name = Faker::Name.last_name + email = "user-#{i+1}@usertest.org" + password = "pass1234" + User.create!(first_name: first_name, + last_name: last_name, + email: email, + password: password) +end + From 96aae9696ab9fcaf94115300ad7deebf1dd78784 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 24 Jul 2017 10:42:37 -0700 Subject: [PATCH 13/35] Added a rake task for DEV so mock users can automatically adopt drains. I created this initially to test out how the API would handle load --- lib/tasks/data.rake | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/tasks/data.rake b/lib/tasks/data.rake index 0773b434..4230b68c 100644 --- a/lib/tasks/data.rake +++ b/lib/tasks/data.rake @@ -27,4 +27,17 @@ namespace :data do end end end + + task auto_adopt: :environment do + # Make random users adopt drains to test server load when generating API data + + if !Rails.env.production? + Thing.first(10_000).each do |t| + if t.user_id.blank? + t.user_id = User.find_by('id' => Random.rand(1..User.last.id)).id + t.save() + end + end + end + end end From 056ed5ccfbf8a05ff115b766e53d20d8db1c4860 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 24 Jul 2017 10:47:47 -0700 Subject: [PATCH 14/35] Passing rubocop --- lib/tasks/data.rake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tasks/data.rake b/lib/tasks/data.rake index 4230b68c..2f4e8727 100644 --- a/lib/tasks/data.rake +++ b/lib/tasks/data.rake @@ -31,11 +31,11 @@ namespace :data do task auto_adopt: :environment do # Make random users adopt drains to test server load when generating API data - if !Rails.env.production? + unless Rails.env.production? Thing.first(10_000).each do |t| if t.user_id.blank? t.user_id = User.find_by('id' => Random.rand(1..User.last.id)).id - t.save() + t.save end end end From 0f816bf6cd848770118e5689fbd5a010f8d50a71 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 25 Jul 2017 16:20:22 -0700 Subject: [PATCH 15/35] Changing !nil to nil... !nil evaluates to true --- app/controllers/adopted_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb index 8424a21b..8ad9e4ee 100644 --- a/app/controllers/adopted_controller.rb +++ b/app/controllers/adopted_controller.rb @@ -2,7 +2,7 @@ class AdoptedController < ApplicationController before_action :authenticate def index - @things = Thing.where.not(user_id: !nil) + @things = Thing.where.not(user_id: nil) render_types end From 395ad2500ed1523718c34d99ecd5260b78d0648c Mon Sep 17 00:00:00 2001 From: root Date: Wed, 26 Jul 2017 14:11:05 -0700 Subject: [PATCH 16/35] Initial comments for cursor funcionality --- app/controllers/adopted_controller.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb index 8ad9e4ee..1dbba9f5 100644 --- a/app/controllers/adopted_controller.rb +++ b/app/controllers/adopted_controller.rb @@ -1,6 +1,11 @@ class AdoptedController < ApplicationController before_action :authenticate + # GET /api/v1/drains/adopted + # Optional params: + # + # format=[json|xml|csv] + # cursor=[-1 | next_cursor | prev_cursor] def index @things = Thing.where.not(user_id: nil) render_types From 227ee7094cda03509ff2d75385bf541de5a452b4 Mon Sep 17 00:00:00 2001 From: James Fowler Date: Sun, 13 Aug 2017 18:00:02 -0700 Subject: [PATCH 17/35] Adding Kaminari gem for pagination --- Gemfile | 1 + Gemfile.lock | 1 + config/initializers/kaminari_config.rb | 10 ++++++++++ 3 files changed, 12 insertions(+) create mode 100644 config/initializers/kaminari_config.rb diff --git a/Gemfile b/Gemfile index 13655796..c2dc7032 100644 --- a/Gemfile +++ b/Gemfile @@ -27,6 +27,7 @@ end group :development do gem 'spring' + gem 'kaminari' end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index 00122903..33ec1254 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -262,6 +262,7 @@ DEPENDENCIES geokit (~> 1.0) haml (~> 5.0) http_accept_language (~> 2.0) + kaminari local_time (~> 2.0) obscenity (~> 1.0, >= 1.0.2) paranoia (~> 2.4) diff --git a/config/initializers/kaminari_config.rb b/config/initializers/kaminari_config.rb new file mode 100644 index 00000000..44f38ca3 --- /dev/null +++ b/config/initializers/kaminari_config.rb @@ -0,0 +1,10 @@ +Kaminari.configure do |config| + config.default_per_page = 1000 + # config.max_per_page = nil + # config.window = 4 + # config.outer_window = 0 + # config.left = 0 + # config.right = 0 + # config.page_method_name = :page + # config.param_name = :page +end From 9ce0b746fa17b24a35a2245bb168f3e9bc8cdfdf Mon Sep 17 00:00:00 2001 From: James Fowler Date: Sun, 13 Aug 2017 18:00:28 -0700 Subject: [PATCH 18/35] added pagination functionality to API --- app/controllers/adopted_controller.rb | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb index 1dbba9f5..a445c557 100644 --- a/app/controllers/adopted_controller.rb +++ b/app/controllers/adopted_controller.rb @@ -1,26 +1,43 @@ class AdoptedController < ApplicationController before_action :authenticate + before_action :make_cur_page, :make_other_pages, only: [:index] # GET /api/v1/drains/adopted # Optional params: # # format=[json|xml|csv] - # cursor=[-1 | next_cursor | prev_cursor] + # page def index - @things = Thing.where.not(user_id: nil) + @results = {next_page: @next_page, prev_page: @prev_page, drains: @things} render_types end private + # Determine if the user supplied a valid page number, if not they get first page + def make_cur_page + page = params[:page].blank? ? 2 : params[:page] + @cur_page = Thing.where.not(user_id: nil).page(page) + @things = format_fields(@cur_page) + end + + # Determine next and previous pages, so the user can navigate if needed + def make_other_pages + @next_page = @cur_page.next_page.nil? ? 0 : @cur_page.next_page + @prev_page = @cur_page.prev_page.nil? ? 0 : @cur_page.prev_page + end + def render_types respond_to do |format| format.csv do headers['Content-Type'] ||= 'text/csv' headers['Content-Disposition'] = 'attachment; filename=\'adopted_drains.csv\'' + + # Assuming that a CSV was requested in order to retrieve all results at once + @allthings = Thing.where.not(user_id: nil) end - format.xml { render xml: format_fields(@things) } - format.all { render json: format_fields(@things) } + format.xml { render xml: @results } + format.all { render json: @results } end end From 3ec42b85c78c9dce2ace60ae3b357852cd5ccc2e Mon Sep 17 00:00:00 2001 From: James Fowler Date: Sun, 13 Aug 2017 18:01:09 -0700 Subject: [PATCH 19/35] Made is so that a CSV template is rendered with all results rather than paged results --- app/views/adopted/index.csv.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/adopted/index.csv.erb b/app/views/adopted/index.csv.erb index bab64908..b327ef32 100644 --- a/app/views/adopted/index.csv.erb +++ b/app/views/adopted/index.csv.erb @@ -1,5 +1,5 @@ <%- headers = ['Lat', 'Lng', 'City ID'] -%> <%= CSV.generate_line headers %> -<%- @things.each do |thing| -%> +<%- @allthings.each do |thing| -%> <%= CSV.generate_line([thing.lat, thing.lng, "N-" + thing.city_id.to_s]) -%> <%- end -%> From b945becf697aaab3cbc3234293a57340bbb81ce7 Mon Sep 17 00:00:00 2001 From: James Fowler Date: Sun, 13 Aug 2017 18:01:39 -0700 Subject: [PATCH 20/35] auto_adopt task comment for further clarification --- lib/tasks/data.rake | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/tasks/data.rake b/lib/tasks/data.rake index 2f4e8727..87ee06af 100644 --- a/lib/tasks/data.rake +++ b/lib/tasks/data.rake @@ -30,6 +30,7 @@ namespace :data do task auto_adopt: :environment do # Make random users adopt drains to test server load when generating API data + # There has to be users in DB for this to work unless Rails.env.production? Thing.first(10_000).each do |t| From 0423a9c168ba4bf51abb04886d7bcb55bf7d3624 Mon Sep 17 00:00:00 2001 From: James Fowler Date: Fri, 15 Dec 2017 14:57:29 -0800 Subject: [PATCH 21/35] Added a total_pages variable, changed default page to 1, and a adopted_drains var --- app/controllers/adopted_controller.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb index a445c557..f2a460f5 100644 --- a/app/controllers/adopted_controller.rb +++ b/app/controllers/adopted_controller.rb @@ -1,6 +1,6 @@ class AdoptedController < ApplicationController before_action :authenticate - before_action :make_cur_page, :make_other_pages, only: [:index] + before_action :get_adopted_things, :make_cur_page, :make_other_pages, only: [:index] # GET /api/v1/drains/adopted # Optional params: @@ -8,23 +8,27 @@ class AdoptedController < ApplicationController # format=[json|xml|csv] # page def index - @results = {next_page: @next_page, prev_page: @prev_page, drains: @things} + @results = { next_page: @next_page, prev_page: @prev_page, total_pages: @adopted_things.page(1).total_pages, drains: @things } render_types end private - + + def get_adopted_things + @adopted_things = Thing.where.not(user_id: nil) + end + # Determine if the user supplied a valid page number, if not they get first page def make_cur_page - page = params[:page].blank? ? 2 : params[:page] - @cur_page = Thing.where.not(user_id: nil).page(page) + page = ((params[:page].blank?) || (params[:page].to_i == 0)) ? 1 : params[:page] + @cur_page = @adopted_things.page(page) @things = format_fields(@cur_page) end # Determine next and previous pages, so the user can navigate if needed def make_other_pages - @next_page = @cur_page.next_page.nil? ? 0 : @cur_page.next_page - @prev_page = @cur_page.prev_page.nil? ? 0 : @cur_page.prev_page + @next_page = @cur_page.next_page.nil? ? -1 : @cur_page.next_page + @prev_page = @cur_page.prev_page.nil? ? -1 : @cur_page.prev_page end def render_types @@ -33,8 +37,7 @@ def render_types headers['Content-Type'] ||= 'text/csv' headers['Content-Disposition'] = 'attachment; filename=\'adopted_drains.csv\'' - # Assuming that a CSV was requested in order to retrieve all results at once - @allthings = Thing.where.not(user_id: nil) + @allthings end format.xml { render xml: @results } format.all { render json: @results } From d8224480266fd48209d11232b943c4b12ee2d6e5 Mon Sep 17 00:00:00 2001 From: James Fowler Date: Fri, 15 Dec 2017 16:08:51 -0800 Subject: [PATCH 22/35] Updating versions --- Gemfile | 4 ++-- Gemfile.lock | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index c2dc7032..fe59d731 100644 --- a/Gemfile +++ b/Gemfile @@ -10,9 +10,10 @@ gem 'local_time', '~> 2.0' gem 'obscenity', '~> 1.0', '>= 1.0.2' gem 'pg' gem 'rails', '~> 4.2.10' -gem 'faker', '1.7.3' +gem 'faker', '~> 1.7.0' gem 'rails_admin', '~> 1.0' gem 'validates_formatting_of', '~> 0.9.0' +gem 'kaminari', '~> 1.0' gem 'paranoia', '~> 2.4' gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw] @@ -27,7 +28,6 @@ end group :development do gem 'spring' - gem 'kaminari' end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index 33ec1254..7d1fa572 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -258,11 +258,11 @@ DEPENDENCIES coveralls devise (~> 3.0) dotenv-rails - faker (= 1.7.3) + faker (~> 1.7.0) geokit (~> 1.0) haml (~> 5.0) http_accept_language (~> 2.0) - kaminari + kaminari (~> 1.0) local_time (~> 2.0) obscenity (~> 1.0, >= 1.0.2) paranoia (~> 2.4) From 2c71f1b2d40b6bb6c56579d00e1104acda0b3d0d Mon Sep 17 00:00:00 2001 From: James Fowler Date: Fri, 15 Dec 2017 16:21:58 -0800 Subject: [PATCH 23/35] Removed format options - JSON is the only format available now --- app/controllers/adopted_controller.rb | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb index f2a460f5..912c7600 100644 --- a/app/controllers/adopted_controller.rb +++ b/app/controllers/adopted_controller.rb @@ -5,11 +5,10 @@ class AdoptedController < ApplicationController # GET /api/v1/drains/adopted # Optional params: # - # format=[json|xml|csv] # page def index @results = { next_page: @next_page, prev_page: @prev_page, total_pages: @adopted_things.page(1).total_pages, drains: @things } - render_types + render json: @results end private @@ -31,19 +30,6 @@ def make_other_pages @prev_page = @cur_page.prev_page.nil? ? -1 : @cur_page.prev_page end - def render_types - respond_to do |format| - format.csv do - headers['Content-Type'] ||= 'text/csv' - headers['Content-Disposition'] = 'attachment; filename=\'adopted_drains.csv\'' - - @allthings - end - format.xml { render xml: @results } - format.all { render json: @results } - end - end - def format_fields(obj) obj.map { |thing| {latitude: thing.lat, longitude: thing.lng, city_id: 'N-' + thing.city_id.to_s} } end From 32f22439375002faf89cabdc3431f081fc744785 Mon Sep 17 00:00:00 2001 From: James Fowler Date: Fri, 15 Dec 2017 19:25:16 -0800 Subject: [PATCH 24/35] Updated pagination defaults --- config/initializers/kaminari_config.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/initializers/kaminari_config.rb b/config/initializers/kaminari_config.rb index 44f38ca3..bd25f213 100644 --- a/config/initializers/kaminari_config.rb +++ b/config/initializers/kaminari_config.rb @@ -1,6 +1,6 @@ Kaminari.configure do |config| - config.default_per_page = 1000 - # config.max_per_page = nil + config.default_per_page = 100 + config.max_per_page = 1000 # config.window = 4 # config.outer_window = 0 # config.left = 0 From ca68a43e02c3caab5574c5836392ff4117913430 Mon Sep 17 00:00:00 2001 From: James Fowler Date: Fri, 15 Dec 2017 19:26:18 -0800 Subject: [PATCH 25/35] Uncommenting so I could add drains --- db/seeds.rb | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 93905333..69d38d26 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,21 +1,14 @@ User.where(email: 'john@example.com').first_or_initialize.tap do |user| + # Add a mock user with admin privileges user.first_name = 'John' user.last_name = 'Doe' user.password = 'password' - user.save! -end - -User.where(email: 'admin@example.com').first_or_initialize.tap do |user| - user.first_name = 'Jane' - user.last_name = 'Doe' - user.password = 'password' user.admin = true user.save! end r = Random.new -=begin 500.times do |i| Thing.where(city_id: i).first_or_initialize.tap do |thing| thing.name = "Some Drain #{i}" @@ -26,9 +19,6 @@ end end -=end - - 1000.times do |i| first_name = Faker::Name.first_name last_name = Faker::Name.last_name @@ -39,4 +29,3 @@ email: email, password: password) end - From c385883a5184f5fa9fc04b88f38a731f39a065be Mon Sep 17 00:00:00 2001 From: James Fowler Date: Fri, 15 Dec 2017 19:27:03 -0800 Subject: [PATCH 26/35] Making this clearer, and printing out info --- lib/tasks/data.rake | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/tasks/data.rake b/lib/tasks/data.rake index 87ee06af..96580351 100644 --- a/lib/tasks/data.rake +++ b/lib/tasks/data.rake @@ -32,12 +32,15 @@ namespace :data do # Make random users adopt drains to test server load when generating API data # There has to be users in DB for this to work - unless Rails.env.production? - Thing.first(10_000).each do |t| + if Rails.env.production? + puts "Can't run this in production" + else + Thing.first(1000).each_with_index do |t, i| if t.user_id.blank? - t.user_id = User.find_by('id' => Random.rand(1..User.last.id)).id + t.user_id = User.order('RANDOM()').limit(1).first.id t.save end + puts i.to_s + " Adopting a drain" end end end From 9bb9fdffc8030087bd7b2989daab518e97f5d194 Mon Sep 17 00:00:00 2001 From: James Fowler Date: Fri, 15 Dec 2017 19:27:50 -0800 Subject: [PATCH 27/35] Response data tests for pagination counts and drain data --- test/controllers/adopted_controller_test.rb | 45 +++++++++++++++------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/test/controllers/adopted_controller_test.rb b/test/controllers/adopted_controller_test.rb index f670225f..1b1151b4 100644 --- a/test/controllers/adopted_controller_test.rb +++ b/test/controllers/adopted_controller_test.rb @@ -4,7 +4,15 @@ class AdoptedControllerTest < ActionController::TestCase setup do request.env['devise.mapping'] = Devise.mappings[:user] @user = users(:erik) + @user2 = users(:dan) @admin = users(:admin) + @thing = things(:thing_1) + @thing2 = things(:thing_2) + + @thing.user_id = @user.id + @thing2.user_id = @user2.id + @thing.save + @thing2.save end test 'should get index' do @@ -17,28 +25,41 @@ class AdoptedControllerTest < ActionController::TestCase test 'should get json' do @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@admin.email, 'correct') - get :index, format: :json + get :index assert_equal 'application/json', @response.content_type end - test 'should get xml' do - @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@admin.email, 'correct') + test 'only admins get access' do + @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@user.email, 'correct') - get :index, format: :xml - assert_equal 'application/xml', @response.content_type + get :index + assert_equal 'text/html', @response.content_type # If user were an admin, content_type would be JSON, since that is default end - test 'should get csv' do + + test 'drain data is correct' do @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@admin.email, 'correct') - get :index, format: :csv - assert_equal 'text/csv', @response.content_type - end + get :index + random_drain = JSON.parse(@response.body)["drains"].first - test 'only admins get access' do - @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@user.email, 'correct') + drain = Thing.find_by(city_id: random_drain["city_id"].gsub("N-","")) + assert_not_nil drain + assert_equal drain.lat.to_s, random_drain["latitude"] + assert_equal drain.lng.to_s, random_drain["longitude"] + + end + + test 'page counts' do + @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@admin.email, 'correct') + get :index - assert_equal 'text/html', @response.content_type # If user were an admin, content_type would be JSON, since that is default + json = JSON.parse(@response.body) + puts(json) + + assert_equal json["next_page"], 2 + assert_equal json["prev_page"], -1 + assert_equal json["total_pages"], 1 end end From de24cf80cfc79b89f93aced03852ea8cdbfe910b Mon Sep 17 00:00:00 2001 From: James Fowler Date: Mon, 8 Jan 2018 18:40:42 -0800 Subject: [PATCH 28/35] Adding seed data for API controller test that asserts page counts --- test/controllers/adopted_controller_test.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/controllers/adopted_controller_test.rb b/test/controllers/adopted_controller_test.rb index 1b1151b4..6d56bf4c 100644 --- a/test/controllers/adopted_controller_test.rb +++ b/test/controllers/adopted_controller_test.rb @@ -1,4 +1,5 @@ require 'test_helper' +require 'Rake' class AdoptedControllerTest < ActionController::TestCase setup do @@ -13,6 +14,8 @@ class AdoptedControllerTest < ActionController::TestCase @thing2.user_id = @user2.id @thing.save @thing2.save + + end test 'should get index' do @@ -39,10 +42,8 @@ class AdoptedControllerTest < ActionController::TestCase test 'drain data is correct' do @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@admin.email, 'correct') - get :index random_drain = JSON.parse(@response.body)["drains"].first - drain = Thing.find_by(city_id: random_drain["city_id"].gsub("N-","")) assert_not_nil drain @@ -52,14 +53,14 @@ class AdoptedControllerTest < ActionController::TestCase end test 'page counts' do + Rails.application.load_seed # Seed the user with users and drains + Rake::Task['data:auto_adopt'].invoke # Adopt the seeded drains with seeded users @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@admin.email, 'correct') - get :index json = JSON.parse(@response.body) - puts(json) assert_equal json["next_page"], 2 assert_equal json["prev_page"], -1 - assert_equal json["total_pages"], 1 + assert_equal json["total_pages"], 5 # Should be 5 - default drains per page is 100 and we seeded the DB with 500 end end From 89dcd5070de157ee8ccf899cdd7342aee62ffa51 Mon Sep 17 00:00:00 2001 From: James Fowler Date: Mon, 8 Jan 2018 18:47:55 -0800 Subject: [PATCH 29/35] Moving adopted_controller before_action filters for :index, to better organize --- app/controllers/adopted_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb index 912c7600..237bab4f 100644 --- a/app/controllers/adopted_controller.rb +++ b/app/controllers/adopted_controller.rb @@ -1,12 +1,14 @@ class AdoptedController < ApplicationController before_action :authenticate - before_action :get_adopted_things, :make_cur_page, :make_other_pages, only: [:index] # GET /api/v1/drains/adopted # Optional params: # # page def index + get_adopted_things + make_cur_page + make_other_pages @results = { next_page: @next_page, prev_page: @prev_page, total_pages: @adopted_things.page(1).total_pages, drains: @things } render json: @results end From eda7dcb536bcbfd9bccf74d2c7941c7c5c2a2a31 Mon Sep 17 00:00:00 2001 From: James Fowler Date: Mon, 8 Jan 2018 18:55:53 -0800 Subject: [PATCH 30/35] Using the Thing.adopted scope in place of a where clause --- app/controllers/adopted_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb index 237bab4f..cdda9ead 100644 --- a/app/controllers/adopted_controller.rb +++ b/app/controllers/adopted_controller.rb @@ -16,7 +16,7 @@ def index private def get_adopted_things - @adopted_things = Thing.where.not(user_id: nil) + @adopted_things = Thing.adopted end # Determine if the user supplied a valid page number, if not they get first page From 186b923256aa931125e42c719f4177b73631c253 Mon Sep 17 00:00:00 2001 From: James Fowler Date: Mon, 8 Jan 2018 18:56:53 -0800 Subject: [PATCH 31/35] Removing API template for rendering CSV, since we are only allowing JSON --- app/views/adopted/index.csv.erb | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 app/views/adopted/index.csv.erb diff --git a/app/views/adopted/index.csv.erb b/app/views/adopted/index.csv.erb deleted file mode 100644 index b327ef32..00000000 --- a/app/views/adopted/index.csv.erb +++ /dev/null @@ -1,5 +0,0 @@ -<%- headers = ['Lat', 'Lng', 'City ID'] -%> -<%= CSV.generate_line headers %> -<%- @allthings.each do |thing| -%> -<%= CSV.generate_line([thing.lat, thing.lng, "N-" + thing.city_id.to_s]) -%> -<%- end -%> From bb6afe41399fff7f726c5fb065c31dcfd690cf24 Mon Sep 17 00:00:00 2001 From: James Fowler Date: Mon, 8 Jan 2018 19:21:27 -0800 Subject: [PATCH 32/35] Getting tests to pass --- Gemfile | 4 ++-- app/controllers/adopted_controller.rb | 14 +++++++------- config/environments/development.rb | 2 +- lib/tasks/data.rake | 9 ++++----- test/controllers/adopted_controller_test.rb | 20 ++++++++------------ 5 files changed, 22 insertions(+), 27 deletions(-) diff --git a/Gemfile b/Gemfile index fe59d731..408630a4 100644 --- a/Gemfile +++ b/Gemfile @@ -3,17 +3,17 @@ ruby '2.2.3' gem 'airbrake', '~> 7.1' gem 'devise', '~> 3.0' +gem 'faker', '~> 1.7.0' gem 'geokit', '~> 1.0' gem 'haml', '~> 5.0' gem 'http_accept_language', '~> 2.0' +gem 'kaminari', '~> 1.0' gem 'local_time', '~> 2.0' gem 'obscenity', '~> 1.0', '>= 1.0.2' gem 'pg' gem 'rails', '~> 4.2.10' -gem 'faker', '~> 1.7.0' gem 'rails_admin', '~> 1.0' gem 'validates_formatting_of', '~> 0.9.0' -gem 'kaminari', '~> 1.0' gem 'paranoia', '~> 2.4' gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw] diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb index cdda9ead..5b66a599 100644 --- a/app/controllers/adopted_controller.rb +++ b/app/controllers/adopted_controller.rb @@ -6,22 +6,22 @@ class AdoptedController < ApplicationController # # page def index - get_adopted_things + adopted_things make_cur_page make_other_pages - @results = { next_page: @next_page, prev_page: @prev_page, total_pages: @adopted_things.page(1).total_pages, drains: @things } + @results = {next_page: @next_page, prev_page: @prev_page, total_pages: @adopted_things.page(1).total_pages, drains: @things} render json: @results end private - - def get_adopted_things + + def adopted_things @adopted_things = Thing.adopted end - + # Determine if the user supplied a valid page number, if not they get first page def make_cur_page - page = ((params[:page].blank?) || (params[:page].to_i == 0)) ? 1 : params[:page] + page = params[:page].blank? || params[:page].to_i.zero? ? 1 : params[:page] @cur_page = @adopted_things.page(page) @things = format_fields(@cur_page) end @@ -41,7 +41,7 @@ def authenticate user = User.find_by(email: username) if user && user.valid_password?(password) return true if user.admin? - render html: '
You must be an admin to access this page
'.html_safe + render html: '
You must be an admin to access this page
' end end end diff --git a/config/environments/development.rb b/config/environments/development.rb index c5fb47f9..149638e7 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -14,7 +14,7 @@ # "localhost"/127.0.0.1 ... when developing locally # remote domainname/IP ... when developing on a remote server this_server = Socket.ip_address_list[1] - this_server_hostname = (this_server.getnameinfo[0] == 'localhost') ? 'localhost' : this_server.ip_address + this_server_hostname = this_server.getnameinfo[0] == 'localhost' ? 'localhost' : this_server.ip_address config.action_controller.asset_host = 'http://' + this_server_hostname + ':3000' config.action_mailer.asset_host = config.action_controller.asset_host diff --git a/lib/tasks/data.rake b/lib/tasks/data.rake index 96580351..9aae4890 100644 --- a/lib/tasks/data.rake +++ b/lib/tasks/data.rake @@ -17,9 +17,7 @@ namespace :data do ENV['MAXIMUM_MOVEMENT_IN_FEET'] || raise('$MAXIMUM_MOVEMENT_IN_FEET required') adoption_deletion_from = Time.zone.parse(ENV['ADOPTION_DELETION_FROM']) - moved_adoptions = AdoptionMover.move_close_deleted_adoptions(adoption_deletion_from, ENV['MAXIMUM_MOVEMENT_IN_FEET']) - CSV($stdout) do |csv| csv << %w[from to] moved_adoptions.each do |from, to| @@ -27,20 +25,21 @@ namespace :data do end end end +end +namespace :modify do task auto_adopt: :environment do # Make random users adopt drains to test server load when generating API data # There has to be users in DB for this to work - if Rails.env.production? - puts "Can't run this in production" + puts "Can't run this in production" else Thing.first(1000).each_with_index do |t, i| if t.user_id.blank? t.user_id = User.order('RANDOM()').limit(1).first.id t.save end - puts i.to_s + " Adopting a drain" + puts i.to_s + ' Adopting a drain' end end end diff --git a/test/controllers/adopted_controller_test.rb b/test/controllers/adopted_controller_test.rb index 6d56bf4c..666a9092 100644 --- a/test/controllers/adopted_controller_test.rb +++ b/test/controllers/adopted_controller_test.rb @@ -14,8 +14,6 @@ class AdoptedControllerTest < ActionController::TestCase @thing2.user_id = @user2.id @thing.save @thing2.save - - end test 'should get index' do @@ -39,28 +37,26 @@ class AdoptedControllerTest < ActionController::TestCase assert_equal 'text/html', @response.content_type # If user were an admin, content_type would be JSON, since that is default end - test 'drain data is correct' do @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@admin.email, 'correct') get :index - random_drain = JSON.parse(@response.body)["drains"].first - drain = Thing.find_by(city_id: random_drain["city_id"].gsub("N-","")) + random_drain = JSON.parse(@response.body)['drains'].first + drain = Thing.find_by(city_id: random_drain['city_id'].gsub('N-', '')) assert_not_nil drain - assert_equal drain.lat.to_s, random_drain["latitude"] - assert_equal drain.lng.to_s, random_drain["longitude"] - + assert_equal drain.lat.to_s, random_drain['latitude'] + assert_equal drain.lng.to_s, random_drain['longitude'] end test 'page counts' do Rails.application.load_seed # Seed the user with users and drains - Rake::Task['data:auto_adopt'].invoke # Adopt the seeded drains with seeded users + Rake::Task['modify:auto_adopt'].invoke # Adopt the seeded drains with seeded users @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@admin.email, 'correct') get :index json = JSON.parse(@response.body) - assert_equal json["next_page"], 2 - assert_equal json["prev_page"], -1 - assert_equal json["total_pages"], 5 # Should be 5 - default drains per page is 100 and we seeded the DB with 500 + assert_equal json['next_page'], 2 + assert_equal json['prev_page'], -1 + assert_equal json['total_pages'], 5 # Should be 5 - default drains per page is 100 and we seeded the DB with 500 end end From 4c71b20c319e86917c4134457ebe6caabee168a7 Mon Sep 17 00:00:00 2001 From: James Fowler Date: Mon, 8 Jan 2018 19:36:12 -0800 Subject: [PATCH 33/35] Changing require Rake to rake --- test/controllers/adopted_controller_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/controllers/adopted_controller_test.rb b/test/controllers/adopted_controller_test.rb index 666a9092..0d9f20cc 100644 --- a/test/controllers/adopted_controller_test.rb +++ b/test/controllers/adopted_controller_test.rb @@ -1,5 +1,5 @@ require 'test_helper' -require 'Rake' +require 'rake' class AdoptedControllerTest < ActionController::TestCase setup do From 5a9210943315f4fb47bba38772346c52c94fc04e Mon Sep 17 00:00:00 2001 From: James Fowler Date: Sun, 4 Feb 2018 19:44:09 -0800 Subject: [PATCH 34/35] Trying to get the Github tests to pass - Unsure of the problem --- removeme | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 removeme diff --git a/removeme b/removeme new file mode 100644 index 00000000..e69de29b From d760550cd80608eb8fe3ffed6e3067e237b10007 Mon Sep 17 00:00:00 2001 From: James Fowler Date: Sun, 4 Feb 2018 19:56:01 -0800 Subject: [PATCH 35/35] Remove test file --- removeme | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 removeme diff --git a/removeme b/removeme deleted file mode 100644 index e69de29b..00000000