Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read-only Bans API #345

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ def steam_id

render json: @user, serializer: UserSerializer
end

def bans
@user = User.find(params[:id])

@ban_models = User.ban_models.map do |_action, bans|
bans.map do |_subject, model|
next if model.subject?

model
end
end.reduce(:+).compact.sort_by(&:association_name)
@bans = @ban_models.map { |model| model.where(user: @user).to_a }.reduce(:+).sort_by(&:created_at)
@new_bans = @ban_models.map(&:new)

render json: @bans, each_serializer: BanSerializer
end
end
end
end
10 changes: 10 additions & 0 deletions app/serializers/api/v1/ban_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module API
module V1
class BanSerializer < ActiveModel::Serializer
type :ban

attributes :id, :created_at, :terminated_at
attribute(:type) { object.class.subject }
end
end
end
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
end

get 'users/steam_id/:id', to: 'users#steam_id'
resources :users, only: [:show]
resources :users, only: [:show] do
member do
get 'bans'
end
end
resources :teams, only: [:show]
end
end
Expand Down
27 changes: 27 additions & 0 deletions docs/api/v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ User:

```

```
Ban:
id: Integer
created_at: DateTime(ISO 8601)
terminated_at: DateTime(ISO 8601)
type: String
```

### GET `/users/:id`

Options:
Expand Down Expand Up @@ -69,6 +77,25 @@ Options:

Example same as above.

### GET `/users/:user_id/bans`

Options:
- `:user_id`, the unique id of the user

Example:
```json
{
"bans": [
{
"id": 1,
"created_at": "2018-01-21T14:12:31.498+10:00",
"terminated_at": "2019-01-21T14:11:00.000+10:00",
"type": "leagues"
}
]
}
```

## Teams

### Resource
Expand Down
43 changes: 43 additions & 0 deletions spec/controllers/api/v1/users_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'rails_helper'

describe API::V1::UsersController do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yea, completely missed this. The API is tested with request tests, eg. spec/requests/api/v1/users_spec.rb.

let(:key) { create(:api_key) }
let(:user) { create(:user) }

describe 'GET #bans' do
context 'when unauthenticated' do
it 'fails with unauthorized error' do
get :bans, params: { id: user.id }

expect(response).to have_http_status(:unauthorized)
expect(json['status']).to eq(401)
expect(json['message']).to eq('Unauthorized API key')
end
end

context 'when authenticated' do
context 'retrieving unbanned player' do
it 'succeeds with 0 bans' do
request.headers['X-API-Key'] = key.key
get :bans, params: { id: user.id }

expect(response).to have_http_status(:success)
expect(json['bans'].length).to eq(0)
end
end

context 'retrieving league banned player' do
it 'succeeds with 1 league ban' do
user.ban(:use, :leagues)

request.headers['X-API-Key'] = key.key
get :bans, params: { id: user.id }

expect(response).to have_http_status(:success)
expect(json['bans'].length).to eq(1)
expect(json['bans'].first['type']).to eq('leagues')
end
end
end
end
end
2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@

# Include url helpers
config.include Rails.application.routes.url_helpers

config.include JsonHelpers
end
5 changes: 5 additions & 0 deletions spec/support/json_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module JsonHelpers
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this, use response.parsed_body. It automagically decodes the json.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did try to use response.parsed_body but it ended up parsing the response as a string, even with as: :json (get :bans, params: { id: user.id }, as: :json). I just created this helper instead 🤷‍♂️

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for the other API requests.

def json
JSON.parse(response.body)
end
end