Showing posts with label agile-web-development-with-rails. Show all posts
Showing posts with label agile-web-development-with-rails. Show all posts

Monday, February 7, 2011

Rails - Task I: Logging In - Noob notes on authlogic

There's a little auto-magic going on that as a rails/ruby noob I didn't expect.

1) The password, password_confirmation fields are not explicitly declared anywhere except for your registration, login views. This mean you have to add the "acts_as_authentic" to your User class BEFORE you can being using these fields in your views and tests.

2) Naming of your DB fields is important, password_confirmation != confirm_password. (Coming from a Java background, this feels weird, not having name control over model fields. Just go with it.)

3) After you do add acts_as_authentic, you have to update user_controller_test. Passing user.parameters doesn't work anymore, since that's the DB model and not what the user will send -- example of the changes that worked for me.

test "should create user" do
assert_difference('User.count') do
post :create, :user => {:email => 'test@functional.com', :password => 'godpassword', :password_confirmation => 'godpassword'}
end


4) Really enjoying the automated testing, they have been catching all kinds of mistakes I make in ignorance, and a great teaching tool. Am beginning to see how much you can skip explicit tutorials and APIs if the testing is robust.

Sunday, February 6, 2011

Rails - Task I: Logging In w/ authlogic_example

Picking up on a new branch, and take a few integration shortcuts to get authlogic up and running.

> git pull git://github.com/trevmex/authlogic_rails3_example

Then working through the merges necessary to merge in authlogic code, with a few skips
* git checkout --ours db/schema.rb
* git checkout --ours db/seeds.rb
* git checkout --theirs Gemfile
* change config/initializers/rspec_generator.rb Replace AuthlogicRails3Example with Depot (or your app name)

Then after code looked reasonably merged

> bundle install
> rake

Get complaints about unknown email_format validator

Seemed to have missed an important set of entries in config/application.rb

#Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += %W(#{config.root}/lib/**/)


This is required to reference the format validator introduced with the AuthLogic Example.

After this fix we get to something interesting... the rspec tests fail!

Rails - Task I: Logging In

For this task I decided to diverge from the book. From what I've heard attending Atlanta's local chapter meetings for OWASP (www.owasp.org), own-rolled authentication is at the root of far too many security flaws and breeches in web applications. Granted this book touches on some of the oft overlooked necessities (e.g. salted hash passwords), but for a real live website, I always push to use a robust, tested, peer-reviewed plugin over hand rolled code. At the very least, typical project timelines leave little time for developers and testers to stay current on the ever evolving threat landscape. If your shop is different, please, please help out the rest of us.

I looked around a bit and decided to go with authlogic. Trevmex has provided a fully Rails3 ready example found at https://github.com/trevmex/authlogic_rails3_example . http://www.dixis.com/?p=352 rewrites the tutorial for a few other steps you need to then incorporate the module into the depot code. (I did start first trying more commonly referenced authlogic_example by binarylogic at https://github.com/binarylogic/authlogic_example, but all I succeeded at was proving what I complete noob I am)

What follows is a combination of various sources. Comments indicate what prompted each step to be taken, but not the rationale or theory behind it.

Pre-Iteration Work: Adding authlogic to depot
$ sudo gem install authlogic #binarylogic

To depot Gemfile add:
# Add support for Authlogic authentication
gem 'authlogic', :git => 'git://github.com/odorcicd/authlogic.git', :branch => 'rails3' #trevmex
gem "rails3-generators" #dixis
> bundle install #'cause rake prompted me to run this after the changes.

> rails generate authlogic:session UserSession #dixis

>rake test

And viola! everything now fails because of 'ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: user_sessions: DELETE FROM "user_sessions" WHERE 1=1'

bummer!

Saturday, January 15, 2011

Rails - Iteration E3 -Fixture tests for prices

Need further study: http://api.rubyonrails.org/classes/Fixtures.html

When I tried to follow simple example e.g.

one_quantity_two:
product_id: one
cart_id: 2
quantity: 2

line_item.product was a nil object.

The format that did work

