Merge pull request #1602 from salbertson/sa-refactor-users-update-specs
Refactor specs for UsersController#update
This commit is contained in:
commit
b474babe26
|
@ -824,41 +824,84 @@ 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)
|
||||||
|
|
||||||
|
put :update, username: user.username, name: 'Jim Tom'
|
||||||
|
|
||||||
|
expect(response).to be_success
|
||||||
|
expect(user.reload.name).to eq 'Jim Tom'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns user JSON' do
|
||||||
|
user = log_in
|
||||||
|
|
||||||
context 'without a token' do
|
|
||||||
it 'should ensure you can update the user' do
|
|
||||||
Guardian.any_instance.expects(:can_edit?).with(user).returns(false)
|
|
||||||
put :update, username: user.username
|
put :update, username: user.username
|
||||||
response.should be_forbidden
|
|
||||||
|
json = JSON.parse(response.body)
|
||||||
|
expect(json['user']['id']).to eq user.id
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'as a user who can edit the user' do
|
context 'when website includes http' do
|
||||||
|
it 'does not add http before updating' do
|
||||||
|
user = log_in
|
||||||
|
|
||||||
before do
|
put :update, username: user.username, website: 'http://example.com'
|
||||||
put :update, username: user.username, bio_raw: 'brand new bio'
|
|
||||||
user.reload
|
expect(user.reload.website).to eq 'http://example.com'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'updates the user' do
|
context 'when website does not include http' do
|
||||||
user.bio_raw.should == 'brand new bio'
|
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
|
||||||
|
|
||||||
it 'returns json success' do
|
context 'without permission to update any attributes' do
|
||||||
response.should be_success
|
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!).with(user).raises(Discourse::InvalidAccess.new)
|
||||||
|
Guardian.stubs(new: guardian).with(user)
|
||||||
|
|
||||||
|
put :update, username: user.username, name: 'Jim Tom'
|
||||||
|
|
||||||
|
expect(response).to be_forbidden
|
||||||
|
expect(user.reload.name).not_to eq 'Jim Tom'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'without permission to update title' do
|
||||||
|
it 'does not allow the user to update their title' do
|
||||||
|
user = Fabricate(:user, title: 'Emperor')
|
||||||
|
log_in_user(user)
|
||||||
|
guardian = Guardian.new(user)
|
||||||
|
guardian.stubs(can_grant_title?: false).with(user)
|
||||||
|
Guardian.stubs(new: guardian).with(user)
|
||||||
|
|
||||||
|
put :update, username: user.username, title: 'Minion'
|
||||||
|
|
||||||
|
expect(user.reload.title).not_to eq 'Minion'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1102,5 +1145,4 @@ describe UsersController do
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue