Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Free specs from seeding #927

Merged
merged 8 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ commands:
command: |
set -euxo pipefail
bundle config path .bundle
bundle exec rails db:setup
bundle exec rails db:create
bundle exec rspec --format progress --format RspecJunitFormatter -o ~/rspec/rspec.xml
yarn_tests:
description: "Run the Mocha test suite"
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/asks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ def new
def create
submission = SubmissionForm.build submission_params
if submission.save
EmailNewSubmission.run! submission: submission, user: current_user
EmailNewSubmission.run!(
submission: submission,
user: current_user,
system_setting: context.system_settings
organization: Organization.instance_owner
)
redirect_to thank_you_path, notice: 'Ask was successfully created.'
else
render_form(submission)
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/offers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ def new
def create
submission = SubmissionForm.build submission_params
if submission.save
EmailNewSubmission.run! submission: submission, user: current_user
EmailNewSubmission.run!(
submission: submission,
user: current_user,
system_setting: context.system_settings,
organization: Organization.instance_owner,
)
redirect_to thank_you_path, notice: 'Offer was successfully created.'
else
render_form(submission)
Expand Down
10 changes: 4 additions & 6 deletions app/controllers/public_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ def contributions
end

def landing_page
@json = {
landing_page_text_what: HtmlSanitizer.new(@system_setting.landing_page_text_what).sanitize,
landing_page_text_who: HtmlSanitizer.new(@system_setting.landing_page_text_who).sanitize,
landing_page_text_how: HtmlSanitizer.new(@system_setting.landing_page_text_how).sanitize,
organization_name: Organization.instance_owner.name,
}.to_json
@json = GenerateLandingPageJson.run!(
system_setting: context.system_settings,
organization: Organization.instance_owner,
)
end

def version
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ def create
@submission = Submission.new(submission_params)

if @submission.save
EmailNewSubmission.run! submission: @submission, user: current_user
EmailNewSubmission.run!(
submission: @submission,
user: current_user,
system_setting: context.system_settings,
organization: Organization.instance_owner,
)
redirect_to submissions_path, notice: 'Submission successfully created.'
else
set_form_dropdowns
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ def urgency_level_display(urgency_level_text)
urgency_level_text
end
end

def site_logo_url
# There should always be a current org, but being defensive here helps simplify tests
Organization.instance_owner&.logo_url.presence || asset_pack_path('media/images/logo.png')
Comment on lines +78 to +79
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I've tripped up on this many times, and this feels to me like a much better home for this conditional than anywhere else it has lived before

end
end
8 changes: 7 additions & 1 deletion app/interactions/email_new_submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

class EmailNewSubmission < ActiveInteraction::Base
object :submission
object :system_setting
object :organization
object :user, default: nil

def execute
email = SubmissionMailer.new_submission_confirmation_email(submission)
email = SubmissionMailer.new_submission_confirmation_email(
submission: submission,
system_setting: system_setting,
organization: organization,
)

status = Messenger.new(email, 'new_submission_confirmation_email').deliver_now

Expand Down
2 changes: 2 additions & 0 deletions app/interactions/email_peer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ class EmailPeer < ActiveInteraction::Base
string :peer_alias
string :message
object :contribution, class: Listing
object :organization
object :user

def execute
peer_to_peer_email = PeerToPeerMatchMailer.peer_to_peer_email(
contribution,
organization: organization,
peer_alias: peer_alias,
message: message,
)
Expand Down
21 changes: 21 additions & 0 deletions app/interactions/generate_landing_page_json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

class GenerateLandingPageJson < ActiveInteraction::Base
object :system_setting
object :organization

def execute
{
landing_page_text_what: sanitize(system_setting.landing_page_text_what),
landing_page_text_who: sanitize(system_setting.landing_page_text_who),
landing_page_text_how: sanitize(system_setting.landing_page_text_how),
organization_name: organization.name,
}.to_json
end

private

def sanitize string
HtmlSanitizer.new(string).sanitize
end
end
5 changes: 1 addition & 4 deletions app/javascript/components/NavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<b-navbar fixed-top transparent shadow>
<template slot="brand">
<b-navbar-item href="/">
<img :src="logoUrl || $options.defaultLogo" alt="mutual-aid-app logo" height="300px" />
<img :src="logoUrl" alt="mutual-aid-app logo" height="300px" />
</b-navbar-item>
</template>

Expand Down Expand Up @@ -36,7 +36,6 @@
</template>

