From df1eae063146dee666a632026da3abc541735715 Mon Sep 17 00:00:00 2001 From: cblanc Date: Thu, 14 May 2015 17:27:12 +0100 Subject: [PATCH] Added ns server specs --- spec/factories/group.rb | 12 +++ spec/factories/grouper.rb | 5 + spec/factories/server.rb | 8 ++ spec/factories/user.rb | 7 ++ .../features/servers/server_administration.rb | 30 ++++++ spec/features/servers/user_servers.rb | 14 +++ spec/models/server_spec.rb | 98 +++++++++++++++++++ spec/spec_helper.rb | 1 + spec/support/features/server_helpers.rb | 49 ++++++++++ 9 files changed, 224 insertions(+) create mode 100644 spec/factories/group.rb create mode 100644 spec/factories/grouper.rb create mode 100644 spec/factories/server.rb create mode 100644 spec/features/servers/server_administration.rb create mode 100644 spec/features/servers/user_servers.rb create mode 100644 spec/models/server_spec.rb create mode 100644 spec/support/features/server_helpers.rb diff --git a/spec/factories/group.rb b/spec/factories/group.rb new file mode 100644 index 0000000..0a101b3 --- /dev/null +++ b/spec/factories/group.rb @@ -0,0 +1,12 @@ +FactoryGirl.define do + factory :group do + sequence(:id) { |n| n + 100 } # Preserve first 100 + sequence(:name) { |n| "Group#{n}" } + association :founder, factory: :user + end + + trait :admin do + name "Admins" + id Group::ADMINS + end +end diff --git a/spec/factories/grouper.rb b/spec/factories/grouper.rb new file mode 100644 index 0000000..54c492b --- /dev/null +++ b/spec/factories/grouper.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :grouper do + sequence(:task) { |n| "Task#{n}" } + end +end \ No newline at end of file diff --git a/spec/factories/server.rb b/spec/factories/server.rb new file mode 100644 index 0000000..837c9ad --- /dev/null +++ b/spec/factories/server.rb @@ -0,0 +1,8 @@ +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 +end \ No newline at end of file diff --git a/spec/factories/user.rb b/spec/factories/user.rb index fc5f7aa..9b15973 100644 --- a/spec/factories/user.rb +++ b/spec/factories/user.rb @@ -13,6 +13,13 @@ FactoryGirl.define do create(:profile, user: user) end + trait :admin do + after(:create) do |user| + group = create(:group, :admin) + create :grouper, user: user, group: group + end + end + factory :user_with_team do after(:create) do |user| create(:team, founder: user) diff --git a/spec/features/servers/server_administration.rb b/spec/features/servers/server_administration.rb new file mode 100644 index 0000000..fa6e125 --- /dev/null +++ b/spec/features/servers/server_administration.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +feature 'Server Administration' do + let!(:admin) { create :user, :admin } + + background do + sign_in_as admin + end + + scenario 'creating a server' do + visit servers_path + expect(page).to have_content('Listing Servers') + click_link 'New server' + test_server_creation_and_editing + visit servers_path + expect(page).to have_content Server.last.name + end + + feature 'Server deletion' do + let!(:server) { create :server } + scenario 'deleting a server' do + visit servers_path + expect(page).to have_content(server.name) + visit server_path(server) + click_link 'Delete Server' + visit servers_path + expect(page).to_not have_content(server.name) + end + end +end diff --git a/spec/features/servers/user_servers.rb b/spec/features/servers/user_servers.rb new file mode 100644 index 0000000..d8b20d1 --- /dev/null +++ b/spec/features/servers/user_servers.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + +feature 'User created servers' do + let!(:user) { create :user } + + background do + sign_in_as user + end + + scenario 'Creating and updating a server' do + visit new_server_path + test_server_creation_and_editing + end +end \ No newline at end of file diff --git a/spec/models/server_spec.rb b/spec/models/server_spec.rb new file mode 100644 index 0000000..19374e8 --- /dev/null +++ b/spec/models/server_spec.rb @@ -0,0 +1,98 @@ +# == Schema Information +# +# Table name: servers +# +# id :integer not null, primary key +# name :string(255) +# description :string(255) +# dns :string(255) +# ip :string(255) +# port :string(255) +# rcon :string(255) +# password :string(255) +# irc :string(255) +# user_id :integer +# official :boolean +# created_at :datetime +# updated_at :datetime +# map :string(255) +# players :integer +# max_players :integer +# ping :string(255) +# version :integer +# domain :integer default(0), not null +# reservation :string(255) +# recording :string(255) +# idle :datetime +# default_id :integer +# active :boolean default(TRUE), not null +# recordable_type :string(255) +# recordable_id :integer +# category_id :integer +# + +require 'spec_helper' + +describe Server do + describe 'create' do + it 'sets category to 45 if domain is NS2' do + server = create :server, domain: Server::DOMAIN_NS2 + expect(server.category_id).to eq(45) + end + it 'sets category to 44 if domain is not NS2' do + server = create :server, domain: Server::DOMAIN_HLDS + expect(server.category_id).to eq(44) + end + end + + describe 'addr' do + it 'returns properly formatted IP and port number' do + ip = '1.1.1.1' + port = '8000' + server = create :server, ip: ip, port: port + expect(server.addr).to eq('1.1.1.1:8000') + end + end + + describe 'to_s' do + it 'returns server name' do + server_name = "Foo" + server = create :server, name: server_name + expect(server.to_s).to eq(server_name) + end + end + + describe 'Permissions' do + let!(:user) { create :user } + let!(:admin) { create :user, :admin } + let!(:server_user) {create :user } + let!(:server) { create :server, user: server_user } + + describe 'can_create?' do + it 'returns true for non-admins' do + expect(server.can_create? user).to be_true + end + end + + describe 'can_destroy?' do + it 'returns true for admin' do + expect(server.can_destroy? admin).to be_true + end + it 'returns false for non-admins' do + expect(server.can_destroy? user).to be_false + end + end + + describe 'can_update?' do + it 'returns true for admin' do + expect(server.can_update? admin).to be_true + end + it 'returns true if server belongs to user' do + expect(server.can_update? server_user).to be_true + end + it 'returns false for non-admins' do + expect(server.can_update? user).to be_false + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f2956e2..8e8b650 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -26,6 +26,7 @@ RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods config.include Controllers::JsonHelpers, type: :controller config.include Features::FormHelpers, type: :feature + config.include Features::ServerHelpers, type: :feature config.include Features::SessionHelpers, type: :feature config.fixture_path = "#{::Rails.root}/spec/fixtures" diff --git a/spec/support/features/server_helpers.rb b/spec/support/features/server_helpers.rb new file mode 100644 index 0000000..b28df90 --- /dev/null +++ b/spec/support/features/server_helpers.rb @@ -0,0 +1,49 @@ +module Features + module ServerHelpers + def test_server_creation_and_editing + dns = 'ServerDns.com' + ip = '192.168.1.1' + port = '8000' + rcon = 'whatsrcon' + password = 'secret' + name = 'MyNsServer' + description = 'My NS Server' + irc = '#some_channel' + + visit new_server_path + fill_in 'Dns', with: dns + fill_in 'server_ip', with: ip + fill_in 'server_port', with: port + fill_in 'Password', with: password + fill_in 'Name', with: name + fill_in 'Description', with: description + fill_in 'Irc', with: irc + check 'Available for officials?' + click_button 'Save' + + expect(page).to have_content(dns) + expect(page).to have_content("#{ip}:#{port}") + expect(page).to have_content(password) + expect(page).to have_content(irc) + expect(page).to have_content(description) + + click_link 'Edit Server' + + fill_in 'Dns', with: "#{dns}2" + fill_in 'server_ip', with: "192.168.1.2" + fill_in 'server_port', with: "8001" + fill_in 'Password', with: "#{password}2" + fill_in 'Name', with: "#{name}2" + fill_in 'Description', with: "#{description}2" + fill_in 'Irc', with: "#{irc}2" + check 'Available for officials?' + click_button 'Save' + + expect(page).to have_content("192.168.1.2:8001") + expect(page).to have_content("#{dns}2") + expect(page).to have_content("#{password}2") + expect(page).to have_content("#{irc}2") + expect(page).to have_content("#{description}2") + end + end +end \ No newline at end of file