Skip to content

Commit 15fd4ff

Browse files
authored
Merge pull request #108 from Jess-White/feature/412-add-users-delete-endpoint
Add delete organization user endpoint
2 parents d412cf3 + 7ba34eb commit 15fd4ff

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

app/controllers/api/organization_users_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ def show
2727
render 'show.json.jb'
2828
end
2929

30+
def destroy
31+
@organization_user = organization_user
32+
@organization_user.destroy!
33+
render 'show.json.jb'
34+
end
35+
3036
private
3137

3238
def organization_user

spec/controllers/api/organization_users_controller_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,57 @@
205205
)
206206
end
207207
end
208+
209+
describe 'DELETE /organizations/:organization_id/users/:id' do
210+
it 'renders 401 if organization does not exist' do
211+
set_auth_header(chidi)
212+
delete :destroy, params: { organization_id: 123, id: chidi.id }
213+
214+
expect(response).to have_http_status(401)
215+
end
216+
217+
it 'renders 401 if user does not exist' do
218+
set_auth_header(chidi)
219+
delete :destroy, params: { organization_id: good_place, id: 123 }
220+
221+
expect(response).to have_http_status(401)
222+
end
223+
224+
it 'renders 401 if requested user is not member of organization' do
225+
shawn = User.find_by!(first_name: 'Shawn')
226+
set_auth_header(chidi)
227+
delete :destroy, params: { organization_id: good_place, id: shawn.id }
228+
229+
expect(response).to have_http_status(401)
230+
end
231+
232+
it 'renders 200 with removed organization user' do
233+
set_auth_header(chidi)
234+
delete :destroy, params: { organization_id: good_place.id, id: chidi.id }
235+
236+
expect(response).to have_http_status(200)
237+
expect(JSON.parse(response.body).keys).to contain_exactly(*user_fields)
238+
expect(JSON.parse(response.body)).to match(
239+
a_hash_including(
240+
'id' => chidi.id,
241+
'created_at' => chidi.created_at.iso8601(3),
242+
'updated_at' => chidi.updated_at.iso8601(3),
243+
'email' => chidi.email,
244+
'first_name' => chidi.first_name,
245+
'last_name' => chidi.last_name
246+
)
247+
)
248+
end
249+
250+
it 'removes user from organization' do
251+
organization_user = OrganizationUser.find_by!(user_id: chidi.id, organization_id: good_place.id)
252+
253+
set_auth_header(chidi)
254+
delete :destroy, params: { organization_id: good_place.id, id: chidi.id }
255+
256+
expect do
257+
OrganizationUser.find(organization_user.id)
258+
end.to raise_error(ActiveRecord::RecordNotFound)
259+
end
260+
end
208261
end

0 commit comments

Comments
 (0)