diff --git a/app/controllers/api/v1/maps_controller.rb b/app/controllers/api/v1/maps_controller.rb new file mode 100644 index 0000000..da7ffbb --- /dev/null +++ b/app/controllers/api/v1/maps_controller.rb @@ -0,0 +1,17 @@ +class Api::V1::MapsController < Api::V1::BaseController + def index + render json: { maps: gather_maps } + end + + private + + def gather_maps + Map.classic.basic.map do |m| + { + id: m.id, + name: m.name, + category_id: m.category_id + } + end + end +end \ No newline at end of file diff --git a/app/controllers/api/v1/servers_controller.rb b/app/controllers/api/v1/servers_controller.rb new file mode 100644 index 0000000..fe5a6cf --- /dev/null +++ b/app/controllers/api/v1/servers_controller.rb @@ -0,0 +1,21 @@ +class Api::V1::ServersController < Api::V1::BaseController + def index + render json: { servers: active_servers } + end + + private + + def active_servers + Server.active.map do |s| + { + id: s.id, + description: s.description, + dns: s.dns, + ip: s.ip, + port: s.port, + password: s.password, + category_id: s.category_id + } + end + end +end \ No newline at end of file diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 9a22225..ea0f56b 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -17,7 +17,13 @@ class Api::V1::UsersController < Api::V1::BaseController steam: { url: @steam.base_url, nickname: @steam.nickname - } + }, + bans: { + gather: @user.banned?(Ban::TYPE_GATHER).present?, + mute: @user.banned?(Ban::TYPE_MUTE).present?, + site: @user.banned?(Ban::TYPE_SITE).present? + }, + team: @user.team.present? ? { id: @user.team.id, name: @user.team.name } : nil } rescue ActiveRecord::RecordNotFound raise ActionController::RoutingError.new('User Not Found') diff --git a/app/controllers/shoutmsgs_controller.rb b/app/controllers/shoutmsgs_controller.rb index 395328a..041116c 100644 --- a/app/controllers/shoutmsgs_controller.rb +++ b/app/controllers/shoutmsgs_controller.rb @@ -15,7 +15,6 @@ class ShoutmsgsController < ApplicationController def create @shoutmsg = Shoutmsg.new params[:shoutmsg] - puts @shoutmsg @shoutmsg.user = cuser raise AccessError unless @shoutmsg.can_create? cuser diff --git a/config/routes.rb b/config/routes.rb index 483f9d2..1442471 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,76 +6,78 @@ Ensl::Application.routes.draw do namespace :api do namespace :v1 do resources :users, only: [:show, :index] + resources :servers, only: [:index] + resources :maps, only: [:index] end end - root to: "articles#news_index" + root to: "articles#news_index" - resources :articles do - resources :versions - end + resources :articles do + resources :versions + end match 'contests/del_map' match 'contests/scores' match 'contests/historical', to: "contests#historical" - resources :contests do - get 'current', on: :collection - end + resources :contests do + get 'current', on: :collection + end resources :log_events - resources :categories - resources :options - resources :polls + resources :categories + resources :options + resources :polls match 'comments/quote' - resources :comments - resources :shoutmsgs - resources :teamers - resources :teams - resources :gathers - resources :gatherers - resources :groups - resources :groupers - resources :forumers - resources :topics + resources :comments + resources :shoutmsgs + resources :teamers + resources :teams + resources :gathers + resources :gatherers + resources :groups + resources :groupers + resources :forumers + resources :topics match 'forums/up' match 'forums/down' - resources :forums - resources :users - resources :locks - resources :contesters - resources :contests - resources :challenges - resources :servers - resources :predictions - resources :rounds - resources :matches do |m| + resources :forums + resources :users + resources :locks + resources :contesters + resources :contests + resources :challenges + resources :servers + resources :predictions + resources :rounds + resources :matches do |m| get :admin, to: "matches#admin", on: :collection get :ref, to: "matches#ref" end - resources :maps - resources :logs - resources :log_files - resources :directories + resources :maps + resources :logs + resources :log_files + resources :directories resources :data_files - resources :predictions - resources :weeks - resources :movies - resources :messages - resources :sites - resources :bans - resources :tweets - resources :issues + resources :predictions + resources :weeks + resources :movies + resources :messages + resources :sites + resources :bans + resources :tweets + resources :issues match 'posts/quote' - resources :posts - resources :brackets + resources :posts + resources :brackets match 'about/action' match 'about/staff' @@ -127,10 +129,10 @@ Ensl::Application.routes.draw do match 'votes/create' - match ':controller/:action', requirements: { action: /A-Za-z/ } - match ':controller/:action/:id' - match ':controller/:action/:id.:format' - match ':controller/:action/:id/:id2' + match ':controller/:action', requirements: { action: /A-Za-z/ } + match ':controller/:action/:id' + match ':controller/:action/:id.:format' + match ':controller/:action/:id/:id2' match 'teamers/replace', to: 'teamers#replace', as: 'teamers_replace' end diff --git a/spec/controllers/api/v1/maps_controller_spec.rb b/spec/controllers/api/v1/maps_controller_spec.rb new file mode 100644 index 0000000..8d1d4ba --- /dev/null +++ b/spec/controllers/api/v1/maps_controller_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +describe Api::V1::MapsController do + before do + request.accept = 'application/json' + end + + describe '#index' do + let!(:map) { create :map } + + it 'returns a list of maps' do + get :index + expect(response).to be_success + expect(json['maps'].length).to eq(1) + json_map = json['maps'][0] + expect(json_map['id']).to eq(map.id) + end + end +end \ No newline at end of file diff --git a/spec/controllers/api/v1/servers_controller_spec.rb b/spec/controllers/api/v1/servers_controller_spec.rb new file mode 100644 index 0000000..8b55eec --- /dev/null +++ b/spec/controllers/api/v1/servers_controller_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe Api::V1::ServersController do + before do + request.accept = 'application/json' + end + + describe '#index' do + let!(:server) { create :server, :active } + let!(:inactive_server) { create :server, :inactive } + + it 'returns a list of servers' do + get :index + expect(response).to be_success + expect(json['servers'].length).to eq(1) + json_server = json['servers'][0] + expect(json_server['id']).to eq(server.id) + end + end +end \ No newline at end of file diff --git a/spec/controllers/api/v1/users_controller_spec.rb b/spec/controllers/api/v1/users_controller_spec.rb index 73f1740..534b939 100644 --- a/spec/controllers/api/v1/users_controller_spec.rb +++ b/spec/controllers/api/v1/users_controller_spec.rb @@ -6,8 +6,8 @@ describe Api::V1::UsersController do end describe '#show' do - before do - @user = create :user_with_team, :chris + before(:each) do + @user = create :user, :chris end it 'returns user data' do @@ -22,6 +22,10 @@ describe Api::V1::UsersController do expect(json).to have_key("steam") expect(json['steam']).to have_key("url") expect(json['steam']).to have_key("nickname") + expect(json['bans']['mute']).to eq(false) + expect(json['bans']['gather']).to eq(false) + expect(json['bans']['site']).to eq(false) + expect(json['team']).to be_nil end it 'returns 404 if user does not exist' do @@ -29,6 +33,33 @@ describe Api::V1::UsersController do get :show, id: -1 }.to raise_error(ActionController::RoutingError) end + + it 'returns correct ban if user muted' do + create :ban, :mute, user: @user + get :show, id: @user.id + expect(response).to be_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 + 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 + 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 + expect(json['team']['id']).to eq(@user_with_team.team.id) + expect(json['team']['name']).to eq(@user_with_team.team.name) + end end describe '#index' do diff --git a/spec/factories/ban.rb b/spec/factories/ban.rb index 81a5c7b..0f998be 100644 --- a/spec/factories/ban.rb +++ b/spec/factories/ban.rb @@ -1,23 +1,31 @@ FactoryGirl.define do - factory :ban do - ban_type Ban::TYPE_SITE - expiry Date.today + 1 - # Hack because of the awkward way bans are created (requires user_name) - before(:create) do |ban| - if ban.user.nil? - user = create :user - ban.user_name = user.username - else - ban.user_name = ban.user.username - end - end - end + factory :ban do + ban_type Ban::TYPE_SITE + expiry Date.today + 1 + # Hack because of the awkward way bans are created (requires user_name) + before(:create) do |ban| + if ban.user.nil? + user = create :user + ban.user_name = user.username + else + ban.user_name = ban.user.username + end + end + end - trait :mute do - ban_type Ban::TYPE_MUTE - end + trait :mute do + ban_type Ban::TYPE_MUTE + end - trait :expired do - expiry Date.yesterday - 1 - end + trait :site do + ban_type Ban::TYPE_SITE + end + + trait :gather do + ban_type Ban::TYPE_GATHER + end + + trait :expired do + expiry Date.yesterday - 1 + end end \ No newline at end of file diff --git a/spec/factories/map.rb b/spec/factories/map.rb new file mode 100644 index 0000000..36ce198 --- /dev/null +++ b/spec/factories/map.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :map do + sequence(:name) { |n| "ns_MapName#{n}" } + end +end diff --git a/spec/factories/server.rb b/spec/factories/server.rb index 837c9ad..e5cb877 100644 --- a/spec/factories/server.rb +++ b/spec/factories/server.rb @@ -1,8 +1,16 @@ FactoryGirl.define do - factory :server do - sequence(:name) { |n| "ServerName#{n}" } - sequence(:dns) { |n| "DNS#{n}" } - sequence(:ip) { |n| "192.168.#{n % 255}.#{n}" } - sequence(:port) { |n| "#{1000 + n}" } - end + factory :server do + sequence(:name) { |n| "ServerName#{n}" } + sequence(:dns) { |n| "DNS#{n}" } + sequence(:ip) { |n| "192.168.#{n % 255}.#{n}" } + sequence(:port) { |n| "#{1000 + n}" } + + trait :active do + active true + end + + trait :inactive do + active false + end + end end \ No newline at end of file