From bc04aca0bb77f98eb29ea451914df425c7d7a16f Mon Sep 17 00:00:00 2001 From: Alessandro Barbieri Date: Tue, 27 Nov 2018 19:32:43 +0100 Subject: [PATCH] Solve part of the BindaCMS/binda-api#31 issue --- app/models/binda/api_user.rb | 8 +++++++- spec/models/binda/api_user_spec.rb | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/models/binda/api_user.rb b/app/models/binda/api_user.rb index 937f365..7360b32 100644 --- a/app/models/binda/api_user.rb +++ b/app/models/binda/api_user.rb @@ -14,7 +14,13 @@ def self.create_api_user STDOUT.puts "What is the api user name? [api_user]" username = STDIN.gets.strip username = 'api_user' if username.blank? - ApiUser.create!( name: username ) + new_user = ApiUser.create!( name: username ) + puts "User \"#{username}\" successfully created." + ::Binda::Structure.all.each do |structure| + new_user.structures << structure + end + new_user.save! + puts "User \"#{username}\" has now granted access to all existing structures." end private diff --git a/spec/models/binda/api_user_spec.rb b/spec/models/binda/api_user_spec.rb index 39d02c6..2e51cfa 100644 --- a/spec/models/binda/api_user_spec.rb +++ b/spec/models/binda/api_user_spec.rb @@ -27,6 +27,15 @@ module Binda users = ::Binda::ApiUser.all expect(users.last.name).to eq('foo') end + + it "has a generator method to create users which are granted access to all existing structures" do + STDIN = StringIO.new("bar\n") + ::Binda::ApiUser.create_api_user + num_of_structures = Structure.all.count + users = ::Binda::ApiUser.all + expect(users.last.name).to eq('bar') + expect(users.last.reload.structures.count).to eq(num_of_structures) + end end end