Skip to content

Commit

Permalink
Merge 80ec80d into df71271
Browse files Browse the repository at this point in the history
  • Loading branch information
gburgett committed Aug 23, 2019
2 parents df71271 + 80ec80d commit 9c7b012
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 46 deletions.

This file was deleted.

@@ -1,8 +1,11 @@
# frozen_string_literal: true

class WCC::Contentful::App::ContactFormController < ApplicationController
include WCC::Contentful::App::PreviewPassword

def create
address = params[:recipient_email] || form_model.to_address(email_object_id: params[:email_object_id])
address =
form_model.to_address(email_object_id: params[:email_object_id])

form_model.send_email(
form_params.merge!(
Expand All @@ -21,7 +24,7 @@ def create
def form_model
raise ArgumentError, 'missing form ID' unless params[:id]

@form_model ||= WCC::Contentful::Model::SectionContactForm.find(params[:id])
@form_model ||= WCC::Contentful::Model::SectionContactForm.find(params[:id], preview: preview?)
end

def form_params
Expand Down
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class WCC::Contentful::App::PagesController < ApplicationController
include WCC::Contentful::App::PreviewPassword

helper ::WCC::Contentful::App::SectionHelper

def index
Expand All @@ -23,21 +25,6 @@ def show

private

def preview?
return super if defined?(super)

@preview ||=
if preview_password.present?
params[:preview]&.chomp == preview_password.chomp
else
false
end
end

def preview_password
WCC::Contentful::App.configuration.preview_password
end

def page_model
WCC::Contentful::Model.resolve_constant('page')
end
Expand Down
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module WCC::Contentful::App::PreviewPassword
def preview?
# check ApplicationController for a :preview? method
return super if defined?(super)

@preview ||=
if preview_password.present?
params[:preview]&.chomp == preview_password.chomp
else
false
end
end

def preview_password
WCC::Contentful::App.configuration.preview_password
end
end
Expand Up @@ -27,7 +27,7 @@ def email_address(entry)

def email_model(email_object_id)
raise ArgumentError, 'contentful entry does not exist' unless
entry = ::WCC::Contentful::Model.find(email_object_id)
entry = ::WCC::Contentful::Model.find(email_object_id, sys.context.to_h)

entry
end
Expand Down
Expand Up @@ -12,6 +12,8 @@
config.webhook_username = 'tester1'
config.webhook_password = 'password1'

config.preview_password = 'test-preview-pw'

# Optional
# config.management_token = # Contentful API management token
# config.content_delivery = # :direct, :eager_sync, or :lazy_sync
Expand Down
5 changes: 5 additions & 0 deletions wcc-contentful-app/spec/rails_helper.rb
Expand Up @@ -30,4 +30,9 @@
config.filter_rails_from_backtrace!

config.use_transactional_fixtures = true

config.before(:each) do
WCC::Contentful::Model.class_variable_get('@@registry').clear
Wisper.clear
end
end
@@ -0,0 +1,72 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe WCC::Contentful::App::ContactFormController, type: :request do
let(:mailer) {
double(deliver: nil)
}

let!(:form) {
contentful_stub('section-contact-form',
internal_title: 'Test')
}

before do
allow(::WCC::Contentful::App::ContactMailer).to receive(:contact_form_email)
.and_return(mailer)
end

it 'does not allow sending arbitrary email to :recipient_email' do
expect(::WCC::Contentful::App::ContactMailer).to_not receive(:contact_form_email)
.with('test@test.com', anything)

post '/contact_form', params: {
id: form.id,
recipient_email: 'test@test.com'
}
end

it 'sends email to person email address' do
person = double(id: 'TestPerson', email: 'test-person@test.com')
allow(::WCC::Contentful::Model).to receive(:find)
.with('TestPerson', anything)
.and_return(person)

expect(::WCC::Contentful::App::ContactMailer).to receive(:contact_form_email)
.with('test-person@test.com', anything)
.and_return(mailer)
expect(mailer).to receive(:deliver)

post '/contact_form', params: {
id: form.id,
email_object_id: 'TestPerson'
}
end

context 'preview: true' do
before do
expect(WCC::Contentful::Model::SectionContactForm)
.to_not receive(:find)
.with(form.id, hash_including(preview: false))
end

it 'looks up person in preview API' do
person = double(id: 'TestPerson', email: 'test-person@test.com')
allow(::WCC::Contentful::Model).to receive(:find)
.with('TestPerson', hash_including(preview: true))
.and_return(person)

expect(::WCC::Contentful::App::ContactMailer).to receive(:contact_form_email)
.with('test-person@test.com', anything)
.and_return(mailer)
expect(mailer).to receive(:deliver)

post '/contact_form', params: {
id: form.id,
email_object_id: 'TestPerson',
preview: WCC::Contentful::App.configuration.preview_password
}
end
end
end
Expand Up @@ -6,6 +6,10 @@ class MyPage < WCC::Contentful::Model::Page
end

RSpec.describe WCC::Contentful::App::PagesController, type: :request do
before do
MyPage.register_for_content_type('page')
end

it 'loads homepage off of site config' do
page = contentful_create('page', slug: '/')
_config = contentful_stub('siteConfig',
Expand All @@ -15,7 +19,8 @@ class MyPage < WCC::Contentful::Model::Page
get '/'

expect(response).to render_template('pages/show')
expect(assigns(:page)).to eq(page)
expect(assigns(:page)).to be_a(page.class)
expect(assigns(:page).raw).to eq(page.raw)
end

it 'loads the "/" page when no site config exists' do
Expand All @@ -26,7 +31,8 @@ class MyPage < WCC::Contentful::Model::Page
get '/'

expect(response).to render_template('pages/show')
expect(assigns(:page)).to eq(page)
expect(assigns(:page)).to be_a(page.class)
expect(assigns(:page).raw).to eq(page.raw)
end

it 'allows overloading the Page model' do
Expand All @@ -47,7 +53,8 @@ class MyPage < WCC::Contentful::Model::Page

get '/test'

expect(assigns(:page)).to eq(page)
expect(assigns(:page)).to be_a(page.class)
expect(assigns(:page).raw).to eq(page.raw)
end

it 'uses redirect when given' do
Expand Down Expand Up @@ -86,7 +93,8 @@ class MyPage < WCC::Contentful::Model::Page
get '/test', params: { preview: pw }
end

expect(assigns(:page)).to eq(page)
expect(assigns(:page)).to be_a(page.class)
expect(assigns(:page).raw).to eq(page.raw)
end

it 'uses preview in redirect as well' do
Expand Down
Expand Up @@ -12,20 +12,52 @@
)
}

after do
# clean out MyContactForm subclass
WCC::Contentful::Model.class_variable_get('@@registry').clear
end

Person = Struct.new(:id, :first_name, :last_name, :email)
let(:person) { Person.new('84', 'test', 'testerson', 'test@test.com') }

it 'defaults to notification email' do
expect(section_contact_form.to_address(email_object_id: nil)).to eq(section_contact_form.notification_email)
expect(section_contact_form.to_address(email_object_id: nil))
.to eq(section_contact_form.notification_email)
end

it 'returns person email if email_object_id provided' do
allow(WCC::Contentful::Model).to receive(:find).with(person.id)
allow(WCC::Contentful::Model).to receive(:find).with(person.id, anything)
.and_return(person)

expect(
section_contact_form.to_address(email_object_id: person.id)
).to eq(person.email)
end

it 'allows app to override recipient email with its own logic' do
class MyContactForm < WCC::Contentful::Model::SectionContactForm
private

def email_address(entry)
return entry.contact_email if entry && defined?(entry.contact_email)

super
end

def email_model(email_object_id, **options)
if email_object_id == 'test'
return OpenStruct.new(id: 'test', contact_email: 'test-opportunity@test.com')
end

super
end
end

my_contact_form = MyContactForm.new(section_contact_form.raw)

expect(
my_contact_form.to_address(email_object_id: 'test')
).to eq('test-opportunity@test.com')
end
end
end
21 changes: 15 additions & 6 deletions wcc-contentful/lib/wcc/contentful/rspec.rb
Expand Up @@ -16,18 +16,27 @@ def contentful_stub(content_type, **attrs)
const = WCC::Contentful::Model.resolve_constant(content_type.to_s)
instance = contentful_create(content_type, **attrs)

# mimic what's going on inside model_singleton_methods.rb
# find, find_by, etc always return a new instance from the same raw
allow(WCC::Contentful::Model).to receive(:find)
.with(instance.id)
.and_return(instance)
.with(instance.id) do
contentful_create(content_type, raw: instance.raw, **attrs)
end
allow(WCC::Contentful::Model).to receive(:find)
.with(instance.id, anything)
.and_return(instance)
.with(instance.id, anything) do |_id, options|

contentful_create(content_type, options, raw: instance.raw, **attrs)
end
allow(const).to receive(:find) { |id, options| WCC::Contentful::Model.find(id, options) }

attrs.each do |k, v|
allow(const).to receive(:find_by)
.with(hash_including(k => v))
.and_return(instance)
.with(hash_including(k => v)) do |filter|
filter = filter&.dup
options = filter&.delete(:options) || {}

contentful_create(content_type, options, raw: instance.raw, **attrs)
end
end

instance
Expand Down
22 changes: 13 additions & 9 deletions wcc-contentful/lib/wcc/contentful/test/factory.rb
Expand Up @@ -7,23 +7,27 @@ module WCC::Contentful::Test::Factory
# Builds a in-memory instance of the Contentful model for the given content_type.
# All attributes that are known to be required fields on the content type
# will return a default value based on the field type.
def contentful_create(content_type, **attrs)
def contentful_create(content_type, context = nil, **attrs)
const = WCC::Contentful::Model.resolve_constant(content_type.to_s)
attrs = attrs.transform_keys { |a| a.to_s.camelize(:lower) }

id = attrs.delete('id')
sys = attrs.delete('sys')
raw = attrs.delete('raw') || default_raw(const, id)
bad_attrs = attrs.reject { |a| const.content_type_definition.fields.key?(a) }
raise ArgumentError, "Attribute(s) do not exist on #{const}: #{bad_attrs.keys}" if bad_attrs.any?

instance = const.new(default_raw(const, id).tap do |raw|
attrs.each do |k, v|
field = const.content_type_definition.fields[k]
raw['sys'].merge!(sys) if sys

raw_value = v
raw_value = to_raw(v, field.type) if %i[Asset Link].include?(field.type)
raw['fields'][field.name][raw.dig('sys', 'locale')] = raw_value
end
end)
attrs.each do |k, v|
field = const.content_type_definition.fields[k]

raw_value = v
raw_value = to_raw(v, field.type) if %i[Asset Link].include?(field.type)
raw['fields'][field.name][raw.dig('sys', 'locale')] = raw_value
end

instance = const.new(raw, context)

attrs.each do |k, v|
field = const.content_type_definition.fields[k]
Expand Down

0 comments on commit 9c7b012

Please sign in to comment.