Skip to content

Commit

Permalink
Add tests for calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
parterburn committed Feb 12, 2024
1 parent 08d832b commit 4fa4a93
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 22 deletions.
7 changes: 4 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ group :development do
end

group :test do
gem 'capybara', '~> 3.35', '>= 3.35.3'
gem 'capybara'
gem 'database_cleaner'
gem 'email_spec', require: false
gem 'rspec', '~> 3.10'
gem 'selenium-webdriver'
gem "rspec-retry" # Automatically retry failed tests that timeout
gem 'webdrivers', '~> 5.0', require: false
gem "selenium-webdriver"
gem 'simplecov', require: false
gem 'webmock', require: false
end
Expand Down
33 changes: 19 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ GEM
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
addressable (2.8.5)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
autoprefixer-rails (9.7.6)
execjs
Expand All @@ -86,7 +86,7 @@ GEM
activesupport (>= 3.0.0)
uniform_notifier (~> 1.11)
byebug (8.2.5)
capybara (3.38.0)
capybara (3.39.2)
addressable
matrix
mini_mime (>= 0.1.3)
Expand All @@ -104,7 +104,6 @@ GEM
ssrf_filter (~> 1.0)
charlock_holmes (0.7.7)
chartkick (5.0.2)
childprocess (3.0.0)
chronic (0.10.2)
cloudflare-rails (3.0.0)
actionpack (>= 6.0, < 7.1.0)
Expand Down Expand Up @@ -307,7 +306,7 @@ GEM
racc (~> 1.4)
orm_adapter (0.5.0)
pg (1.5.3)
public_suffix (5.0.3)
public_suffix (5.0.4)
puma (6.3.0)
nio4r (~> 2.0)
racc (1.7.3)
Expand Down Expand Up @@ -358,7 +357,7 @@ GEM
rb-inotify (0.10.1)
ffi (~> 1.0)
redcarpet (3.6.0)
regexp_parser (2.1.1)
regexp_parser (2.9.0)
responders (3.1.0)
actionpack (>= 5.2)
railties (>= 5.2)
Expand All @@ -369,13 +368,10 @@ GEM
netrc (~> 0.8)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.6)
rinku (2.0.6)
rotp (6.3.0)
rouge (4.2.0)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.2)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
Expand All @@ -392,6 +388,8 @@ GEM
rspec-expectations (~> 3.10)
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-retry (0.6.2)
rspec-core (> 3.3)
rspec-support (3.10.2)
ruby-openai (5.0.0)
faraday (>= 1)
Expand All @@ -412,9 +410,10 @@ GEM
sprockets (> 3.0)
sprockets-rails
tilt
selenium-webdriver (3.142.7)
childprocess (>= 0.5, < 4.0)
rubyzip (>= 1.2.2)
selenium-webdriver (4.9.0)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
sentry-rails (5.16.1)
railties (>= 5.0)
sentry-ruby (~> 5.16.1)
Expand Down Expand Up @@ -463,10 +462,15 @@ GEM
uniform_notifier (1.16.0)
warden (1.2.9)
rack (>= 2.0.9)
webdrivers (5.3.1)
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
selenium-webdriver (~> 4.0, < 4.11)
webmock (3.8.3)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
websocket (1.2.10)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand All @@ -488,7 +492,7 @@ DEPENDENCIES
binding_of_caller
bullet
byebug
capybara (~> 3.35, >= 3.35.3)
capybara
carrierwave (~> 3)
charlock_holmes (~> 0.7.7)
chartkick (~> 5)
Expand Down Expand Up @@ -536,8 +540,8 @@ DEPENDENCIES
rest-client
reverse_markdown
rinku
rspec (~> 3.10)
rspec-rails
rspec-retry
ruby-openai
rubyzip (~> 2)
sass-rails
Expand All @@ -556,6 +560,7 @@ DEPENDENCIES
turnstile-captcha
twbs_sass_rails
uglifier
webdrivers (~> 5.0)
webmock
words_counted
zip-zip
Expand Down
2 changes: 1 addition & 1 deletion app/views/entries/_features.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
— reflections from your day is the most popular use case of any journal. This can be a scary one to get started with. If self-reflection is overwhelming to even think about, keep reading.
%li
%strong Memory keeper for major events/milestones
— we easily forget. Write it down now and the daily emails will bring you smiles when it reminds you of all your favorite moments. I started journaling when I first met my wife 5 years ago — I took 5 minutes to write about all of the dates we went on. Reading those past entries to each other has become a routine that deepens our relationship now.
— we easily forget. Write it down now and the daily emails will bring you smiles when it reminds you of all your favorite moments. I started journaling when I first met my wife #{distance_of_time_in_words(Date.parse("2011-09-01"), Date.today)} ago — I took 5 minutes to write about all of the dates we went on. Reading those past entries to each other has become a routine that deepens our relationship now.
%li
%strong Goals/Health Tracking
— use #hashtags in your posts for easily tagging entries with your different goals or visits to different doctors.
Expand Down
1 change: 1 addition & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@
# config.action_view.raise_on_missing_translations = true

