-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
236 simple api rebased #290
base: main
Are you sure you want to change the base?
Changes from all commits
614ba53
2274f4d
c007d1c
e1c2f24
8b188b5
d16cbb3
6615a92
ac6c3f7
b7d863a
e2052d0
ffbe684
cfee6da
96aae96
056ed5c
0f816bf
395ad25
227ee70
9ce0b74
3ec42b8
b945bec
0423a9c
d822448
2c71f1b
32f2243
ca68a43
c385883
9bb9fdf
de24cf8
89dcd50
eda7dcb
186b923
bb6afe4
4c71b20
5a92109
d760550
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class AdoptedController < ApplicationController | ||
before_action :authenticate | ||
|
||
# GET /api/v1/drains/adopted | ||
# Optional params: | ||
# | ||
# page | ||
def index | ||
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 | ||
|
||
private | ||
|
||
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.zero? ? 1 : params[:page] | ||
@cur_page = @adopted_things.page(page) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This coupling feels a bit weird as it requires the developer to have called I think this can be simplified a bit if adopted_things = Things.adopted.order(:created_at).page(params[:page])
results = {
next_page: adopted_things.next_page, # I think it's fine if this is null to the caller
prev_page: adopted_things.prev_page,
total_pages: adopted_things.total_pages,
things: format_fields(things)
}
render json: @results and then the |
||
@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? ? -1 : @cur_page.next_page | ||
@prev_page = @cur_page.prev_page.nil? ? -1 : @cur_page.prev_page | ||
end | ||
|
||
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| | ||
user = User.find_by(email: username) | ||
if user && user.valid_password?(password) | ||
return true if user.admin? | ||
render html: '<div> You must be an admin to access this page </div>' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feels like we should return a 401 here in addition to the rendered HTML. |
||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module AdoptedHelper | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Kaminari.configure do |config| | ||
config.default_per_page = 100 | ||
config.max_per_page = 1000 | ||
# config.window = 4 | ||
# config.outer_window = 0 | ||
# config.left = 0 | ||
# config.right = 0 | ||
# config.page_method_name = :page | ||
# config.param_name = :page | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,8 @@ | ||
User.where(email: '[email protected]').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: '[email protected]').first_or_initialize.tap do |user| | ||
user.first_name = 'Jane' | ||
user.last_name = 'Doe' | ||
user.password = 'password' | ||
user.admin = true | ||
user.save! | ||
end | ||
|
@@ -24,3 +18,14 @@ | |
thing.save! | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'test_helper' | ||
require 'rake' | ||
|
||
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 | ||
@request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(@user.email, 'correct') | ||
|
||
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 | ||
assert_equal 'application/json', @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 | ||
|
||
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 | ||
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['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 | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed for all environments? It feels like it is only needed for
test
and maybedevelopment
.