one_quantity_two:
product_id: <%= Fixtures.identify(:one) %>
cart_id: 2
quantity: 2

Most likely culprit --> rails/ruby version?

FYI - cause of the critical bug?

bad --> @line_item = @cart.add_product(:product.id)
good --> @line_item = @cart.add_product(product.id)

First case not passing id, instead passing (I think) an object reference! gotta love those one character typos!

Rails - Iteration E1 - line_item aggregation starts new

Take 2 on smarter cart!

First: add a unit test for the cart.add_product method

test "cart aggregates line items" do
c = carts(:one)
assert_equal 0, c.line_items.count, "cart items not empty at start of test"
p = products(:ruby)
item = c.add_product(p.id)
assert_equal 1, c.line_items.count, "first product add did not create new line item"
assert_equal 1, item.quantity, "new item quantity not 1"
item = c.add_product(p.id) #hold reference for future testing
assert_equal 1, c.line_items.count, "readd product caused increase in line items count"
assert_equal products(:ruby).id, item.product.id, "a problem with product id between line item and orig"
end

(Note to self: originally tried to do above like:
- assert_difference('c.line_items.count') do
- p = products(:ruby)
- c.add_product(p.id)
- c.save
- end
Note that the assert_difference requires a DB save event. Decided I didn't like that extra dependency on a unit test. Also note that the assert_difference requires a string not an object expression.)

Second: modify line_item_controller test to show aggregation of line items

test "should create/update line_item" do
#original test when just creating new items (iteration D)
assert_difference('LineItem.count') do
post :create, :product_id => products(:ruby).id
end
assert_redirected_to cart_path(assigns(:line_item).cart)
#new test - just repeat old and this time no_difference
assert_no_difference('LineItem.count') do
post :create, :product_id => products(:ruby).id
end
assert_redirected_to cart_path(assigns(:line_item).cart)

now when I complete the line item tests everything (including listing of cart after add) works as intended.

Rails - Iteration E1 - recovery from new defect introduced

Interesting, at end of last class all of my tests passed, but today I test my app and find that I have a critical defect!

Post /lines_items?product_id=4 fails horribly when trying to show the cart page with updated list.
Cart page complains about no 'title' attribute for a nil object
/line_items shows that my cart now has a line item for a product id 123456 <- which of course doesn't exist.

Old instincts: well time to add a check for null products and skip processing anything won't work.
New TDD instincts: Whoa! how can all my tests be passing? Why is the product id changing on add_product?

Step 1: Recreate the defect in a test.
Looks like my line_items_controller_test only confirms line item addition and redirect to cart page. But defect manifest at cart page....
Decided to try my hand at 'integration' test, since now testing flow from one controller to another.
> rails generate integration_test user_cart_session_test

Then add test method to the resulting skeleton:

test "line item adds to cart" do
item = products(:ruby)
post "/line_items", :product_id => item.id
follow_redirect!
assert_response :success
end

> rake test:integration --> Same problem as reported in the app!

Last Step: Rollback to a clean branch of code and start lesson over.

> git commit -a -m "test for my broken code"
> git checkout master
> rake test --> lots of problems? Wait the DB was mucked with
> rake db:drop
> rake db:migrate
> rake test --> ah! that's better!
> git checkout class.chapter10 test/integration/ --> pull over my new fangled test
rails test --> look now it's passing, got rid of my bug!
git add test/integration/
git commit -m "adding a functional test for cart and line items"

Note To Self: never ever forget to branch when starting new development! Love this cycle
1) break code unexpectedly
2) build test to reproduce break
3) revert code, import test and start dev over again (and always have access to previous work!)

Wednesday, January 5, 2011

Rails4 - Iteration D3 - functional tests for new cart button

When building the store page, we went through several examples of adding functional tests to confirm the store index page contained expected page elements. Seemed logical to extend these existing tests to track the addition of the cart button. In doing so learned a couple of interesting features of assert_select

1) use a block to test that each .entry now displays button "Add to Cart"
assert_select '.entry' do
assert_select '.button_to input[value=?]', /Add to Cart/
end

