Skip to content
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
12 changes: 11 additions & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
engines:
brakeman:
enabled: true
exclude_paths:
# Excluding User Flows tools since these are not loaded
# except when explicitly called from the User Flow rake tasks
- 'lib/user_flow_exporter.rb'
- 'lib/rspec/formatters/user_flow_formatter.rb'
bundler-audit:
enabled: true
coffeelint:
Expand All @@ -19,6 +24,8 @@ engines:
- 'node_modules/**/*'
- 'db/schema.rb'
- 'app/forms/password_form.rb'
- 'lib/user_flow_exporter.rb'
- 'lib/rspec/formatters/user_flow_formatter.rb'
eslint:
enabled: true
fixme:
Expand All @@ -38,6 +45,8 @@ engines:
exclude_paths:
- 'spec/**/*'
- 'db/migrate/*'
- 'lib/user_flow_exporter.rb'
- 'lib/rspec/formatters/user_flow_formatter.rb'
rubocop:
enabled: true
scss-lint:
Expand All @@ -50,4 +59,5 @@ ratings:
- '**.rb'
- '**.go'
exclude_paths:
- 'lib/rspec/formatters/*'
- 'lib/user_flow_exporter.rb'
- 'lib/rspec/formatters/user_flow_formatter.rb'
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ AllCops:
- 'config/initializers/devise.rb'
- 'db/migrate/*'
- 'spec/services/pii/nist_encryption_spec.rb'
- 'lib/rspec/user_flow_formatter.rb'
- 'lib/user_flow_exporter.rb'
TargetRubyVersion: 2.3
UseCache: true

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ $ RAILS_ASSET_HOST=localhost:3000 rake spec:user_flows

Then, visit http://localhost:3000/user_flows in your browser!

##### Exporting

