From fd59bb915f6cdbdb19b54d9cb1f5b07624b17df6 Mon Sep 17 00:00:00 2001
From: Marcela Ribeiro de Oliveira <mro15@inf.ufpr.br>
Date: Thu, 3 Nov 2016 08:54:04 -0200
Subject: [PATCH] fix some tests

---
 app/controllers/v1/collections_controller.rb  |  7 ++-
 .../v1/collections_controller_test.rb         | 45 +++++++++++++------
 test/fixtures/collection_item.yml             |  4 ++
 test/fixtures/collections.yml                 |  1 -
 test/fixtures/educational_stages.yml          |  2 +
 test/fixtures/subjects.yml                    |  2 +
 6 files changed, 45 insertions(+), 16 deletions(-)
 create mode 100644 test/fixtures/collection_item.yml
 create mode 100644 test/fixtures/educational_stages.yml
 create mode 100644 test/fixtures/subjects.yml

diff --git a/app/controllers/v1/collections_controller.rb b/app/controllers/v1/collections_controller.rb
index 6df123fd..eb09094a 100644
--- a/app/controllers/v1/collections_controller.rb
+++ b/app/controllers/v1/collections_controller.rb
@@ -60,14 +60,17 @@ class V1::CollectionsController < ApplicationController
 
   # POST /v1/collections/1/items
   def add_object
-    return render nothing: true, status: :unprocessable_entity if params.nil? || items_params[:items].nil?
+    return render nothing: true, status: :unprocessable_entity if items_params.blank? || items_params[:items].blank?
     @collection.add_items(items_params[:items])
     render json: @collection, status: :ok
   end
 
   # DELETE /v1/collections/1/items
   def delete_object
-    return render nothing: true, status: :unprocessable_entity if params.nil? || items_params[:items].nil?
+    Rails.logger.fatal "=============================="
+    Rails.logger.fatal items_params.inspect
+    Rails.logger.fatal params.inspect
+    return render nothing: true, status: :unprocessable_entity if items_params.blank? || items_params[:items].blank?
     @collection.delete_items(items_params[:items])
     render json: @collection, status: :ok
   end
diff --git a/test/controllers/v1/collections_controller_test.rb b/test/controllers/v1/collections_controller_test.rb
index db1faee7..11d766b0 100644
--- a/test/controllers/v1/collections_controller_test.rb
+++ b/test/controllers/v1/collections_controller_test.rb
@@ -8,12 +8,28 @@ class V1::CollectionsControllerTest < ActionController::TestCase
     skip('Unsolved issue: ArgumentError: wrong number of arguments (2 for 0)')
   end
 
-  test 'should user post collection to create and return :created code' do
+ test 'should user post collection to create and return :created code' do
+      auth_request users(:jack)
+      post :create, params: { collection: { name: 'my collection',
+                                  description: 'testing collection',
+                                  owner_type: 'User', owner_id: users(:jack).id,
+                                  items: [{id: learning_objects(:user_lo).id, type: "LearningObject"}],
+                                  tags: [{"name":"teste"}, {"name": "tag de teste"}],
+                                  subjects: [{id: subjects(:subject_one).id}],
+                                  educational_stages: [{id: educational_stages(:stage_teste).id}]}
+                            }
+      assert_response :created
+  end
+
+  test 'should institution post collection to create and return :created code' do
     auth_request users(:jack)
