pg_rails 7.5.4 → 7.5.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f12846cd393967f0aac07acebf37b6799a9e67983da3ee892311aac4fd8a93b
4
- data.tar.gz: dc6a9d65a1454b891de4bcd751f5f74f1b51a27d2fdc46c0baaf216db28a9306
3
+ metadata.gz: b62210d098125764e407b10038d44a3e3fe91a52f6eff700fd83555dded8d194
4
+ data.tar.gz: 4e9ac223dd652c00045af466a82af01ea07062f8968947286c304014ea3cb5ac
5
5
  SHA512:
6
- metadata.gz: a832616bb110d3662bc1b2f07991f4dc9015ba95a0e17584b749c36058cea9d87928ca7403cc3c46a62789f13abdaff3c827be6bce8d3468420861380bddff3b
7
- data.tar.gz: 3c103db1aa747b5fd09757c680505d203d06afad591b46ee1903d36037a35de5b2b02ef0475f15f4f8e222accfafb21a8f75a2932b83e93f7fa6c2a82eb763b7
6
+ metadata.gz: b4cb69cca012bf23c8e1229353cd8b346150bc3029b9e45e995ce2f8ccb74d375c4019ce50ca894aaa9ef7cb5036781dcec219fa8ce108b64264e786d7467cda
7
+ data.tar.gz: 074b884d3867fffc9a1835aa59ca9c69dc9b64c60042d49151bbc9b60bad72d12488964be429e4eb21626dcb251b6ae8ed4b61d58a40f62cc7d845a12118e72b
@@ -18,11 +18,17 @@ module PgEngine
18
18
  # raise ActsAsTenant::Errors::NoTenantSet
19
19
  # end
20
20
  # FIXME: if current_tenant.present? check it's not discarded
21
-
21
+ # FIXME: if session['current_user_account'] present? check
22
+ # user belongs to it, and if not, cleanup
22
23
  if Current.user.present?
23
24
  user_accounts = Current.user.user_accounts.kept
24
25
  if ActsAsTenant.current_tenant.present?
25
26
  unless user_accounts.exists?(account: ActsAsTenant.current_tenant)
27
+ pg_warn <<~WARN
28
+ #{Current.user.to_gid} not belongs to \
29
+ #{ActsAsTenant.current_tenant.to_gid}. Signed out
30
+ WARN
31
+
26
32
  sign_out(Current.user)
27
33
  throw :warden, scope: :user, message: :invalid
28
34
  end
@@ -32,8 +38,9 @@ module PgEngine
32
38
  account = if user_accounts.count == 1
33
39
  user_accounts.first.account
34
40
  elsif session['current_user_account'].present?
35
- UserAccount.where(id: session['current_user_account']).first&.account
41
+ user_accounts.where(id: session['current_user_account']).first&.account
36
42
  end
43
+
37
44
  set_current_tenant(account)
38
45
  end
39
46
  end
@@ -6,12 +6,14 @@ module PgEngine
6
6
 
7
7
  # :nocov:
8
8
  def login_as
9
+ # FIXME: make a POST route
9
10
  return head :bad_request unless dev_user_or_env?
10
11
 
11
12
  usuario = User.find(params[:id])
12
13
  if usuario.confirmed_at.present?
13
14
  sign_in(:user, usuario)
14
- redirect_to after_sign_in_path_for(usuario)
15
+ # redirect_to after_sign_in_path_for(usuario)
16
+ redirect_to users_root_path
15
17
  else
16
18
  go_back('No está confirmado')
17
19
  end
@@ -9,12 +9,20 @@ module Users
9
9
 
10
10
  layout 'pg_layout/centered'
11
11
 
12
+ around_action :set_without_tenant
13
+
14
+ def set_without_tenant(&)
15
+ ActsAsTenant.without_tenant(&)
16
+ end
17
+
12
18
  def list
