From 03ef45225c9df0f1f908c0bf6d38d968d1f68d1d Mon Sep 17 00:00:00 2001 From: Chris Blanchard Date: Sat, 1 Aug 2015 16:20:49 +0100 Subject: [PATCH 1/5] Added server api --- app/controllers/api/v1/servers_controller.rb | 21 ++++ config/routes.rb | 97 ++++++++++--------- .../api/v1/servers_controller_spec.rb | 20 ++++ spec/factories/server.rb | 20 ++-- 4 files changed, 104 insertions(+), 54 deletions(-) create mode 100644 app/controllers/api/v1/servers_controller.rb create mode 100644 spec/controllers/api/v1/servers_controller_spec.rb 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/config/routes.rb b/config/routes.rb index 483f9d2..0cf0a48 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,76 +6,77 @@ Ensl::Application.routes.draw do namespace :api do namespace :v1 do resources :users, only: [:show, :index] + resources :servers, 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 +128,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/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/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 From a3479a6fb250d3c163ac306d632244ac219d066d Mon Sep 17 00:00:00 2001 From: Chris Blanchard Date: Sat, 1 Aug 2015 16:33:40 +0100 Subject: [PATCH 2/5] Ooops --- app/controllers/shoutmsgs_controller.rb | 1 - 1 file changed, 1 deletion(-) 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 From a3356e57d3199094b0e6c74f360682083d0fd360 Mon Sep 17 00:00:00 2001 From: Chris Blanchard Date: Sat, 1 Aug 2015 16:34:01 +0100 Subject: [PATCH 3/5] Added maps api --- app/controllers/api/v1/maps_controller.rb | 17 +++++++++++++++++ config/routes.rb | 1 + .../api/v1/maps_controller_spec.rb | 19 +++++++++++++++++++ spec/factories/map.rb | 5 +++++ 4 files changed, 42 insertions(+) create mode 100644 app/controllers/api/v1/maps_controller.rb create mode 100644 spec/controllers/api/v1/maps_controller_spec.rb create mode 100644 spec/factories/map.rb 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/config/routes.rb b/config/routes.rb index 0cf0a48..1442471 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,7 @@ Ensl::Application.routes.draw do namespace :v1 do resources :users, only: [:show, :index] resources :servers, only: [:index] + resources :maps, only: [:index] end 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/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 From f9f4485c9500016d28b67685d97b4c19d2f71b8c Mon Sep 17 00:00:00 2001 From: Chris Blanchard Date: Sat, 1 Aug 2015 16:44:39 +0100 Subject: [PATCH 4/5] Added ban information to user api --- app/controllers/api/v1/users_controller.rb | 5 ++ .../api/v1/users_controller_spec.rb | 19 ++++++++ spec/factories/ban.rb | 46 +++++++++++-------- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 9a22225..af2f5ff 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -17,6 +17,11 @@ 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? } } rescue ActiveRecord::RecordNotFound diff --git a/spec/controllers/api/v1/users_controller_spec.rb b/spec/controllers/api/v1/users_controller_spec.rb index 73f1740..4cc19e6 100644 --- a/spec/controllers/api/v1/users_controller_spec.rb +++ b/spec/controllers/api/v1/users_controller_spec.rb @@ -29,6 +29,25 @@ 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 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 From cdcf9fedf8d068d44867595f0d5b41131033b6bd Mon Sep 17 00:00:00 2001 From: Chris Blanchard Date: Sat, 1 Aug 2015 17:05:17 +0100 Subject: [PATCH 5/5] Added team information to usre --- app/controllers/api/v1/users_controller.rb | 3 ++- spec/controllers/api/v1/users_controller_spec.rb | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index af2f5ff..ea0f56b 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -22,7 +22,8 @@ class Api::V1::UsersController < Api::V1::BaseController 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/spec/controllers/api/v1/users_controller_spec.rb b/spec/controllers/api/v1/users_controller_spec.rb index 4cc19e6..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 @@ -48,6 +52,14 @@ describe Api::V1::UsersController do 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