2) check that each form for Add to Cart contains the product_id information, in this case using a substitution marker ? to check the action contents.
assert_select '.entry form[action=?]', /.*product_id=\d+.*/

Note: the substitution regex did not recognize partial matches. For example, /product_id=\d+/ failed to find any elements because 'product_id' is only a partial match to the actual action value /line_items?product_id=4 needed to add .* at start and end to pad out for the remaining action text.

Note for future study: How do I put variables into the regex? for example might be nice to check that the action also contains correct target 'line_items' but I don't really want to hard code that. Instead want to test with line_items_path variable?

Friday, December 31, 2010

Rails4 - Iteration B2 Product controller tests fail.

The book solution demos adding an @update product not conflicting with the default fixtures :one and :two, which by default have the same title. (after adding uniqueness test on title, these fail).

It also works to fix the test data, just update the products.yml file and provide a unique title for :one and :two

one:
title: MyString One
description: MyText
image_url: MyString
price: 9.99

two:
title: MyString Two
description: MyText
image_url: MyString
price: 9.99

This allows the update test to pass no problem.

Note: you do still need to provide a unique title in should create product (where using :one as a template). Simple as adding one line into the method:
#after validation added, now required to provide a unique title.
@product.title="a unique test title"
#this is the part that takes the form input..
post :create, :product => @product.attributes

Thursday, December 30, 2010

Rails4: Iteration B2 - generic def for new default valid products

Building on example for "def new_product(image_url)" pg. 101

#a generic method to build new test produce with valid data
#but also allow insertion of any attributes that need to be over-ridden
#takes a hash of attribute values, checks for each and populates default as needed.
#don't add this to real Product, 'cause production code shouldn't provide defaults.
def new_valid_product(data)
Product.new(
:title => data.has_key?(:title) ? data[:title] : "A Book Title",
:description => data.has_key?(:description) ? data[:description] : "ipso lorum... a default description",
:price => data.has_key?(:price) ? data[:price] : 123.45,
:image_url => data.has_key?(:image_url) ? data[:image_url] : "test.gif"
)
end

#a quick test to warn me if any rules change that invalidate the default values.
test "default product attributes are valid" do
product = new_valid_product({})
assert !product.errors[:title].any?, "default title in error"
assert !product.errors[:description].any?, "default_description in error"
assert !product.errors[:price].any?, "default_price in error"
assert !product.errors[:image_url].any?, "default image_url in error"
end

#a quick test to prove to myself that the method accepts input data & uses it correctly.
test "default product attributes can be changed" do
product = new_valid_product({:title => "T", :description => "D", :price => 1, :image_url => "I"})
assert_equal "T", product.title
assert_equal "D", product.description
assert_equal 1, product.price
assert_equal "I", product.image_url
end

Note: My guess it that this kind of method is not really useful, Ruby has fixtures, my suspicion is that in time I'll find it much easier to define and get a fixture, then change a value or two, rather than write this kind of utility method for each model unit test class.

Tuesday, December 28, 2010

Rails4 - curl to download directory content

thanks to: http://www.linuxquestions.org/questions/programming-9/using-curl-to-dl-files-from-http-sites-with-wildcard-379067/ for the initial curl script line and to http://www.linuxquestions.org/questions/programming-9/extract-substring-using-sed-and-regular-expressions-regexp-702074/ for help extracting the filename from html.

for file in `curl http://media.pragprog.com/titles/rails4/code/depot_b/public/images/ | perl -wlne 'print $1 if
/href="(.*[gif|png|jpg])">/'`;do curl -o ${file} http://media.pragprog.com/titles/rails4/code/depot_b/public/images/${file};done

*Note: not happy with having to specify the file types, but the page HTML contains other links like Download, Parent etc so href=.* litters the results with junk files.


*Note would have been easier with wget, but for some reason darwin-ports for leopard DMG was unavailable.
should I get darwin-ports installed try this instead.

run from depot/public/images directory
wget -rkp -np -nH --cut-dirs=1 http://media.pragprog.com/titles/rails4/code/depot_b/public/images

@see http://psung.blogspot.com/2008/06/using-wget-or-curl-to-download-web.html