Commit a77a0edb

mo khan <mo@mokhan.ca>
2013-07-19 22:15:33
add specs for api creations controller
1 parent 693f92b
Changed files (3)
app
spec
app/controllers/api/v1/creations_controller.rb
@@ -5,7 +5,7 @@ module Api
       respond_to :json
 
       def index
-        @creations = Creation.includes(:user, :photos).where(:is_restricted => false).where('photos_count > 0').uniq
+        @creations = Creation.visible_creations
       end
 
       private
app/models/creation.rb
@@ -57,4 +57,8 @@ class Creation < ActiveRecord::Base
       favorites.create({:user_id => user.id})
     end
   end
+
+  def self.visible_creations
+    Creation.includes(:user, :photos).where(:is_restricted => false).where('photos_count > 0').uniq
+  end
 end
spec/controllers/api/v1/creations_controller_spec.rb
@@ -0,0 +1,18 @@
+require "spec_helper"
+
+describe Api::V1::CreationsController do
+  context "when signed in" do
+    let(:creation) { create(:creation) }
+    let(:user) { create(:user) }
+
+    before :each do
+      request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(user.authentication_token)
+      Creation.stub(:visible_creations).and_return([creation])
+      get :index, :format => :json
+    end
+
+    it "should return all viewable creations" do
+      assigns(:creations).should include(creation)
+    end
+  end
+end