forked from openware/peatio
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
287 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module API | ||
module Entities | ||
class Label < Grape::Entity | ||
format_with(:iso_timestamp, &:iso8601) | ||
|
||
expose :key, documentation: { type: 'String', desc: 'Label key. [a-z0-9_-]+ should be used. Min - 3, max - 255 characters.' } | ||
expose :value, documentation: { type: 'String', desc: 'Label value. [A-Za-z0-9_-] should be used. Min - 3, max - 255 characters.' } | ||
expose :scope, documentation: { type: 'String', desc: "Label scope: 'public' or 'private'" } | ||
|
||
with_options(format_with: :iso_timestamp) do | ||
expose :created_at | ||
expose :updated_at | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'doorkeeper/grape/helpers' | ||
|
||
module API | ||
module V1 | ||
# Responsible for CRUD for labes | ||
class Labels < Grape::API | ||
helpers Doorkeeper::Grape::Helpers | ||
|
||
before { doorkeeper_authorize! } | ||
|
||
before do | ||
def current_account | ||
Account.find(doorkeeper_token.resource_owner_id) | ||
end | ||
end | ||
|
||
resource :labels do | ||
desc 'List all labels for current account.' | ||
get do | ||
present current_account.labels, with: API::Entities::Label | ||
end | ||
|
||
desc 'Return a label by key.' | ||
params do | ||
requires :key, type: String, allow_blank: false, desc: 'Label key.' | ||
end | ||
route_param :key do | ||
get do | ||
label = current_account.labels.find_by(key: params[:key]) | ||
return error!('Couldn\'t find Label', 404) if label.blank? | ||
|
||
present label, with: API::Entities::Label | ||
end | ||
end | ||
|
||
desc "Create a label with 'public' scope." | ||
params do | ||
requires :key, type: String, allow_blank: false, desc: 'Label key.' | ||
requires :value, type: String, allow_blank: false, desc: 'Label value.' | ||
end | ||
post do | ||
label = | ||
current_account.labels.new( | ||
key: params[:key], | ||
value: params[:value], | ||
scope: 'public' | ||
) | ||
if label.save | ||
present label, with: API::Entities::Label | ||
else | ||
error!(label.errors.as_json(full_messages: true), 422) | ||
end | ||
end | ||
|
||
desc "Update a label with 'public' scope." | ||
params do | ||
requires :key, type: String, allow_blank: false, desc: 'Label key.' | ||
requires :value, type: String, allow_blank: false, desc: 'Label value.' | ||
end | ||
patch ':key' do | ||
label = current_account.labels.find_by(key: params[:key]) | ||
return error!('Couldn\'t find Label', 404) if label.blank? | ||
return error!('Can\'t update Label.', 400) if label.private? | ||
|
||
label.update(value: params[:value]) | ||
present label, with: API::Entities::Label | ||
end | ||
|
||
desc "Delete a label with 'public' scope." | ||
params do | ||
requires :key, type: String, allow_blank: false, desc: 'Label key.' | ||
end | ||
delete ':key' do | ||
label = current_account.labels.find_by(key: params[:key]) | ||
return error!('Couldn\'t find Label', 404) if label.blank? | ||
return error!('Can\'t update Label.', 400) if label.private? | ||
|
||
label.destroy | ||
status 204 | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
# Resposible for storing configurations | ||
class Label < ApplicationRecord | ||
belongs_to :account | ||
|
||
SCOPES = | ||
HashWithIndifferentAccess.new( | ||
public: 'public', private: 'private' | ||
) | ||
|
||
SCOPES.keys.each do |name| | ||
define_method "#{name}?" do | ||
scope == SCOPES[name] | ||
end | ||
end | ||
|
||
validates :scope, | ||
inclusion: { in: SCOPES.keys } | ||
|
||
validates :key, | ||
length: 3..255, | ||
format: { with: /\A[a-z0-9_-]+\z/ }, | ||
uniqueness: { scope: :account_id } | ||
|
||
validates :value, | ||
length: 3..255, | ||
format: { with: /\A[A-Za-z0-9_-]+\z/ } | ||
end | ||
|
||
# == Schema Information | ||
# Schema version: 20180402133658 | ||
# | ||
# Table name: labels | ||
# | ||
# id :integer not null, primary key | ||
# account_id :integer | ||
# key :string(255) not null | ||
# value :string(255) not null | ||
# scope :string(255) default("public"), not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_labels_on_account_id (account_id) | ||
# index_labels_on_key_and_value_and_account_id (key,value,account_id) UNIQUE | ||
# | ||
# Foreign Keys | ||
# | ||
# fk_rails_... (account_id => accounts.id) ON DELETE => cascade | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateLabels < ActiveRecord::Migration[5.1] | ||
def change | ||
create_table :labels do |t| | ||
t.references :account | ||
t.string :key, null: false | ||
t.string :value, null: false | ||
t.string :scope, null: false, default: 'public' | ||
|
||
t.timestamps | ||
end | ||
add_index :labels, %i[key value account_id], unique: true | ||
add_foreign_key :labels, :accounts, on_delete: :cascade | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'Labels API.' do | ||
include_context 'doorkeeper authentication' | ||
|
||
let!(:label) { create :label, account: current_account } | ||
|
||
let(:post_params) do | ||
{ | ||
key: ::Faker::Internet.slug(nil, '-'), | ||
value: ::Faker::Internet.slug(nil, '-') | ||
} | ||
end | ||
|
||
context 'Happy paths for the current account.' do | ||
it 'Return lables for current account' do | ||
get '/api/v1/labels', headers: auth_header | ||
expect(response.status).to eq(200) | ||
expect(json_body.size).to eq(1) | ||
expect(json_body.first[:key]).to eq(label.key) | ||
end | ||
|
||
it 'Return a label by key' do | ||
get "/api/v1/labels/#{label.key}", headers: auth_header | ||
expect(response.status).to eq(200) | ||
expect(json_body[:key]).to eq(label.key) | ||
end | ||
|
||
it 'Create a label' do | ||
post '/api/v1/labels', params: post_params, headers: auth_header | ||
expect(response.status).to eq(201) | ||
persisted = Label.find_by(key: json_body[:key]) | ||
expect(persisted.key).to eq(post_params[:key]) | ||
expect(persisted.value).to eq(post_params[:value]) | ||
expect(persisted.scope).to eq('public') | ||
expect(persisted.account_id).to eq(current_account.id) | ||
end | ||
|
||
it 'Update a label' do | ||
patch "/api/v1/labels/#{label.key}", params: post_params, headers: auth_header | ||
expect(response.status).to eq(200) | ||
persisted = Label.find_by(key: label.key) | ||
expect(persisted.value).to eq(post_params[:value]) | ||
expect(persisted.scope).to eq('public') | ||
end | ||
|
||
it 'Delete a label' do | ||
delete "/api/v1/labels/#{label.key}", headers: auth_header | ||
expect(response.status).to eq(204) | ||
expect(Label.find_by(key: label.key)).to be_nil | ||
end | ||
end | ||
|
||
context 'Errors.' do | ||
it 'Respond with errors on create if existing key is used for current account' do | ||
post '/api/v1/labels', params: post_params.merge(key: label.key), headers: auth_header | ||
expect(response.status).to eq(422) | ||
expect(response.body).to be_include('Key has already been taken') | ||
end | ||
|
||
it 'Respond with error if attempted to update a private label' do | ||
label.update(scope: 'private') | ||
patch "/api/v1/labels/#{label.key}", params: post_params, headers: auth_header | ||
expect(response.status).to eq(400) | ||
end | ||
|
||
it 'Respond with error if key not found' do | ||
get '/api/v1/labels/blah', headers: auth_header | ||
expect(response.status).to eq(404) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :label do | ||
key { ::Faker::Internet.slug(nil, '-') } | ||
value { ::Faker::Internet.slug(nil, '-') } | ||
scope 'public' | ||
account { create(:account) } | ||
end | ||
end |