Invalid steam ids on the users API now returns null for steam attributes

This commit is contained in:
Chris Blanchard 2015-08-17 19:06:47 +01:00
parent e6f05374d3
commit a3d9f5bc06
3 changed files with 24 additions and 5 deletions

View file

@ -5,7 +5,7 @@ class Api::V1::UsersController < Api::V1::BaseController
def show
@user = User.find(params[:id])
@steam = SteamCondenser::Community::SteamId.from_steam_id("STEAM_#{@user.steamid}")
@steam = steam_profile @user
render json: {
id: @user.id,
@ -15,8 +15,8 @@ class Api::V1::UsersController < Api::V1::BaseController
avatar: @user.profile.avatar.url,
admin: @user.admin?,
steam: {
url: @steam.base_url,
nickname: @steam.nickname
url: @steam.nil? ? nil : @steam.base_url,
nickname: @steam.nil? ? nil : @steam.nickname
},
bans: {
gather: @user.banned?(Ban::TYPE_GATHER).present?,
@ -28,4 +28,12 @@ class Api::V1::UsersController < Api::V1::BaseController
rescue ActiveRecord::RecordNotFound
raise ActionController::RoutingError.new('User Not Found')
end
private
def steam_profile user
SteamCondenser::Community::SteamId.from_steam_id("STEAM_#{user.steamid}")
rescue SteamCondenser::Error => e
return nil
end
end

View file

@ -28,6 +28,17 @@ describe Api::V1::UsersController do
expect(json['team']).to be_nil
end
it 'returns data for users with invalid steam ids' do
@user.steamid = '0:0:000'
@user.save!
get :show, id: @user.id
expect(response).to be_success
expect(json['steam']['url']).to be_nil
expect(json['steam']['nickname']).to be_nil
end
it 'returns 404 if user does not exist' do
expect {
get :show, id: -1

View file

@ -23,8 +23,8 @@ describe Api::V1::UsersCollection do
expect(collection.execute_query.size).to eq(3)
end
it 'returns 5 columns' do
expect(collection.execute_query.first.size).to eq(5)
it 'returns 6 columns' do
expect(collection.execute_query.first.size).to eq(6)
end
end
end