config.action_mailer.default_url_options = { host: 'localhost' }
config.action_controller.asset_host = 'http://127.0.0.1:64515'
config.active_job.queue_adapter = :inline
end
14 changes: 14 additions & 0 deletions spec/features/entries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@
visit day_entry_url(year: entry.date.year + 1, month: entry.date.month, day: entry.date.day)
expect(page).to have_content 'Not authorized'
end

it 'should not show me other users entries' do
sign_in user
visit group_entries_url(group: not_my_entry.id)
expect(page).to have_content 'No entry found.'
expect(page).to_not have_content not_my_entry.body
end

it 'should show me the calendar with my entries', js: true do
sign_in paid_user
entry.update(date: Date.today, image: nil)
visit entries_calendar_path
expect(page).to have_content ActionController::Base.helpers.strip_tags(paid_entry.sanitized_body&.gsub(/\n/, ''))&.truncate(50, separator: ' ')
end
end

end
6 changes: 4 additions & 2 deletions spec/models/email_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@

describe "#process" do
it "creates an entry based on the email token" do
user.entries.destroy_all
email = FactoryBot.build(
:email,
to: [{ token: user.user_key, host: ENV['SMTP_DOMAIN'], email: "#{user.user_key}@#{ENV['SMTP_DOMAIN']}"}],
body: "I am great"
)

EmailProcessor.new(email).process
expect(user.entries.first.body).to eq("<p>I am great<br></p>")
expect(user.entries.reload.first.body).to eq("<p>I am great<br></p>")
end

it "creates an entry from email if the token is wrong but email matches a user" do
user.entries.destroy_all
email = FactoryBot.build(
:email,
from: ({ email: user.email }),
Expand All @@ -24,7 +26,7 @@
)

EmailProcessor.new(email).process
expect(user.entries.first.body).to eq("<p>I am great<br></p>")
expect(user.entries.reload.first.body).to eq("<p>I am great<br></p>")
end
end
end
4 changes: 4 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'webdrivers'

# Add additional requires below this line. Rails is not loaded until this point!

Expand Down Expand Up @@ -48,6 +49,9 @@

config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::IntegrationHelpers, type: :feature
config.include Capybara::DSL

Capybara.app_host = 'http://127.0.0.1:64515'

# Fail Focused Specs on CI
config.before focus: true do
Expand Down
33 changes: 33 additions & 0 deletions spec/support/config/capybara.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "capybara/rails"
require "capybara/rspec"
require "rack/utils"
require "byebug"

require "selenium/webdriver"

Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new app,
browser: :chrome
end

Capybara.register_driver :selenium_chrome_headless do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument("--window-size=1440,1024")
options.add_argument("--headless=new")
options.add_argument("--disable-gpu") # Temporarily needed if running on Windows.
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")

Capybara::Selenium::Driver.new(app,
browser: :chrome,
options: options
)
end

# ENABLE :chrome AS THE DRIVER BELOW FOR DEBUGGING
# Capybara.javascript_driver = :chrome
Capybara.javascript_driver = :selenium_chrome_headless

Capybara.app = Rack::ShowExceptions.new(Dabbleme::Application)
Capybara.server_port = 64515
Capybara.default_max_wait_time = 60
5 changes: 3 additions & 2 deletions spec/support/shared_contexts/has_all_objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
Payment.create(user_id: paid_user.id, amount: 3.0, comments: 'Monthly', date: Time.now - 800000)
end

let(:entry) { FactoryBot.create(:entry, body: Faker::Lorem.paragraph, user: user, date: DateTime.now.in_time_zone(user.send_timezone)) }
let(:not_my_entry) { FactoryBot.create(:entry, body: Faker::Lorem.paragraph) }
let!(:entry) { FactoryBot.create(:entry, body: Faker::Lorem.paragraph, user: user, date: DateTime.now.in_time_zone(user.send_timezone)) }
let!(:paid_entry) { FactoryBot.create(:entry, body: Faker::Lorem.paragraph, user: paid_user, date: DateTime.now.in_time_zone(user.send_timezone)) }
let!(:not_my_entry) { FactoryBot.create(:entry, body: Faker::Lorem.paragraph, user: free_ai) }
end

0 comments on commit 4fa4a93

Please sign in to comment.