Skip to content

Commit

Permalink
Response data tests for pagination counts and drain data
Browse files Browse the repository at this point in the history
  • Loading branch information
James Fowler authored and James Fowler committed Dec 16, 2017
1 parent c375680 commit ea959a8
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions test/controllers/adopted_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit ea959a8

Please sign in to comment.