Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
farhatahmad committed Nov 1, 2023
1 parent 73ac67a commit cd75d6a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/migrations/external_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def decrypted_params

raise ActiveSupport::MessageEncryptor::InvalidMessage unless encrypted_params.is_a? String

crypt = ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base[0..31], cipher: 'aes-256-gcm', serializer: Marshal)
crypt = ActiveSupport::MessageEncryptor.new(Rails.application.secret_key_base[0..31], cipher: 'aes-256-gcm', serializer: Marshal)
decrypted_params = crypt.decrypt_and_verify(encrypted_params) || {}

raise ActiveSupport::MessageEncryptor::InvalidMessage unless decrypted_params.is_a? Hash
Expand Down
30 changes: 26 additions & 4 deletions spec/controllers/migrations/external_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@

describe 'because the ciphertext was not generated with the same configuration' do
it 'returns :bad_request without creating a role' do
key = Rails.application.secrets.secret_key_base[1..32]
key = Rails.application.secret_key_base[1..32]

encrypted_params = encrypt_params({ role: { name: 'CrazyRole', role_permissions: {} } }, key:, expires_in: 10.seconds)
expect { post :create_role, params: { v2: { encrypted_params: } } }.not_to change(Role, :count)
Expand Down Expand Up @@ -188,6 +188,28 @@
expect(response).to have_http_status(:created)
expect(user.password_digest).to be_present
end

it 'creates the user without a password if provider is not greenlight' do
tenant = create(:tenant)
role = create(:role, name: valid_user_role.name, provider: tenant.name)
valid_user_params[:provider] = tenant.name

encrypted_params = encrypt_params({ user: valid_user_params }, expires_in: 10.seconds)

expect_any_instance_of(described_class).to receive(:generate_secure_pwd).and_call_original
expect { post :create_user, params: { v2: { encrypted_params: } } }.to change(User, :count).from(0).to(1)
expect(ActionMailer::MailDeliveryJob).not_to have_been_enqueued

user = User.take
expect(user.name).to eq(valid_user_params[:name])
expect(user.email).to eq(valid_user_params[:email])
expect(user.language).to eq(valid_user_params[:language])
expect(user.role).to eq(role)
expect(user.session_token).to be_present
expect(user.provider).to eq(tenant.name)
expect(response).to have_http_status(:created)
expect(user.password_digest).not_to be_present
end
end

context 'when the provider does not exists' do
Expand Down Expand Up @@ -429,7 +451,7 @@

describe 'because the ciphertext was not generated with the same configuration' do
it 'returns :bad_request without creating a user' do
key = Rails.application.secrets.secret_key_base[1..32]
key = Rails.application.secret_key_base[1..32]

encrypted_params = encrypt_params({ user: valid_user_params }, key:, expires_in: 10.seconds)
expect_any_instance_of(described_class).not_to receive(:generate_secure_pwd)
Expand Down Expand Up @@ -547,7 +569,7 @@

describe 'because the ciphertext was not generated with the same configuration' do
it 'returns :bad_request without creating a room' do
key = Rails.application.secrets.secret_key_base[1..32]
key = Rails.application.secret_key_base[1..32]
encrypted_params = encrypt_params({ room: valid_room_params }, key:, expires_in: 10.seconds)
expect { post :create_room, params: { v2: { encrypted_params: } } }.not_to change(Room, :count)
expect(response).to have_http_status(:bad_request)
Expand Down Expand Up @@ -647,7 +669,7 @@
private

def encrypt_params(params, key: nil, expires_at: nil, expires_in: nil, purpose: nil)
key = Rails.application.secrets.secret_key_base[0..31] if key.nil?
key = Rails.application.secret_key_base[0..31] if key.nil?
crypt = ActiveSupport::MessageEncryptor.new(key, cipher: 'aes-256-gcm', serializer: Marshal)
crypt.encrypt_and_sign(params, expires_at:, expires_in:, purpose:)
end
Expand Down

0 comments on commit cd75d6a

Please sign in to comment.