Skip to content

Commit

Permalink
Workflow Update
Browse files Browse the repository at this point in the history
  • Loading branch information
greeenboi committed Sep 30, 2024
1 parent b4436a0 commit 210fc4e
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 57 deletions.
54 changes: 33 additions & 21 deletions .github/workflows/rubyonrails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,45 @@ on:
jobs:
test:
runs-on: ubuntu-latest
env:
RAILS_ENV: test
MAILJET_DEFAULT_MAIL: ${{ secrets.MAILJET_DEFAULT_MAIL }}
MAILJET_DEFAULT_NAME: ${{ secrets.MAILJET_DEFAULT_NAME }}
MAILJET_API_KEY: ${{ secrets.MAILJET_API_KEY }}
MAILJET_SECRET_KEY: ${{ secrets.MAILJET_SECRET_KEY }}
steps:
- name: Checkout code
uses: actions/checkout@v4
# Add or replace dependency steps here

- name: Install Ruby and gems
uses: ruby/setup-ruby@78c01b705fd9d5ad960d432d3a0cfa341d50e410 # v1.179.1
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
# Add or replace test runners here

- name: Run tests
run: bin/rake
run: bundle exec rake spec

lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install Ruby and gems
uses: ruby/setup-ruby@78c01b705fd9d5ad960d432d3a0cfa341d50e410 # v1.179.1
- name: Upload test results
uses: actions/upload-artifact@v3
if: failure()
with:
bundler-cache: true
- name: Generate binstubs
run: bundle binstubs bundler-audit brakeman rubocop
# Add or replace any other lints here
- name: Security audit dependencies
run: bin/bundler-audit --update
- name: Security audit application code
run: bin/brakeman -q -w2
- name: Lint Ruby files
run: bin/rubocop --parallel
name: test-results
path: tmp/test-results
# lint:
# runs-on: ubuntu-latest
# steps:
# - name: Checkout code
# uses: actions/checkout@v4
# - name: Install Ruby and gems
# uses: ruby/setup-ruby@78c01b705fd9d5ad960d432d3a0cfa341d50e410 # v1.179.1
# with:
# bundler-cache: true
# - name: Generate binstubs
# run: bundle binstubs bundler-audit brakeman rubocop
# # Add or replace any other lints here
# - name: Security audit dependencies
# run: bin/bundler-audit --update
# - name: Security audit application code
# run: bin/brakeman -q -w2
# - name: Lint Ruby files
# run: bin/rubocop --parallel
12 changes: 12 additions & 0 deletions .idea/ruby-mailer.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ ruby "3.2.4"
gem 'dotenv-rails'

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"
Expand Down Expand Up @@ -47,3 +44,10 @@ group :development do
# gem "spring"
end

group :test do
gem 'webmock'
end

group :development, :test do
gem 'rspec-rails'
end
32 changes: 32 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,24 @@ GEM
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
base64 (0.2.0)
bigdecimal (3.1.8)
bootsnap (1.18.4)
msgpack (~> 1.2)
builder (3.3.0)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
crack (1.0.0)
bigdecimal
rexml
crass (1.0.6)
date (3.3.4)
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)
Expand All @@ -101,6 +107,7 @@ GEM
net-http
globalid (1.2.1)
activesupport (>= 6.1)
hashdiff (1.1.1)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
Expand Down Expand Up @@ -145,6 +152,7 @@ GEM
racc (~> 1.4)
psych (5.1.2)
stringio
public_suffix (6.0.1)
puma (6.4.3)
nio4r (~> 2.0)
racc (1.8.1)
Expand Down Expand Up @@ -192,6 +200,24 @@ GEM
psych (>= 4.0.0)
reline (0.5.10)
io-console (~> 0.5)
rexml (3.3.8)
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)
Expand All @@ -200,6 +226,10 @@ GEM
tzinfo-data (1.2024.2)
tzinfo (>= 1.0.0)
uri (0.13.1)
webmock (3.24.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
webrick (1.8.2)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
Expand All @@ -219,7 +249,9 @@ DEPENDENCIES
puma (>= 5.0)
rack-cors
rails (~> 7.1.4)
rspec-rails
tzinfo-data
webmock

RUBY VERSION
ruby 3.2.4p170
Expand Down
9 changes: 9 additions & 0 deletions lib/tasks/test.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# lib/tasks/test.rake

require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = '--require spec_helper'
end

task :default => :spec
77 changes: 44 additions & 33 deletions spec/controller/api/v1/emails_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,61 +1,72 @@
# spec/controllers/api/v1/emails_controller_spec.rb

require 'rails_helper'
require 'webmock/rspec'

RSpec.describe Api::V1::EmailsController, type: :controller do
describe 'POST #create' do
let(:valid_attributes) do
let(:valid_params) do
{
email: {
to: 'x9ahn.test@inbox.testmail.app',
to_name: 'x9ahn',
subject: 'Test Email',
text_content: 'This is a test email.',
html_content: '<p>This is a test email.</p>',
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')
]
text_content: 'This is a test email',
html_content: '<p>This is a test email</p>'
}
}
end

it 'sends an email' do
expect {
post :create, params: valid_attributes
}.to change { ActionMailer::Base.deliveries.count }.by(1)
let(:test_mail_response) do
{
"result" => "success",
"message" => nil,
"count" => 0,
"limit" => 10,
"offset" => 0,
"emails" => []
}
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')
before do
stub_request(:get, "https://api.testmail.app/api/json")
.with(query: hash_including({
"apikey" => "449ffa39-91f2-4732-af21-aab8db5eee38",
"namespace" => "x9ahn",
"pretty" => "true"
}))
.to_return(status: 200, body: test_mail_response.to_json, headers: { 'Content-Type' => 'application/json' })
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 <recipient1@example.com>', 'Recipient Two <recipient2@example.com>')
end
context 'when the email is sent successfully' do
before do
allow(Mailjet::Send).to receive(:create).and_return(double(success?: true))
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')
it 'returns a success response and checks test mail server' do
post :create, params: valid_params
expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)).to eq({ 'message' => 'Email sent successfully' })

# Verify that the test mail server was called
expect(WebMock).to have_requested(:get, "https://api.testmail.app/api/json")
.with(query: hash_including({
"apikey" => "449ffa39-91f2-4732-af21-aab8db5eee38",
"namespace" => "x9ahn",
"pretty" => "true"
}))
end
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"))
allow(Mailjet::Send).to receive(:create).and_return(double(success?: false))
end

it 'returns an error response' do
post :create, params: valid_attributes
post :create, params: valid_params
expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)).to include('error' => 'Failed to send email')
expect(JSON.parse(response.body)).to eq({ 'error' => 'Failed to send email' })
end
end
end
Expand Down

0 comments on commit 210fc4e

Please sign in to comment.