Done testing
authorNeil Smith <neil.git@njae.me.uk>
Fri, 20 Mar 2009 15:14:25 +0000 (15:14 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Fri, 20 Mar 2009 15:14:25 +0000 (15:14 +0000)
21 files changed:
test/fixtures/products.yml
test/fixtures/users.yml
test/functional/admin_controller_test.rb
test/integration/user_stories_test.rb [new file with mode: 0644]
test/unit/cart_test.rb [new file with mode: 0644]
test/unit/product_test.rb
vendor/rails/actionmailer/test/fixtures/path.with.dots/funky_path_mailer/multipart_with_template_path_with_dots.erb [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/_subtemplate.text.plain.erb [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/body_ivar.erb [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/custom_templating_extension.text.html.haml [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/custom_templating_extension.text.plain.haml [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.ignored.erb [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.rhtml.bak [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.html.erb [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.plain.erb [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.yaml.erb [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/included_subtemplate.text.plain.erb [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/rxml_template.builder [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/rxml_template.rxml [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/signed_up.html.erb [new file with mode: 0644]
vendor/rails/actionmailer/test/fixtures/test_mailer/signed_up_with_url.erb [new file with mode: 0644]

index bdddb6bf628fd8d59df5899916309d43b1531606..0afc4797b0af41638884755466166e55b34ca019 100644 (file)
@@ -1,11 +1,14 @@
 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
 
-one:
-  title: MyString
-  description: MyText
-  image_url: 
+ruby_book:
+  title: Programming Ruby
+  description: Dummy description
+  price: 1234
+  image_url: ruby.png
 
-two:
-  title: MyString
-  description: MyText
-  image_url: 
+rails_book:
+  title: Agile Web Development with Rails
+  description: Dummy description
+  price: 2345
+  image_url: rails.png
+  
\ No newline at end of file
index ccf5bd13fb3043d8449b3f9e3ef59e3924c2170d..c35ec63df88811f511c38975502ab370aabfba1f 100644 (file)
@@ -1,11 +1,7 @@
 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
 
-one:
-  name: MyString
-  hashed_password: MyString
-  salt: MyString
-
-two:
-  name: MyString
-  hashed_password: MyString
-  salt: MyString
+<% SALT = "NaCl" unless defined?(SALT) %>
+dave:
+  name: dave
+  salt: <%= SALT %>
+  hashed_password: <%= User.encrypted_password('secret' , SALT) %>
\ No newline at end of file
index 9bbf29bcad820427fe5c3cee3e526d2ede55734a..ccc4eee48e0e35450ab18952a29cf44474f30100 100644 (file)
@@ -1,8 +1,32 @@
 require 'test_helper'
 
 class AdminControllerTest < ActionController::TestCase
-  # Replace this with your real tests.
-  test "the truth" do
-    assert true
+
+  fixtures :users
+
+  test "index without user" do
+    get :index
+    assert_redirected_to :action => "login"
+    assert_equal "Please log in", flash[:notice]
+  end
+  
+  test "index with user" do
+    get :index, {}, { :user_id => users(:dave).id }
+    assert_response :success
+    assert_template "index"
+  end
+
+  test "login" do
+    dave = users(:dave)
+    post :login, :name => dave.name, :password => 'secret'
+    assert_redirected_to :action => "index"
+    assert_equal dave.id, session[:user_id]
+  end
+
+  test "bad password" do
+    dave = users(:dave)
+    post :login, :name => dave.name, :password => 'wrong'
+    assert_template "login"
   end
+
 end
diff --git a/test/integration/user_stories_test.rb b/test/integration/user_stories_test.rb
new file mode 100644 (file)
index 0000000..6c39e75
--- /dev/null
@@ -0,0 +1,59 @@
+require 'test_helper'
+
+class UserStoriesTest < ActionController::IntegrationTest
+  fixtures :products
+
+  # Replace this with your real tests.
+  test "the truth" do
+    assert true
+  end
+
+  # A user goes to the index page. They select a product, adding it to their
+  # cart, and check out, filling in their details on the checkout form. When
+  # they submit, an order is created containing their information, along with a
+  # single line item corresponding to the product they added to their cart.
+
+  test "buying a product" do
+    LineItem.delete_all
+    Order.delete_all
+    ruby_book = products(:ruby_book)
+
+    get "/store/index"
+    assert_response :success
+    assert_template "index"
+
+    xml_http_request :put, "/store/add_to_cart" , :id => ruby_book.id
+    assert_response :success
+
+    cart = session[:cart]
+    assert_equal 1, cart.items.size
+    assert_equal ruby_book, cart.items[0].product
+
+    post "/store/checkout"
+    assert_response :success
+    assert_template "checkout"
+
+    post_via_redirect "/store/save_order" ,
+      :order => { :name     => "Dave Thomas" ,
+                  :address  => "123 The Street" ,
+                  :email    => "dave@pragprog.com" ,
+                  :pay_type => "cheque" }
+    assert_response :success
+    assert_template "index"
+#    assert_template "store/checkout"
+
+    assert_equal 0, session[:cart].items.size
+    orders = Order.find(:all)
+    assert_equal 1, orders.size
+
+    order = orders[0]
+    assert_equal "Dave Thomas",       order.name
+    assert_equal "123 The Street",    order.address
+    assert_equal "dave@pragprog.com", order.email
+    assert_equal "cheque",            order.pay_type
+
+    assert_equal 1, order.line_items.size
+    line_item = order.line_items[0]
+    assert_equal ruby_book, line_item.product
+  end
+end
diff --git a/test/unit/cart_test.rb b/test/unit/cart_test.rb
new file mode 100644 (file)
index 0000000..0748bc6
--- /dev/null
@@ -0,0 +1,28 @@
+require 'test_helper'
+
+class CartTest < ActiveSupport::TestCase
+
+  fixtures :products
+
+  def setup
+    @cart = Cart.new
+    @rails = products(:rails_book)
+    @ruby = products(:ruby_book)
+  end
+
+  def test_add_unique_products
+    @cart.add_product @rails
+    @cart.add_product @ruby
+    assert_equal 2, @cart.items.size
+    assert_equal @rails.price + @ruby.price, @cart.total_price
+  end
+
+  def test_add_duplicate_product
+    @cart.add_product @rails
+    @cart.add_product @rails
+    assert_equal 2*@rails.price, @cart.total_price
+    assert_equal 1, @cart.items.size
+    assert_equal 2, @cart.items[0].quantity
+  end
+
+end
\ No newline at end of file
index bb39ca7c568963cb03747d7ad0fa567d5d4aa1ac..88246d2d942dba36dc5dc59399d8effee692a003 100644 (file)
@@ -1,8 +1,58 @@
 require 'test_helper'
 
 class ProductTest < ActiveSupport::TestCase
-  # Replace this with your real tests.
-  test "the truth" do
-    assert true
+
+  fixtures :products
+
+  test "Invalid with empty attributes" do
+    product = Product.new
+    assert !product.valid?
+    assert product.errors.invalid?(:title)
+    assert product.errors.invalid?(:description)
+    assert product.errors.invalid?(:price)
+    assert product.errors.invalid?(:image_url)
   end
+
+  test "positive price" do
+    product = Product.new(:title => "My Book Title" ,
+      :description => "yyy" ,
+      :image_url => "zzz.jpg" )
+    product.price = -1
+    assert !product.valid?
+    assert_equal "should be at least 0.01" , product.errors.on(:price)
+    product.price = 0
+    assert !product.valid?
+    assert_equal "should be at least 0.01" , product.errors.on(:price)
+    product.price = 1
+    assert product.valid?
+  end
+
+  test "image url" do
+    ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg http://a.b.c/x/y/z/fred.gif }
+    bad = %w{ fred.doc fred.gif/more fred.gif.more }
+    ok.each do |name|
+      product = Product.new(:title => "My Book Title" ,
+        :description => "yyy" ,
+        :price => 1,
+        :image_url => name)
+      assert product.valid?, product.errors.full_messages
+    end
+    bad.each do |name|
+      product = Product.new(:title => "My Book Title" ,
+        :description => "yyy" ,
+        :price => 1,
+        :image_url => name)
+      assert !product.valid?, "saving #{name}"
+    end
+  end
+
+  test "unique title" do
+    product = Product.new(:title => products(:ruby_book).title,
+      :description => "yyy" ,
+      :price => 1,
+      :image_url => "fred.gif" )
+    assert !product.save
+    assert_equal I18n.translate('activerecord.errors.messages.taken'), product.errors.on(:title)
+  end
+
 end
diff --git a/vendor/rails/actionmailer/test/fixtures/path.with.dots/funky_path_mailer/multipart_with_template_path_with_dots.erb b/vendor/rails/actionmailer/test/fixtures/path.with.dots/funky_path_mailer/multipart_with_template_path_with_dots.erb
new file mode 100644 (file)
index 0000000..897a506
--- /dev/null
@@ -0,0 +1 @@
+Have a lovely picture, from me. Enjoy!
\ No newline at end of file
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/_subtemplate.text.plain.erb b/vendor/rails/actionmailer/test/fixtures/test_mailer/_subtemplate.text.plain.erb
new file mode 100644 (file)
index 0000000..3b4ba35
--- /dev/null
@@ -0,0 +1 @@
+let's go!
\ No newline at end of file
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/body_ivar.erb b/vendor/rails/actionmailer/test/fixtures/test_mailer/body_ivar.erb
new file mode 100644 (file)
index 0000000..1421e5c
--- /dev/null
@@ -0,0 +1,2 @@
+body: <%= @body %>
+bar: <%= @bar %>
\ No newline at end of file
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/custom_templating_extension.text.html.haml b/vendor/rails/actionmailer/test/fixtures/test_mailer/custom_templating_extension.text.html.haml
new file mode 100644 (file)
index 0000000..847d065
--- /dev/null
@@ -0,0 +1,6 @@
+%p Hello there, 
+
+%p 
+  Mr.
+  = @recipient
+  from haml
\ No newline at end of file
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/custom_templating_extension.text.plain.haml b/vendor/rails/actionmailer/test/fixtures/test_mailer/custom_templating_extension.text.plain.haml
new file mode 100644 (file)
index 0000000..847d065
--- /dev/null
@@ -0,0 +1,6 @@
+%p Hello there, 
+
+%p 
+  Mr.
+  = @recipient
+  from haml
\ No newline at end of file
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.ignored.erb b/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.ignored.erb
new file mode 100644 (file)
index 0000000..6940419
--- /dev/null
@@ -0,0 +1 @@
+Ignored when searching for implicitly multipart parts.
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.rhtml.bak b/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.rhtml.bak
new file mode 100644 (file)
index 0000000..6940419
--- /dev/null
@@ -0,0 +1 @@
+Ignored when searching for implicitly multipart parts.
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.html.erb b/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.html.erb
new file mode 100644 (file)
index 0000000..946d99e
--- /dev/null
@@ -0,0 +1,10 @@
+<html>
+  <body>
+    HTML formatted message to <strong><%= @recipient %></strong>.
+  </body>
+</html>
+<html>
+  <body>
+    HTML formatted message to <strong><%= @recipient %></strong>.
+  </body>
+</html>
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.plain.erb b/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.plain.erb
new file mode 100644 (file)
index 0000000..a6c8d54
--- /dev/null
@@ -0,0 +1,2 @@
+Plain text to <%= @recipient %>.
+Plain text to <%= @recipient %>.
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.yaml.erb b/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.yaml.erb
new file mode 100644 (file)
index 0000000..c14348c
--- /dev/null
@@ -0,0 +1 @@
+yaml to: <%= @recipient %>
\ No newline at end of file
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/included_subtemplate.text.plain.erb b/vendor/rails/actionmailer/test/fixtures/test_mailer/included_subtemplate.text.plain.erb
new file mode 100644 (file)
index 0000000..a93c30e
--- /dev/null
@@ -0,0 +1 @@
+Hey Ho, <%= render :partial => "subtemplate" %>
\ No newline at end of file
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/rxml_template.builder b/vendor/rails/actionmailer/test/fixtures/test_mailer/rxml_template.builder
new file mode 100644 (file)
index 0000000..d566bd8
--- /dev/null
@@ -0,0 +1,2 @@
+xml.instruct!
+xml.test
\ No newline at end of file
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/rxml_template.rxml b/vendor/rails/actionmailer/test/fixtures/test_mailer/rxml_template.rxml
new file mode 100644 (file)
index 0000000..d566bd8
--- /dev/null
@@ -0,0 +1,2 @@
+xml.instruct!
+xml.test
\ No newline at end of file
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/signed_up.html.erb b/vendor/rails/actionmailer/test/fixtures/test_mailer/signed_up.html.erb
new file mode 100644 (file)
index 0000000..a85d5fa
--- /dev/null
@@ -0,0 +1,3 @@
+Hello there, 
+
+Mr. <%= @recipient %>
\ No newline at end of file
diff --git a/vendor/rails/actionmailer/test/fixtures/test_mailer/signed_up_with_url.erb b/vendor/rails/actionmailer/test/fixtures/test_mailer/signed_up_with_url.erb
new file mode 100644 (file)
index 0000000..4c5806d
--- /dev/null
@@ -0,0 +1,5 @@
+Hello there, 
+
+Mr. <%= @recipient %>. Please see our greeting at <%= @welcome_url %> <%= welcome_url %>
+
+<%= image_tag "somelogo.png" %>
\ No newline at end of file