From b15e1f888ce922ae6395775b1ab0de13072b920b Mon Sep 17 00:00:00 2001 From: Ari Timonen Date: Mon, 23 Mar 2020 04:22:12 +0200 Subject: [PATCH] Fix more conditions and tests --- app/controllers/api/v1/users_controller.rb | 4 +- app/controllers/comments_controller.rb | 2 +- app/helpers/gathers_helper.rb | 2 +- app/models/contester.rb | 2 +- app/models/forumer.rb | 3 +- app/models/map.rb | 4 +- app/models/server.rb | 4 +- app/models/user.rb | 2 +- .../api/v1/servers_controller_spec.rb | 2 +- .../api/v1/teams_controller_spec.rb | 6 +-- .../api/v1/users_controller_spec.rb | 48 +++++++++---------- 11 files changed, 38 insertions(+), 41 deletions(-) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 3848d10..dfc5dbc 100755 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -8,9 +8,9 @@ class Api::V1::UsersController < Api::V1::BaseController @user = User.find(params[:id]) elsif params[:format] == "steamid" steamid_i = params[:id].to_i - @user = User.first(conditions: { steamid: format("0:%d:%d", steamid_i % 2, steamid_i >> 1) }) + @user = User.where(steamid: format("0:%d:%d", steamid_i % 2, steamid_i >> 1)).first elsif params[:format] == "steamidstr" - @user = User.first(conditions: { steamid: params[:id] }) + @user = User.where(steamid: params[:id]).first end if @user.nil? diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 76fa173..744dae4 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -7,7 +7,7 @@ class CommentsController < ApplicationController end def show - @comments = Comment.recent5.all conditions: { commentable_id: params[:id2], commentable_type: params[:id] } + @comments = Comment.recent5.where(commentable_id: params[:id2], commentable_type: params[:id]) render partial: 'list', layout: false end diff --git a/app/helpers/gathers_helper.rb b/app/helpers/gathers_helper.rb index 90dbaee..70ff9fa 100644 --- a/app/helpers/gathers_helper.rb +++ b/app/helpers/gathers_helper.rb @@ -5,7 +5,7 @@ module GathersHelper render partial: 'running', layout: false elsif @gather.status == Gather::STATE_VOTING - if @gatherer and @gather.gatherer_votes.first(conditions: { user_id: cuser.id }) + if @gatherer and @gather.gatherer_votes.where(user_id: cuser.id) headers['Gather'] = 'voted' else headers['Gather'] = 'voting' diff --git a/app/models/contester.rb b/app/models/contester.rb index 306864f..6ffc54a 100644 --- a/app/models/contester.rb +++ b/app/models/contester.rb @@ -73,7 +73,7 @@ class Contester < ActiveRecord::Base end def get_matches - contest.matches.all :conditions => ["contester1_id = ? OR contester2_id = ?", id, id] + contest.matches.where("contester1_id = ? OR contester2_id = ?", id, id) end def init_variables diff --git a/app/models/forumer.rb b/app/models/forumer.rb index 923049d..dadfa37 100644 --- a/app/models/forumer.rb +++ b/app/models/forumer.rb @@ -22,8 +22,7 @@ class Forumer < ActiveRecord::Base include Extra - scope :access, - lambda { |level| {:conditions => ["access >= ?", level]} } + scope :access, -> (level) { where("access >= ?", level) } validates_uniqueness_of :group_id, :scope => [:forum_id, :access] validates_presence_of [:group_id, :forum_id] diff --git a/app/models/map.rb b/app/models/map.rb index 88f090a..eebdee1 100644 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -22,9 +22,7 @@ class Map < ActiveRecord::Base scope :basic, -> { where(deleted: false).order("name") } scope :with_name, -> (name) { where(name: name) } scope :classic, -> { where("name LIKE 'ns_%'") } - scope :of_category, - lambda { |category| { - :conditions => {:category_id => category.id} }} + scope :of_category, ->(category) { where(category_id: category.id) } validates_length_of :name, :maximum => 20 validates_length_of :download, :maximum => 100 diff --git a/app/models/server.rb b/app/models/server.rb index 039b777..24d18e2 100644 --- a/app/models/server.rb +++ b/app/models/server.rb @@ -131,7 +131,7 @@ class Server < ActiveRecord::Base end def self.move addr, newaddr, newpwd - self.hltvs.all(:conditions => {:reservation => addr}).each do |hltv| + self.hltvs.where(reservation => addr).each do |hltv| hltv.reservation = newaddr hltv.pwd = newpwd hltv.save! @@ -139,7 +139,7 @@ class Server < ActiveRecord::Base end def self.stop addr - self.hltvs.all(:conditions => {:reservation => addr}).each do |hltv| + self.hltvs.where(:reservation => addr).each do |hltv| hltv.reservation = nil hltv.save! end diff --git a/app/models/user.rb b/app/models/user.rb index f4c6538..eb1b778 100755 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -108,7 +108,7 @@ class User < ActiveRecord::Base .order("num DESC") } scope :banned, -> { joins("LEFT JOIN bans ON bans.user_id = users.id AND expiry > UTC_TIMESTAMP()") - .conditions("bans.id IS NOT NULL") } + .where("bans.id IS NOT NULL") } scope :idle, -> { where("lastvisit < ?", 30.minutes.ago.utc) } scope :lately, -> { diff --git a/spec/controllers/api/v1/servers_controller_spec.rb b/spec/controllers/api/v1/servers_controller_spec.rb index 9b9e4f5..43d71f6 100755 --- a/spec/controllers/api/v1/servers_controller_spec.rb +++ b/spec/controllers/api/v1/servers_controller_spec.rb @@ -11,7 +11,7 @@ describe Api::V1::ServersController do it "returns a list of servers" do get :index - expect(response).to be_success + expect(response).to have_http_status(:success) expect(json["servers"].length).to eq(1) json_server = json["servers"][0] expect(json_server["id"]).to eq(server.id) diff --git a/spec/controllers/api/v1/teams_controller_spec.rb b/spec/controllers/api/v1/teams_controller_spec.rb index fd0f999..3cb664f 100644 --- a/spec/controllers/api/v1/teams_controller_spec.rb +++ b/spec/controllers/api/v1/teams_controller_spec.rb @@ -16,8 +16,8 @@ describe Api::V1::TeamsController do end it "returns team data" do - get :show, id: @team.id - expect(response).to be_success + get :show, params: { id: @team.id } + expect(response).to have_http_status(:success) expect(json["id"]).to eq(@team.id) expect(json["name"]).to eq(@team.name) @@ -28,7 +28,7 @@ describe Api::V1::TeamsController do it "returns 404 if team not found" do expect { - get :show, id: Team.last.id + 1 + get :show, params: { id: Team.last.id + 1 } }.to raise_error(ActionController::RoutingError) end end diff --git a/spec/controllers/api/v1/users_controller_spec.rb b/spec/controllers/api/v1/users_controller_spec.rb index 2c4beb1..643eed9 100755 --- a/spec/controllers/api/v1/users_controller_spec.rb +++ b/spec/controllers/api/v1/users_controller_spec.rb @@ -31,16 +31,16 @@ describe Api::V1::UsersController do end it "returns user data" do - get :show, id: @user.id + get :show, params: { id: @user.id } - expect(response).to be_success + expect(response).to have_http_status(:success) user_expectation(json, @user) end it "returns user data for query with id specified as format" do - get :show, id: @user.id, format: "id" + get :show, params: { id: @user.id, format: "id" } - expect(response).to be_success + expect(response).to have_http_status(:success) user_expectation(json, @user) end @@ -48,16 +48,16 @@ describe Api::V1::UsersController do m = @user.steamid.match(/\A0:([01]):(\d{1,10})\Z/) steamid = (m[2].to_i << 1) + m[1].to_i - get :show, id: steamid, format: "steamid" + get :show, params: { id: steamid, format: "steamid" } - expect(response).to be_success + expect(response).to have_http_status(:success) user_expectation(json, @user) end it "returns user data for a string steamid query" do - get :show, id: @user.steamid, format: "steamidstr" + get :show, params: { id: @user.steamid, format: "steamidstr" } - expect(response).to be_success + expect(response).to have_http_status(:success) user_expectation(json, @user) end @@ -65,27 +65,27 @@ describe Api::V1::UsersController do @user.steamid = nil @user.save! - get :show, id: @user.id + get :show, params: { id: @user.id } - expect(response).to be_success + expect(response).to have_http_status(:success) expect(json["steam"]).to be_nil end it "returns gather moderator status" do group = create :group, :gather_moderator create :grouper, user: @user, group: group - get :show, id: @user.id + get :show, params: { id: @user.id } expect(json["moderator"]).to eq(true) end it "returns 404 if user does not exist" do - get :show, id: -1 + get :show, params: { id: -1 } expect(response.status).to eq(404) expect(json["error"]).to eq("User not found") end it "returns 404 if user does not exist by steamid" do - get :show, id: -1, format: "steamid" + get :show, params: { id: -1, format: "steamid" } expect(response.status).to eq(404) expect(json["error"]).to eq("User not found") end @@ -93,9 +93,9 @@ describe Api::V1::UsersController do it "queries the steam condenser for an invalid steamid" do @user.update_attribute(:steamid, "0:0:0") - get :show, id: @user.id + get :show, params: { id: @user.id } - expect(response).to be_success + expect(response).to have_http_status(:success) expect(json["steam"]).to_not be_nil expect(json["steam"]["id"]).to eq(@user.steamid) expect(json["steam"]["url"]).to be_nil @@ -104,30 +104,30 @@ describe Api::V1::UsersController do it "returns correct ban if user muted" do create :ban, :mute, user: @user - get :show, id: @user.id - expect(response).to be_success + get :show, params: { id: @user.id } + expect(response).to have_http_status(:success) expect(json["bans"]["mute"]).to eq(true) end it "returns correct ban if user gather banned" do create :ban, :gather, user: @user - get :show, id: @user.id - expect(response).to be_success + get :show, params: { id: @user.id } + expect(response).to have_http_status(:success) expect(json["bans"]["gather"]).to eq(true) end it "returns correct ban if user site banned" do create :ban, :site, user: @user - get :show, id: @user.id - expect(response).to be_success + get :show, params: { id: @user.id } + expect(response).to have_http_status(:success) expect(json["bans"]["site"]).to eq(true) end it "returns team information" do @user.destroy @user_with_team = create :user_with_team, :chris - get :show, id: @user_with_team.id - expect(response).to be_success + get :show, params: { id: @user_with_team.id } + expect(response).to have_http_status(:success) expect(json["team"]["id"]).to eq(@user_with_team.team.id) expect(json["team"]["name"]).to eq(@user_with_team.team.name) end @@ -143,7 +143,7 @@ describe Api::V1::UsersController do get :index - expect(response).to be_success + expect(response).to have_http_status(:success) expect(json["users"].size).to eq(users.size) end