beta 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/.gitignore +11 -0
  2. data/.rspec +2 -0
  3. data/.rvmrc +1 -0
  4. data/CHANGELOG.md +11 -0
  5. data/Gemfile +4 -0
  6. data/README.md +40 -0
  7. data/Rakefile +11 -0
  8. data/beta.gemspec +28 -0
  9. data/lib/beta.rb +7 -0
  10. data/lib/beta/access_helpers.rb +37 -0
  11. data/lib/beta/configuration.rb +13 -0
  12. data/lib/beta/version.rb +3 -0
  13. data/spec/access_helpers_spec.rb +58 -0
  14. data/spec/controllers/dummy_controller_spec.rb +33 -0
  15. data/spec/dummy/Rakefile +7 -0
  16. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  17. data/spec/dummy/app/controllers/dummy_controller.rb +16 -0
  18. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  19. data/spec/dummy/app/models/user.rb +2 -0
  20. data/spec/dummy/app/views/dummy/index.html.erb +1 -0
  21. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  22. data/spec/dummy/config.ru +4 -0
  23. data/spec/dummy/config/application.rb +43 -0
  24. data/spec/dummy/config/boot.rb +10 -0
  25. data/spec/dummy/config/database.yml +25 -0
  26. data/spec/dummy/config/environment.rb +5 -0
  27. data/spec/dummy/config/environments/development.rb +26 -0
  28. data/spec/dummy/config/environments/production.rb +49 -0
  29. data/spec/dummy/config/environments/test.rb +35 -0
  30. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  31. data/spec/dummy/config/initializers/inflections.rb +10 -0
  32. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  33. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  34. data/spec/dummy/config/initializers/session_store.rb +8 -0
  35. data/spec/dummy/config/locales/en.yml +5 -0
  36. data/spec/dummy/config/routes.rb +60 -0
  37. data/spec/dummy/public/404.html +26 -0
  38. data/spec/dummy/public/422.html +26 -0
  39. data/spec/dummy/public/500.html +26 -0
  40. data/spec/dummy/public/favicon.ico +0 -0
  41. data/spec/dummy/public/javascripts/application.js +2 -0
  42. data/spec/dummy/public/javascripts/controls.js +965 -0
  43. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  44. data/spec/dummy/public/javascripts/effects.js +1123 -0
  45. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  46. data/spec/dummy/public/javascripts/rails.js +191 -0
  47. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  48. data/spec/dummy/script/rails +6 -0
  49. data/spec/spec_helper.rb +15 -0
  50. metadata +209 -0
