cornerstone 0.0.1 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. data/.gitignore +6 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +23 -0
  4. data/Gemfile.lock +168 -0
  5. data/Guardfile +22 -0
  6. data/README.rdoc +4 -1
  7. data/Rakefile +11 -1
  8. data/TODO +11 -0
  9. data/VERSION +1 -0
  10. data/app/assets/images/.gitkeep +0 -0
  11. data/app/assets/javascripts/.gitkeep +0 -0
  12. data/app/assets/javascripts/cornerstone.js +11 -0
  13. data/app/assets/javascripts/cornerstone/discussions.js +2 -0
  14. data/app/assets/javascripts/cornerstone/help.js +2 -0
  15. data/app/assets/stylesheets/.gitkeep +0 -0
  16. data/app/assets/stylesheets/cornerstone.css +8 -0
  17. data/app/assets/stylesheets/cornerstone/discussions.css +4 -0
  18. data/app/assets/stylesheets/cornerstone/help.css +4 -0
  19. data/app/controllers/.gitkeep +0 -0
  20. data/app/controllers/cornerstone/admin/application_controller.rb +7 -0
  21. data/app/controllers/cornerstone/admin/articles_controller.rb +61 -0
  22. data/app/controllers/cornerstone/admin/categories_controller.rb +46 -0
  23. data/app/controllers/cornerstone/admin/discussions_controller.rb +32 -0
  24. data/app/controllers/cornerstone/application_controller.rb +6 -0
  25. data/app/controllers/cornerstone/discussions_controller.rb +58 -0
  26. data/app/controllers/cornerstone/help_controller.rb +11 -0
  27. data/app/controllers/cornerstone/posts_controller.rb +66 -0
  28. data/app/helpers/.gitkeep +0 -0
  29. data/app/helpers/cornerstone/application_helper.rb +5 -0
  30. data/app/helpers/cornerstone/discussions_helper.rb +16 -0
  31. data/app/helpers/cornerstone/help_helper.rb +4 -0
  32. data/app/mailers/cornerstone/cornerstone_mailer.rb +31 -0
  33. data/app/models/.gitkeep +0 -0
  34. data/app/models/cornerstone/article.rb +20 -0
  35. data/app/models/cornerstone/category.rb +45 -0
  36. data/app/models/cornerstone/discussion.rb +90 -0
  37. data/app/models/cornerstone/post.rb +103 -0
  38. data/app/models/cornerstone/post_observer.rb +23 -0
  39. data/app/views/.gitkeep +0 -0
  40. data/app/views/cornerstone/admin/articles/_article.html.erb +9 -0
  41. data/app/views/cornerstone/admin/articles/_form.html.erb +22 -0
  42. data/app/views/cornerstone/admin/articles/edit.html.erb +7 -0
  43. data/app/views/cornerstone/admin/articles/index.html.erb +25 -0
  44. data/app/views/cornerstone/admin/articles/new.html.erb +6 -0
  45. data/app/views/cornerstone/admin/articles/show.html.erb +4 -0
  46. data/app/views/cornerstone/admin/categories/_category.html.erb +12 -0
  47. data/app/views/cornerstone/admin/categories/_form.html.erb +17 -0
  48. data/app/views/cornerstone/admin/categories/edit.html.erb +6 -0
  49. data/app/views/cornerstone/admin/categories/index.html.erb +11 -0
  50. data/app/views/cornerstone/admin/categories/new.html.erb +6 -0
  51. data/app/views/cornerstone/admin/discussions/edit.html.erb +7 -0
  52. data/app/views/cornerstone/cornerstone_mailer/new_discussion.html.erb +14 -0
  53. data/app/views/cornerstone/cornerstone_mailer/new_discussion.text.erb +5 -0
  54. data/app/views/cornerstone/cornerstone_mailer/new_discussion_user.html.erb +7 -0
  55. data/app/views/cornerstone/cornerstone_mailer/new_post.html.erb +7 -0
  56. data/app/views/cornerstone/cornerstone_mailer/new_post.text.erb +8 -0
  57. data/app/views/cornerstone/discussions/_discussion.html.erb +12 -0
  58. data/app/views/cornerstone/discussions/_discussion_category.html.erb +14 -0
  59. data/app/views/cornerstone/discussions/_form.html.erb +33 -0
  60. data/app/views/cornerstone/discussions/_latest_discussion.html.erb +4 -0
  61. data/app/views/cornerstone/discussions/categorical_index.html.erb +27 -0
  62. data/app/views/cornerstone/discussions/index.html.erb +25 -0
  63. data/app/views/cornerstone/discussions/new.html.erb +6 -0
  64. data/app/views/cornerstone/discussions/show.html.erb +19 -0
  65. data/app/views/cornerstone/help/index.html.erb +7 -0
  66. data/app/views/cornerstone/posts/_fields.html.erb +44 -0
  67. data/app/views/cornerstone/posts/_form.html.erb +19 -0
  68. data/app/views/cornerstone/posts/_post.html.erb +17 -0
  69. data/app/views/cornerstone/posts/edit.html.erb +9 -0
  70. data/app/views/cornerstone/shared/_errors.html.erb +11 -0
  71. data/app/views/cornerstone/shared/_flash_messages.html.erb +15 -0
  72. data/app/views/layouts/cornerstone/application.html.erb +16 -0
  73. data/config/cucumber.yml +8 -0
  74. data/config/locales/cornerstone.action_mailer.en.yml +9 -0
  75. data/config/routes.rb +24 -0
  76. data/cornerstone-0.0.1.gem +0 -0
  77. data/cornerstone.gemspec +33 -0
  78. data/db/migrate/20110723004024_create_cornerstone_discussions.rb +17 -0
  79. data/db/migrate/20110804190853_create_cornerstone_categories.rb +15 -0
  80. data/db/migrate/20110809233551_create_cornerstone_posts.rb +13 -0
  81. data/db/migrate/20111006172857_create_cornerstone_articles.rb +12 -0
  82. data/lib/cornerstone.rb +6 -0
  83. data/lib/cornerstone/acts_as_cornerstone_user.rb +79 -0
  84. data/lib/cornerstone/config.rb +33 -0
  85. data/lib/cornerstone/controller_additions.rb +25 -0
  86. data/lib/cornerstone/engine.rb +8 -1
  87. data/lib/cornerstone/exceptions.rb +16 -0
  88. data/lib/cornerstone/helpers.rb +35 -0
  89. data/lib/tasks/cucumber.rake +65 -0
  90. data/lib/templates/cornerstone_config.rb +31 -0
  91. data/script/cucumber +10 -0
  92. data/script/rails +7 -0
  93. data/spec/controllers/cornerstone/admin/articles_controller_spec.rb +250 -0
  94. data/spec/controllers/cornerstone/admin/categories_controller_spec.rb +205 -0
  95. data/spec/controllers/cornerstone/admin/discussions_controller_spec.rb +95 -0
  96. data/spec/controllers/cornerstone/application_controller_spec.rb +34 -0
  97. data/spec/controllers/cornerstone/discussions_controller_spec.rb +157 -0
  98. data/spec/controllers/cornerstone/help_controller_spec.rb +20 -0
  99. data/spec/controllers/cornerstone/posts_controller_spec.rb +212 -0
  100. data/spec/dummy/Rakefile +7 -0
  101. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  102. data/spec/dummy/app/assets/javascripts/tester.js +2 -0
  103. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  104. data/spec/dummy/app/assets/stylesheets/tester.css +4 -0
  105. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  106. data/spec/dummy/app/controllers/tester_controller.rb +7 -0
  107. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  108. data/spec/dummy/app/helpers/tester_helper.rb +6 -0
  109. data/spec/dummy/app/mailers/.gitkeep +0 -0
  110. data/spec/dummy/app/models/.gitkeep +0 -0
  111. data/spec/dummy/app/models/user.rb +23 -0
  112. data/spec/dummy/app/views/devise/confirmations/new.html.erb +12 -0
  113. data/spec/dummy/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  114. data/spec/dummy/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  115. data/spec/dummy/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  116. data/spec/dummy/app/views/devise/passwords/edit.html.erb +16 -0
  117. data/spec/dummy/app/views/devise/passwords/new.html.erb +12 -0
  118. data/spec/dummy/app/views/devise/registrations/edit.html.erb +29 -0
  119. data/spec/dummy/app/views/devise/registrations/new.html.erb +22 -0
  120. data/spec/dummy/app/views/devise/sessions/new.html.erb +19 -0
  121. data/spec/dummy/app/views/devise/shared/_links.erb +25 -0
  122. data/spec/dummy/app/views/devise/unlocks/new.html.erb +12 -0
  123. data/spec/dummy/app/views/layouts/application.html.erb +15 -0
  124. data/spec/dummy/app/views/tester/index.html.erb +14 -0
  125. data/spec/dummy/config.ru +4 -0
  126. data/spec/dummy/config/application.rb +42 -0
  127. data/spec/dummy/config/boot.rb +10 -0
  128. data/spec/dummy/config/database.yml +28 -0
  129. data/spec/dummy/config/environment.rb +5 -0
  130. data/spec/dummy/config/environments/development.rb +31 -0
  131. data/spec/dummy/config/environments/production.rb +54 -0
  132. data/spec/dummy/config/environments/test.rb +39 -0
  133. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  134. data/spec/dummy/config/initializers/cornerstone_config.rb +30 -0
  135. data/spec/dummy/config/initializers/devise.rb +204 -0
  136. data/spec/dummy/config/initializers/inflections.rb +10 -0
  137. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  138. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  139. data/spec/dummy/config/initializers/session_store.rb +8 -0
  140. data/spec/dummy/config/initializers/wrap_parameters.rb +12 -0
  141. data/spec/dummy/config/locales/devise.en.yml +53 -0
  142. data/spec/dummy/config/locales/en.yml +5 -0
  143. data/spec/dummy/config/routes.rb +65 -0
  144. data/spec/dummy/db/migrate/20110724011421_create_user.rb +11 -0
  145. data/spec/dummy/db/migrate/20110724194307_devise_create_users.rb +41 -0
  146. data/spec/dummy/db/migrate/20110804174004_add_name_to_user.rb +5 -0
  147. data/spec/dummy/db/schema.rb +76 -0
  148. data/spec/dummy/log/.gitkeep +0 -0
  149. data/spec/dummy/public/404.html +26 -0
  150. data/spec/dummy/public/422.html +26 -0
  151. data/spec/dummy/public/500.html +26 -0
  152. data/spec/dummy/public/favicon.ico +0 -0
  153. data/spec/dummy/script/rails +6 -0
  154. data/spec/dummy/test/functional/tester_controller_test.rb +9 -0
  155. data/spec/dummy/test/unit/helpers/tester_helper_test.rb +4 -0
  156. data/spec/fixtures/cornerstone/cornerstone_mailer/new_discussion +3 -0
  157. data/spec/lib/cornerstone/acts_as_cornerstone_user_spec.rb +56 -0
  158. data/spec/lib/cornerstone/helpers_spec.rb +32 -0
  159. data/spec/mailers/cornerstone/cornerstone_mailer_spec.rb +55 -0
  160. data/spec/models/cornerstone/article_spec.rb +25 -0
  161. data/spec/models/cornerstone/category_spec.rb +97 -0
  162. data/spec/models/cornerstone/discussion_spec.rb +243 -0
  163. data/spec/models/cornerstone/post_observer_spec.rb +65 -0
  164. data/spec/models/cornerstone/post_spec.rb +210 -0
  165. data/spec/requests/emails_spec.rb +51 -0
  166. data/spec/requests/interact_discussions_spec.rb +103 -0
  167. data/spec/requests/manage_articles_spec.rb +59 -0
  168. data/spec/requests/manage_categories_spec.rb +64 -0
  169. data/spec/requests/view_home_spec.rb +26 -0
  170. data/spec/spec_helper.rb +40 -0
  171. data/spec/support/devise.rb +4 -0
  172. data/spec/support/factories.rb +62 -0
  173. data/spec/support/general_helper_methods.rb +20 -0
  174. data/spec/support/mailer_macros.rb +15 -0
  175. data/spec/support/mass_assignment.rb +46 -0
  176. data/tmp/log/development.log +0 -0
  177. metadata +301 -20
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cornerstone::Admin::DiscussionsController do
4
+
5
+ def mock_category(stubs={})
6
+ @mock_category ||= mock_model(Cornerstone::Category, stubs).as_null_object
7
+ end
8
+
9
+ def mock_discussion(stubs={})
10
+ @mock_discussion ||= mock_model(Cornerstone::Discussion, stubs).as_null_object
11
+ end
12
+
13
+ describe "GET edit" do
14
+ context "with an administrator" do
15
+ before do
16
+ sign_in_admin
17
+ end
18
+ it "assigns the discussion as @discussion" do
19
+ Cornerstone::Discussion.stub_chain(:includes, :find) {mock_discussion}
20
+ get :edit, :id => "2", :use_route => :cornerstone
21
+ assigns(:discussion).should eql(mock_discussion)
22
+ end
23
+
24
+ it "exposes discussion categories for selection as @categories" do
25
+ Cornerstone::Discussion.stub_chain(:includes, :find) {mock_discussion}
26
+ Cornerstone::Category.should_receive(:discussions) {[mock_category]}
27
+ get :edit, :id => "2", :use_route => :cornerstone
28
+ assigns(:categories).should eql([mock_category])
29
+ end
30
+ end
31
+
32
+ context "with a normal user" do
33
+ it "raises the unauthorized error" do
34
+ lambda {
35
+ get :edit, :id => "2", :use_route => :cornerstone
36
+ }.should raise_error(Cornerstone::AccessDenied)
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "PUT update" do
42
+ context "with an administrator" do
43
+ before do
44
+ sign_in_admin
45
+ end
46
+ it "exposes the discussion as @discussion" do
47
+ Cornerstone::Discussion.stub_chain(:includes, :find).with("8") {mock_discussion}
48
+ put :update, :id => "8", :discussion => {}, :use_route => :cornerstone
49
+ assigns[:discussion].should == mock_discussion
50
+ end
51
+
52
+ it "updates the requested discussion with the given parameters" do
53
+ Cornerstone::Discussion.stub_chain(:includes, :find).with("8") {mock_discussion}
54
+ mock_discussion.should_receive(:update_attributes).with({"these" => "params"})
55
+ put :update, :id => "8", :discussion => {"these" => "params"},
56
+ :use_route => :cornerstone
57
+ end
58
+ context "with valid parameters" do
59
+ before do
60
+ Cornerstone::Discussion.stub_chain(:includes, :find).with("8")
61
+ .and_return(mock_discussion(:update_attributes => true))
62
+ end
63
+
64
+ it "redirects to the discussion" do
65
+ put :update, :id => "8", :discussion => {"these" => "params"},
66
+ :use_route => :cornerstone
67
+ response.should redirect_to(category_discussion_path(mock_discussion.category, mock_discussion))
68
+ end
69
+ end
70
+ context "with invalid parameters" do
71
+ before do
72
+ Cornerstone::Discussion.stub_chain(:includes, :find).with("8")
73
+ .and_return(mock_discussion(:update_attributes => false))
74
+ end
75
+
76
+ it "renders the edit template" do
77
+ put :update, :id => "8", :discussion => {},
78
+ :use_route => :cornerstone
79
+ response.should render_template :edit
80
+ end
81
+ end
82
+ end
83
+ context "with a normal user" do
84
+ it "raises the unauthorized error" do
85
+ lambda {
86
+ get :edit, :id => "2", :use_route => :cornerstone
87
+ }.should raise_error(Cornerstone::AccessDenied)
88
+ end
89
+ end
90
+ end
91
+
92
+ pending "DESTROY"
93
+
94
+ end
95
+
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cornerstone::ApplicationController do
4
+
5
+ describe "Helpers" do
6
+
7
+ ActionController::Base.send :include, Cornerstone::Helpers
8
+
9
+
10
+ describe "#cornerstone_user" do
11
+
12
+ context "with warden" do
13
+
14
+ include Devise::TestHelpers
15
+
16
+ before do
17
+ Cornerstone::Config.auth_with = [:warden]
18
+ @request.env["devise.mapping"] = Devise.mappings[:user]
19
+ @user = Factory(:user)
20
+ sign_in @user
21
+ end
22
+
23
+ # TODO: Make this test work
24
+ it "should return the signed in user" #do
25
+ # controller.cornerstone_user.should == @user
26
+ # end
27
+
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
@@ -0,0 +1,157 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cornerstone::DiscussionsController do
4
+
5
+ def mock_category(stubs={})
6
+ @mock_category ||= mock_model(Cornerstone::Category, stubs).as_null_object
7
+ end
8
+
9
+ def mock_discussion(stubs={})
10
+ @mock_discussion ||= mock_model(Cornerstone::Discussion, stubs).as_null_object
11
+ end
12
+
13
+ describe "GET index" do
14
+ it "should expose categories for discussion as @categories" do
15
+ Cornerstone::Category.should_receive(:discussions) {mock_category}
16
+ get :index, :use_route => :cornerstone
17
+ assigns[:categories].should equal(mock_category)
18
+ end
19
+ end
20
+
21
+ describe "GET new" do
22
+ it "should expose a new discussion as @discussion" do
23
+ Cornerstone::Discussion.should_receive(:new) {mock_discussion}
24
+ get :new, :use_route => :cornerstone
25
+ assigns[:discussion].should equal(mock_discussion)
26
+ end
27
+
28
+ it "should build a post for the first post of the discussion" do
29
+ Cornerstone::Discussion.stub(:new) {mock_discussion}
30
+ posts_assoc = mock("posts assoc")
31
+ post = mock_model(Cornerstone::Post).as_null_object
32
+ mock_discussion.should_receive(:posts) {posts_assoc}
33
+ posts_assoc.should_receive(:build) {post}
34
+ get :new, :use_route => :cornerstone
35
+ end
36
+
37
+ it "should set the discussion's category if given in parameters" do
38
+ Cornerstone::Category.should_receive(:find).with("1") {mock_category}
39
+ Cornerstone::Discussion.should_receive(:new) {mock_discussion}
40
+ get :new, :cat => "1", :use_route => :cornerstone
41
+ end
42
+
43
+ it "should not set the discussion's category if not given param[:cat]" do
44
+ Cornerstone::Category.should_not_receive(:find)
45
+ Cornerstone::Discussion.should_receive(:new) {mock_discussion}
46
+ get :new, :use_route => :cornerstone
47
+ end
48
+
49
+ it "exposes discussion categories for selection as @categories" do
50
+ Cornerstone::Category.should_receive(:discussions) {[mock_category]}
51
+ get :new, :use_route => :cornerstone
52
+ assigns(:categories).should eql([mock_category])
53
+ end
54
+
55
+ end
56
+
57
+ describe "POST create" do
58
+ context "with valid parameters" do
59
+ it "exposes a newly created discussion as @discussion" do
60
+ Cornerstone::Discussion.should_receive(:new)
61
+ .with({'these' => 'params'}) {mock_discussion :save => true}
62
+ post :create, :discussion => {:these => 'params'}, :use_route => :cornerstone
63
+ assigns(:discussion).should equal(mock_discussion)
64
+ end
65
+
66
+ it "redirects to the discussion" do
67
+ Cornerstone::Discussion.stub!(:new) {mock_discussion(:save => true,
68
+ :category => mock_category)}
69
+ post :create, :discussion => {}, :use_route => :cornerstone
70
+ response.should redirect_to(category_discussion_path(mock_category, mock_discussion))
71
+ end
72
+
73
+ context "and a logged in user" do
74
+ before do
75
+ @user = Factory(:user)
76
+ sign_in @user
77
+ Cornerstone::Discussion.stub!(:new) {mock_discussion(:save => true,
78
+ :category => mock_category)}
79
+ controller.stub!(:current_cornerstone_user) {@user}
80
+ end
81
+
82
+ it "should assign the logged in user as the owner of the discussion" do
83
+ mock_discussion.stub(:user=).with(@user)
84
+ post :create, :discussion => {}, :use_route => :cornerstone
85
+ end
86
+
87
+ it "should assign the logged in user as the owner of the post" do
88
+ posted = mock_model(Cornerstone::Post).as_null_object
89
+ posts = mock("posts")
90
+ mock_discussion.should_receive(:posts) {posts}
91
+ posts.should_receive(:first) {posted}
92
+ posted.should_receive(:user=).with(@user)
93
+ post :create, :discussion => {}, :use_route => :cornerstone
94
+ end
95
+ end
96
+ end
97
+
98
+ context "with invalid parameters" do
99
+ before do
100
+ Cornerstone::Discussion.stub!(:new) {mock_discussion(:save => false)}
101
+ end
102
+
103
+ it "exposes a newly created but unsaved discussion as @discussion" do
104
+ post :create, :discussion => {}, :use_route => :cornerstone
105
+ assigns(:discussion).should equal(mock_discussion)
106
+ end
107
+
108
+ it "it exposes discussion categories for selection as @categories" do
109
+ Cornerstone::Category.should_receive(:discussions) {mock_category}
110
+ post :create, :discussion => {}, :use_route => :cornerstone
111
+ assigns(:categories).should equal(mock_category)
112
+ end
113
+
114
+ it "re-renders the 'new' template" do
115
+ post :create, :discussion => {}, :use_route => :cornerstone
116
+ response.should render_template(:new)
117
+ end
118
+ end
119
+
120
+ end
121
+
122
+ describe "GET Category" do
123
+ it "assigns the given category as @category" do
124
+ Cornerstone::Category.stub_chain(:includes, :find).with("8") {mock_category}
125
+ get :category, :category => "8", :use_route => :cornerstone
126
+ assigns[:category].should == mock_category
127
+ end
128
+ it "assigns the given category's discussions as @discussions" do
129
+ Cornerstone::Category.stub_chain(:includes, :find).with("8") {mock_category}
130
+ mock_category.should_receive(:discussions) {[mock_discussion]}
131
+ get :category, :category => "8", :use_route => :cornerstone
132
+ assigns[:discussions].should == [mock_discussion]
133
+ end
134
+ end
135
+
136
+ describe "GET Show" do
137
+ it "assigns the discussion as @discussion" do
138
+ Cornerstone::Discussion.stub_chain(:includes, :find).with("8") {mock_discussion}
139
+ get :show, :category => mock_category.id, :id => "8", :use_route => :cornerstone
140
+ assigns[:discussion].should == mock_discussion
141
+ end
142
+ it "assigns the discussion's posts as @posts" do
143
+ Cornerstone::Discussion.stub_chain(:includes, :find).with("8") {mock_discussion}
144
+ mock_post = mock_model(Cornerstone::Post).as_null_object
145
+ mock_discussion.should_receive(:posts) {[mock_post]}
146
+ get :show, :category => mock_category.id, :id => "8", :use_route => :cornerstone
147
+ assigns[:posts].should == [mock_post]
148
+ end
149
+ it "creates a new post for the reply form" do
150
+ Cornerstone::Discussion.stub_chain(:includes, :find).with("8") {mock_discussion}
151
+ get :show, :category => mock_category.id, :id => "8", :use_route => :cornerstone
152
+ assigns[:new_post].should be_a_new(Cornerstone::Post)
153
+ end
154
+ end
155
+
156
+ end
157
+
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cornerstone::HelpController do
4
+
5
+ def mock_category(stubs={})
6
+ @mock_category ||= mock_model(Cornerstone::Category, stubs)
7
+ end
8
+
9
+ describe "GET index" do
10
+
11
+ it "exposes all discussion categories as @discussion_categories" do
12
+ Cornerstone::Category.stub_chain(:select, :discussions) {mock_category}
13
+ get :index, :use_route => :cornerstone
14
+ assigns[:discussion_categories].should == mock_category
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+
@@ -0,0 +1,212 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cornerstone::PostsController do
4
+
5
+ def mock_discussion(stubs={})
6
+ @mock_discussion ||= mock_model(Cornerstone::Discussion, stubs).as_null_object
7
+ end
8
+
9
+ def mock_post(stubs={})
10
+ @mock_post ||= mock_model(Cornerstone::Post, stubs).as_null_object
11
+ end
12
+
13
+ describe "POST create" do
14
+ before do
15
+ @discussion = Factory(:discussion)
16
+ end
17
+
18
+ it "finds and exposes the discussion as @discussion" do
19
+ post :create, :discussion_id => @discussion.id, :post => {}, :use_route => :cornerstone
20
+ assigns[:discussion].should == @discussion
21
+ end
22
+
23
+ it "exposes a newly created post as @post from the params" do
24
+ attrs = Factory.attributes_for(:post)
25
+ post :create, :discussion_id => @discussion.id, :post => attrs, :use_route => :cornerstone
26
+ assigns[:post].body.should == attrs[:body]
27
+ assigns[:post].discussion.should == @discussion
28
+ end
29
+
30
+ it "sets the current user if there is one" do
31
+ @user = Factory(:user)
32
+ sign_in @user
33
+ attrs = Factory.attributes_for(:post)
34
+ controller.stub!(:current_cornerstone_user) {@user}
35
+ post :create, :discussion_id => @discussion.id, :post => attrs, :use_route => :cornerstone
36
+ assigns[:post].user.should == @user
37
+ end
38
+
39
+ context "with valid parameters" do
40
+ before do
41
+ Cornerstone::Post.any_instance.stub(:save) {true}
42
+ end
43
+ it "redirects to the discussion" do
44
+ attrs = Factory.attributes_for(:post)
45
+ post :create, :discussion_id => @discussion.id, :post => attrs, :use_route => :cornerstone
46
+ response.should redirect_to(category_discussion_path(@discussion.category, @discussion))
47
+ end
48
+
49
+ context "discussion status" do
50
+ it "is changed to closed if params dictate" do
51
+ attrs = Factory.attributes_for(:post)
52
+ post :create, :discussion_id => @discussion.id, :post => attrs,
53
+ :comment_close => true, :use_route => :cornerstone
54
+ @discussion.reload.status.should == Cornerstone::Discussion::STATUS.last
55
+ end
56
+ it "is changed to open if discussion was previously closed" do
57
+ @discussion.status = Cornerstone::Discussion::STATUS.last
58
+ @discussion.save!
59
+ attrs = Factory.attributes_for(:post)
60
+ post :create, :discussion_id => @discussion.id, :post => attrs,
61
+ :use_route => :cornerstone
62
+ @discussion.reload.status.should == Cornerstone::Discussion::STATUS.first
63
+ end
64
+ end
65
+ end
66
+
67
+ context "with invalid parameters" do
68
+ before do
69
+ Cornerstone::Post.any_instance.stub(:save) {false}
70
+ end
71
+
72
+ it "assigns a new post as @new_post" do
73
+ post :create, :discussion_id => @discussion.id, :post => {}, :use_route => :cornerstone
74
+ assigns[:new_post].should be_a_new(Cornerstone::Post)
75
+ end
76
+
77
+ it "assigns the existing posts as @posts" do
78
+ p = Factory(:post_no_user, :discussion => @discussion)
79
+ post :create, :discussion_id => @discussion.id, :post => {}, :use_route => :cornerstone
80
+ assigns[:posts].should == [p]
81
+ end
82
+
83
+ it "renders the discussion show template" do
84
+ post :create, :discussion_id => @discussion.id, :post => {}, :use_route => :cornerstone
85
+ response.should render_template "cornerstone/discussions/show"
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+ describe "GET edit" do
92
+
93
+ it "assigns the post as @post" do
94
+ Cornerstone::Post.stub_chain(:includes, :find).with("8") {mock_post}
95
+ get :edit, :discussion_id => "2", :id => "8", :use_route => :cornerstone
96
+ assigns[:post].should == mock_post
97
+ end
98
+
99
+ it "assigns the post's discussion as @discussion" do
100
+ Cornerstone::Post.stub_chain(:includes, :find).with("8") {mock_post}
101
+ mock_post.should_receive(:discussion) {mock_discussion}
102
+ get :edit, :discussion_id => "2", :id => "8", :use_route => :cornerstone
103
+ assigns[:discussion].should == mock_discussion
104
+ end
105
+
106
+ it "should render the edit template" do
107
+ Cornerstone::Post.stub_chain(:includes, :find).with("8") {mock_post}
108
+ get :edit, :discussion_id => "2", :id => "8", :use_route => :cornerstone
109
+ response.should render_template :edit
110
+ end
111
+
112
+ it "raises Cornerstone::AccessDenied if the user did not create the post" do
113
+ user = Factory(:user)
114
+ sign_in user
115
+ user2 = Factory(:user)
116
+ post = Factory(:post_w_user, :user => user2)
117
+
118
+ lambda {
119
+ get :edit, :discussion_id => "2", :id => post.id, :use_route => :cornerstone
120
+ }.should raise_error(Cornerstone::AccessDenied)
121
+ end
122
+ end
123
+
124
+ describe "PUT update" do
125
+ it "assigns the post as @post" do
126
+ Cornerstone::Post.stub_chain(:includes, :find).with("8") {mock_post}
127
+ put :update, :discussion_id => "2", :id => "8", :post => {}, :use_route => :cornerstone
128
+ assigns[:post].should == mock_post
129
+ end
130
+
131
+ it "assigns the post's discussion as @discussion" do
132
+ Cornerstone::Post.stub_chain(:includes, :find).with("8") {mock_post}
133
+ mock_post.should_receive(:discussion) {mock_discussion}
134
+ put :update, :discussion_id => "2", :id => "8", :post => {}, :use_route => :cornerstone
135
+ assigns[:discussion].should == mock_discussion
136
+ end
137
+
138
+ it "raises Cornerstone::AccessDenied if the user did not create the post" do
139
+ user = Factory(:user)
140
+ sign_in user
141
+ user2 = Factory(:user)
142
+ post = Factory(:post_w_user, :user => user2)
143
+
144
+ lambda {
145
+ put :update, :discussion_id => "2", :id => post.id, :post => {},
146
+ :use_route => :cornerstone
147
+ }.should raise_error(Cornerstone::AccessDenied)
148
+ end
149
+
150
+ context "with valid parameters" do
151
+ it "updates the post with the params" do
152
+ Cornerstone::Post.stub_chain(:includes, :find).with("8") {mock_post}
153
+ mock_post.should_receive(:update_attributes).with("these" => "params") {true}
154
+ mock_post.stub(:discussion) {mock_discussion}
155
+ put :update, :discussion_id => "2", :id => "8", :post => {"these" => "params"},
156
+ :use_route => :cornerstone
157
+ end
158
+ it "redirects to the discussion" do
159
+ Cornerstone::Post.any_instance.stub(:update_attributes) {true}
160
+ Cornerstone::Post.stub_chain(:includes, :find).with("8") {mock_post}
161
+ mock_post.stub(:discussion) {mock_discussion}
162
+ put :update, :discussion_id => "2", :id => "8", :post => {}, :use_route => :cornerstone
163
+ response.should redirect_to category_discussion_path(mock_discussion.category, mock_discussion)
164
+ end
165
+ end
166
+ context "with in-valid parameters" do
167
+ # TODO: spec not working for render
168
+ # it "renders the edit page" do
169
+ # Cornerstone::Post.stub_chain(:includes, :find).with("8") {mock_post}
170
+ # mock_post.should_receive(:update_attributes) {false}
171
+ # mock_post.stub(:discussion) {mock_discussion}
172
+ # put :update, :discussion_id => "2", :id => "8", :post => {}, :use_route => :cornerstone
173
+ # response.should render_template :edit
174
+ # end
175
+ end
176
+ end
177
+
178
+ describe "DELETE destroy" do
179
+ it "assigns the post as @post" do
180
+ Cornerstone::Post.stub_chain(:includes, :find).with("8") {mock_post}
181
+ mock_post.stub(:discussion) {mock_discussion}
182
+ delete :destroy, :discussion_id => "2", :id => "8", :use_route => :cornerstone
183
+ assigns[:post].should == mock_post
184
+ end
185
+
186
+ it "assigns the post's discussion as @discussion" do
187
+ Cornerstone::Post.stub_chain(:includes, :find).with("8") {mock_post}
188
+ mock_post.should_receive(:discussion) {mock_discussion}
189
+ delete :destroy, :discussion_id => "2", :id => "8", :use_route => :cornerstone
190
+ assigns[:discussion].should == mock_discussion
191
+ end
192
+
193
+ it "redirects to the discussion" do
194
+ Cornerstone::Post.stub_chain(:includes, :find).with("8") {mock_post(:destroy => true)}
195
+ mock_post.stub(:discussion) {mock_discussion}
196
+ delete :destroy, :discussion_id => "2", :id => "8", :use_route => :cornerstone
197
+ response.should redirect_to category_discussion_path(mock_discussion.category, mock_discussion)
198
+ end
199
+
200
+ it "raises Cornerstone::AccessDenied if the user did not create the post" do
201
+ user = Factory(:user)
202
+ sign_in user
203
+ user2 = Factory(:user)
204
+ post = Factory(:post_w_user, :user => user2)
205
+
206
+ lambda {
207
+ delete :destroy, :discussion_id => "2", :id => post.id, :use_route => :cornerstone
208
+ }.should raise_error(Cornerstone::AccessDenied)
209
+ end
210
+ end
211
+
212
+ end