13
- @user_accounts = Current.user.user_accounts
19
+ @user_accounts = Current.user.user_accounts.kept
14
20
  end
15
21
 
16
22
  def switch
17
- user_account = UserAccount.find(params[:user_account_id])
23
+ # FIXME: handle not found
24
+ scope = Current.user.user_accounts.kept
25
+ user_account = scope.find(UserAccount.decode_id(params[:user_account_id]))
18
26
  session['current_user_account'] = user_account.id
19
27
  redirect_to root_path
20
28
  end
@@ -7,15 +7,19 @@ module Users
7
7
  def create
8
8
  build_resource(sign_up_params)
9
9
 
10
- resource.save
11
- yield resource if block_given?
12
- if resource.persisted?
13
- expire_data_after_sign_in!
14
- render_message
15
- else
16
- clean_up_passwords resource
17
- set_minimum_password_length
18
- respond_with resource
10
+ ActiveRecord::Base.transaction do
11
+ resource.save
12
+ yield resource if block_given?
13
+ if resource.persisted?
14
+ create_account_for(resource)
15
+
16
+ expire_data_after_sign_in!
17
+ render_message
18
+ else
19
+ clean_up_passwords resource
20
+ set_minimum_password_length
21
+ respond_with resource
22
+ end
19
23
  end
20
24
  end
21
25
 
@@ -27,5 +31,12 @@ module Users
27
31
  HTML
28
32
  render turbo_stream: turbo_stream.update('form-signup', msg)
29
33
  end
34
+
35
+ private
36
+
37
+ def create_account_for(user)
38
+ account = Account.create!(nombre: user.email)
39
+ user.user_accounts.create!(account:)
40
+ end
30
41
  end
31
42
  end
@@ -38,6 +38,10 @@ class Account < ApplicationRecord
38
38
  parent.table[:nombre]
39
39
  end
40
40
 
41
+ before_validation do
42
+ self.plan = 0 if plan.blank?
43
+ end
44
+
41
45
  def to_s
42
46
  nombre
43
47
  end
@@ -41,17 +41,7 @@ class User < ApplicationRecord
41
41
  # Hace falta?
42
42
  has_many :accounts, through: :user_accounts
43
43
 
44
- # default_scope lambda {
45
- # if ActsAsTenant.current_tenant.present?
46
- # joins(:user_accounts).where('user_accounts.account_id': ActsAsTenant.current_tenant.id)
47
- # else
48
- # raise ActsAsTenant::Errors::NoTenantSet unless Current.user.blank? || ActsAsTenant.unscoped?
49
-
50
- # # Aún no autenticó devise o es el admin
51
- # all
52
-
53
- # end
54
- # }
44
+ acts_as_tenant :account, through: :user_accounts
55
45
 
56
46
  has_many :notifications, as: :recipient, class_name: 'Noticed::Notification'
57
47
 
@@ -80,26 +70,6 @@ class User < ApplicationRecord
80
70
  kept? ? super : :locked
81
71
  end
82
72
 
83
- after_create do
84
- create_account unless orphan
85
- end
86
-
87
- def create_account
88
- account =
89
- if ActsAsTenant.current_tenant.present?
90
- # :nocov:
91
- raise PgEngine::Error, 'user not invited' unless Rails.env.test?
92
- # :nocov:
93
-
94
- ActsAsTenant.current_tenant
95
- else
96
- Account.create(nombre: email, plan: 0)
97
- end
98
- ua = user_accounts.create(account:)
99
-
100
- raise(Error, 'no se pudo crear la cuenta') unless ua.persisted?
101
- end
102
-
103
73
  def password_required?
104
74
  !persisted? || !password.nil? || !password_confirmation.nil?
105
75
  end
@@ -26,13 +26,14 @@ class UserAccount < ApplicationRecord
26
26
  audited
27
27
  include Hashid::Rails
28
28
 
