aboutsummaryrefslogtreecommitdiffstats
path: root/spec/requests/refinery/blog/admin/posts_spec.rb
blob: acd03484476094d97dd213f629a955ebc5c53ac9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require "spec_helper"

module Refinery
  module Blog
    module Admin
      describe Post do
        login_refinery_user

        let!(:blog_category) { FactoryGirl.create(:blog_category, :title => "Video Games") }

        context "when no blog posts" do
          before(:each) { subject.class.destroy_all }

          describe "blog post listing" do
            before(:each) { visit refinery.blog_admin_posts_path }

            it "invites to create new post" do
              page.should have_content("There are no Blog Posts yet. Click \"Create new post\" to add your first blog post.")
            end
          end

          describe "new blog post form" do
            before(:each) do
              visit refinery.blog_admin_posts_path
              click_link "Create new post"
            end

            it "should have Tags" do
              page.should have_content("Tags")
            end

            it "should have Video Games" do
              page.should have_content(blog_category.title)
            end

            describe "create blog post" do
              before(:each) do
                fill_in "Title", :with => "This is my blog post"
                fill_in "post_body", :with => "And I love it"
                check blog_category.title
                click_button "Save"
              end

              it "should succeed" do
                page.should have_content("was successfully added.")
              end

              it "should be the only blog post" do
                subject.class.all.size.should eq(1)
              end

              it "should belong to me" do
                subject.class.first.author.should eq(::Refinery::User.last)
              end

              it "should save categories" do
                subject.class.last.categories.count.should eq(1)
                subject.class.last.categories.first.title.should eq(blog_category.title)
              end
            end

            describe "create blog post with tags" do
              before(:each) do
                @tag_list = "chicago, bikes, beers, babes"
                fill_in "Title", :with => "This is a tagged blog post"
                fill_in "post_body", :with => "And I also love it"
                fill_in "Tags", :with => @tag_list
                click_button "Save"
              end

              it "should succeed" do
                page.should have_content("was successfully added.")
              end

              it "should be the only blog post" do
                subject.class.all.size.should eq(1)
              end

              it "should have the specified tags" do
                subject.class.last.tag_list.sort.should eq(@tag_list.split(', ').sort)
              end
            end
          end
        end

        context "when has blog posts" do
          let!(:blog_post) { FactoryGirl.create(:blog_post) }

          describe "blog post listing" do
            before(:each) { visit refinery.blog_admin_posts_path }

            describe "edit blog post" do
              it "should succeed" do
                page.should have_content(blog_post.title)

                click_link("Edit this blog post")
                current_path.should == refinery.edit_blog_admin_post_path(blog_post)

                fill_in "Title", :with => "hax0r"
                click_button "Save"

                page.should_not have_content(blog_post.title)
                page.should have_content("'hax0r' was successfully updated.")
              end
            end

            describe "deleting blog post" do
              it "should succeed" do
                page.should have_content(blog_post.title)

                click_link "Remove this blog post forever"

                page.should have_content("'#{blog_post.title}' was successfully removed.")
              end
            end

            describe "view live" do
              it "redirects to blog post in the frontend" do
                click_link "View this blog post live"

                current_path.should == refinery.blog_post_path(blog_post)
                page.should have_content(blog_post.title)
              end
            end
          end

          context "when uncategorized post" do
            it "shows up in the list" do
              visit refinery.uncategorized_blog_admin_posts_path
              page.should have_content(blog_post.title)
            end
          end

          context "when categorized post" do
            it "won't show up in the list" do
              blog_post.categories << blog_category
              blog_post.save!

              visit refinery.uncategorized_blog_admin_posts_path
              page.should_not have_content(blog_post.title)
            end
          end
        end

        context "with multiple users" do
          let!(:other_guy) { Factory(:refinery_user, :username => "Other Guy") }

          describe "create blog post with alternate author" do
            before(:each) do
              visit refinery.blog_admin_posts_path
              click_link "Create new post"

              fill_in "Title", :with => "This is some other guy's blog post"
              fill_in "post_body", :with => "I totally didn't write it."

              click_link "Advanced Options"

              select other_guy.username, :from => "Author"

              click_button "Save"
            end

            it "should succeed" do
              page.should have_content("was successfully added.")
            end

            it "belongs to another user" do
              subject.class.last.author.should eq(other_guy)
            end
          end
        end
      end
    end
  end
end