Skip to content

Commit

Permalink
User from the XYZ office IPs is taken to XYZ board
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Grippi and Jeff Saracco committed Nov 6, 2012
1 parent 0c1497d commit c44c430
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 21 deletions.
13 changes: 12 additions & 1 deletion app/controllers/standups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class StandupsController < ApplicationController
before_filter :load_standup, except: [:index, :new, :create]
before_filter :load_standup, except: [:index, :new, :create, :route]

def create
@standup = Standup.new(params[:standup])
Expand Down Expand Up @@ -43,6 +43,17 @@ def destroy
redirect_to standups_path
end

def route
ip_key = AuthorizedIps.corresponding_ip_key(request.env['REMOTE_ADDR'])
standup = Standup.find_by_ip_key(ip_key)

if ip_key && standup
redirect_to standup_items_path(standup)
else
redirect_to standups_path
end
end


private

Expand Down
2 changes: 1 addition & 1 deletion app/models/standup.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Standup < ActiveRecord::Base
attr_accessible :title, :to_address, :subject_prefix
attr_accessible :title, :to_address, :subject_prefix, :ip_key

has_many :items, dependent: :destroy
has_many :posts, dependent: :destroy
Expand Down
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
</ul>
<% if session['logged_in'] %>
<ul class="nav pull-right">
<li>
<%= link_to "All Standups", standups_path %>
</li>
<li>
<%= link_to '/logout' do %>
<i class='icon-signout hidden-phone'></i>
Expand Down
7 changes: 7 additions & 0 deletions app/views/standups/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
</div>
</div>

<div class='ip_key'>
<div class='controls'>
<%= form.label :ip_key %>
<%= form.select(:ip_key, AuthorizedIps::AUTHORIZED_IP_ADDRESSES.keys) %>
</div>
</div>

<div class='to_address'>
<div class='controls'>
<%= form.label :to_address %>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
match '/auth/:provider/callback', to: 'sessions#create'
match '/logout', to: 'sessions#destroy'

root :to => 'standups#index'
root :to => 'standups#route'
end
5 changes: 5 additions & 0 deletions db/migrate/20121106195229_add_ip_key_to_standups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddIpKeyToStandups < ActiveRecord::Migration
def change
add_column :standups, :ip_key, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20121105161650) do
ActiveRecord::Schema.define(:version => 20121106195229) do

create_table "items", :force => true do |t|
t.text "title"
Expand Down Expand Up @@ -45,6 +45,7 @@
t.string "to_address"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "ip_key"
end

end
10 changes: 9 additions & 1 deletion lib/authorized_ips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ def self.authorized_ip?(address)
AUTHORIZED_IP_ADDRESSES.each_value.to_a.flatten.any? { |block| block.include?(address) }
end

def self.corresponding_ip_key(address)
matching_standup = nil
AUTHORIZED_IP_ADDRESSES.each do |standup, ip_list|
matching_standup = standup.to_s if ip_list.any? {|ip| ip.include?(address)}
end
matching_standup
end

def development?
@request.env["REMOTE_ADDR"] == "127.0.0.1" && @request.env["HTTP_X_REAL_IP"].to_s == ""
end
end
end
21 changes: 21 additions & 0 deletions spec/controllers/standups_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,25 @@
response.should redirect_to standups_path
end
end

describe "#route" do
let!(:new_york) { create(:standup, ip_key: "nyc") }
let!(:san_fran) { create(:standup, ip_key: "sf") }

it "redirects to the standup that corresponds with the given ip" do
with_authorized_ips({nyc: [IPAddr.new("127.0.0.1/32")], sf: [IPAddr.new("123.4.5.6/32")]}) do
request.env['REMOTE_ADDR'] = '123.4.5.6'
get :route
response.should redirect_to standup_items_path(san_fran)
end
end

it "redirects to the standup index page if no standup with a corresponding ip" do
with_authorized_ips do
request.env['REMOTE_ADDR'] = '111.9.9.9'
get :route
response.should redirect_to standups_path
end
end
end
end
8 changes: 7 additions & 1 deletion spec/lib/authorized_ips_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@
AuthorizedIps.authorized_ip?('127.0.0.1').should == true
end
end
end

it "can tell what standup corresponds to an ip" do
with_authorized_ips({nyc: [IPAddr.new("127.0.0.1/32")]}) do
AuthorizedIps.corresponding_ip_key('127.0.0.1/32').should == "nyc"
end
end
end
27 changes: 15 additions & 12 deletions spec/requests/standups_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
end

it "creates new standups", js: true do
page.should have_content 'Choose a Standup'
click_link('New Standup')
with_authorized_ips({london: [IPAddr.new("127.0.0.1/32")]}) do
page.should have_content 'Choose a Standup'
click_link('New Standup')

fill_in 'standup_title', with: "London"
fill_in 'standup_subject_prefix', with: "[Standup][ENG]"
fill_in 'standup_to_address', with: "all@pivtoallabs.com"
click_button 'Create Standup'
fill_in 'standup_title', with: "London"
fill_in 'standup_subject_prefix', with: "[Standup][ENG]"
select 'london', from: "standup_ip_key"
fill_in 'standup_to_address', with: "all@pivtoallabs.com"
click_button 'Create Standup'

current_page = current_url
current_page.should match(/http:\/\/127\.0\.0\.1:\d*\/standups\/\d*/)
page.should have_content 'London'
page.find('a.posts', text: 'London').click
click_link('Current Post')
current_page.should match(/http:\/\/127\.0\.0\.1:\d*\/standups\/\d*/)
current_page = current_url
current_page.should match(/http:\/\/127\.0\.0\.1:\d*\/standups\/\d*/)
page.should have_content 'London'
page.find('a.posts', text: 'London').click
click_link('Current Post')
current_page.should match(/http:\/\/127\.0\.0\.1:\d*\/standups\/\d*/)
end
end
end
8 changes: 5 additions & 3 deletions spec/support/with_authorized_ips.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module WithAuthorizedIps
def with_authorized_ips(ips)
def with_authorized_ips(ips=nil)
old_value = AuthorizedIps::AUTHORIZED_IP_ADDRESSES
Kernel::silence_warnings { AuthorizedIps.const_set('AUTHORIZED_IP_ADDRESSES', {sf: [IPAddr.new("127.0.0.1/27")]}) }

ips ||= {sf: [IPAddr.new("127.0.0.1/27")]}
Kernel::silence_warnings { AuthorizedIps.const_set('AUTHORIZED_IP_ADDRESSES', ips) }

yield

Kernel::silence_warnings { AuthorizedIps.const_set('AUTHORIZED_IP_ADDRESSES', old_value) }
end
end
end

0 comments on commit c44c430

Please sign in to comment.