<?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; cucumber</title>
	<atom:link href="http://devcaffeine.com/tags/cucumber/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>Specify a relative time in Cucumber</title>
		<link>http://devcaffeine.com/2009/08/17/relative-time-in-cucumber/</link>
		<comments>http://devcaffeine.com/2009/08/17/relative-time-in-cucumber/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 17:52:59 +0000</pubDate>
		<dc:creator>cflipse</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://devcaffeine.com/?p=96</guid>
		<description><![CDATA[Prepping for working on a feature at $work that involves limiting documents by relative time, I hashed out the following steps.  Posting here in case they prove useful for anyone.
I want to be able to specify an arbitrary time, in english, relative to the current time, in my Cucumber steps.  For example,

Scenario: Only [...]]]></description>
			<content:encoded><![CDATA[<p>Prepping for working on a feature at $work that involves limiting documents by relative time, I hashed out the following steps.  Posting here in case they prove useful for anyone.</p>
<p>I want to be able to specify an arbitrary time, in english, relative to the current time, in my Cucumber steps.  For example,</p>

<div class="wp_syntax"><div class="code"><pre class="cucumber" style="font-family:monospace;">Scenario: Only posts from the last two months are shown
  Given the following posts:
    | title           | posted_at           |
    | recent post     | 2 days ago          |
    | recent post 2   | yesterday           |
    | older post      | 2 months ago        |
    | really old post | 2 months, 1 day ago |
    | tomorrow's post | 1 day from now      |
  When I go to the posts page
  Then I should see 3 posts</pre></div></div>

<p>The desire, there, is to be able to specify a date relative to the current time, in plain english.  The tricky bit is being able to chain them together, as in <code>1 year, 3 months, 2 days ago</code></p>
<p>I got this worked out by stringing a few regular expressions together in my step definition helper.  It depends on the 2.days.ago type dsl in ActiveSupport.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Given <span style="color:#006600; font-weight:bold;">/</span>^the following posts?:?<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>table<span style="color:#006600; font-weight:bold;">|</span>
  table.<span style="color:#9900CC;">map_column</span>!<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'posted_at'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>date<span style="color:#006600; font-weight:bold;">|</span> interpret_time<span style="color:#006600; font-weight:bold;">&#40;</span>date<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  table.<span style="color:#9900CC;">hashes</span>.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>hash<span style="color:#006600; font-weight:bold;">|</span> Factory <span style="color:#ff3333; font-weight:bold;">:post</span>, hash<span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> interpret_time<span style="color:#006600; font-weight:bold;">&#40;</span>time<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> time.<span style="color:#9900CC;">blank</span>?
  <span style="color:#0000FF; font-weight:bold;">return</span> time <span style="color:#9966CC; font-weight:bold;">if</span> time.<span style="color:#9900CC;">kind_of</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Time</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">or</span> time.<span style="color:#9900CC;">kind_of</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Date</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  named_time<span style="color:#006600; font-weight:bold;">&#40;</span>time<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> time_by_regex<span style="color:#006600; font-weight:bold;">&#40;</span>time<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>time<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> named_time<span style="color:#006600; font-weight:bold;">&#40;</span>time<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">case</span> time.<span style="color:#9900CC;">downcase</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;today&quot;</span> : <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">today</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;now&quot;</span> : <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
  <span style="color:#008000; font-style:italic;"># etc ...</span>
  <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> time_by_regex<span style="color:#006600; font-weight:bold;">&#40;</span>time<span style="color:#006600; font-weight:bold;">&#41;</span>
  directional_regex = <span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>ago<span style="color:#006600; font-weight:bold;">|</span>from now<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span>i
  time_regex = <span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>minute<span style="color:#006600; font-weight:bold;">|</span>hour<span style="color:#006600; font-weight:bold;">|</span>day<span style="color:#006600; font-weight:bold;">|</span>week<span style="color:#006600; font-weight:bold;">|</span>month<span style="color:#006600; font-weight:bold;">|</span>year<span style="color:#006600; font-weight:bold;">&#41;</span>s?<span style="color:#006600; font-weight:bold;">/</span>i
&nbsp;
  direction = time.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span>directional_regex<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">flatten</span>
  shifts = time.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span>time_regex<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> direction.<span style="color:#9900CC;">empty</span>? <span style="color:#9966CC; font-weight:bold;">or</span> shifts.<span style="color:#9900CC;">empty</span>?
  forward = direction.<span style="color:#9900CC;">first</span> == <span style="color:#996600;">&quot;from now&quot;</span>
&nbsp;
  result = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
  shifts.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>count, unit<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#008000; font-style:italic;">#depends on the 1.week type DSL for time measurements</span>
    adjust = count.<span style="color:#9900CC;">to_i</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>unit<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># move time in correct direction</span>
    result = forward ? <span style="color:#006600; font-weight:bold;">&#40;</span>result <span style="color:#006600; font-weight:bold;">+</span> adjust<span style="color:#006600; font-weight:bold;">&#41;</span> : <span style="color:#006600; font-weight:bold;">&#40;</span>result <span style="color:#006600; font-weight:bold;">-</span> adjust<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  result
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://devcaffeine.com/2009/08/17/relative-time-in-cucumber/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>
	</channel>
</rss>