@@ -0,0 +1,11 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ coverage
6
+ rdoc
7
+ doc
8
+ .yardoc
9
+ .bundle
10
+ spec/dummy/log/
11
+ spec/dummy/log/*.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format nested
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create use ruby-1.9.3-p0@beta_gem
@@ -0,0 +1,11 @@
1
+ # CHANGELOG
2
+
3
+ ## 1.0.0
4
+
5
+ * Public release
6
+ * Added Beta.environments to control what environments Beta access is controlled for
7
+ * Added specs
8
+
9
+ ## 0.0.2
10
+
11
+ * Internal release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in beta.gemspec
4
+ gemspec
@@ -0,0 +1,40 @@
1
+ # Beta
2
+
3
+ Beta is an access control library that uses a Redis whitelist to control authorization.
4
+
5
+ # Install
6
+
7
+ `gem install beta`
8
+
9
+ or
10
+
11
+ `gem 'beta'`
12
+
13
+ # Usage
14
+
15
+ First we setup our initializer.
16
+
17
+ ``` ruby
18
+ Beta.config do |config|
19
+ redis = $redis
20
+ uid = 'mlg_id'
21
+ namespace = 'awesome-app'
22
+ redirect_url = 'http://majorleaguegaming.com'
23
+ environments = [:production]
24
+ end
25
+ ```
26
+
27
+ Then, in our controller, include our access helpers and add the before filter:
28
+
29
+ ```
30
+ include Beta::AccessHelpers
31
+ before_filter :whitelist
32
+
33
+ ```
34
+
35
+ Requires the existence of a `current_user` method.
36
+
37
+ ## Other helpers
38
+
39
+ * `is_whitelisted?(user)` checks to see if the given user is on the list.
40
+ * `current_user_on_whitelist?` leverages `is_whitelisted?` to tell you if the current user is special. Requires the existence of a `current_user` method.
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ spec.rspec_opts = ['--backtrace']
8
+ # spec.ruby_opts = ['-w']
9
+ end
10
+
11
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "beta/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "beta"
7
+ s.version = Beta::VERSION
8
+ s.authors = ["Andrew Nordman", "Logan Koester", "Matt Wilson", "David Czarnecki"]
9
+ s.email = ["cadwallion@gmail.com", "lkoester@majorleaguegaming.com", "mwilson@majorleaguegaming.com", "dczarnecki@majorleaguegaming.com"]
10
+ s.homepage = "https://github.com/agoragames/beta"
11
+ s.summary = %q{Beta restriction gem}
12
+ s.description = %q{Gem to handle multiple common cases of beta rollout}
13
+
14
+ s.rubyforge_project = "beta"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rake"
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "rspec-rails"
24
+ s.add_development_dependency "rails", "~>3.1.0"
25
+ s.add_development_dependency "fakeredis"
26
+
27
+ s.add_dependency "redis"
28
+ end
@@ -0,0 +1,7 @@
1
+ require "beta/version"
2
+ require "beta/configuration"
3
+ require "beta/access_helpers"
4
+
5
+ module Beta
6
+ extend Configuration
7
+ end
@@ -0,0 +1,37 @@
1
+ module Beta
2
+ # AccessHelpers encapsulates the functionality for whitespacing. This will
3
+ # usually be included in ApplicationController and acccessed as before_filters
4
+ module AccessHelpers
5
+ # Uses redis whitelist and cookies to detect beta access. Redirects on failure.
6
+ #
7
+ # @param [String, nil] Location to redirect to on failure. Defaults to system config of Beta URL
8
+ def whitelist redirection = nil
9
+ return true unless Beta.environments.include?(::Rails.env)
10
+
11
+ if cookies.signed["#{Beta.namespace}-beta"] == "#{Beta.namespace}-beta-#{request.remote_addr}"
12
+ return true
13
+ end
14
+
15
+ return false unless authenticate
16
+
17
+ unless current_user_on_whitelist?
18
+ redirect_to(redirection || Beta.redirect_url)
19
+ return
20
+ end
21
+
22
+ cookies.signed["#{Beta.namespace}-beta"] = "#{Beta.namespace}-beta-#{request.remote_addr}"
23
+ end
24
+
25
+ # Uses redis whitelist to detect if the given user has beta access
26
+ #
27
+ # @param [User] the user to be checked against the list
28
+ def is_whitelisted? user
29
+ Beta.redis.sismember("#{Beta.namespace}:#{::Rails.env}:beta", user.try(Beta.uid))
30
+ end
31
+
32
+ # Uses `is_whitelisted?` with the value of `current_user`
33
+ def current_user_on_whitelist?
34
+ is_whitelisted? current_user
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,13 @@
1
+ module Beta
2
+ module Configuration
3
+ attr_accessor :redis, :namespace, :redirect_url, :uid, :environments
4
+
5
+ def config
6
+ yield self
7
+ end
8
+
9
+ def environments
10
+ @environments ||= ["production"]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Beta
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ class HelperInstance
4
+ include Beta::AccessHelpers
5
+
6
+ def current_user
7
+ User.new
8
+ end
9
+ end
10
+
11
+ class User
12
+ def uid
13
+ 35
14
+ end
15
+ end
16
+
17
+ describe Beta::AccessHelpers do
18
+ before(:each) do
19
+ @redis = Redis.new
20
+ @redis.flushdb
21
+
22
+ Beta.config do |config|
23
+ config.redis = @redis
24
+ config.namespace = 'gem'
25
+ config.redirect_url = 'http://www.site.com/not-authorized'
26
+ config.uid = 'uid'
27
+ end
28
+ end
29
+
30
+ describe "#is_whitelisted?" do
31
+ it 'should return true if a user is whitelisted' do
32
+ user = User.new
33
+ helper = HelperInstance.new
34
+ @redis.sadd("#{Beta.namespace}:#{::Rails.env}:beta", user.uid)
35
+ helper.is_whitelisted?(user).should be(true)
36
+ end
37
+
38
+ it 'should return false if a user is not whitelisted' do
39
+ user = User.new
40
+ helper = HelperInstance.new
41
+ helper.is_whitelisted?(user).should be(false)
42
+ end
43
+ end
44
+
45
+ describe "current_user_on_whitelist?" do
46
+ it 'should return true if a user is on whitelist' do
47
+ helper = HelperInstance.new
48
+ @redis.sadd("#{Beta.namespace}:#{::Rails.env}:beta", 35)
49
+ helper.current_user_on_whitelist?.should be(true)
50
+ end
51
+
52
+ it 'should return false if a user is not the whitelist' do
53
+ user = User.new
54
+ helper = HelperInstance.new
55
+ helper.current_user_on_whitelist?.should be(false)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe DummyController do
4
+ before(:each) do
5
+ @redis = Redis.new
6
+ @redis.flushdb
7
+
8
+ Beta.config do |config|
9
+ config.redis = @redis
10
+ config.namespace = 'gem'
11
+ config.redirect_url = 'http://www.site.com/not-authorized'
12
+ config.uid = 'uid'
13
+ config.environments = ["test"]
14
+ end
15
+ end
16
+
17
+ it 'should redirect if user is not whitelisted' do
18
+ get 'index'
19
+
20
+ response.should be_redirect
21
+ response.should redirect_to('http://www.site.com/not-authorized')
22
+ response.cookies.should be_empty
23
+ end
24
+
25
+ it 'should not redirect if user is whitelisted' do
26
+ Beta.redis.sadd("#{Beta.namespace}:#{::Rails.env}:beta", 35)
27
+ get 'index'
28
+
29
+ response.should be_ok
30
+ response.cookies.should_not be_empty
31
+ response.cookies["#{Beta.namespace}-beta"].should_not be_nil
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,16 @@
1
+ class DummyController < ApplicationController
2
+ include Beta::AccessHelpers
3
+ before_filter :whitelist
4
+
5
+ def index
6
+ @user = params[:user] || "David"
7
+ end
8
+
9
+ def current_user
10
+ User.new
11
+ end
12
+
13
+ def authenticate
14
+ true
15
+ end
16
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class User
2
+ end
@@ -0,0 +1 @@
1
+ Dummy#index
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,43 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "active_model/railtie"
4
+ require "action_controller/railtie"
5
+ require "action_view/railtie"
6
+ require "action_mailer/railtie"
7
+
8
+ Bundler.require
9
+
10
+ module Dummy
11
+ class Application < Rails::Application
12
+ # Settings in config/environments/* take precedence over those specified here.
13
+ # Application configuration should go into files in config/initializers
14
+ # -- all .rb files in that directory are automatically loaded.
15
+
16
+ # Custom directories with classes and modules you want to be autoloadable.
17
+ # config.autoload_paths += %W(#{config.root}/extras)
18
+
19
+ # Only load the plugins named here, in the order given (default is alphabetical).
20
+ # :all can be used as a placeholder for all plugins not explicitly named.
21
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
22
+
23
+ # Activate observers that should always be running.
24
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
25
+
26
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
27
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
28
+ # config.time_zone = 'Central Time (US & Canada)'
29
+
30
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
31
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
32
+ # config.i18n.default_locale = :de
33
+
34
+ # JavaScript files you want as :defaults (application.js is always included).
35
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
36
+
37
+ # Configure the default encoding used in templates for Ruby 1.9.
38
+ config.encoding = "utf-8"
39
+
40
+ # Configure sensitive parameters which will be filtered from the log file.
41
+ config.filter_parameters += [:password]
42
+ end
43
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,25 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ development:
4
+ adapter: sqlite3
5
+ database: db/development.sqlite3
6
+ pool: 5
7
+ timeout: 5000
8
+
9
+ # Warning: The database defined as "test" will be erased and
10
+ # re-generated from your development database when you run "rake".
11
+ # Do not set this db to the same as development or production.
12
+ test: &test
13
+ adapter: sqlite3
14
+ database: db/test.sqlite3
15
+ pool: 5
16
+ timeout: 5000
17
+
18
+ production:
19
+ adapter: sqlite3
20
+ database: db/production.sqlite3
21
+ pool: 5
22
+ timeout: 5000
23
+
24
+ cucumber:
25
+ <<: *test
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,26 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the webserver when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_view.debug_rjs = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Don't care if the mailer can't send
18
+ config.action_mailer.raise_delivery_errors = false
19
+
20
+ # Print deprecation notices to the Rails logger
21
+ config.active_support.deprecation = :log
22
+
23
+ # Only use best-standards-support built into browsers
24
+ config.action_dispatch.best_standards_support = :builtin
25
+ end
26
+