From b0748b595b9c1d5ad9f6f461935db914c3a86bd3 Mon Sep 17 00:00:00 2001
From: Neil Smith <neil.git@njae.me.uk>
Date: Fri, 20 Mar 2009 15:14:25 +0000
Subject: [PATCH] Done testing

---
 test/fixtures/products.yml                    | 19 +++---
 test/fixtures/users.yml                       | 14 ++---
 test/functional/admin_controller_test.rb      | 30 +++++++++-
 test/integration/user_stories_test.rb         | 59 +++++++++++++++++++
 test/unit/cart_test.rb                        | 28 +++++++++
 test/unit/product_test.rb                     | 56 +++++++++++++++++-
 ...multipart_with_template_path_with_dots.erb |  1 +
 .../test_mailer/_subtemplate.text.plain.erb   |  1 +
 .../test/fixtures/test_mailer/body_ivar.erb   |  2 +
 ...custom_templating_extension.text.html.haml |  6 ++
 ...ustom_templating_extension.text.plain.haml |  6 ++
 .../implicitly_multipart_example.ignored.erb  |  1 +
 .../implicitly_multipart_example.rhtml.bak    |  1 +
 ...implicitly_multipart_example.text.html.erb | 10 ++++
 ...mplicitly_multipart_example.text.plain.erb |  2 +
 ...implicitly_multipart_example.text.yaml.erb |  1 +
 .../included_subtemplate.text.plain.erb       |  1 +
 .../test_mailer/rxml_template.builder         |  2 +
 .../fixtures/test_mailer/rxml_template.rxml   |  2 +
 .../fixtures/test_mailer/signed_up.html.erb   |  3 +
 .../test_mailer/signed_up_with_url.erb        |  5 ++
 21 files changed, 227 insertions(+), 23 deletions(-)
 create mode 100644 test/integration/user_stories_test.rb
 create mode 100644 test/unit/cart_test.rb
 create mode 100644 vendor/rails/actionmailer/test/fixtures/path.with.dots/funky_path_mailer/multipart_with_template_path_with_dots.erb
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/_subtemplate.text.plain.erb
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/body_ivar.erb
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/custom_templating_extension.text.html.haml
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/custom_templating_extension.text.plain.haml
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.ignored.erb
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.rhtml.bak
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.html.erb
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.plain.erb
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.yaml.erb
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/included_subtemplate.text.plain.erb
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/rxml_template.builder
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/rxml_template.rxml
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/signed_up.html.erb
 create mode 100644 vendor/rails/actionmailer/test/fixtures/test_mailer/signed_up_with_url.erb

diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml
index bdddb6b..0afc479 100644
--- a/test/fixtures/products.yml
+++ b/test/fixtures/products.yml
@@ -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
diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml
index ccf5bd1..c35ec63 100644
--- a/test/fixtures/users.yml
+++ b/test/fixtures/users.yml
@@ -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
diff --git a/test/functional/admin_controller_test.rb b/test/functional/admin_controller_test.rb
index 9bbf29b..ccc4eee 100644
--- a/test/functional/admin_controller_test.rb
+++ b/test/functional/admin_controller_test.rb
@@ -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
index 0000000..6c39e75
--- /dev/null
+++ b/test/integration/user_stories_test.rb
@@ -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
index 0000000..0748bc6
--- /dev/null
+++ b/test/unit/cart_test.rb
@@ -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
diff --git a/test/unit/product_test.rb b/test/unit/product_test.rb
index bb39ca7..88246d2 100644
--- a/test/unit/product_test.rb
+++ b/test/unit/product_test.rb
@@ -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
index 0000000..897a506
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/path.with.dots/funky_path_mailer/multipart_with_template_path_with_dots.erb
@@ -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
index 0000000..3b4ba35
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/_subtemplate.text.plain.erb
@@ -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
index 0000000..1421e5c
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/body_ivar.erb
@@ -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
index 0000000..847d065
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/custom_templating_extension.text.html.haml
@@ -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
index 0000000..847d065
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/custom_templating_extension.text.plain.haml
@@ -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
index 0000000..6940419
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.ignored.erb
@@ -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
index 0000000..6940419
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.rhtml.bak
@@ -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
index 0000000..946d99e
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.html.erb
@@ -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
index 0000000..a6c8d54
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.plain.erb
@@ -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
index 0000000..c14348c
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/implicitly_multipart_example.text.yaml.erb
@@ -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
index 0000000..a93c30e
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/included_subtemplate.text.plain.erb
@@ -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
index 0000000..d566bd8
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/rxml_template.builder
@@ -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
index 0000000..d566bd8
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/rxml_template.rxml
@@ -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
index 0000000..a85d5fa
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/signed_up.html.erb
@@ -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
index 0000000..4c5806d
--- /dev/null
+++ b/vendor/rails/actionmailer/test/fixtures/test_mailer/signed_up_with_url.erb
@@ -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
-- 
2.43.0