29
- belongs_to :user
30
- belongs_to :account
29
+ belongs_to :user, inverse_of: :user_accounts
30
+ acts_as_tenant :account
31
31
 
32
32
  belongs_to :creado_por, optional: true, class_name: 'User'
33
33
  belongs_to :actualizado_por, optional: true, class_name: 'User'
34
34
 
35
35
  # scope :kept, -> { undiscarded.joins(:account).merge(Account.kept) }
36
+ # FIXME: merge with User.kept
36
37
  scope :kept, -> { joins(:account).merge(Account.kept) }
37
38
 
38
39
  enumerize :profiles, in: {
@@ -22,7 +22,7 @@ class UserPolicy < ApplicationPolicy
22
22
  user.developer? || user == record
23
23
  end
24
24
 
25
- # def base_access_to_collection?
26
- # user&.present?
27
- # end
25
+ def base_access_to_collection?
26
+ user.present?
27
+ end
28
28
  end
@@ -6,8 +6,10 @@ div style="max-width: 30em"
6
6
  table.table.table-sm
7
7
  tr
8
8
  th Nombre
9
+ th Email
9
10
  th Roles
10
- - @account.user_accounts.each do |user_account|
11
+ - @account.user_accounts.kept.each do |user_account|
11
12
  tr
12
13
  td = user_account.user
14
+ td = user_account.user.email
13
15
  td = user_account.profiles.texts.join(', ')
@@ -1,6 +1,6 @@
1
1
  ActsAsTenant.configure do |config|
2
- config.require_tenant = lambda do
3
- Current.namespace == :users
2
+ config.require_tenant = lambda do |options|
3
+ Current.namespace.present? || !options[:model].in?([User, UserAccount, Email, EmailLog])
4
4
  end
5
5
 
6
6
  # Customize the query for loading the tenant in background jobs
@@ -92,9 +92,9 @@ es:
92
92
  new:
93
93
  sign_up: Crear una cuenta
94
94
  passwords:
95
- send_instructions: Vas a recibir un correo con instrucciones sobre cómo resetear tu contraseña en unos pocos minutos.
95
+ send_instructions: Vas a recibir un correo con instrucciones para restablecer tu contraseña en unos pocos minutos.
96
96
  new:
97
- send_me_reset_password_instructions: Enviar instrucciones para restablecer contraseña
97
+ send_me_reset_password_instructions: Restablecer contraseña
98
98
  forgot_your_password: ¿Olvidaste tu contraseña?
99
99
  shared:
100
100
  links:
@@ -38,7 +38,7 @@ RSpec.describe Admin::AccountsController do
38
38
 
39
39
  let(:invalid_attributes) do
40
40
  {
41
- plan: nil
41
+ nombre: nil
42
42
  }
43
43
  end
44
44
 
@@ -96,20 +96,24 @@ RSpec.describe Admin::UsersController do
96
96
 
97
97
  describe 'POST #create' do
98
98
  context 'with valid params' do
99
+ subject do
100
+ post :create, params: { user: valid_attributes }
101
+ end
102
+
99
103
  it 'creates a new User' do
100
- expect do
101
- post :create, params: { user: valid_attributes }
102
- end.to change(User.unscoped, :count).by(1)
104
+ expect { subject }.to change(User.unscoped, :count).by(1)
103
105
  end
104
106
 
105
107
  it 'dont creates a new account' do
106
- expect do
107
- post :create, params: { user: valid_attributes }
108
- end.not_to change(Account.unscoped, :count)
108
+ expect { subject }.not_to change(Account.unscoped, :count)
109
+ end
110
+
111
+ it 'dont creates a new user account' do
112
+ expect { subject }.not_to change(UserAccount.unscoped, :count)
109
113
  end
110
114
 
111
115
  it 'redirects to the created user' do
112
- post :create, params: { user: valid_attributes }
116
+ subject
113
117
  expect(response).to redirect_to([:admin, User.unscoped.last])
114
118
  end
115
119
  end
Binary file
@@ -15,52 +15,34 @@ RSpec.describe User do
15
15
  expect(user.default_account).to be_present
16
16
  end
17
17
 
18
- context 'si es orphan' do
19
- let(:user) { create(:user, orphan: true) }
20
-
21
- it do
22
- expect(user.accounts).to be_empty
23
- end
24
-
25
- it do
26
- expect { user.default_account }.to raise_error(User::Error)
27
- end
28
- end
29
-
30
- context 'Si falla la creación de cuenta, que rollbackee la transaction de create user' do
31
- # rubocop:disable Lint/SuppressedException
32
- subject do
33
- user.save
34
- rescue User::Error
35
- end
36
- # rubocop:enable Lint/SuppressedException
37
-
38
- let(:user) do
39
- build(:user)
40
- end
41
-
42
- before do
43
- # rubocop:disable RSpec/MessageChain
44
- allow(user).to receive_message_chain(:user_accounts, :create) {
45
- instance_double(UserAccount, persisted?: false)
46
- }
47
- # rubocop:enable RSpec/MessageChain
48
- end
49
-
50
- it do
51
- expect { subject }.not_to change(described_class, :count)
52
- end
53
-
54
- it do
55
- subject
56
- expect(user).not_to be_persisted
57
- end
58
- end
59
-
60
18
  describe 'search ransacker' do
61
19
  it 'searchs' do
62
20
  results = described_class.ransack(search_cont: user.nombre).result.to_a
63
21
  expect(results).to eq [user]
64
22
  end
65
23
  end
24
+
25
+ describe 'default scope' do
26
+ before do
27
+ ActsAsTenant.current_tenant = nil
28
+ ActsAsTenant.test_tenant = nil
29
+ Current.reset
30
+ end
31
+
32
+ it 'scopes according to tenant' do
33
+ account = create(:account)
34
+ other_account = create(:account)
35
+ usr1 = usr2 = usr3 = usr4 = nil
36
+ ActsAsTenant.with_tenant(other_account) do
37
+ usr3 = create(:user)
38
+ usr4 = create(:user)
39
+ end
40
+ ActsAsTenant.with_tenant(account) do
41
+ usr1 = create(:user)
42
+ usr2 = create(:user)
43
+ expect(described_class.all).to contain_exactly(usr1, usr2)
44
+ end
45
+ expect(described_class.all).to contain_exactly(usr1, usr2, usr3, usr4)
46
+ end
47
+ end
66
48
  end
@@ -1,16 +1,8 @@
1
1
  require 'rails_helper'
2
2
 
3
- describe Users::RegistrationsController do
4
- render_views
5
-
6
- before do
7
- # rubocop:disable RSpec/InstanceVariable
8
- @request.env['devise.mapping'] = Devise.mappings[:user]
9
- # rubocop:enable RSpec/InstanceVariable
10
- end
11
-
3
+ describe 'registrations controller' do
12
4
  describe '#new' do
13
- subject { get :new }
5
+ subject { get '/users/sign_up' }
14
6
 
15
7
  it do
16
8
  subject
@@ -20,7 +12,12 @@ describe Users::RegistrationsController do
20
12
 
21
13
  describe '#create' do
22
14
  subject do
23
- post :create, params: { user: { nombre:, apellido:, email:, password:, password_confirmation: } }
15
+ post '/users', params: { user: { nombre:, apellido:, email:, password:, password_confirmation: } }
16
+ end
17
+
18
+ before do
19
+ ActsAsTenant.current_tenant = nil
20
+ ActsAsTenant.test_tenant = nil
24
21
  end
25
22
 
26
23
  let(:nombre) { Faker::Name.first_name }
@@ -33,6 +30,14 @@ describe Users::RegistrationsController do
33
30
  expect { subject }.to change(User, :count).by(1)
34
31
  end
35
32
 
33
+ it do
34
+ expect { subject }.to change(UserAccount, :count).by(1)
35
+ end
36
+
37
+ it do
38
+ expect { subject }.to change(Account, :count).by(1)
39
+ end
40
+
36
41
  it do
37
42
  subject
38
43
  expect(response.body).to include I18n.t('devise.registrations.signed_up_but_unconfirmed')
@@ -46,10 +51,28 @@ describe Users::RegistrationsController do
46
51
  expect(response).not_to be_successful
47
52
  end
48
53
  end
54
+
55
+ context 'cuando falla la creación de la UserAccount' do
56
+ before do
57
+ allow_any_instance_of(Users::RegistrationsController).to receive(:create_account_for).and_raise(StandardError)
58
+ end
59
+
60
+ it do
61
+ expect { subject }.not_to change(User, :count)
62
+ end
63
+
64
+ it do
65
+ expect { subject }.not_to change(UserAccount, :count)
66
+ end
67
+
68
+ it do
69
+ expect { subject }.not_to change(Account, :count)
70
+ end
71
+ end
49
72
  end
50
73
 
51
74
  describe '#edit' do
52
- subject { get :edit }
75
+ subject { get '/users/edit' }
53
76
 
54
77
  let(:logger_user) { create :user, :developer }
55
78
 
@@ -38,11 +38,17 @@ describe 'redirection' do
38
38
 
39
39
  context 'when has been removed from account' do
40
40
  let!(:other_account) { create :account }
41
- let!(:other_user_account) { logged_user.user_accounts.create(account: other_account) }
41
+ let!(:other_user_account) do
42
+ ActsAsTenant.with_tenant(other_account) do
43
+ logged_user.user_accounts.create!
44
+ end
45
+ end
42
46
 
43
47
  before do
44
48
  third_account = create :account
45
- logged_user.user_accounts.create(account: third_account)
49
+ ActsAsTenant.with_tenant(third_account) do
50
+ logged_user.user_accounts.create!
51
+ end
46
52
  end
47
53
 
48
54
  it 'redirects to switcher' do
@@ -29,7 +29,9 @@ describe 'Tenants' do
29
29
  let(:other_account) { create :account }
30
30
 
31
31
  before do
32
- logged_user.user_accounts.create!(account: other_account)
32
+ ActsAsTenant.with_tenant(other_account) do
33
+ logged_user.user_accounts.create!
34
+ end
33
35
  end
34
36
 
35
37
  it 'shows the switcher' do
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgRails
4
- VERSION = '7.5.4'
4
+ VERSION = '7.5.6'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.5.4
4
+ version: 7.5.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martín Rosso
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-14 00:00:00.000000000 Z
11
+ date: 2024-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -849,7 +849,6 @@ files:
849
849
  - pg_engine/spec/controllers/public/mensaje_contactos_controller_spec.rb
850
850
  - pg_engine/spec/controllers/public/webhooks_controller_spec.rb
851
851
  - pg_engine/spec/controllers/users/confirmations_controller_spec.rb
852
- - pg_engine/spec/controllers/users/registrations_controller_spec.rb
853
852
  - pg_engine/spec/decorators/email_decorator_spec.rb
854
853
  - pg_engine/spec/factories/accounts.rb
855
854
  - pg_engine/spec/factories/email_logs.rb
@@ -883,6 +882,7 @@ files:
883
882
  - pg_engine/spec/requests/users/accounts_spec.rb
884
883
  - pg_engine/spec/requests/users/base_controller_spec.rb
885
884
  - pg_engine/spec/requests/users/date_jumper_spec.rb
885
+ - pg_engine/spec/requests/users/registrations_spec.rb
886
886
  - pg_engine/spec/requests/users/switcher_spec.rb
887
887
  - pg_engine/spec/system/alerts_spec.rb
888
888
  - pg_engine/spec/system/breadcrumbs_spec.rb