Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/api/users_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class UsersApi < Grape::API
desc 'Get user'
get '/users/:id', requirements: { id: /[0-9]*/ } do
user = User.find(params[:id])
unless (user.id == current_user.id) || (authorise? current_user, User, :admin_users)
# Users may only access their own data unless they are teaching staff
unless user.id == current_user.id || Role.teaching_staff_ids.include?(current_user.role_id)
error!({ error: "Cannot find User with id #{params[:id]}" }, 403)
end

Expand Down
17 changes: 14 additions & 3 deletions test/api/users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,29 @@ def test_get_a_users_details
# Add username and auth_token to Header
add_auth_header_for(user: User.first)

# perform the GET
# perform the GET
get "/api/users/#{expected_user.id}"
returned_user = last_response_body

# Check if the call succeeds
assert_equal 200, last_response.status

# Check the returned details match as expected
response_keys = %w(first_name last_name email student_id nickname receive_task_notifications receive_portfolio_notifications receive_feedback_notifications opt_in_to_research has_run_first_time_setup)
assert_json_matches_model(expected_user, returned_user, response_keys)
end


def test_student_cannot_access_other_user
student = create(:user, :student)
other_student = create(:user, :student)

add_auth_header_for(user: student)

get "/api/users/#{other_student.id}"

assert_equal 403, last_response.status
end

def test_get_convenors

# Add username and auth_token to Header
Expand Down