summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/user.rb2
-rw-r--r--lib/temporary_user.rb8
-rw-r--r--test/unit/temporary_user_test.rb28
3 files changed, 30 insertions, 8 deletions
diff --git a/app/models/user.rb b/app/models/user.rb
index 1116e6c..9cebbca 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -5,8 +5,6 @@ require 'temporary_user'
class User < CouchRest::Model::Base
include LoginFormatValidation
- use_database :users
-
property :login, String, :accessible => true
property :password_verifier, String, :accessible => true
property :password_salt, String, :accessible => true
diff --git a/lib/temporary_user.rb b/lib/temporary_user.rb
index e41add5..d0db1c4 100644
--- a/lib/temporary_user.rb
+++ b/lib/temporary_user.rb
@@ -13,6 +13,7 @@
module TemporaryUser
extend ActiveSupport::Concern
+ USER_DB = 'users'
TMP_USER_DB = 'tmp_users'
TMP_LOGIN = 'tmp_user' # created and deleted frequently
TEST_LOGIN = 'test_user' # created, rarely deleted
@@ -38,12 +39,15 @@ module TemporaryUser
end
alias :find :get
- # calls db_name(TMP_LOGIN), then creates a CouchRest::Database
- # from the name
+ def database
+ @database ||= prepare_database USER_DB
+ end
+
def tmp_database
@tmp_database ||= prepare_database TMP_USER_DB
end
+
# create the tmp db if it doesn't exist.
# requires admin access.
def create_tmp_database!
diff --git a/test/unit/temporary_user_test.rb b/test/unit/temporary_user_test.rb
index 38ccd67..2c9e70f 100644
--- a/test/unit/temporary_user_test.rb
+++ b/test/unit/temporary_user_test.rb
@@ -6,16 +6,37 @@ class TemporaryUserTest < ActiveSupport::TestCase
InviteCodeValidator.any_instance.stubs(:validate)
end
- test "tmp_user saved to tmp_users" do
- begin
- assert User.ancestors.include?(TemporaryUser)
+ test "TemporaryUser concern is applied" do
+ assert User.ancestors.include?(TemporaryUser)
+ end
+
+ test "temporary user has tmp_users as db" do
+ tmp_user = User.new :login => 'tmp_user_'+SecureRandom.hex(5).downcase
+ assert_equal 'leap_web_tmp_users', tmp_user.database.name
+ end
+ test "normal user has users as db" do
+ user = User.new :login => 'a'+SecureRandom.hex(5).downcase
+ assert_equal 'leap_web_users', user.database.name
+ end
+
+ test "user saved to users" do
+ begin
assert_difference('User.database.info["doc_count"]') do
normal_user = User.create!(:login => 'a'+SecureRandom.hex(5).downcase,
:password_verifier => 'ABCDEF0010101', :password_salt => 'ABCDEF')
refute normal_user.database.to_s.include?('tmp')
end
+ ensure
+ begin
+ normal_user.destroy
+ rescue
+ end
+ end
+ end
+ test "tmp_user saved to tmp_users" do
+ begin
assert_difference('User.tmp_database.info["doc_count"]') do
tmp_user = User.create!(:login => 'tmp_user_'+SecureRandom.hex(5).downcase,
:password_verifier => 'ABCDEF0010101', :password_salt => 'ABCDEF')
@@ -23,7 +44,6 @@ class TemporaryUserTest < ActiveSupport::TestCase
end
ensure
begin
- normal_user.destroy
tmp_user.destroy
rescue
end