<script>
import logo from 'images/logo.png'
import {DeleteButton} from 'components/forms'
import {FeedbackButton} from 'components/forms'

Expand All @@ -54,7 +53,5 @@ export default {
return this.visibleButtons.includes(button)
},
},

defaultLogo: logo,
}
</script>
16 changes: 12 additions & 4 deletions app/mailers/peer_to_peer_match_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
# frozen_string_literal: true

class PeerToPeerMatchMailer < ApplicationMailer
def peer_to_peer_email(contribution, peer_alias:, message:)
@peer_email_address = contribution.person.email
def peer_to_peer_email(contribution, peer_alias:, message:, organization:)
peer_email_address = contribution.person.email

email_subject = if contribution.ask?
"#{peer_alias} is offering to meet your ask"
elsif contribution.offer? # TODO: check if community resource when added
"#{peer_alias} would like to accept your offer"
end

mail(to: @peer_email_address, subject: email_subject) do |format|
format.html { render locals: { contribution: contribution, message: message } }
mail(to: peer_email_address, subject: email_subject) do |format|
format.html do
render locals: {
contribution: contribution,
message: message,
organization: organization,
peer_email_address: peer_email_address,
}
end
end
end
end
18 changes: 7 additions & 11 deletions app/mailers/submission_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,28 @@

# TODO: could do with specs
class SubmissionMailer < ApplicationMailer
def new_submission_confirmation_email(submission, system_setting=nil)
def new_submission_confirmation_email(submission:, system_setting:, organization:)
@submission = submission

instance_owner = Organization.instance_owner
@organization_name = instance_owner.name
@system_setting = system_setting || SystemSetting.first
@system_setting = system_setting
@organization_name = organization.name

@form_name = @submission.form_name
if @form_name.downcase.include?('ask')
@form_contact = instance_owner.ask_form_contact
@form_contact = organization.ask_form_contact
elsif @form_name.downcase.include?('offer')
@form_contact = instance_owner.offer_form_contact
@form_contact = organization.offer_form_contact
elsif @form_name.downcase.include?('community_resource')
@form_contact = instance_owner.community_resources_contact
@form_contact = organization.community_resources_contact
end

@person = @submission.person
@locale = @person.preferred_locale || 'en'

@system_settings = SystemSetting.current_settings