The user flows tool also has an export feature which allows you to export everything for the web. You may host these assets with someting like [`simplehttpserver`](https://www.npmjs.com/package/simplehttpserver) or publish to [Federalist](https://federalist.18f.gov/). To publish user flows for Federalist, first make sure the application is running locally (eg. localhost:3000) and run:

```
$ RAILS_ASSET_HOST=localhost:3000 FEDERALIST_PATH=/site/user/repository rake spec:user_flows:web
```

This will output your site to `public/site/user/repository` for quick publishing to [Federalist](https://federalist-docs.18f.gov/pages/using-federalist/). To test compatibility, run `simplehttpserver` from the app's `public` folder and visit `http://localhost:8000/<FEDERALIST PATH>/user_flows` in your browser.

### Load testing

We provide some [Locust.io] Python scripts you can run to test how the
Expand Down
6 changes: 4 additions & 2 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
config.action_mailer.asset_host = Figaro.env.mailer_domain_name
config.action_mailer.default_options = { from: Figaro.env.email_from }

config.assets.debug = true

if ENV.key?('RAILS_ASSET_HOST')
config.action_controller.asset_host = ENV['RAILS_ASSET_HOST']
else
config.action_controller.asset_host = '//'
end

config.assets.debug = true
config.assets.digest = true
config.assets.digest = ENV.key?('RAILS_DISABLE_ASSET_DIGEST') ? false : true

config.middleware.use RackSessionAccess::Middleware
config.lograge.enabled = true
config.lograge.custom_options = ->(event) { event.payload }
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/formatters/user_flow_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class UserFlowFormatter < RSpec::Core::Formatters::DocumentationFormatter
:stop

def initialize(output)
@html = '<html><head><link href="assets/application.css" rel="stylesheet"></head>' \
@html = '<html><head><link href="/assets/application.self.css" rel="stylesheet"></head>' \
'<body class="p2 container black">' \
'<h1 class="p2 my2 inline-block navy border border-dashed">' \
'<img class="mr1 align-middle" src="images/logo.svg" width="140">' \
'<img class="mr1 align-middle" src="/assets/logo.svg" width="140">' \
'<span class="inline-block">/ user flows</span></h1>' \
'<pre class="h5 line-height-2 sans-serif">'
@user_flows_html_file = Capybara.save_path.join('index.html').to_s
Expand Down
8 changes: 8 additions & 0 deletions lib/tasks/user_flows.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@ unless Rails.env.production?
--require ./lib/rspec/formatters/user_flow_formatter.rb
--format UserFlowFormatter]
end

desc 'Exports user flows for the web'
task 'user_flows:web' do |t|
ENV['RAILS_DISABLE_ASSET_DIGEST'] = 'true'
require './lib/user_flow_exporter'
Rake::Task['spec:user_flows'].invoke
UserFlowExporter.run
end
end
end
91 changes: 91 additions & 0 deletions lib/user_flow_exporter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# This module is part of the User Flows toolchest
#
# UserFlowExporter.run - scrapes user flows for use on the web
#
# Dependencies:
# - Must be running the application locally eg (localhost:3000)
# - Must have wget installed and available on your PATH
#
# Executing:
# Start the application with:
# $ make run
# Export flows with:
# $ RAILS_ASSET_HOST=localhost:3000 FEDERALIST_PATH=/site/user/repo rake spec:user_flows:web
# Use the files output to public/<FEDERALIST PATH> in a Github repo connected to Federalist
# $ cp -r ./public/site/user/repo ~/code/login-user-flows
# And commit the changes in the Federalist repo!

module UserFlowExporter
ASSET_HOST = ENV['RAILS_ASSET_HOST'] || 'localhost:3000'
# Coming soon: signal testing for different devices
# USER_AGENT = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092416 Firefox/3.0.3"
FEDERALIST_PATH = ENV['FEDERALIST_PATH'] || '/flows_export/'

def self.run
Kernel.puts "Preparing to scrape user flows...\n"
url = "http://#{ASSET_HOST}/user_flows/"
# The web-friendly flows are still output to the public directory
# in order to quickly test the content by visiting your locally
# hosted application (eg. localhost:3000/site/18f/identity-ux/user_flows)

if FEDERALIST_PATH[0] != '/'
raise 'Federalist path must start with a slash (eg. /site/18f/identity-ux)'
end

output_dir = "public#{FEDERALIST_PATH}"

# -r = recursively mirrors site
# -H = span hosts (e.g. include assets from other domains)
# -p = download all assets associated with the page
# --no-host-directories = removes domain prefix from output path
# -P = output prefix (a.k.a the directory to dump the assets)
# --domains = whitelist of domains to include when following links
scrape_cmd = "wget -r -H -p --no-host-directories " \
"-P '#{output_dir}' " \
"--domains 'localhost' " \
"'#{url}'"
system(scrape_cmd)

massage_html(output_dir)
massage_assets(output_dir)

Kernel.puts 'Done! The user flows are now prepared for use on the interwebs!'
end

private

def self.massage_html(dir)
Dir.glob("#{dir}/**/*.html") do |html|
File.open(html) do |f|
contents = File.read(f.path)
contents.gsub!("http://#{ASSET_HOST}/", "#{FEDERALIST_PATH}/")
contents.gsub!('.css?body=1', '.css')
contents.gsub!('.js?body=1', '.js')
contents.gsub!('href="/assets/', "href=\"#{FEDERALIST_PATH}/assets/")
contents.gsub!('src="/assets/', "src=\"#{FEDERALIST_PATH}/assets/")
contents.gsub!("href='/user_flows/", "href='#{FEDERALIST_PATH}/user_flows/")

contents.gsub!("<base href='#{ASSET_HOST}' />", "<base href='#{FEDERALIST_PATH}/' />")

File.open(f.path, "w") {|file| file.puts contents }
Kernel.puts "Updated #{f.path} references"
end
end
end

def self.massage_assets(dir)
Dir.glob("#{dir}/assets/**/**") do |file|
if file[-11..-1] == '.css?body=1'
new_filename = file.gsub('.css?body=1', '.css')
`mv #{file} #{new_filename}`
Kernel.puts "Moved #{file} to #{new_filename}"
end

if file[-10..-1] == '.js?body=1'
new_filename = file.gsub('.js?body=1', '.js')
`mv #{file} #{new_filename}`
Kernel.puts "Moved #{file} to #{new_filename}"
end
end
end
end
Loading