Add test coverage for #update

This commit is contained in:
Scott Albertson 2013-11-01 11:42:16 -07:00
parent 58f96bdfb5
commit 3cc17ad4cd
1 changed files with 36 additions and 21 deletions

View File

@ -836,25 +836,52 @@ describe UsersController do
context 'with authenticated user' do context 'with authenticated user' do
context 'with permission to update' do context 'with permission to update' do
it 'allows the update' do it 'allows the update' do
user = create_authenticated_user('Billy Bob') user = Fabricate(:user, name: 'Billy Bob')
stub_guardian(user) do |guardian| log_in_user(user)
guardian.stubs(:ensure_can_edit!).with(user)
end
put :update, username: user.username, name: 'Jim Tom' put :update, username: user.username, name: 'Jim Tom'
expect(response).to be_success expect(response).to be_success
expect(user.reload.name).to eq 'Jim Tom' expect(user.reload.name).to eq 'Jim Tom'
end end
it 'returns user JSON' do
user = log_in
put :update, username: user.username
json = JSON.parse(response.body)
expect(json['user']['id']).to eq user.id
end
context 'when website includes http' do
it 'does not add http before updating' do
user = log_in
put :update, username: user.username, website: 'http://example.com'
expect(user.reload.website).to eq 'http://example.com'
end
end
context 'when website does not include http' do
it 'adds http before updating' do
user = log_in
put :update, username: user.username, website: 'example.com'
expect(user.reload.website).to eq 'http://example.com'
end
end
end end
context 'without permission to update' do context 'without permission to update' do
it 'does not allow the update' do it 'does not allow the update' do
user = create_authenticated_user('Billy Bob') user = Fabricate(:user, name: 'Billy Bob')
stub_guardian(user) do |guardian| log_in_user(user)
guardian.stubs(:ensure_can_edit!). guardian = Guardian.new(user)
with(user).raises(Discourse::InvalidAccess.new) guardian.stubs(:ensure_can_edit!).with(user).raises(Discourse::InvalidAccess.new)
end Guardian.stubs(new: guardian).with(user)
put :update, username: user.username, name: 'Jim Tom' put :update, username: user.username, name: 'Jim Tom'
@ -1104,16 +1131,4 @@ describe UsersController do
end end
end end
private
def create_authenticated_user(name)
log_in_user(Fabricate(:user, name: name))
end
def stub_guardian(user)
guardian = Guardian.new(user)
yield(guardian)
Guardian.stubs(new: guardian).with(user)
end
end end