Make #update specs consistent
* Use expect syntax * Avoid lets * Stub Guardian method used in the controller
This commit is contained in:
parent
7a567d730d
commit
2e7696630b
|
@ -824,40 +824,43 @@ describe UsersController do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.update' do
|
describe '#update' do
|
||||||
|
context 'with guest' do
|
||||||
context 'not logged in' do
|
it 'raises an error' do
|
||||||
it 'raises an error when not logged in' do
|
|
||||||
expect do
|
expect do
|
||||||
xhr :put, :update, username: 'somename'
|
xhr :put, :update, username: 'guest'
|
||||||
end.to raise_error(Discourse::NotLoggedIn)
|
end.to raise_error(Discourse::NotLoggedIn)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'logged in' do
|
context 'with authenticated user' do
|
||||||
let!(:user) { log_in }
|
context 'with permission to update' do
|
||||||
|
it 'allows the update' do
|
||||||
|
user = Fabricate(:user, name: 'Billy Bob')
|
||||||
|
log_in_user(user)
|
||||||
|
guardian = Guardian.new(user)
|
||||||
|
guardian.stubs(:ensure_can_edit!)
|
||||||
|
Guardian.stubs(new: guardian).with(user)
|
||||||
|
|
||||||
context 'without a token' do
|
put :update, username: user.username, name: 'Jim Tom'
|
||||||
it 'should ensure you can update the user' do
|
|
||||||
Guardian.any_instance.expects(:can_edit?).with(user).returns(false)
|
expect(response).to be_success
|
||||||
put :update, username: user.username
|
expect(user.reload.name).to eq 'Jim Tom'
|
||||||
response.should be_forbidden
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'as a user who can edit the user' do
|
context 'without permission to update' do
|
||||||
|
it 'does not allow the update' do
|
||||||
|
user = Fabricate(:user, name: 'Billy Bob')
|
||||||
|
log_in_user(user)
|
||||||
|
guardian = Guardian.new(user)
|
||||||
|
guardian.stubs(:ensure_can_edit!).raises(Discourse::InvalidAccess.new)
|
||||||
|
Guardian.stubs(new: guardian).with(user)
|
||||||
|
|
||||||
before do
|
put :update, username: user.username, name: 'Jim Tom'
|
||||||
put :update, username: user.username, bio_raw: 'brand new bio'
|
|
||||||
user.reload
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'updates the user' do
|
expect(response).to be_forbidden
|
||||||
user.bio_raw.should == 'brand new bio'
|
expect(user.reload.name).not_to eq 'Jim Tom'
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns json success' do
|
|
||||||
response.should be_success
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1102,5 +1105,4 @@ describe UsersController do
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue