From 9adbde13619de8b2c300056b062d12f0961cb710 Mon Sep 17 00:00:00 2001 From: ankonym Date: Mon, 21 Sep 2015 18:34:04 +0200 Subject: Make invite code configurable Through the config param 'invite_required', providers can decide whether users need to provide an invite code upon signup. The default setting is false. --- app/models/account.rb | 9 +++++--- app/models/user.rb | 2 +- app/views/users/new.html.haml | 5 ++++- config/defaults.yml | 1 + test/integration/browser/account_test.rb | 18 +++++++++++++++- test/support/browser_integration_test.rb | 35 ++++++++++++++++++++++++-------- test/unit/account_test.rb | 27 ++++++++++++++++-------- test/unit/invite_code_validator_test.rb | 4 ++++ 8 files changed, 78 insertions(+), 23 deletions(-) diff --git a/app/models/account.rb b/app/models/account.rb index c398d93..a5cd833 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -28,9 +28,12 @@ class Account identity.errors.each do |attr, msg| user.errors.add(attr, msg) end - user_invite_code = InviteCode.find_by_invite_code user.invite_code - user_invite_code.invite_count += 1 - user_invite_code.save + + if APP_CONFIG[:invite_required] + user_invite_code = InviteCode.find_by_invite_code user.invite_code + user_invite_code.invite_count += 1 + user_invite_code.save + end end rescue StandardError => ex user.errors.add(:base, ex.to_s) if user diff --git a/app/models/user.rb b/app/models/user.rb index c0079d5..3daee0f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -40,7 +40,7 @@ class User < CouchRest::Model::Base :mx_with_fallback => true - validates_with InviteCodeValidator, on: :create + validates_with InviteCodeValidator, on: :create, if: -> {APP_CONFIG[:invite_required]} timestamps! diff --git a/app/views/users/new.html.haml b/app/views/users/new.html.haml index cd4bf70..b44f77c 100644 --- a/app/views/users/new.html.haml +++ b/app/views/users/new.html.haml @@ -17,8 +17,11 @@ = f.input :login, :label => t(:username), :required => false, :input_html => { :id => :srp_username } = f.input :password, :required => false, :validate => true, :input_html => { :id => :srp_password } = f.input :password_confirmation, :required => false, :validate => true, :input_html => { :id => :srp_password_confirmation } - = f.input :invite_code, :input_html => { :id => :srp_invite_code } + - if APP_CONFIG[:invite_required] + = f.input :invite_code, :input_html => { :id => :srp_invite_code } + - else + = f.input :invite_code, :as => "hidden", :input_html => { :value => " ", :id => :srp_invite_code } = f.button :wrapped, cancel: home_path -# diff --git a/config/defaults.yml b/config/defaults.yml index dfa2a9a..906b446 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -82,6 +82,7 @@ common: &common - support - billing allow_registration: true + invite_required: false config_file_paths: soledad-service: 'public/1/config/soledad-service.json' eip-service: 'public/1/config/eip-service.json' diff --git a/test/integration/browser/account_test.rb b/test/integration/browser/account_test.rb index 6ab9eb2..cbe7ba9 100644 --- a/test/integration/browser/account_test.rb +++ b/test/integration/browser/account_test.rb @@ -6,7 +6,7 @@ class AccountTest < BrowserIntegrationTest Identity.destroy_all_disabled end - test "signup successfully" do + test "signup successfully when invited" do username, password = submit_signup assert page.has_content?("Welcome #{username}") click_on 'Log Out' @@ -16,6 +16,22 @@ class AccountTest < BrowserIntegrationTest user.account.destroy end + test "signup successfully without invitation" do + with_config invite_required: false do + + username ||= "test_#{SecureRandom.urlsafe_base64}".downcase + password ||= SecureRandom.base64 + + visit '/users/new' + fill_in 'Username', with: username + fill_in 'Password', with: password + fill_in 'Password confirmation', with: password + click_on 'Sign Up' + + assert page.has_content?("Welcome #{username}") + end + end + test "signup with username ending in dot json" do username = Faker::Internet.user_name + '.json' submit_signup username diff --git a/test/support/browser_integration_test.rb b/test/support/browser_integration_test.rb index 34ec9a6..35887cc 100644 --- a/test/support/browser_integration_test.rb +++ b/test/support/browser_integration_test.rb @@ -47,15 +47,32 @@ class BrowserIntegrationTest < ActionDispatch::IntegrationTest end def submit_signup(username = nil, password = nil) - username ||= "test_#{SecureRandom.urlsafe_base64}".downcase - password ||= SecureRandom.base64 - visit '/users/new' - fill_in 'Username', with: username - fill_in 'Password', with: password - fill_in 'Invite code', with: @testcode.invite_code - fill_in 'Password confirmation', with: password - click_on 'Sign Up' - return username, password + + with_config invite_required: true do + + username ||= "test_#{SecureRandom.urlsafe_base64}".downcase + password ||= SecureRandom.base64 + visit '/users/new' + fill_in 'Username', with: username + fill_in 'Password', with: password + fill_in 'Invite code', with: @testcode.invite_code + fill_in 'Password confirmation', with: password + click_on 'Sign Up' + return username, password + end + + with_config invite_required: false do + + username ||= "test_#{SecureRandom.urlsafe_base64}".downcase + password ||= SecureRandom.base64 + visit '/users/new' + fill_in 'Username', with: username + fill_in 'Password', with: password + fill_in 'Password confirmation', with: password + click_on 'Sign Up' + return username, password + end + end # currently this only works for tests with poltergeist. diff --git a/test/unit/account_test.rb b/test/unit/account_test.rb index 0882c43..6b814b6 100644 --- a/test/unit/account_test.rb +++ b/test/unit/account_test.rb @@ -11,7 +11,7 @@ class AccountTest < ActiveSupport::TestCase Identity.destroy_all_disabled end - test "create a new account" do + test "create a new account when invited" do user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code)) assert user.valid?, "unexpected errors: #{user.errors.inspect}" assert user.persisted? @@ -21,6 +21,16 @@ class AccountTest < ActiveSupport::TestCase user.account.destroy end + test "create a new account" do + with_config invite_required: false do + user = Account.create(FactoryGirl.attributes_for(:user)) + assert user.valid?, "unexpected errors: #{user.errors.inspect}" + assert user.persisted? + user.account.destroy + end + end + + test "create and remove a user account" do # We keep an identity that will block the handle from being reused. assert_difference "Identity.count" do @@ -50,13 +60,14 @@ class AccountTest < ActiveSupport::TestCase end test "Invite code count goes up by 1 when the invite code is entered" do - - user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code)) - user_code = InviteCode.find_by_invite_code user.invite_code - user_code.save - user.save - assert user.persisted? - assert_equal 1, user_code.invite_count + with_config invite_required: true do + user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code)) + user_code = InviteCode.find_by_invite_code user.invite_code + user_code.save + user.save + assert user.persisted? + assert_equal 1, user_code.invite_count + end end diff --git a/test/unit/invite_code_validator_test.rb b/test/unit/invite_code_validator_test.rb index 75e691d..ee8f1b3 100644 --- a/test/unit/invite_code_validator_test.rb +++ b/test/unit/invite_code_validator_test.rb @@ -2,9 +2,11 @@ require 'test_helper' class InviteCodeValidatorTest < ActiveSupport::TestCase test "user should not be created with invalid invite code" do + with_config invite_required: true do invalid_user = FactoryGirl.build(:user) assert !invalid_user.valid? + end end test "user should be created with valid invite code" do @@ -16,11 +18,13 @@ class InviteCodeValidatorTest < ActiveSupport::TestCase end test "trying to create a user with invalid invite code should add error" do + with_config invite_required: true do invalid_user = FactoryGirl.build(:user, :invite_code => "a non-existent code") invalid_user.valid? errors = {invite_code: ["This is not a valid code"]} assert_equal errors, invalid_user.errors.messages + end end end \ No newline at end of file -- cgit v1.2.3