mirror of
https://github.com/ENSL/ensl.org.git
synced 2024-12-27 21:10:54 +00:00
commit
cf2fcd6e89
11 changed files with 213 additions and 77 deletions
17
app/controllers/api/v1/maps_controller.rb
Normal file
17
app/controllers/api/v1/maps_controller.rb
Normal file
|
@ -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
|
21
app/controllers/api/v1/servers_controller.rb
Normal file
21
app/controllers/api/v1/servers_controller.rb
Normal file
|
@ -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
|
|
@ -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')
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -6,6 +6,8 @@ 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
|
||||
|
||||
|
|
19
spec/controllers/api/v1/maps_controller_spec.rb
Normal file
19
spec/controllers/api/v1/maps_controller_spec.rb
Normal file
|
@ -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
|
20
spec/controllers/api/v1/servers_controller_spec.rb
Normal file
20
spec/controllers/api/v1/servers_controller_spec.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -17,6 +17,14 @@ FactoryGirl.define do
|
|||
ban_type Ban::TYPE_MUTE
|
||||
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
|
||||
|
|
5
spec/factories/map.rb
Normal file
5
spec/factories/map.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
FactoryGirl.define do
|
||||
factory :map do
|
||||
sequence(:name) { |n| "ns_MapName#{n}" }
|
||||
end
|
||||
end
|
|
@ -4,5 +4,13 @@ FactoryGirl.define do
|
|||
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
|
Loading…
Reference in a new issue