<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>/dev/caffeine &#187; rails</title>
	<atom:link href="http://devcaffeine.com/tags/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://devcaffeine.com</link>
	<description>anything worth taking seriously is worth making fun of</description>
	<lastBuildDate>Thu, 24 Sep 2009 20:50:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Integration testing SSL with Cucumber</title>
		<link>http://devcaffeine.com/2009/02/16/integration-testing-ssl-with-cucumber/</link>
		<comments>http://devcaffeine.com/2009/02/16/integration-testing-ssl-with-cucumber/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 18:50:27 +0000</pubDate>
		<dc:creator>cflipse</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://devcaffeine.com/?p=81</guid>
		<description><![CDATA[Cucumber, for those who aren&#8217;t up to speed on the latest in testing hotness, is an integration testing framework for Ruby.  While it&#8217;s not strictly limited to integration testing Rails applications, that&#8217;s the context i use it in most of the time.
I saw a question on the rspec list recently asking about how you [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/aslakhellesoy/cucumber/wikis">Cucumber</a>, for those who aren&#8217;t up to speed on the latest in testing hotness, is an integration testing framework for Ruby.  While it&#8217;s not strictly limited to integration testing Rails applications, that&#8217;s the context i use it in most of the time.</p>
<p>I saw a question on the rspec list recently asking about how you test an SSL required page within cucumber, and I realized that I&#8217;ve had that, and even more cert-related chaos, figured out at $WORK, and that I should probably talk a little bit about it.</p>
<p>Simple scenario is that you&#8217;ve got some controller on your site that is supposed to only be accessible via https, and you want to make sure it behaves correctly in cucumber.  In your standard deployment scenario, mongrel never really sees anything about SSL; that&#8217;s all handled by Apache.  Apache will set some headers for you, which rails can check to determine if you&#8217;re using SSL.  Since you&#8217;re not testing with Apache at this testing level, you just need to set the relevant HTTP headers.</p>
<blockquote><p>
Given I am using HTTPS<br />
When I visit /payments<br />
Then I should see the payments page</p>
<p>Given I am not using HTTPS<br />
When I visit /payments<br />
Then I should see an SSL error page
</p></blockquote>
<p>If I&#8217;m using <a href="http://gitrdoc.com/brynary/webrat/tree/master/">webrat</a> (which is <i>highly</i> recommended here) this becomes as simple as setting a header value.  In recent versions of webrat, that boils down to:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Given <span style="color:#006600; font-weight:bold;">/</span>I am using HTTPS<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  header<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;HTTPS&quot;</span>, <span style="color:#996600;">&quot;on&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
Given <span style="color:#006600; font-weight:bold;">/</span>I am <span style="color:#9966CC; font-weight:bold;">not</span> using HTTPS<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#008000; font-style:italic;"># this space left intentionally blank</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>If you&#8217;re NOT using webrat, but still at the rails integration test level,  the call is a bit uglier</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  get <span style="color:#ff3333; font-weight:bold;">:show</span>, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1234</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:https</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'on'</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>The <code>get</code> method at the integration test level actually takes a third argument, which is the HTTP headers set on that request.  At the functional / controller example level, the same method <i>might</i> work &#8212; I don&#8217;t know, I havn&#8217;t actually tested this at that level, instead I&#8217;ve just stubbed the <code>https?</code> method as needed.</p>
<p>Now, for bonus points.  At $WORK, I exist in one of those ideal closed environments where x509 authentication actually stands a <i>chance</i> of working.  That is, everyone gets a client certificate, which they send to us with the HTTPS request.  We check against an external service, see that the cert is good, and viola, we can pre-populate with all sorts of juicy user information.  Pipe dream on the internet, but it works well on an intranet. (having worked with this stuff, it&#8217;s no surprise to me at <i>all</i> that x509 client certs havn&#8217;t even managed to catch on with <i>banks</i> much less the general public &#8230;)</p>
<p>Apache takes care of checking the certificate, making sure it&#8217;s valid and well formed, and that it&#8217;s not in any CRL, and that it&#8217;s trusted.  Makes my life easier, at the Rails level, we just have to check against the external service and we&#8217;re off to the races.  And, happily, apache will pass things on in the headers.</p>
<blockquote><p>
Given I use the client certificate for &#8220;Joe&#8221;<br />
Then when I visit /administration<br />
I should be let in
</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Given <span style="color:#006600; font-weight:bold;">/</span>I use the client certificate <span style="color:#9966CC; font-weight:bold;">for</span> <span style="color:#996600;">&quot;(.*)&quot;</span><span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>cert_user<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#008000; font-style:italic;">#lookup or generate stub certificate information</span>
  header<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;X_CLIENT_CERT&quot;</span>, certificate.<span style="color:#9900CC;">certificate</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  header<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;X_CLIENT_DN&quot;</span>, certificate.<span style="color:#9900CC;">dn</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  header<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;HTTPS&quot;</span>, <span style="color:#996600;">&quot;on&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  DirectoryService.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">stub</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:lookup</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">with</span><span style="color:#006600; font-weight:bold;">&#40;</span>certificate.<span style="color:#9900CC;">dn</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">and_return</span> certificate.<span style="color:#9900CC;">remote_xml</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I&#8217;m hand-waving around some of the stubbing that would have to go on here, as far as generating dummy information goes, but the basic idea should be sound.  In this case, I&#8217;m stubbing the entire <code>DiectoryService.lookup</code> method for a particular DN, which I set in the headers, and returning &#8220;remote_xml&#8221; which is a stand in for the XML document that the remote system returns.  This lets me cut out the remote system, which wouldn&#8217;t appreciate being hit for all sorts of trash data every time the tests run, while still exercising most of the relevant internal code.</p>
<p>The general idea of an integration test is to see all of the pieces of the system working together.  At this point, Cucumber is exercising everything in the stack below Apache. Since certificates and x509 client certificates are largely dealt with <i>by</i> apache, we have to stub those ins and outs to work through the rest of the system.  The system is otherwise left unstubbed, and we check those integration points during manual testing.</p>
]]></content:encoded>
			<wfw:commentRss>http://devcaffeine.com/2009/02/16/integration-testing-ssl-with-cucumber/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Spec a FormBuilder in RSpec</title>
		<link>http://devcaffeine.com/2009/02/06/spec-a-formbuilder-in-rspec/</link>
		<comments>http://devcaffeine.com/2009/02/06/spec-a-formbuilder-in-rspec/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 16:04:31 +0000</pubDate>
		<dc:creator>cflipse</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://devcaffeine.com/?p=62</guid>
		<description><![CDATA[yesterday, I started trying to work on a helper for generating a frequently-repeated calendar widget for the rails app at $WORK.  Being the good BDDer that I am, I launched off a new spec to test the form builder &#8212; and to attempt to backfill specs and cover up the evidence of my naughty [...]]]></description>
			<content:encoded><![CDATA[<p>yesterday, I started trying to work on a helper for generating a frequently-repeated calendar widget for the rails app at $WORK.  Being the good <abbr title="behavior driven development">BDD</abbr>er that I am, I launched off a new spec to test the form builder &#8212; and to attempt to backfill specs and cover up the evidence of my naughty non-testing past.</p>
<p>This is a form builder we use in a couple of heavily used forms on the site, so we &#8220;know&#8221; this works, aside from the new code I want to add.  But in moving in to backfill some specs, I discovered that, like many things relating to form builders in rails, it&#8217;s not the easiest thing to do, nor is it exactly well documented &#8230;</p>
<p>After a bit of digging, I finally managed to work out the right setup to get this going.  Documented below for posterity</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># spec/helper/random_form_builder_spec.rb</span>
  describe RandomFormBuilder <span style="color:#9966CC; font-weight:bold;">do</span>
    attr_reader <span style="color:#ff3333; font-weight:bold;">:builder</span>, <span style="color:#ff3333; font-weight:bold;">:helper</span>
    before <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#0066ff; font-weight:bold;">@helper</span> = <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActionView::Helpers::FormHelper</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@object</span> = stub_model<span style="color:#006600; font-weight:bold;">&#40;</span>Random<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@builder</span> = RandomFormBuilder.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:random</span>, <span style="color:#0066ff; font-weight:bold;">@object</span>, <span style="color:#0066ff; font-weight:bold;">@helper</span>, <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You can then spec as normal, calling methods on builder and verifying the result.</p>
<p>The tricky part is the constructor.</p>
<dl>
<dt>:random</dt>
<dd>This is the name of the object, by analog, the first argument of a  <tt>#form_for</tt></dd>
<dt>@object</dt>
<dd>The object you run with; where the form pulls field values from</dd>
<dt>@helper</dt>
<dd>this is the tricky one.  A form builder is really just a pass-through, decorating calls down to methods that live in FormHelper.  If you don&#8217;t have a template that defines the FormHelper methods, failures abound.</p>
<dt>{}</dt>
</dd>
<dd>Options for the FormBuilder.  Here, unused</dd>
<dt>nil</dt>
<dd>A proc</dd>
</dl>
]]></content:encoded>
			<wfw:commentRss>http://devcaffeine.com/2009/02/06/spec-a-formbuilder-in-rspec/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Oracle and rails 1.2.1</title>
		<link>http://devcaffeine.com/2007/02/08/rails-12-database-indexes/</link>
		<comments>http://devcaffeine.com/2007/02/08/rails-12-database-indexes/#comments</comments>
		<pubDate>Thu, 08 Feb 2007 23:09:53 +0000</pubDate>
		<dc:creator>cflipse</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[work]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://devcaffeine.com/2007/02/08/rails-12-database-indexes/</guid>
		<description><![CDATA[Grrr &#8230; So, in the course of updating to rails 1.2, it seems that the automagically generated names for database indexes were changed.  Went from &#8220;#{table}_#{columns}_index&#8221; to &#8220;index_#{table}_on_#{columns}&#8221; &#8230; an annoying little change that bumps me up against some sort of Oracle column name length ceiling.  And has the effect of probably breaking [...]]]></description>
			<content:encoded><![CDATA[<p>Grrr &#8230; So, in the course of updating to rails 1.2, it seems that the automagically generated names for database indexes were changed.  Went from &#8220;#{table}_#{columns}_index&#8221; to &#8220;index_#{table}_on_#{columns}&#8221; &#8230; an annoying little change that bumps me up against some sort of Oracle column name length ceiling.  And has the effect of probably breaking down migrations.</p>
<p>So, I&#8217;m gonna jump into the migrations and patch the existing add_index calls to specify a :name => value for all of the existing indexes, so that migrations will actually <i>work</i> and leave me with a database that matches the one in production &#8230;. </p>
<p>Figuring out why I suddenly couldn&#8217;t build my test database was not exactly the way I meant to spend the last hour &#8230;.</p>
<p><b>Update</b></p>
<p>Changeset 4768 is responsible for all this.  Apparently, to allow reverse parsing of the indexes from the index name.  Not alltogether a bad goal, but it still breaks down migrations &#8230;  (thanks to <a href=http://blog.twifkak.com/articles/2007/02/08/using-rails-migrations-add-remove_index-upgrading-to-1-2">Devin</a> for find this one)</p>
<p>And, I discovered another problem last night.  Seems that all of my text fields have a default value of &#8220;empy_clob()&#8221;  &#8230; not the function, but <i>that actual string</i>.  Sort of trashes one&#8217;s assumptions about unset fields.  Apparently (thanks Roy) this has been worked out in <a href="http://dev.rubyonrails.org/ticket/7344">ticket 7344</a> and already applied to Edge.  Some sort of thing that only applies to Oracle, because, of course, nobody tests Rails on Oracle, it&#8217;s all MySQL.  And I can&#8217;t really look into the patch attached to the ticket, because I keep getting Trac errors.  Shaping up to have been a lovely adventure &#8230;</p>
<p><b>update</b><br />
Finally got through to the site, and worked out a monkey patch to fix this.  For those of you who might be running with oracle, and can&#8217;t upgrade to Edge (gee, wonder why not) the patch is below.   This is a temporary thing, given that the ticket is closed, and will hopefully be fixed in 1.2.2 &#8230;<br />
[ruby]<br />
class ActiveRecord::ConnectionAdapters::OracleColumn<br />
  attr_writer :default<br />
end if defined? ActiveRecord::ConnectionAdapters::OracleColumn</p>
<p>class ActiveRecord::ConnectionAdapters::OracleAdapter<br />
  def quote(value, column = nil) #:nodoc:<br />
    if value &#038;&#038; column &#038;&#038; [:text, :binary].include?(column.type)<br />
      %Q{empty_#{ column.sql_type.downcase rescue &#8216;blob&#8217; }()}<br />
    else<br />
       super<br />
    end<br />
  end</p>
<p>  alias columns_orig columns<br />
  def columns(*a)<br />
    columns_orig(*a).each do |col|<br />
      col.default = nil if col.default =~ /^(null|empty_[bc]lob())$/i<br />
    end<br />
  end<br />
end if defined? ActiveRecord::ConnectionAdapters::OracleAdapter<br />
[/ruby]</p>
<p>This appears to do the trick &#8230; a dump of the test schema no longer defaults all of our text columns to &#8220;empty_clob()&#8221; and tests are again passing  &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://devcaffeine.com/2007/02/08/rails-12-database-indexes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Railsconf 2007</title>
		<link>http://devcaffeine.com/2007/02/08/railsconf-2007/</link>
		<comments>http://devcaffeine.com/2007/02/08/railsconf-2007/#comments</comments>
		<pubDate>Thu, 08 Feb 2007 17:07:49 +0000</pubDate>
		<dc:creator>cflipse</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[conferences]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://devcaffeine.com/2007/02/08/railsconf-2007/</guid>
		<description><![CDATA[I&#8217;m signed up for railsconf 2007, which runs in late May of 2007, up in Portland, Oregon.  Looks like I&#8217;m one of about 10 NoVaRUG members going.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m signed up for <a href="http://conferences.oreillynet.com/rails/">railsconf 2007</a>, which runs in late May of 2007, up in Portland, Oregon.  Looks like I&#8217;m one of about 10 <a href="http://novarug.org">NoVaRUG</a> members going.</p>
]]></content:encoded>
			<wfw:commentRss>http://devcaffeine.com/2007/02/08/railsconf-2007/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
