summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/functional/api/users_controller_test.rb22
-rw-r--r--test/integration/api/srp_test.rb2
-rw-r--r--test/integration/api/update_account_test.rb8
-rw-r--r--test/integration/browser/account_livecycle_test.rb15
-rw-r--r--test/unit/account_test.rb56
5 files changed, 81 insertions, 22 deletions
diff --git a/test/functional/api/users_controller_test.rb b/test/functional/api/users_controller_test.rb
index 88ecae0..ee183f9 100644
--- a/test/functional/api/users_controller_test.rb
+++ b/test/functional/api/users_controller_test.rb
@@ -4,28 +4,42 @@ class Api::UsersControllerTest < ApiControllerTest
test "user can change settings" do
user = find_record :user
- changed_attribs = record_attributes_for :user_with_settings
+ attribs = record_attributes_for(:user)
+ changed_attribs = attribs.slice 'login',
+ 'password_verifier',
+ 'password_salt'
account_settings = stub
account_settings.expects(:update).with(changed_attribs)
Account.expects(:new).with(user).returns(account_settings)
login user
- api_put :update, :user => changed_attribs, :id => user.id, :format => :json
+ api_put :update, :user => attribs, :id => user.id, :format => :json
assert_equal user, assigns[:user]
assert_response 204
assert @response.body.blank?, "Response should be blank"
end
+ test "deal with empty settings" do
+ user = find_record :user
+ login user
+ assert_raises ActionController::ParameterMissing do
+ api_put :update, :id => user.id, :format => :json
+ end
+ end
+
test "admin can update user" do
user = find_record :user
- changed_attribs = record_attributes_for :user_with_settings
+ attribs = record_attributes_for(:user)
+ changed_attribs = attribs.slice 'login',
+ 'password_verifier',
+ 'password_salt'
account_settings = stub
account_settings.expects(:update).with(changed_attribs)
Account.expects(:new).with(user).returns(account_settings)
login :is_admin? => true
- api_put :update, :user => changed_attribs, :id => user.id, :format => :json
+ api_put :update, :user => attribs, :id => user.id, :format => :json
assert_equal user, assigns[:user]
assert_response 204
diff --git a/test/integration/api/srp_test.rb b/test/integration/api/srp_test.rb
index b9605f9..ef5d9b8 100644
--- a/test/integration/api/srp_test.rb
+++ b/test/integration/api/srp_test.rb
@@ -46,7 +46,7 @@ class SrpTest < RackTest
@password = password
end
- def update_user(params)
+ def update_user(params = {})
put api_url("users/#{@user.id}.json"),
user_params(params),
auth_headers
diff --git a/test/integration/api/update_account_test.rb b/test/integration/api/update_account_test.rb
index f083dbc..dd28b06 100644
--- a/test/integration/api/update_account_test.rb
+++ b/test/integration/api/update_account_test.rb
@@ -19,6 +19,14 @@ class UpdateAccountTest < SrpTest
assert_login_required
end
+ test "empty request" do
+ authenticate
+ update_user
+ refute last_response.successful?
+ assert_equal 400, last_response.status
+ assert_equal '', last_response.body
+ end
+
test "update password via api" do
authenticate
update_user password: "No! Verify me instead."
diff --git a/test/integration/browser/account_livecycle_test.rb b/test/integration/browser/account_livecycle_test.rb
index cfab444..68775d3 100644
--- a/test/integration/browser/account_livecycle_test.rb
+++ b/test/integration/browser/account_livecycle_test.rb
@@ -63,6 +63,16 @@ class AccountLivecycleTest < BrowserIntegrationTest
assert_invalid_login(page)
end
+ test "failed login with locale" do
+ page.driver.add_header 'Accept-Language', 'de'
+ visit '/'
+ click_on 'Anmelden'
+ fill_in 'Nutzername', with: 'username'
+ fill_in 'Password', with: 'falsches password'
+ click_on 'Session erstellen'
+ assert_invalid_login(page, locale: :de)
+ end
+
test "account destruction" do
username, password = submit_signup
@@ -115,9 +125,10 @@ class AccountLivecycleTest < BrowserIntegrationTest
click_on 'Log In'
end
- def assert_invalid_login(page)
+ def assert_invalid_login(page, locale: nil)
assert page.has_selector? '.btn-primary.disabled'
- assert page.has_content? sanitize(I18n.t(:invalid_user_pass), tags: [])
+ message = I18n.t :invalid_user_pass, locale: locale
+ assert page.has_content? sanitize(message, tags: [])
assert page.has_no_selector? '.btn-primary.disabled'
end
diff --git a/test/unit/account_test.rb b/test/unit/account_test.rb
index 058e196..61b40b5 100644
--- a/test/unit/account_test.rb
+++ b/test/unit/account_test.rb
@@ -12,7 +12,7 @@ class AccountTest < ActiveSupport::TestCase
end
test "create a new account when invited" do
- user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code))
+ user = Account.create(user_attributes( :invite_code => @testcode.invite_code))
assert user.valid?, "unexpected errors: #{user.errors.inspect}"
assert user.persisted?
assert id = user.identity
@@ -23,7 +23,7 @@ class AccountTest < ActiveSupport::TestCase
test "fail to create account without invite" do
with_config invite_required: true do
- user = Account.create(FactoryGirl.attributes_for(:user))
+ user = Account.create(user_attributes)
assert !user.valid?, "user should not be valid"
assert !user.persisted?, "user should not have been saved"
assert_has_errors user, invite_code: "This is not a valid code"
@@ -32,7 +32,7 @@ class AccountTest < ActiveSupport::TestCase
test "allow invite_required override" do
with_config invite_required: true do
- user = Account.create(FactoryGirl.attributes_for(:user), :invite_required => false)
+ user = Account.create(user_attributes, :invite_required => false)
assert user.valid?, "unexpected errors: #{user.errors.inspect}"
assert user.persisted?, "user should have been saved"
user.account.destroy
@@ -41,7 +41,7 @@ class AccountTest < ActiveSupport::TestCase
test "create a new account" do
with_config invite_required: false do
- user = Account.create(FactoryGirl.attributes_for(:user))
+ user = Account.create(user_attributes)
assert user.valid?, "unexpected errors: #{user.errors.inspect}"
assert user.persisted?
user.account.destroy
@@ -50,7 +50,7 @@ class AccountTest < ActiveSupport::TestCase
test "error on reused username" do
with_config invite_required: false do
- attributes = FactoryGirl.attributes_for :user
+ attributes = user_attributes
user = Account.create attributes
dup = Account.create attributes
assert !dup.valid?
@@ -72,17 +72,17 @@ class AccountTest < ActiveSupport::TestCase
# We keep an identity that will block the handle from being reused.
assert_difference "Identity.count" do
assert_no_difference "User.count" do
- user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code))
+ user = Account.create(user_attributes( :invite_code => @testcode.invite_code))
user.account.destroy
end
end
end
test "change username and create alias" do
- user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code))
+ user = Account.create(user_attributes( :invite_code => @testcode.invite_code))
old_id = user.identity
old_email = user.email_address
- user.account.update(FactoryGirl.attributes_for(:user))
+ user.account.update(user_attributes)
user.reload
old_id.reload
assert user.valid?
@@ -97,7 +97,7 @@ class AccountTest < ActiveSupport::TestCase
end
test "create recovery code if it does not exist" do
- user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code))
+ user = Account.create(user_attributes( :invite_code => @testcode.invite_code))
user.account.update(:recovery_code_verifier => "abc", :recovery_code_salt => "123")
user.reload
@@ -108,7 +108,7 @@ class AccountTest < ActiveSupport::TestCase
end
test "update recovery code that already exists" do
- user = Account.create(FactoryGirl.attributes_for(:user,
+ user = Account.create(user_attributes(
:invite_code => @testcode.invite_code,
:recovery_code_verifier => "000",
:recovery_code_salt => "111"))
@@ -123,7 +123,7 @@ class AccountTest < ActiveSupport::TestCase
end
test "update password" do
- user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code))
+ user = Account.create(user_attributes( :invite_code => @testcode.invite_code))
user.account.update(:password_verifier => "551A8B", :password_salt => "551A8B")
assert_equal "551A8B", user.password_verifier
@@ -134,18 +134,38 @@ class AccountTest < ActiveSupport::TestCase
test "Invite code count goes up by 1 when the invite code is entered" do
with_config invite_required: true do
- user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code))
+ user = Account.create(user_attributes( :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
+
+ test "Single use invite code is destroyed when used by test user" do
+ with_config invite_required: true do
+ attrs = user_attributes invite_code: @testcode.invite_code
+ attrs[:login] = 'test_user_' + attrs[:login]
+ user = Account.create(attrs)
+ user.save
+ assert user.persisted?, user.errors.inspect
+ assert_nil InviteCode.find_by_invite_code user.invite_code
+ end
+ end
+ test "Single use invite code is destroyed when used by tmp user" do
+ with_config invite_required: true do
+ attrs = user_attributes invite_code: @testcode.invite_code
+ attrs[:login] = 'tmp_user_' + attrs[:login]
+ user = Account.create(attrs)
+ user.save
+ assert user.persisted?, user.errors.inspect
+ assert_nil InviteCode.find_by_invite_code user.invite_code
+ end
end
test "Invite code stays zero when invite code is not used" do
- #user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code))
+ #user = Account.create(user_attributes( :invite_code => @testcode.invite_code))
invalid_user = FactoryGirl.build(:user, :invite_code => @testcode.invite_code)
invalid_user.save
user_code = InviteCode.find_by_invite_code invalid_user.invite_code
@@ -155,7 +175,7 @@ class AccountTest < ActiveSupport::TestCase
end
test "disabled accounts have no cert fingerprints" do
- user = Account.create(FactoryGirl.attributes_for(:user))
+ user = Account.create(user_attributes)
cert = stub(expiry: 1.month.from_now, fingerprint: SecureRandom.hex)
user.identity.register_cert cert
user.identity.save
@@ -184,6 +204,8 @@ class AccountTest < ActiveSupport::TestCase
end
end
+ protected
+
# Tests for the presence of the errors given.
# Does not test for the absence of other errors - so there may be more.
def assert_has_errors(record, errors)
@@ -194,4 +216,8 @@ class AccountTest < ActiveSupport::TestCase
end
end
+ def user_attributes(attrs = {})
+ FactoryGirl.attributes_for :user, attrs
+ end
+
end