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

Added admin api for managing tags #26872

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 77 additions & 0 deletions app/controllers/api/v1/admin/tags_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

class Api::V1::Admin::TagsController < Api::BaseController
include Authorization
before_action -> { authorize_if_got_token! :'admin:read' }, only: [:index, :show]
before_action -> { authorize_if_got_token! :'admin:write' }, only: :update
Comment on lines +5 to +6
Copy link
Contributor

Choose a reason for hiding this comment

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

It probably would make sense to have finer permissions, e.g. admin:read:tags and admin:write:tags, but that can be changed later.


before_action :set_tags, only: :index
before_action :set_tag, except: :index

after_action :insert_pagination_headers, only: :index
after_action :verify_authorized

LIMIT = 100
PAGINATION_PARAMS = %i(limit).freeze

def index
authorize :tag, :index?
render json: @tags, each_serializer: REST::Admin::TagSerializer
end

def show
authorize @tag, :show?
render json: @tag, serializer: REST::Admin::TagSerializer
end

def update
authorize @tag, :update?
if @tag.update(tag_params.merge(reviewed_at: Time.now.utc))
render json: @tag, serializer: REST::Admin::TagSerializer
else
render json: @tag, serializer: REST::Admin::TagUpdateErrorSerializer, status: 422
ClearlyClaire marked this conversation as resolved.
Show resolved Hide resolved
end
end

private

def set_tag
@tag = Tag.find(params[:id])
end

def set_tags
@tags = Tag.all.to_a_paginated_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
end

def tag_params
params.permit(:display_name, :trendable, :usable, :listable)
end

def insert_pagination_headers
set_pagination_headers(next_path, prev_path)
end

def next_path
api_v1_admin_tags_url(pagination_params(max_id: pagination_max_id)) if records_continue?
end

def prev_path
api_v1_admin_tags_url(pagination_params(min_id: pagination_since_id)) unless @tags.empty?
end

def pagination_max_id
@tags.last.id
end

def pagination_since_id
@tags.first.id
end

def records_continue?
@tags.size == limit_param(LIMIT)
end

def pagination_params(core_params)
params.slice(*PAGINATION_PARAMS).permit(*PAGINATION_PARAMS).merge(core_params)
end
end
1 change: 1 addition & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#

class Tag < ApplicationRecord
include Paginable
has_and_belongs_to_many :statuses
has_and_belongs_to_many :accounts

Expand Down
11 changes: 11 additions & 0 deletions app/serializers/rest/admin/tag_update_error_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class REST::Admin::TagUpdateErrorSerializer < ActiveModel::Serializer
attributes :errors

has_one :tag, serializer: REST::Admin::TagSerializer

def tag
object
end
end
2 changes: 2 additions & 0 deletions config/routes/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@
post :test
end
end

resources :tags, only: [:index, :show, :update]
end
end

Expand Down
53 changes: 53 additions & 0 deletions spec/controllers/api/v1/admin/tags_controller_spec.rb
ClearlyClaire marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require 'rails_helper'

describe Api::V1::Admin::TagsController do
render_views

let(:role) { UserRole.find_by(name: 'Admin') }
let(:user) { Fabricate(:user, role: role) }
let(:scopes) { 'admin:read admin:write' }
ClearlyClaire marked this conversation as resolved.
Show resolved Hide resolved
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
let(:tag) { Fabricate(:tag) }

before do
allow(controller).to receive(:doorkeeper_token) { token }
end

describe 'GET #index' do
it 'returns http success' do
get :index
expect(response).to have_http_status(200)
end
ClearlyClaire marked this conversation as resolved.
Show resolved Hide resolved
end

describe 'GET #show' do
it 'returns http success' do
get :show, params: { id: tag.id }

expect(response).to have_http_status(200)
end
end

describe 'PUT #update' do
let(:scopes) { 'admin:write' }
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add a scope of admin:write:taxonomies, instead of just using the global admin:write permission here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is that a new scope? I don't believe it's an option when creating a token.

Copy link
Contributor

Choose a reason for hiding this comment

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

That is not a scope that exists.


before do
put :update, params: { id: tag.id, display_name: tag.name.upcase }
end

it 'returns http success' do
expect(response).to have_http_status(200)
end

it 'updates the display_name' do
expect(tag.reload.display_name).to eq tag.name.upcase
end

it 'returns http unprocessable entity' do
put :update, params: { id: tag.id, display_name: tag.name + tag.id.to_s }
expect(response).to have_http_status 422
end
ClearlyClaire marked this conversation as resolved.
Show resolved Hide resolved
end
end