From 8cc5ba134f6c5a1a06d91407aa78b962545c54ac Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 17 Apr 2014 11:42:13 +0200 Subject: initial commit for the service level api :api/service will return a hash of the current users service level This is failiing if the user is not logged in. Instead it should return the service description for an anonymous user. --- app/controllers/v1/services_controller.rb | 8 ++++++++ app/models/service_level.rb | 1 + config/routes.rb | 1 + test/functional/v1/services_controller_test.rb | 23 +++++++++++++++++++++++ 4 files changed, 33 insertions(+) create mode 100644 app/controllers/v1/services_controller.rb create mode 100644 test/functional/v1/services_controller_test.rb diff --git a/app/controllers/v1/services_controller.rb b/app/controllers/v1/services_controller.rb new file mode 100644 index 0000000..594940e --- /dev/null +++ b/app/controllers/v1/services_controller.rb @@ -0,0 +1,8 @@ +class V1::ServicesController < ApplicationController + + respond_to :json + + def show + respond_with current_user.effective_service_level + end +end diff --git a/app/models/service_level.rb b/app/models/service_level.rb index 299aaf1..31a713b 100644 --- a/app/models/service_level.rb +++ b/app/models/service_level.rb @@ -16,4 +16,5 @@ class ServiceLevel APP_CONFIG[:service_levels][@id] end + delegate :to_json, to: :config_hash end diff --git a/config/routes.rb b/config/routes.rb index b930bd1..f612b47 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -25,6 +25,7 @@ LeapWeb::Application.routes.draw do resources :users, :only => [:create, :update, :destroy, :index] resources :messages, :only => [:index, :update] resource :cert, :only => [:show] + resource :service, :only => [:show] end scope "(:locale)", :locale => MATCH_LOCALE do diff --git a/test/functional/v1/services_controller_test.rb b/test/functional/v1/services_controller_test.rb new file mode 100644 index 0000000..35a9de6 --- /dev/null +++ b/test/functional/v1/services_controller_test.rb @@ -0,0 +1,23 @@ +require 'test_helper' + +class V1::ServicesControllerTest < ActionController::TestCase + + test "anonymous user can request service info" do + get :show, format: :json + assert_json_response name: 'anonymous', + cert_prefix: 'LIMITED', + description: 'anonymous account, with rate limited VPN' + end + + test "user can see their service info" do + login + get :show, format: :json + assert_json_response name: 'free', + cert_prefix: 'LIMITED', + description: 'free account, with rate limited VPN', + cost: 0, + quota: 100 + end + +end + -- cgit v1.2.3 From 614745c84cab37dd03f2bd8f06160fd01c7fabdb Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 17 Apr 2014 12:06:38 +0200 Subject: UnauthenticatedUser as current_user this still allows us to do current_user.service_level. Have not gone through the rest of the code yet. Only made sure logged_in? now tests for is_a? User instead of !!current_user --- app/controllers/controller_extension/authentication.rb | 12 +++++++++--- app/models/unauthenticated_user.rb | 7 +++++++ config/defaults.yml | 1 + 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/controllers/controller_extension/authentication.rb b/app/controllers/controller_extension/authentication.rb index 03d3989..2bc0aee 100644 --- a/app/controllers/controller_extension/authentication.rb +++ b/app/controllers/controller_extension/authentication.rb @@ -8,11 +8,11 @@ module ControllerExtension::Authentication end def current_user - @current_user ||= token_authenticate || warden.user + @current_user ||= token_authenticate || warden.user || unauthenticated end def logged_in? - !!current_user + current_user.is_a? User end def require_login @@ -42,7 +42,7 @@ module ControllerExtension::Authentication end def admin? - current_user && current_user.is_admin? + current_user.is_admin? end def require_admin @@ -72,4 +72,10 @@ module ControllerExtension::Authentication request.env['warden.options'] && request.env['warden.options'][:attempted_path] end + + protected + + def unauthenticated + UnauthenticatedUser.new + end end diff --git a/app/models/unauthenticated_user.rb b/app/models/unauthenticated_user.rb index 0fc17d2..ba6470a 100644 --- a/app/models/unauthenticated_user.rb +++ b/app/models/unauthenticated_user.rb @@ -3,4 +3,11 @@ class UnauthenticatedUser < Object # will probably want something here to return service level as APP_CONFIG[:service_levels][0] but not sure how will be accessing. + def is_admin? + false + end + + def effective_service_level + ServiceLevel.new id: APP_CONFIG[:unauthenticated_service_level] + end end diff --git a/config/defaults.yml b/config/defaults.yml index e7d0f5e..47c3ad7 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -67,6 +67,7 @@ service_levels: &service_levels USD: 10 EUR: 10 default_service_level: 1 + unauthenticated_service_level: 0 development: <<: *downloads -- cgit v1.2.3 From 7a9ece43bd61246b450471ed6bb1089570321e38 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 17 Apr 2014 19:27:47 +0200 Subject: make use of the UnauthorizedUser Null Pattern for current_user - use it to get rid of some conditionals --- app/controllers/v1/certs_controller.rb | 44 +++--------------- app/controllers/v1/messages_controller.rb | 5 +- app/models/service_level.rb | 14 +++++- app/models/unauthenticated_user.rb | 20 ++++++-- config/defaults.yml | 13 ++++-- .../support/app/controllers/tickets_controller.rb | 36 +++++++++------ engines/support/app/views/tickets/new.html.haml | 18 ++------ engines/support/app/views/tickets/show.html.haml | 4 +- test/functional/v1/certs_controller_test.rb | 54 +++++++++++----------- 9 files changed, 104 insertions(+), 104 deletions(-) diff --git a/app/controllers/v1/certs_controller.rb b/app/controllers/v1/certs_controller.rb index 64cfa7f..580c90c 100644 --- a/app/controllers/v1/certs_controller.rb +++ b/app/controllers/v1/certs_controller.rb @@ -1,50 +1,20 @@ class V1::CertsController < ApplicationController - before_filter :require_login, :unless => :anonymous_certs_allowed? + before_filter :require_eip_access # GET /cert def show - @cert = ClientCertificate.new(:prefix => certificate_prefix) + @cert = ClientCertificate.new(:prefix => service_level.cert_prefix) render text: @cert.to_s, content_type: 'text/plain' end protected - def anonymous_certs_allowed? - APP_CONFIG[:allow_anonymous_certs] + def require_eip_access + access_denied unless service_level.provides?(:eip) end - # - # this is some temporary logic until we store the service level in the user db. - # - # better logic might look like this: - # - # if logged_in? - # service_level = user.service_level - # elsif allow_anonymous? - # service_level = service_levels[:anonymous] - # else - # service_level = nil - # end - # - # if service_level.bandwidth == 'limited' && allow_limited? - # prefix = limited - # elsif allow_unlimited? - # prefix = unlimited - # else - # prefix = nil - # end - # - def certificate_prefix - if logged_in? - if APP_CONFIG[:allow_unlimited_certs] - APP_CONFIG[:unlimited_cert_prefix] - elsif APP_CONFIG[:allow_limited_certs] - APP_CONFIG[:limited_cert_prefix] - end - elsif !APP_CONFIG[:allow_limited_certs] - APP_CONFIG[:unlimited_cert_prefix] - else - APP_CONFIG[:limited_cert_prefix] - end + + def service_level + current_user.effective_service_level end end diff --git a/app/controllers/v1/messages_controller.rb b/app/controllers/v1/messages_controller.rb index f71d0f1..85156b7 100644 --- a/app/controllers/v1/messages_controller.rb +++ b/app/controllers/v1/messages_controller.rb @@ -7,12 +7,11 @@ module V1 respond_to :json def index - render json: (current_user ? current_user.messages : [] ) + render json: current_user.messages end def update - message = Message.find(params[:id]) - if (message and current_user) + if message = Message.find(params[:id]) message.mark_as_read_by(current_user) message.save render json: true diff --git a/app/models/service_level.rb b/app/models/service_level.rb index 31a713b..d0bd9b3 100644 --- a/app/models/service_level.rb +++ b/app/models/service_level.rb @@ -13,8 +13,20 @@ class ServiceLevel end def config_hash - APP_CONFIG[:service_levels][@id] + @config_hash || APP_CONFIG[:service_levels][@id].with_indifferent_access end delegate :to_json, to: :config_hash + + def provides?(service) + services.include? service.to_s + end + + def services + config_hash[:services] || [] + end + + def cert_prefix + config_hash[:cert_prefix] + end end diff --git a/app/models/unauthenticated_user.rb b/app/models/unauthenticated_user.rb index ba6470a..7845a6f 100644 --- a/app/models/unauthenticated_user.rb +++ b/app/models/unauthenticated_user.rb @@ -1,13 +1,27 @@ # The nil object for the user class class UnauthenticatedUser < Object - # will probably want something here to return service level as APP_CONFIG[:service_levels][0] but not sure how will be accessing. + def effective_service_level + ServiceLevel.new id: APP_CONFIG[:unauthenticated_service_level] + end def is_admin? false end - def effective_service_level - ServiceLevel.new id: APP_CONFIG[:unauthenticated_service_level] + def id + nil + end + + def email_address + nil + end + + def login + nil + end + + def messages + [] end end diff --git a/config/defaults.yml b/config/defaults.yml index 47c3ad7..383aa1c 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -7,11 +7,6 @@ cert_options: &cert_options client_cert_lifespan: 2 client_cert_bit_size: 2024 client_cert_hash: "SHA256" - allow_limited_certs: false - allow_unlimited_certs: true - allow_anonymous_certs: false - limited_cert_prefix: "LIMITED" - unlimited_cert_prefix: "UNLIMITED" downloads: &downloads client_download_domain: https://dl.bitmask.net @@ -53,12 +48,17 @@ service_levels: &service_levels name: anonymous cert_prefix: "LIMITED" description: "anonymous account, with rate limited VPN" + services: + - eip 1: name: free cert_prefix: "LIMITED" description: "free account, with rate limited VPN" cost: 0 quota: 100 + services: + - eip + - email 2: name: premium cert_prefix: "UNLIMITED" @@ -66,6 +66,9 @@ service_levels: &service_levels cost: USD: 10 EUR: 10 + services: + - eip + - email default_service_level: 1 unauthenticated_service_level: 0 diff --git a/engines/support/app/controllers/tickets_controller.rb b/engines/support/app/controllers/tickets_controller.rb index d65ee43..cf8743a 100644 --- a/engines/support/app/controllers/tickets_controller.rb +++ b/engines/support/app/controllers/tickets_controller.rb @@ -5,7 +5,8 @@ class TicketsController < ApplicationController #has_scope :open, :type => boolean before_filter :require_login, :only => [:index] - before_filter :fetch_ticket, :only => [:show, :update, :destroy] # don't now have an edit method + before_filter :fetch_ticket, :only => [:show, :update, :destroy] + before_filter :require_ticket_access, :only => [:show, :update, :destroy] before_filter :fetch_user before_filter :set_title @@ -17,11 +18,11 @@ class TicketsController < ApplicationController def create @ticket = Ticket.new(params[:ticket]) - @ticket.comments.last.posted_by = (logged_in? ? current_user.id : nil) #protecting posted_by isn't working, so this should protect it. + #protecting posted_by isn't working, so this should protect it: + @ticket.comments.last.posted_by = current_user.id @ticket.comments.last.private = false unless admin? - @ticket.created_by = current_user.id if logged_in? - @ticket.email = current_user.email_address if logged_in? and current_user.email_address - + @ticket.created_by = current_user.id + @ticket.email = current_user.email_address if current_user.email_address if @ticket.save flash[:notice] = t(:thing_was_successfully_created, :thing => t(:ticket)) end @@ -58,7 +59,7 @@ class TicketsController < ApplicationController end if @ticket.comments_changed? - @ticket.comments.last.posted_by = (current_user ? current_user.id : nil) + @ticket.comments.last.posted_by = current_user.id @ticket.comments.last.private = false unless admin? end @@ -120,19 +121,28 @@ class TicketsController < ApplicationController return ticket end - def ticket_access? - @ticket and (admin? or !@ticket.created_by or (current_user and current_user.id == @ticket.created_by)) - end - def fetch_ticket @ticket = Ticket.find(params[:id]) - if !@ticket and admin? - redirect_to auto_tickets_path, :alert => t(:no_such_thing, :thing => 'ticket') - return + if !@ticket + if admin? + redirect_to auto_tickets_path, + alert: t(:no_such_thing, thing: 'ticket') + else + access_denied + end end + end + + def require_ticket_access access_denied unless ticket_access? end + def ticket_access? + admin? or + @ticket.created_by.blank? or + current_user.id == @ticket.created_by + end + def fetch_user if params[:user_id] @user = User.find(params[:user_id]) diff --git a/engines/support/app/views/tickets/new.html.haml b/engines/support/app/views/tickets/new.html.haml index 8f217a5..e391499 100644 --- a/engines/support/app/views/tickets/new.html.haml +++ b/engines/support/app/views/tickets/new.html.haml @@ -2,22 +2,14 @@ = render 'tickets/tabs' -- if admin? && @user - - email = @user.email_address - - regarding = @user.login -- elsif logged_in? - - email = current_user.email_address - - regarding = current_user.login +- user = @user if admin? +- user ||= current_user = simple_form_for @ticket, :validate => true, :html => {:class => 'form-horizontal'} do |f| = hidden_ticket_fields = f.input :subject - - if logged_in? - = f.input :email, input_html: {value: email} - = f.input :regarding_user, input_html: {value: regarding} - - else - = f.input :email - = f.input :regarding_user + = f.input :email, input_html: {value: user.email} + = f.input :regarding_user, input_html: {value: user.login} = f.simple_fields_for :comments, @comment do |c| = c.input :body, :label => t(:description), :as => :text, :input_html => {:class => "full-width", :rows=> 5} - if admin? @@ -27,4 +19,4 @@ - if logged_in? = link_to t(:cancel), auto_tickets_path, :class => :btn - else - = link_to t(:cancel), home_path, :class => 'btn' \ No newline at end of file + = link_to t(:cancel), home_path, :class => 'btn' diff --git a/engines/support/app/views/tickets/show.html.haml b/engines/support/app/views/tickets/show.html.haml index bfdb773..edb6e6f 100644 --- a/engines/support/app/views/tickets/show.html.haml +++ b/engines/support/app/views/tickets/show.html.haml @@ -7,6 +7,6 @@ = render :partial => 'tickets/comment', :collection => @ticket.comments %tr %td.user - = logged_in? ? current_user.login : t(:anonymous) + = current_user.login || t(:anonymous) %td.comment - = render 'tickets/new_comment_form' \ No newline at end of file + = render 'tickets/new_comment_form' diff --git a/test/functional/v1/certs_controller_test.rb b/test/functional/v1/certs_controller_test.rb index 2c70e52..3631947 100644 --- a/test/functional/v1/certs_controller_test.rb +++ b/test/functional/v1/certs_controller_test.rb @@ -3,42 +3,42 @@ require 'test_helper' class V1::CertsControllerTest < ActionController::TestCase test "send limited cert without login" do - with_config allow_limited_certs: true, allow_anonymous_certs: true do - cert = stub :to_s => "limited cert" - ClientCertificate.expects(:new).with(:prefix => APP_CONFIG[:limited_cert_prefix]).returns(cert) - get :show - assert_response :success - assert_equal cert.to_s, @response.body - end + cert = expect_cert('LIMITED') + get :show + assert_response :success + assert_equal cert.to_s, @response.body + end + + test "send limited cert" do + login + cert = expect_cert('LIMITED') + get :show + assert_response :success + assert_equal cert.to_s, @response.body end test "send unlimited cert" do - with_config allow_unlimited_certs: true do - login - cert = stub :to_s => "unlimited cert" - ClientCertificate.expects(:new).with(:prefix => APP_CONFIG[:unlimited_cert_prefix]).returns(cert) - get :show - assert_response :success - assert_equal cert.to_s, @response.body - end + login effective_service_level: ServiceLevel.new(id: 2) + cert = expect_cert('UNLIMITED') + get :show + assert_response :success + assert_equal cert.to_s, @response.body end - test "login required if anonymous certs disabled" do - with_config allow_anonymous_certs: false do + test "redirect if no eip service offered" do + with_config({service_levels: {0 => {services: []}}}) do get :show assert_response :redirect end end - test "send limited cert" do - with_config allow_limited_certs: true, allow_unlimited_certs: false do - login - cert = stub :to_s => "real cert" - ClientCertificate.expects(:new).with(:prefix => APP_CONFIG[:limited_cert_prefix]).returns(cert) - get :show - assert_response :success - assert_equal cert.to_s, @response.body - end - end + protected + def expect_cert(prefix) + cert = stub :to_s => "#{prefix.downcase} cert" + ClientCertificate.expects(:new). + with(:prefix => prefix). + returns(cert) + return cert + end end -- cgit v1.2.3 From fca9752315a0b46b52facf0e54c35214198fe8ae Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 17 Apr 2014 20:03:47 +0200 Subject: adjust test to service list in config --- test/functional/v1/services_controller_test.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/functional/v1/services_controller_test.rb b/test/functional/v1/services_controller_test.rb index 35a9de6..bcb7abc 100644 --- a/test/functional/v1/services_controller_test.rb +++ b/test/functional/v1/services_controller_test.rb @@ -6,7 +6,8 @@ class V1::ServicesControllerTest < ActionController::TestCase get :show, format: :json assert_json_response name: 'anonymous', cert_prefix: 'LIMITED', - description: 'anonymous account, with rate limited VPN' + description: 'anonymous account, with rate limited VPN', + services: ["eip"] end test "user can see their service info" do @@ -16,7 +17,8 @@ class V1::ServicesControllerTest < ActionController::TestCase cert_prefix: 'LIMITED', description: 'free account, with rate limited VPN', cost: 0, - quota: 100 + quota: 100, + services: ["eip", "email"] end end -- cgit v1.2.3 From 40dfa63aa6fc7aa3614f2a7952d088d8ff067f70 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 18 Apr 2014 10:29:07 +0200 Subject: minor fix: User#email_address not User#email --- engines/support/app/views/tickets/new.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engines/support/app/views/tickets/new.html.haml b/engines/support/app/views/tickets/new.html.haml index e391499..8a89703 100644 --- a/engines/support/app/views/tickets/new.html.haml +++ b/engines/support/app/views/tickets/new.html.haml @@ -8,7 +8,7 @@ = simple_form_for @ticket, :validate => true, :html => {:class => 'form-horizontal'} do |f| = hidden_ticket_fields = f.input :subject - = f.input :email, input_html: {value: user.email} + = f.input :email, input_html: {value: user.email_address} = f.input :regarding_user, input_html: {value: user.login} = f.simple_fields_for :comments, @comment do |c| = c.input :body, :label => t(:description), :as => :text, :input_html => {:class => "full-width", :rows=> 5} -- cgit v1.2.3 From 9216ab8252246a263c5d17f6755a7d3887145f94 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 18 Apr 2014 11:55:40 +0200 Subject: change service level configuration strategy The changes to the configuration required some non minor changes to the platform and also added some flexibility we don't require yet - and thus some new possibilities for errors. So instead we still use the allow_..._certs and ..._cert_prefix options. They basically provide the framework in which service levels can operate. The service level configuration will not include the cert prefix anymore. It only states if the service level is rate limited or not. This avoids conflicts between the two configuration options. I also removed the anonymous service level entirely. It was also turning a boolean decision (do we provide anonymous eip or not) into something way more complex. Instead I added the AnonymousServiceLevel class to handle the corner cases for people who are not logged in. Furthermore i renamed the UnauthenticatedUser to AnonymousUser so it matches the Anonymous Service Level nicely. It's also shorter and more intuitive. --- .../controller_extension/authentication.rb | 6 ++--- app/controllers/v1/certs_controller.rb | 6 ++--- app/models/anonymous_service_level.rb | 31 ++++++++++++++++++++++ app/models/anonymous_user.rb | 27 +++++++++++++++++++ app/models/service_level.rb | 30 ++++++++++++--------- app/models/unauthenticated_user.rb | 27 ------------------- app/views/users/_change_service_level.html.haml | 4 +-- config/defaults.yml | 21 +++++---------- test/functional/v1/certs_controller_test.rb | 30 +++++++++++---------- test/functional/v1/services_controller_test.rb | 23 +++++++++++----- test/unit/anonymous_user_test.rb | 23 ++++++++++++++++ test/unit/unauthenticated_user_test.rb | 7 ----- 12 files changed, 145 insertions(+), 90 deletions(-) create mode 100644 app/models/anonymous_service_level.rb create mode 100644 app/models/anonymous_user.rb delete mode 100644 app/models/unauthenticated_user.rb create mode 100644 test/unit/anonymous_user_test.rb delete mode 100644 test/unit/unauthenticated_user_test.rb diff --git a/app/controllers/controller_extension/authentication.rb b/app/controllers/controller_extension/authentication.rb index 2bc0aee..1f73f38 100644 --- a/app/controllers/controller_extension/authentication.rb +++ b/app/controllers/controller_extension/authentication.rb @@ -8,7 +8,7 @@ module ControllerExtension::Authentication end def current_user - @current_user ||= token_authenticate || warden.user || unauthenticated + @current_user ||= token_authenticate || warden.user || anonymous end def logged_in? @@ -75,7 +75,7 @@ module ControllerExtension::Authentication protected - def unauthenticated - UnauthenticatedUser.new + def anonymous + AnonymousUser.new end end diff --git a/app/controllers/v1/certs_controller.rb b/app/controllers/v1/certs_controller.rb index 580c90c..73409ef 100644 --- a/app/controllers/v1/certs_controller.rb +++ b/app/controllers/v1/certs_controller.rb @@ -1,6 +1,6 @@ class V1::CertsController < ApplicationController - before_filter :require_eip_access + before_filter :require_login, :unless => :anonymous_certs_allowed? # GET /cert def show @@ -10,8 +10,8 @@ class V1::CertsController < ApplicationController protected - def require_eip_access - access_denied unless service_level.provides?(:eip) + def anonymous_certs_allowed? + APP_CONFIG[:allow_anonymous_certs] end def service_level diff --git a/app/models/anonymous_service_level.rb b/app/models/anonymous_service_level.rb new file mode 100644 index 0000000..c51ce9e --- /dev/null +++ b/app/models/anonymous_service_level.rb @@ -0,0 +1,31 @@ +class AnonymousServiceLevel + + delegate :to_json, to: :config_hash + + def cert_prefix + if APP_CONFIG[:allow_limited_certs] + APP_CONFIG[:limited_cert_prefix] + else + APP_CONFIG[:unlimited_cert_prefix] + end + end + + def description + if APP_CONFIG[:allow_anonymous_certs] + "anonymous access to the VPN" + else + "please login to access our services" + end + end + + protected + + def config_hash + { name: "anonymous", + description: description, + cost: 0, + eip_rate_limit: APP_CONFIG[:allow_limited_certs] + } + end + +end diff --git a/app/models/anonymous_user.rb b/app/models/anonymous_user.rb new file mode 100644 index 0000000..360a577 --- /dev/null +++ b/app/models/anonymous_user.rb @@ -0,0 +1,27 @@ +# The nil object for the user class +class AnonymousUser < Object + + def effective_service_level + AnonymousServiceLevel.new + end + + def is_admin? + false + end + + def id + nil + end + + def email_address + nil + end + + def login + nil + end + + def messages + [] + end +end diff --git a/app/models/service_level.rb b/app/models/service_level.rb index d0bd9b3..06ad202 100644 --- a/app/models/service_level.rb +++ b/app/models/service_level.rb @@ -4,29 +4,35 @@ class ServiceLevel @id = attributes[:id] || APP_CONFIG[:default_service_level] end - def self.authenticated_select_options - APP_CONFIG[:service_levels].map { |id,config_hash| [config_hash[:description], id] if config_hash[:name] != 'anonymous'}.compact + def self.select_options + APP_CONFIG[:service_levels].map do |id,config_hash| + [config_hash[:description], id] + end end def id @id end - def config_hash - @config_hash || APP_CONFIG[:service_levels][@id].with_indifferent_access - end - delegate :to_json, to: :config_hash - def provides?(service) - services.include? service.to_s + def cert_prefix + if limited_cert? + APP_CONFIG[:limited_cert_prefix] + else + APP_CONFIG[:unlimited_cert_prefix] + end end - def services - config_hash[:services] || [] + protected + + def limited_cert? + APP_CONFIG[:allow_limited_certs] && + (!APP_CONFIG[:allow_unlimited_certs] || config_hash[:eip_rate_limit]) end - def cert_prefix - config_hash[:cert_prefix] + def config_hash + @config_hash || APP_CONFIG[:service_levels][@id].with_indifferent_access end + end diff --git a/app/models/unauthenticated_user.rb b/app/models/unauthenticated_user.rb deleted file mode 100644 index 7845a6f..0000000 --- a/app/models/unauthenticated_user.rb +++ /dev/null @@ -1,27 +0,0 @@ -# The nil object for the user class -class UnauthenticatedUser < Object - - def effective_service_level - ServiceLevel.new id: APP_CONFIG[:unauthenticated_service_level] - end - - def is_admin? - false - end - - def id - nil - end - - def email_address - nil - end - - def login - nil - end - - def messages - [] - end -end diff --git a/app/views/users/_change_service_level.html.haml b/app/views/users/_change_service_level.html.haml index 61e67d9..42315a2 100644 --- a/app/views/users/_change_service_level.html.haml +++ b/app/views/users/_change_service_level.html.haml @@ -8,11 +8,11 @@ %legend= t(:service_level) - if @user != current_user = t(:desired_service_level) - = f.select :desired_service_level_code, ServiceLevel.authenticated_select_options, :selected => @user.desired_service_level.id + = f.select :desired_service_level_code, ServiceLevel.select_options, :selected => @user.desired_service_level.id - if @user != current_user %p = t(:effective_service_level) - = f.select :effective_service_level_code, ServiceLevel.authenticated_select_options, :selected => @user.effective_service_level.id + = f.select :effective_service_level_code, ServiceLevel.select_options, :selected => @user.effective_service_level.id .control-group .controls = f.submit t(:save), :class => 'btn', :data => {"loading-text" => "Saving..."} diff --git a/config/defaults.yml b/config/defaults.yml index 383aa1c..a7b70a3 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -7,6 +7,11 @@ cert_options: &cert_options client_cert_lifespan: 2 client_cert_bit_size: 2024 client_cert_hash: "SHA256" + allow_limited_certs: false + allow_unlimited_certs: true + allow_anonymous_certs: false + limited_cert_prefix: "LIMITED" + unlimited_cert_prefix: "UNLIMITED" downloads: &downloads client_download_domain: https://dl.bitmask.net @@ -44,33 +49,19 @@ common: &common service_levels: &service_levels service_levels: - 0: - name: anonymous - cert_prefix: "LIMITED" - description: "anonymous account, with rate limited VPN" - services: - - eip 1: name: free - cert_prefix: "LIMITED" description: "free account, with rate limited VPN" cost: 0 + eip_rate_limit: true quota: 100 - services: - - eip - - email 2: name: premium - cert_prefix: "UNLIMITED" description: "premium account, with unlimited vpn" cost: USD: 10 EUR: 10 - services: - - eip - - email default_service_level: 1 - unauthenticated_service_level: 0 development: <<: *downloads diff --git a/test/functional/v1/certs_controller_test.rb b/test/functional/v1/certs_controller_test.rb index 3631947..fb8e9c4 100644 --- a/test/functional/v1/certs_controller_test.rb +++ b/test/functional/v1/certs_controller_test.rb @@ -2,19 +2,23 @@ require 'test_helper' class V1::CertsControllerTest < ActionController::TestCase - test "send limited cert without login" do - cert = expect_cert('LIMITED') - get :show - assert_response :success - assert_equal cert.to_s, @response.body + test "send unlimited cert without login" do + with_config allow_anonymous_certs: true do + cert = expect_cert('UNLIMITED') + get :show + assert_response :success + assert_equal cert.to_s, @response.body + end end test "send limited cert" do - login - cert = expect_cert('LIMITED') - get :show - assert_response :success - assert_equal cert.to_s, @response.body + with_config allow_limited_certs: true do + login + cert = expect_cert('LIMITED') + get :show + assert_response :success + assert_equal cert.to_s, @response.body + end end test "send unlimited cert" do @@ -26,10 +30,8 @@ class V1::CertsControllerTest < ActionController::TestCase end test "redirect if no eip service offered" do - with_config({service_levels: {0 => {services: []}}}) do - get :show - assert_response :redirect - end + get :show + assert_response :redirect end protected diff --git a/test/functional/v1/services_controller_test.rb b/test/functional/v1/services_controller_test.rb index bcb7abc..b81103f 100644 --- a/test/functional/v1/services_controller_test.rb +++ b/test/functional/v1/services_controller_test.rb @@ -2,23 +2,32 @@ require 'test_helper' class V1::ServicesControllerTest < ActionController::TestCase - test "anonymous user can request service info" do + test "anonymous user gets login required service info" do get :show, format: :json assert_json_response name: 'anonymous', - cert_prefix: 'LIMITED', - description: 'anonymous account, with rate limited VPN', - services: ["eip"] + eip_rate_limit: false, + description: 'please login to access our services', + cost: 0 + end + + test "anonymous user gets vpn service info" do + with_config allow_anonymous_certs: true do + get :show, format: :json + assert_json_response name: 'anonymous', + eip_rate_limit: false, + description: 'anonymous access to the VPN', + cost: 0 + end end test "user can see their service info" do login get :show, format: :json assert_json_response name: 'free', - cert_prefix: 'LIMITED', + eip_rate_limit: true, description: 'free account, with rate limited VPN', cost: 0, - quota: 100, - services: ["eip", "email"] + quota: 100 end end diff --git a/test/unit/anonymous_user_test.rb b/test/unit/anonymous_user_test.rb new file mode 100644 index 0000000..6e94d39 --- /dev/null +++ b/test/unit/anonymous_user_test.rb @@ -0,0 +1,23 @@ +require 'test_helper' + +class AnonymousUserTest < ActiveSupport::TestCase + + setup do + @anonymous = AnonymousUser.new + end + + test "has nil values" do + assert_nil @anonymous.id + assert_nil @anonymous.email_address + assert_nil @anonymous.login + end + + test "has no messages" do + assert_equal [], @anonymous.messages + end + + test "has anonymous service level" do + assert @anonymous.effective_service_level.is_a? AnonymousServiceLevel + end + +end diff --git a/test/unit/unauthenticated_user_test.rb b/test/unit/unauthenticated_user_test.rb deleted file mode 100644 index e5fafb8..0000000 --- a/test/unit/unauthenticated_user_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class UnauthenticatedUserTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end -end -- cgit v1.2.3 From 966e390d401b84dad98127e647d2ec634f1cbc15 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 18 Apr 2014 12:39:27 +0200 Subject: bringing back empty cert prefixes if neither limited nor unlimited certs are allowed there will be no prefix. Not sure if this is desired - but it's the way things used to be before the refactoring --- app/models/anonymous_service_level.rb | 2 +- app/models/service_level.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/anonymous_service_level.rb b/app/models/anonymous_service_level.rb index c51ce9e..47b7cfb 100644 --- a/app/models/anonymous_service_level.rb +++ b/app/models/anonymous_service_level.rb @@ -5,7 +5,7 @@ class AnonymousServiceLevel def cert_prefix if APP_CONFIG[:allow_limited_certs] APP_CONFIG[:limited_cert_prefix] - else + elsif APP_CONFIG[:allow_unlimited_certs] APP_CONFIG[:unlimited_cert_prefix] end end diff --git a/app/models/service_level.rb b/app/models/service_level.rb index 06ad202..5dd8838 100644 --- a/app/models/service_level.rb +++ b/app/models/service_level.rb @@ -19,7 +19,7 @@ class ServiceLevel def cert_prefix if limited_cert? APP_CONFIG[:limited_cert_prefix] - else + elsif APP_CONFIG[:allow_unlimited_certs] APP_CONFIG[:unlimited_cert_prefix] end end -- cgit v1.2.3 From be81b7430e0a2046125be7c3a4b01b8725f4afe6 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 18 Apr 2014 12:51:18 +0200 Subject: adopt service_level config to platform settings cost -> rate quota -> storage --- app/models/anonymous_service_level.rb | 1 - config/defaults.yml | 5 ++--- test/functional/v1/services_controller_test.rb | 9 +++------ 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/app/models/anonymous_service_level.rb b/app/models/anonymous_service_level.rb index 47b7cfb..4366a4a 100644 --- a/app/models/anonymous_service_level.rb +++ b/app/models/anonymous_service_level.rb @@ -23,7 +23,6 @@ class AnonymousServiceLevel def config_hash { name: "anonymous", description: description, - cost: 0, eip_rate_limit: APP_CONFIG[:allow_limited_certs] } end diff --git a/config/defaults.yml b/config/defaults.yml index a7b70a3..47cb641 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -52,13 +52,12 @@ service_levels: &service_levels 1: name: free description: "free account, with rate limited VPN" - cost: 0 eip_rate_limit: true - quota: 100 + storage: 100 2: name: premium description: "premium account, with unlimited vpn" - cost: + rate: USD: 10 EUR: 10 default_service_level: 1 diff --git a/test/functional/v1/services_controller_test.rb b/test/functional/v1/services_controller_test.rb index b81103f..e4058c0 100644 --- a/test/functional/v1/services_controller_test.rb +++ b/test/functional/v1/services_controller_test.rb @@ -6,8 +6,7 @@ class V1::ServicesControllerTest < ActionController::TestCase get :show, format: :json assert_json_response name: 'anonymous', eip_rate_limit: false, - description: 'please login to access our services', - cost: 0 + description: 'please login to access our services' end test "anonymous user gets vpn service info" do @@ -15,8 +14,7 @@ class V1::ServicesControllerTest < ActionController::TestCase get :show, format: :json assert_json_response name: 'anonymous', eip_rate_limit: false, - description: 'anonymous access to the VPN', - cost: 0 + description: 'anonymous access to the VPN' end end @@ -26,8 +24,7 @@ class V1::ServicesControllerTest < ActionController::TestCase assert_json_response name: 'free', eip_rate_limit: true, description: 'free account, with rate limited VPN', - cost: 0, - quota: 100 + storage: 100 end end -- cgit v1.2.3