Skip to content

Commit

Permalink
Sends notification when proxy depositor specified
Browse files Browse the repository at this point in the history
Also refactors spec a little bit to:
  * have one expectation per test.
  * use rspec context to describe different conditions.

Closes #1656
  • Loading branch information
afred committed Mar 28, 2016
1 parent 41d64e4 commit b03b42f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
Expand Up @@ -24,6 +24,7 @@ def create
head :ok
else
grantor.can_receive_deposits_from << grantee
send_proxy_depositor_added_messages(grantor, grantee)
render json: { name: grantee.name, delete_path: sufia.user_depositor_path(grantor.user_key, grantee.user_key) }
end
end
Expand All @@ -45,5 +46,12 @@ def authorize_and_return_grantor
authorize! :edit, grantor
grantor
end

def send_proxy_depositor_added_messages(grantor, grantee)
message_to_grantee = "#{grantor.name} has assigned you as a proxy depositor"
message_to_grantor = "You have assigned #{grantee.name} as a proxy depositor"
::User.batchuser.send_message(grantor, message_to_grantor, "Proxy Depositor Added")
::User.batchuser.send_message(grantee, message_to_grantee, "Proxy Depositor Added")
end
end
end
37 changes: 29 additions & 8 deletions spec/controllers/depositors_controller_spec.rb
Expand Up @@ -9,16 +9,37 @@
sign_in user
end

describe "create" do
it "is successful" do
expect { post :create, user_id: user.user_key, grantee_id: grantee.user_key, format: 'json' }.to change { ProxyDepositRights.count }.by(1)
expect(response).to be_success
describe "#create" do
context 'when the grantee has not yet been designated as a depositor' do
let(:request_to_grant_proxy) { post :create, user_id: user.user_key, grantee_id: grantee.user_key, format: 'json' }

it 'is successful' do
expect { request_to_grant_proxy }.to change { ProxyDepositRights.count }.by(1)
expect(response).to be_success
end

it 'sends a message to the grantor' do
expect { request_to_grant_proxy }.to change { user.mailbox.inbox.count }.by(1)
end

it 'sends a message to the grantee' do
expect { request_to_grant_proxy }.to change { grantee.mailbox.inbox.count }.by(1)
end
end

it "does not add current user" do
expect { post :create, user_id: user.user_key, grantee_id: user.user_key, format: 'json' }.to change { ProxyDepositRights.count }.by(0)
expect(response).to be_success
expect(response.body).to be_blank
context 'when the grantee is already an allowed depositor' do
# For this test we just set the grantor to be eq to grantee.
let(:redundant_request_to_grant_proxy) { post :create, user_id: user.user_key, grantee_id: user.user_key, format: 'json' }

it 'does not add the user, and returns a 200, with empty response body' do
expect { redundant_request_to_grant_proxy }.to change { ProxyDepositRights.count }.by(0)
expect(response).to be_success
expect(response.body).to be_blank
end

it 'does not send a message to the user' do
expect { redundant_request_to_grant_proxy }.to_not change { user.mailbox.inbox.count }
end
end
end

Expand Down

0 comments on commit b03b42f

Please sign in to comment.