From c17e789bccf625d54dc7d01b91d5664cecbb6047 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Thu, 8 Aug 2019 23:46:24 +0200 Subject: [PATCH] regenerate random symbol for provider. #335 --- Gemfile | 1 + Gemfile.lock | 7 +++++-- app/controllers/providers_controller.rb | 11 +++++++++-- app/models/concerns/helpable.rb | 4 ++++ config/routes.rb | 1 + spec/concerns/helpable_spec.rb | 6 ++++++ spec/requests/providers_spec.rb | 23 ++++++++++++++++------- 7 files changed, 42 insertions(+), 11 deletions(-) diff --git a/Gemfile b/Gemfile index e79a0a458..f706b082f 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 456cb8d10..8329c368b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/app/controllers/providers_controller.rb b/app/controllers/providers_controller.rb index 33fc2b08a..4036f96e8 100644 --- a/app/controllers/providers_controller.rb +++ b/app/controllers/providers_controller.rb @@ -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] @@ -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 @@ -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 diff --git a/app/models/concerns/helpable.rb b/app/models/concerns/helpable.rb index 621529c29..934360a7d 100644 --- a/app/models/concerns/helpable.rb +++ b/app/models/concerns/helpable.rb @@ -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? diff --git a/config/routes.rb b/config/routes.rb index c939678ef..d48f4100f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/spec/concerns/helpable_spec.rb b/spec/concerns/helpable_spec.rb index f778373b3..002ce0800 100644 --- a/spec/concerns/helpable_spec.rb +++ b/spec/concerns/helpable_spec.rb @@ -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" diff --git a/spec/requests/providers_spec.rb b/spec/requests/providers_spec.rb index e25fc5c4a..47424688b 100644 --- a/spec/requests/providers_spec.rb +++ b/spec/requests/providers_spec.rb @@ -473,13 +473,8 @@ it 'creates a provider' do post '/providers', params, headers - expect(json.dig('data', 'attributes', 'systemEmail')).to eq("doe@joe.joe") - 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("doe@joe.joe") end end @@ -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("doe@joe.joe") 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" => "doe@joe.joe", + "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