Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of git://github.com/buntine/radiant-comments

  • Loading branch information...
commit a6682d60e06d426522f3f0bdcb92854c4fa9b89f 2 parents 00c9de9 + 8d20eed
Jim Gay authored May 01, 2009
2  app/models/comment.rb
@@ -113,7 +113,7 @@ def unapprove!
113 113
   end
114 114
   
115 115
   private
116  
-  
  116
+
117 117
     def auto_approve
118 118
       self.approved_at = Time.now if auto_approve?
119 119
     end
138  spec/models/comment_spec.rb
... ...
@@ -0,0 +1,138 @@
  1
+require File.dirname(__FILE__) + '/../spec_helper'
  2
+
  3
+describe Comment do
  4
+
  5
+  before do
  6
+    @akismet = mock("akismet", :valid? => true, :commentCheck => false)
  7
+    @mollom_response = mock("response", :ham? => true, :session_id => '00011001')
  8
+    @mollom = mock("mollom", :key_ok? => false, :check_content => @mollom_response)
  9
+    @page = Page.create(page_params(:title => "Test"))
  10
+
  11
+    Akismet.stub!(:new).and_return(@akismet)
  12
+    Mollom.stub!(:new).and_return(@mollom)
  13
+  end
  14
+
  15
+  after do
  16
+    Page.find(:all).each { |page| page.destroy }
  17
+    Comment.find(:all).each { |comment| comment.destroy }
  18
+  end
  19
+
  20
+  describe Comment, "when creating" do
  21
+
  22
+    it "should successfully create comment" do
  23
+      comment = create_comment
  24
+
  25
+      comment.valid?.should be_true
  26
+      comment.save.should be_true
  27
+    end
  28
+
  29
+    it "should set content_html with filter when saving" do
  30
+      comment = create_comment
  31
+      comment.save
  32
+
  33
+      comment.content_html.should eql('<p>Test...</p>')
  34
+    end
  35
+
  36
+    it "should auto_approve the comment if it is valid according to Akismet" do
  37
+      @akismet.should_receive(:valid?).and_return(true)
  38
+      @akismet.should_receive(:commentCheck).and_return(false) # False is good here :)
  39
+
  40
+      comment = create_comment
  41
+      comment.save
  42
+
  43
+      comment.approved_at.should_not be_nil
  44
+      comment.approved_at.should be_instance_of(Time)
  45
+    end
  46
+
  47
+    it "should NOT auto_approve the comment if it is INVALID" do
  48
+      @akismet.should_receive(:valid?).and_return(true)
  49
+      @akismet.should_receive(:commentCheck).and_return(true)
  50
+
  51
+      comment = create_comment
  52
+      comment.save
  53
+
  54
+      comment.approved_at.should be_nil
  55
+    end
  56
+
  57
+    it "should auto_approve the comment if it is valid according to Mollom" do
  58
+      @akismet.should_receive(:valid?).and_return(false)
  59
+      @mollom.should_receive(:key_ok?).at_least(1).times.and_return(true)
  60
+      @mollom.should_receive(:check_content).and_return(@mollom_response)
  61
+
  62
+      File.stub!(:open)
  63
+
  64
+      comment = create_comment
  65
+      comment.save
  66
+
  67
+      comment.approved_at.should_not be_nil
  68
+      comment.approved_at.should be_instance_of(Time)
  69
+    end
  70
+
  71
+    it "should save the mollom servers if the key is valid" do
  72
+      @akismet.should_receive(:valid?).and_return(false)
  73
+      @mollom.should_receive(:key_ok?).at_least(1).times.and_return(true)
  74
+      @mollom.should_receive(:check_content).and_return(@mollom_response)
  75
+      @mollom.should_receive(:server_list).and_return({:server1 => "one", :server2 => "two"})
  76
+      
  77
+      io = mock("io")
  78
+      io.should_receive(:write).with({:server1 => "one", :server2 => "two"}.to_yaml)
  79
+      File.should_receive(:open).and_yield(io)
  80
+
  81
+      comment = create_comment
  82
+      comment.save
  83
+    end
  84
+
  85
+    it "should NOT save the mollom servers if the key is INVALID" do
  86
+      @akismet.should_receive(:valid?).and_return(false)
  87
+      @mollom.should_receive(:key_ok?).at_least(1).times.and_return(false)
  88
+
  89
+      File.should_not_receive(:open)
  90
+
  91
+      comment = create_comment
  92
+      comment.save
  93
+    end
  94
+
  95
+    it "should validate that author is supplied" do
  96
+      comment = create_comment(:author => nil)
  97
+      comment.valid?.should be_false
  98
+    end
  99
+
  100
+    it "should validate that author_email is supplied" do
  101
+      comment = create_comment(:author_email => nil)
  102
+      comment.valid?.should be_false
  103
+    end
  104
+
  105
+    it "should validate that content is supplied" do
  106
+      comment = create_comment(:content => nil)
  107
+      comment.valid?.should be_false
  108
+    end
  109
+
  110
+  end
  111
+
  112
+  def create_comment(opts={})
  113
+    Comment.new({ :page => @page, :author => "Test", :author_email => "test@test.com", :author_ip => "10.1.10.1",
  114
+                  :content => "Test..." }.merge(opts))
  115
+  end
  116
+
  117
+  def page_params(attributes={})
  118
+    title = attributes[:title] || unique_page_title
  119
+
  120
+    attributes = {
  121
+      :title => title,
  122
+      :breadcrumb => title,
  123
+      :slug => title.symbolize.to_s.gsub("_", "-"),
  124
+      :class_name => nil,
  125
+      :status_id => Status[:published].id,
  126
+      :published_at => Time.now.to_s(:db)
  127
+    }.update(attributes)
  128
+    attributes[:parent_id] = 10
  129
+    attributes
  130
+  end
  131
+
  132
+  @@unique_page_title_call_count = 0
  133
+  def unique_page_title
  134
+    @@unique_page_title_call_count += 1
  135
+    "Page #{@@unique_page_title_call_count}"
  136
+  end
  137
+
  138
+end

0 notes on commit a6682d6

Please sign in to comment.
Something went wrong with that request. Please try again.