From ed397d44a1d85aeeb6d04faac4d48d3f0021a217 Mon Sep 17 00:00:00 2001 From: greeenboi Date: Mon, 30 Sep 2024 20:10:03 +0530 Subject: [PATCH] chore: updates to cors and api route --- .idea/material_theme_project_new.xml | 12 + .idea/misc.xml | 4 + .idea/modules.xml | 8 + .idea/ruby-mailer.iml | 247 ++++++++++++++++++ .rspec | 1 + Gemfile | 10 +- Gemfile.lock | 19 -- app/controllers/api/v1/emails_controller.rb | 2 +- config/environments/development.rb | 1 + config/initializers/cors.rb | 2 +- .../api/v1/emails_controller_spec.rb | 62 +++++ spec/fixtures/test_attachment1.pdf | Bin 0 -> 17 bytes spec/rails_helper.rb | 55 ++++ spec/spec_helper.rb | 94 +++++++ 14 files changed, 491 insertions(+), 26 deletions(-) create mode 100644 .idea/material_theme_project_new.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/ruby-mailer.iml create mode 100644 .rspec create mode 100644 spec/controller/api/v1/emails_controller_spec.rb create mode 100644 spec/fixtures/test_attachment1.pdf create mode 100644 spec/rails_helper.rb create mode 100644 spec/spec_helper.rb diff --git a/.idea/material_theme_project_new.xml b/.idea/material_theme_project_new.xml new file mode 100644 index 0000000..663b801 --- /dev/null +++ b/.idea/material_theme_project_new.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..de4910b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6731e38 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/ruby-mailer.iml b/.idea/ruby-mailer.iml new file mode 100644 index 0000000..0734107 --- /dev/null +++ b/.idea/ruby-mailer.iml @@ -0,0 +1,247 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$MODULE_DIR$/app + + + file://$MODULE_DIR$/app/assets + + + file://$MODULE_DIR$/app/channels + + + file://$MODULE_DIR$/app/controllers + + + file://$MODULE_DIR$/app/helpers + + + file://$MODULE_DIR$/app/mailers + + + file://$MODULE_DIR$/app/models + + + file://$MODULE_DIR$/app/views + + + file://$MODULE_DIR$/config + + + file://$MODULE_DIR$/config/cable.yml + + + file://$MODULE_DIR$/config/database.yml + + + file://$MODULE_DIR$/config/environment.rb + + + file://$MODULE_DIR$/config/environments + + + file://$MODULE_DIR$/config/initializers + + + file://$MODULE_DIR$/config/locales + + + file://$MODULE_DIR$/config/routes + + + file://$MODULE_DIR$/config/routes.rb + + + file://$MODULE_DIR$/config + + + file://$MODULE_DIR$/db + + + file://$MODULE_DIR$/db/migrate + + + file://$MODULE_DIR$/db/seeds.rb + + + file://$MODULE_DIR$/lib + + + file://$MODULE_DIR$/lib/assets + + + file://$MODULE_DIR$/lib/tasks + + + file://$MODULE_DIR$/lib/templates + + + file://$MODULE_DIR$/log/development.log + + + file://$MODULE_DIR$/public + + + file://$MODULE_DIR$/public/javascripts + + + file://$MODULE_DIR$/public/stylesheets + + + file://$MODULE_DIR$/tmp + + + file://$MODULE_DIR$/vendor + + + file://$MODULE_DIR$/vendor/assets + + + \ No newline at end of file diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/Gemfile b/Gemfile index 001003e..fe00ba9 100644 --- a/Gemfile +++ b/Gemfile @@ -2,12 +2,12 @@ source "https://rubygems.org" ruby "3.2.4" -gem 'dotenv-rails', groups: [:development, :test] +gem 'dotenv-rails' -group :development, :test do - gem 'rspec-rails' - gem 'mailjet' -end +gem 'mailjet' +# group :development, :test do +# gem 'rspec-rails' +# end # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem "rails", "~> 7.1.4" diff --git a/Gemfile.lock b/Gemfile.lock index eade8f2..c01f5fb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -87,7 +87,6 @@ GEM debug (1.9.2) irb (~> 1.10) reline (>= 0.3.8) - diff-lcs (1.5.1) dotenv (3.1.4) dotenv-rails (3.1.4) dotenv (= 3.1.4) @@ -191,23 +190,6 @@ GEM psych (>= 4.0.0) reline (0.5.10) io-console (~> 0.5) - rspec-core (3.13.1) - rspec-support (~> 3.13.0) - rspec-expectations (3.13.3) - diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.13.0) - rspec-mocks (3.13.1) - diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.13.0) - rspec-rails (7.0.1) - actionpack (>= 7.0) - activesupport (>= 7.0) - railties (>= 7.0) - rspec-core (~> 3.13) - rspec-expectations (~> 3.13) - rspec-mocks (~> 3.13) - rspec-support (~> 3.13) - rspec-support (3.13.1) stringio (3.1.1) thor (1.3.2) timeout (0.4.1) @@ -232,7 +214,6 @@ DEPENDENCIES puma (>= 5.0) rack-cors rails (~> 7.1.4) - rspec-rails tzinfo-data RUBY VERSION diff --git a/app/controllers/api/v1/emails_controller.rb b/app/controllers/api/v1/emails_controller.rb index 4519de9..19a0916 100644 --- a/app/controllers/api/v1/emails_controller.rb +++ b/app/controllers/api/v1/emails_controller.rb @@ -15,7 +15,7 @@ def send_email Mailjet::Send.create(messages: [{ 'From' => { 'Email' => ENV['MAILJET_DEFAULT_MAIL'], - 'Name' => 'Suvan GS' + 'Name' => ENV['MAILJET_DEFAULT_NAME'] }, 'To' => [ { diff --git a/config/environments/development.rb b/config/environments/development.rb index 38ec6a2..9853b8c 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -32,6 +32,7 @@ # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = false + config.action_mailer.delivery_method = :mailjet config.action_mailer.perform_caching = false diff --git a/config/initializers/cors.rb b/config/initializers/cors.rb index 23c14cc..4f9bb34 100644 --- a/config/initializers/cors.rb +++ b/config/initializers/cors.rb @@ -7,7 +7,7 @@ Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do - origins "localhost:3000" + origins "localhost:3000", "localhost:5500" resource "*", headers: :any, diff --git a/spec/controller/api/v1/emails_controller_spec.rb b/spec/controller/api/v1/emails_controller_spec.rb new file mode 100644 index 0000000..6033161 --- /dev/null +++ b/spec/controller/api/v1/emails_controller_spec.rb @@ -0,0 +1,62 @@ +# spec/controllers/api/v1/emails_controller_spec.rb +require 'rails_helper' + +RSpec.describe Api::V1::EmailsController, type: :controller do + describe 'POST #create' do + let(:valid_attributes) do + { + email: { + subject: 'Test Email', + text_content: 'This is a test email.', + html_content: '