system_email = ENV['SYSTEM_EMAIL']
smtp_from_email = ENV['SMTP_FROM_EMAIL']
contact_email = @form_contact&.person&.email || "#{smtp_from_email}"
contact_name = @form_contact&.person&.name || @form_contact&.name || contact_email
contact_email_with_name = %("#{contact_name} (#{instance_owner.name})" <#{contact_email}>)
contact_email_with_name = %("#{contact_name} (#{organization.name})" <#{contact_email}>)
bcc_emails = [contact_email, smtp_from_email, system_email].uniq.join('; ')

@subject = "#{ENV["SYSTEM_APP_NAME"]} confirmation (" + @person.updated_at.to_date.to_s + ')'
Expand Down
2 changes: 1 addition & 1 deletion app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="row text-center login-screen" style="vertical-align:middle;">
<div class="col-md-6 col-md-offset-2 logo-image text-center">
<%= render 'shared/organization_logo' %>
<%= image_tag site_logo_url, width: 300, alt: 'Logo' %>
</div>
<div class="col-md-4 text-center">
<h2>Sign up</h2>
Expand Down
2 changes: 1 addition & 1 deletion app/views/devise/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="columns text-center login-screen" style="vertical-align:middle;">
<div class="column is-half logo-image text-center">
<%= render 'shared/organization_logo' %>
<%= image_tag site_logo_url, width: 300, alt: 'Logo' %>
</div>
<div class="column is-half text-center">
<div class="section">
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<script>
document.addEventListener('DOMContentLoaded', () => {
EntryPoints.navBar('#navBar', <%= raw({
logoUrl: Organization.instance_owner.present? ? Organization.instance_owner.logo_url : "",
logoUrl: site_logo_url,
visibleButtons: policy(:nav_bar).visible_buttons,
}.to_json) %>)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div>
Good news! You have a match through <%= Organization.instance_owner.name %>.
Good news! You have a match through <%= organization.name %>.
<br><br>
<%= contribution.person.name %> would like to connect with you about <%= contribution.tag_list.last %>, and they sent this message:
<br>
Expand Down
5 changes: 0 additions & 5 deletions app/views/shared/_organization_logo.html.erb

This file was deleted.

11 changes: 3 additions & 8 deletions doc/testing.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
## Running specs
Running `bin/test` will run ruby tests (rspec) and then the js tests (mocha) if all the rspec tests pass
Running `bin/test` will run ruby tests (rspec) and then the js tests (mocha via mochapack) if all the rspec tests pass

To run front end tests and back end tests individually:

* Vue.js front end tests: `yarn test` or `yarn test -w` (mocha and chai are included in the `package.json` )
* Rails front end and back end tests: `bin/rspec` (rspec is included in the Gemfile)

Note that we currently rely on seeding in some of our backend specs, so before running `rspec`, first seed the test database:
```
bin/rake db:seed RAILS_ENV=test
```
* Front end: `yarn test` or `yarn test -w` (to watch for changes and rerun)
* Back end: `bin/rspec` or `rerun bin/rspec spec/some/dir/or_spec.rb` (to watch for changes and rerun)

## Request specs
When writing rspec tests within the spec/request directory, you can use `Warden::Test:Helpers`
Expand Down
3 changes: 2 additions & 1 deletion spec/forms/community_resource_form_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
require 'rails_helper'

RSpec.describe CommunityResourceForm do
let!(:location_type) { create :location_type }
let(:params) {{
name: 'Free breakfast program',
description: 'Feed the people!',
publish_from: '1969-01-01',
location: {
city: 'Oakland',
location_type: LocationType.first.id,
location_type: location_type.id,
},
organization_attributes: {
name: 'Black Panther Party',
Expand Down
17 changes: 13 additions & 4 deletions spec/interactions/email_new_submission_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
require 'rails_helper'

RSpec.describe EmailNewSubmission do
let(:user) { create :user }
let(:submission) { create :submission }

subject(:interaction) { EmailNewSubmission.run! submission: submission, user: user }
let(:user) { create :user }
let(:submission) { create :submission }
let(:system_setting) { build :system_setting }
let(:organization) { build :organization }

subject(:interaction) do
EmailNewSubmission.run!(
submission: submission,
user: user,
system_setting: system_setting,
organization: organization,
)
end

let(:last_email) { ActionMailer::Base.deliveries.last }
let(:last_log) { CommunicationLog.last }
Expand Down
19 changes: 14 additions & 5 deletions spec/interactions/email_peer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
require 'rails_helper'

RSpec.describe EmailPeer do
let(:user) { create(:user, :with_person) }
let(:user) { create(:user, :with_person) }
let(:contribution) { create :listing }
let(:peer_alias) { "peer_alias" }
let(:message) { "contribution P2P message" }

subject(:interaction) { EmailPeer.run!(contribution: contribution, user: user, peer_alias: peer_alias, message: message) }
let(:organization) { build :organization }
let(:peer_alias) { "peer_alias" }
let(:message) { "contribution P2P message" }

subject(:interaction) do
EmailPeer.run!(
contribution: contribution,
organization: organization,
user: user,
peer_alias: peer_alias,
message: message
)
end

let(:last_email) { ActionMailer::Base.deliveries.last }
let(:last_log) { CommunicationLog.last }
Expand Down
27 changes: 27 additions & 0 deletions spec/interactions/generate_landing_page_json_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true
require 'rails_helper'

RSpec.describe GenerateLandingPageJson do
let(:system_setting) do
build :system_setting, {
landing_page_text_what: 'what?',
landing_page_text_who: 'who?',
landing_page_text_how: 'how?',
}
end

let(:organization) { build :organization, name: 'org_name' }

subject(:json) do
GenerateLandingPageJson.run! system_setting: system_setting, organization: organization
end

it 'pulls landing page text from system settings' do
expect(JSON.parse(json)).to include(
"landing_page_text_what" => 'what?',
"landing_page_text_who" => 'who?',
"landing_page_text_how" => 'how?',
)
end
end

3 changes: 2 additions & 1 deletion spec/requests/community_resources_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'rails_helper'

RSpec.describe '/community_resources', type: :request do
let!(:location_type) { create :location_type }
let(:community_resource) { create :community_resource }

let(:params) {{ community_resource: {
Expand All @@ -15,7 +16,7 @@
city: 'Kings Park',
state: 'NY',
zip: '11754',
location_type: 1,
location_type: location_type.id,
}
}}}

Expand Down
Loading