Skip to content

Commit

Permalink
regenerate random symbol for provider. #335
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Aug 8, 2019
1 parent 2f02d9d commit c17e789
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ gem 'fast_jsonapi', '~> 1.3'
gem 'jwt'
gem 'bcrypt', '~> 3.1.7'
gem 'pwqgen.rb', '~> 0.1.0'
gem 'string_pattern'
gem 'simple_command'
gem 'kaminari', '~> 1.0', '>= 1.0.1'
gem 'cancancan', '~> 2.0'
Expand Down
7 changes: 5 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ GEM
audited (4.9.0)
activerecord (>= 4.2, < 6.1)
aws-eventstream (1.0.3)
aws-partitions (1.196.0)
aws-partitions (1.198.0)
aws-sdk-core (3.62.0)
aws-eventstream (~> 1.0, >= 1.0.2)
aws-partitions (~> 1.0)
Expand Down Expand Up @@ -243,7 +243,7 @@ GEM
promise.rb (~> 0.7.2)
graphql-errors (0.3.0)
graphql (>= 1.6.0, < 2)
haml (5.1.1)
haml (5.1.2)
temple (>= 0.8.0)
tilt
hamster (3.0.0)
Expand Down Expand Up @@ -492,6 +492,8 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
string_pattern (2.1.4)
regexp_parser (~> 1.3, >= 1.3.0)
strip_attributes (1.9.0)
activemodel (>= 3.0, < 7.0)
sxp (1.0.2)
Expand Down Expand Up @@ -607,6 +609,7 @@ DEPENDENCIES
spring
spring-commands-rspec
spring-watcher-listen (~> 2.0.0)
string_pattern
strip_attributes (~> 1.8)
turnout (~> 2.5)
vcr (~> 3.0.3)
Expand Down
11 changes: 9 additions & 2 deletions app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ProvidersController < ApplicationController
prepend_before_action :authenticate_user!
before_action :set_provider, only: [:show, :update, :destroy]
before_action :set_include
load_and_authorize_resource :except => [:totals]
load_and_authorize_resource :except => [:totals, :random]

def index
sort = case params[:sort]
Expand Down Expand Up @@ -180,7 +180,9 @@ def show

def create
logger = Logger.new(STDOUT)
@provider = Provider.new(safe_params)

# generate random symbol if not symbol is provided
@provider = Provider.new(safe_params.reverse_merge(symbol: generate_random_provider_symbol))
authorize! :create, @provider

if @provider.save
Expand Down Expand Up @@ -306,6 +308,11 @@ def destroy
end
end

def random
symbol = generate_random_provider_symbol
render json: { symbol: symbol }.to_json
end

protected

def set_include
Expand Down
4 changes: 4 additions & 0 deletions app/models/concerns/helpable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def get_url
end
end

def generate_random_provider_symbol
"4:X".gen
end

def generate_random_dois(str, options={})
prefix = validate_prefix(str)
fail IdentifierError, "No valid prefix found" unless prefix.present?
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
get '/dois/text/csv', :to => 'dois#index', defaults: { format: :csv }
get '/dois/text/x-bibliography', :to => 'dois#index', defaults: { format: :citation }
get '/providers/text/csv', :to => 'providers#index', defaults: { format: :csv }
get 'providers/random', :to => 'providers#random'

# manage DOIs
post 'dois/validate', :to => 'dois#validate'
Expand Down
6 changes: 6 additions & 0 deletions spec/concerns/helpable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
describe Doi, vcr: true do
subject { create(:doi) }

context "generate_random_symbol" do
it 'should generate' do
expect(subject.generate_random_symbol).to match(/\A[A-Z]{4}\Z/)
end
end

context "validate_prefix" do
it 'should validate' do
str = "10.14454"
Expand Down
23 changes: 16 additions & 7 deletions spec/requests/providers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,8 @@
it 'creates a provider' do
post '/providers', params, headers

expect(json.dig('data', 'attributes', 'systemEmail')).to eq("[email protected]")
end

it 'returns status code 200' do
post '/providers', params, headers

expect(last_response.status).to eq(200)
expect(json.dig('data', 'attributes', 'systemEmail')).to eq("[email protected]")
end
end

Expand All @@ -502,13 +497,27 @@
it 'creates a provider' do
post '/providers', params, headers

expect(last_response.status).to eq(200)
expect(json.dig('data', 'attributes', 'systemEmail')).to eq("[email protected]")
end
end

it 'returns status code 200' do
context 'generate random symbol' do
let(:params) do
{ "data" => { "type" => "providers",
"attributes" => {
"name" => "Admin",
"displayName" => "Admin",
"region" => "EMEA",
"systemEmail" => "[email protected]",
"country" => "GB" } } }
end

it 'creates a provider' do
post '/providers', params, headers

expect(last_response.status).to eq(200)
expect(json.dig('data', 'attributes', 'symbol')).to match(/\A[A-Z]{4}\Z/)
end
end

Expand Down

0 comments on commit c17e789

Please sign in to comment.