This is a test email.

', + recipients: [ + { email: 'recipient1@example.com', name: 'Recipient One' }, + { email: 'recipient2@example.com', name: 'Recipient Two' } + ], + attachments: [ + fixture_file_upload(Rails.root.join('spec', 'fixtures', 'test_attachment1.pdf'), 'application/pdf'), + fixture_file_upload(Rails.root.join('spec', 'fixtures', 'test_attachment2.txt'), 'text/plain') + ] + } + } + end + + it 'sends an email' do + expect { + post :create, params: valid_attributes + }.to change { ActionMailer::Base.deliveries.count }.by(1) + end + + it 'returns a success response' do + post :create, params: valid_attributes + expect(response).to have_http_status(:ok) + expect(JSON.parse(response.body)).to include('message' => 'Email sent successfully') + end + + it 'sends to multiple recipients' do + post :create, params: valid_attributes + email = ActionMailer::Base.deliveries.last + expect(email.to).to contain_exactly('Recipient One ', 'Recipient Two ') + end + + it 'includes attachments' do + post :create, params: valid_attributes + email = ActionMailer::Base.deliveries.last + expect(email.attachments.size).to eq(2) + expect(email.attachments.first.filename).to eq('test_attachment1.pdf') + expect(email.attachments.last.filename).to eq('test_attachment2.txt') + end + + context 'when the email fails to send' do + before do + allow_any_instance_of(Mail::Message).to receive(:deliver_now!).and_raise(StandardError.new("Sending failed")) + end + + it 'returns an error response' do + post :create, params: valid_attributes + expect(response).to have_http_status(:unprocessable_entity) + expect(JSON.parse(response.body)).to include('error' => 'Failed to send email') + end + end + end +end \ No newline at end of file diff --git a/spec/fixtures/test_attachment1.pdf b/spec/fixtures/test_attachment1.pdf new file mode 100644 index 0000000000000000000000000000000000000000..507447ecfadf987077665e3e09af193f2b3afea4 GIT binary patch literal 17 YcmWGZEiO?AaB)*e&d)1J%`4#o05kgqzyJUM literal 0 HcmV?d00001 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..315d870 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,55 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +require 'spec_helper' +ENV['RAILS_ENV'] ||= 'test' +require_relative '../config/environment' +# Prevent database truncation if the environment is production +abort("The Rails environment is running in production mode!") if Rails.env.production? +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Rails.root.glob('spec/support/**/*.rb').sort_by(&:to_s).each { |f| require f } + +RSpec.configure do |config| + # Remove this line to enable support for ActiveRecord + config.use_active_record = false + + # If you enable ActiveRecord support you should uncomment these lines, + # note if you'd prefer not to run each example within a transaction, you + # should set use_transactional_fixtures to false. + # + # config.fixture_path = Rails.root.join('spec/fixtures') + # config.use_transactional_fixtures = true + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, type: :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://rspec.info/features/7-0/rspec-rails + config.infer_spec_type_from_file_location! + config.include ActionDispatch::TestProcess + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..327b58e --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,94 @@ +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/ + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end