-    post :create, params: { collection: { name: 'my collection', description: 'testing collection', owner_type: 'User', owner_id: users(:jack).id, items: [{id: learning_objects(:user_lo).id, type: "LearningObject"}]} } assert_response :created end test 'should institution post collection to create and return :created code' do auth_request users(:jack) post :create, params: { collection: { name: 'my collection',
+    post :create, params: { collection: { name: 'my collection',
                                description: 'testing collection',
                                owner_type: 'Institution', owner_id: institutions(:ufpr).id,
-                               items: [{id: learning_objects(:user_lo).id, type: "LearningObject"}]}
+                               items: [{id: learning_objects(:user_lo).id, type: "LearningObject"}],
+                               subjects: [{id: subjects(:subject_one).id}],
+                                 tags: [{"name":"teste"}, {"name": "tag de teste"}],
+                               educational_stages: [{id: educational_stages(:stage_teste).id}]}
                           }
     assert_response :created
   end
@@ -27,24 +43,27 @@ class V1::CollectionsControllerTest < ActionController::TestCase
   end
 
   test 'should user add items to collection and return :ok code' do
-    post :add_items, params: { id: collections(:ufpr).id, items: { id: learning_objects(:lo_metadata2).id, type: 'LearningObject'} }
+    auth_request users(:john)
+    post :add_object, params: { id: collections(:ufpr).id, collection: {items: [{ id: learning_objects(:lo_metadata2).id, type: 'LearningObject'}] }}
 
     assert_response :ok
   end
 
   test 'should user remove items to collection and return :ok code' do
-    post :add_items, params: { id: collections(:ufpr).id, items: { id: learning_objects(:lo_metadata).id, type: 'LearningObject'} }
+    auth_request users(:john)
+    post :add_object, params: { id: collections(:ufpr).id, collection: { items: [{ id: learning_objects(:lo_metadata).id, type: 'LearningObject'}] }}
     assert_response :ok
   end
 
-  test 'should user add items to collection and return :unprocessable_entity code' do post :add_items, params: { id: collections(:ufpr).id, items: { id: learning_objects(:lo_metadata2).id, type: 'LearningObject'} }
-    post :add_items, params: { id: collections(:ufpr).id }
-
+  test 'should user add items to collection and return :unprocessable_entity code' do
+    auth_request users(:john)
+    post :add_object, params: { id: collections(:ufpr).id, collection: { items: [] } }
     assert_response :unprocessable_entity
   end
 
   test 'should user remove items to collection and return :unprocessable_entity code' do
-    post :add_items, params: { id: collections(:ufpr).id }
+    auth_request users(:john)
+    post :add_object, params: { id: collections(:ufpr).id }
     assert_response :unprocessable_entity
   end
 
@@ -53,7 +72,7 @@ class V1::CollectionsControllerTest < ActionController::TestCase
     auth_request users(:jack)
     tag = tags(:tag_three).name
 
-    post :tagging, params: { id: collections(:ufpr).id, tag: { name: tag, owner_id: users(:jack).id, owner_type: 'User' } }
+    post :tagging, params: { id: collections(:ufpr).id, collection: {tag: { name: tag } }}
     assert_response :created
   end
 
@@ -62,7 +81,7 @@ class V1::CollectionsControllerTest < ActionController::TestCase
     auth_request users(:john)
     tag = tags(:tag_one).name
 
-    post :untagging, params: { id: collections(:ufpr).id, tag: { name: tag, owner_id: users(:jack).id, owner_type: 'User' } }
+    post :untagging, params: { id: collections(:ufpr).id, collection:{ tags: { name: tag } }}
     assert_response :ok
   end
 
@@ -73,7 +92,7 @@ class V1::CollectionsControllerTest < ActionController::TestCase
     institution.users << users(:jack)
     tag = tags(:tag_one).name
 
-    post :tagging, params: { id: collections(:ufpr).id, tag: { name: tag, owner_id: institutions(:ufpr).id, owner_type: 'Institution' } }
+    post :tagging, params: { id: collections(:ufpr).id, collection: { tags: { name: tag } }}
     assert_response :created
   end
 
@@ -83,7 +102,7 @@ class V1::CollectionsControllerTest < ActionController::TestCase
     institution.users << users(:john)
     tag = tags(:tag_two).name
 
-    delete :untagging, params: { id: collections(:ufpr).id, tag: { name: tag , owner_id: institutions(:ufpr).id, owner_type: 'Institution' } }
+    delete :untagging, params: { id: collections(:ufpr).id, collection: { tags: { name: tag } }}
     assert_response :ok
   end
 
diff --git a/test/fixtures/collection_item.yml b/test/fixtures/collection_item.yml
new file mode 100644
index 00000000..2a2af3f0
--- /dev/null
+++ b/test/fixtures/collection_item.yml
@@ -0,0 +1,4 @@
+item1:
+  collectionable_id: :user_lo.id (User)
+  collectionable_type: 'LearningObject'
+  order: 1
diff --git a/test/fixtures/collections.yml b/test/fixtures/collections.yml
index 249c1783..1482a161 100644
--- a/test/fixtures/collections.yml
+++ b/test/fixtures/collections.yml
@@ -2,4 +2,3 @@ ufpr:
   name: 'UFPR collection'
   owner: john (User)
   privacy: 'public'
-  items: lo_metadata (LearningObject)
diff --git a/test/fixtures/educational_stages.yml b/test/fixtures/educational_stages.yml
new file mode 100644
index 00000000..74f833f1
--- /dev/null
+++ b/test/fixtures/educational_stages.yml
@@ -0,0 +1,2 @@
+stage_teste:
+  name: "teste"
diff --git a/test/fixtures/subjects.yml b/test/fixtures/subjects.yml
new file mode 100644
index 00000000..2cbfceca
--- /dev/null
+++ b/test/fixtures/subjects.yml
@@ -0,0 +1,2 @@
+subject_one:
+  name: 'teste'
-- 
GitLab