Skip to content

Commit

Permalink
Add /application_users/username/:username endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanstitt committed Feb 26, 2015
1 parent 0d4c177 commit 59e928b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
30 changes: 28 additions & 2 deletions app/controllers/api/v1/application_users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Api::V1::ApplicationUsersController < OpenStax::Api::V1::ApiController
#before_filter :get_app_user, :only => [:show, :update, :destroy]

resource_description do
api_versions "v1"
short_description 'Records which users interact with which applications, as well the users'' preferences for each app.'
Expand Down Expand Up @@ -82,7 +82,7 @@ class Api::V1::ApplicationUsersController < OpenStax::Api::V1::ApiController
Example:
`last_name, username DESC` &ndash; sorts by last name ascending, then by username descending
`last_name, username DESC` &ndash; sorts by last name ascending, then by username descending
EOS
def index
OSU::AccessPolicy.require_action_allowed!(:search, current_api_user, ApplicationUser)
Expand Down Expand Up @@ -134,6 +134,32 @@ def index
# standard_destroy(ApplicationUser, app_user.id)
#end

###############################################################
# username
###############################################################

api :GET, '/application_users/username/:username',
'Gets a single User with the specified username.'
description <<-EOS
All users of OpenStax have an associated User object. This endpoint
allows querying additional data on a user when all that is known is the
User's username.
Admins (for Accounts only) are identified by the is_administrator boolean.
Some additional user information can be found in associations, such as
email addresses in ContactInfos and the password hash in Identity.
Users have the following String attributes:
username, first_name, last_name, full_name, title
#{json_schema(Api::V1::UserSearchRepresenter, include: :readable)}
EOS
def username
OSU::AccessPolicy.require_action_allowed!(:search, current_api_user, User)
outputs = SearchUsers.call("username:#{params[:username]}", {exact: true}).outputs
respond_with outputs, represent_with: Api::V1::UserSearchRepresenter
end

###############################################################
# updates
###############################################################
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

resources :application_users, only: [:index] do
collection do
get 'username/:username', action: 'username'
get 'updates'
put 'updated'
end
Expand Down
21 changes: 20 additions & 1 deletion spec/controllers/api/v1/application_users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@
end
end

describe "username" do
it "returns a single result" do
api_get :username, trusted_application_token, parameters: { username: 'foo_bb' }
expect(response.code).to eq('200')
expected_response = {
total_count: 1,
items: [
{
id: bob_brown.id,
username: bob_brown.username,
first_name: bob_brown.first_name,
last_name: bob_brown.last_name
}
]
}.to_json
expect(response.body).to eq(expected_response)
end
end

describe "index" do

it "returns a single result well" do
Expand Down Expand Up @@ -301,4 +320,4 @@

end

end
end

0 comments on commit 59e928b

Please sign in to comment.