From e4390e2ee5b2df20038f12865db462cf1e208ee6 Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 24 Dec 2013 12:23:04 -0800 Subject: Add API tests and some refactoring of messages so we can get a user's messages within the webapp. --- .../test/functional/v1/messages_controller_test.rb | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 users/test/functional/v1/messages_controller_test.rb (limited to 'users/test/functional') diff --git a/users/test/functional/v1/messages_controller_test.rb b/users/test/functional/v1/messages_controller_test.rb new file mode 100644 index 0000000..de2182b --- /dev/null +++ b/users/test/functional/v1/messages_controller_test.rb @@ -0,0 +1,44 @@ +require 'test_helper' + + +class V1::MessagesControllerTest < ActionController::TestCase + + #TODO ensure authentication for all tests here + + setup do + @user = FactoryGirl.build(:user) + @user.save + @message = Message.new(:text => 'a test message') + @message.save + @user_message = UserMessage.new(:message_id => @message.id, :user_id => @user.id) + @user_message.save + end + + teardown do + @user_message.destroy + @user.destroy + @message.destroy + end + + test "get messages for user" do + get :user_messages, :user_id => @user.id + assert response.body.include? @message.text + assert response.body.include? @message.id + end + + test "mark message read for user" do + assert !@user_message.seen + put :mark_read, :user_id => @user.id, :message_id => @message.id + @user_message.reload + assert @user_message.seen + end + + test "do not get seen messages" do + @user_message.seen = true + @user_message.save + get :user_messages, :user_id => @user.id + assert !(response.body.include? @message.text) + assert !(response.body.include? @message.id) + end + +end -- cgit v1.2.3 From 7f12c795207ac818bffac42aa581bf1165f9e424 Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 24 Dec 2013 13:27:22 -0800 Subject: Catching some corner cases & new tests. --- users/test/functional/v1/messages_controller_test.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'users/test/functional') diff --git a/users/test/functional/v1/messages_controller_test.rb b/users/test/functional/v1/messages_controller_test.rb index de2182b..7bffa8f 100644 --- a/users/test/functional/v1/messages_controller_test.rb +++ b/users/test/functional/v1/messages_controller_test.rb @@ -1,6 +1,5 @@ require 'test_helper' - class V1::MessagesControllerTest < ActionController::TestCase #TODO ensure authentication for all tests here @@ -31,6 +30,7 @@ class V1::MessagesControllerTest < ActionController::TestCase put :mark_read, :user_id => @user.id, :message_id => @message.id @user_message.reload assert @user_message.seen + assert_json_response true end test "do not get seen messages" do @@ -41,4 +41,14 @@ class V1::MessagesControllerTest < ActionController::TestCase assert !(response.body.include? @message.id) end + test "empty messages for non-existing user" do + get :user_messages, :user_id => 'some random string' + assert_json_response [] + end + + test "mark read responds even with bad inputs" do + put :mark_read, :user_id => 'nonsense', :message_id => 'more nonsense' + assert_json_response false + end + end -- cgit v1.2.3 From 16b28882aa7659fb89e1661ed8af0c0db72642c8 Mon Sep 17 00:00:00 2001 From: jessib Date: Mon, 30 Dec 2013 15:22:44 -0800 Subject: Change structure to be more no-sql-y, rather than relational. --- .../test/functional/v1/messages_controller_test.rb | 33 ++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'users/test/functional') diff --git a/users/test/functional/v1/messages_controller_test.rb b/users/test/functional/v1/messages_controller_test.rb index 7bffa8f..d7ada82 100644 --- a/users/test/functional/v1/messages_controller_test.rb +++ b/users/test/functional/v1/messages_controller_test.rb @@ -1,20 +1,23 @@ require 'test_helper' class V1::MessagesControllerTest < ActionController::TestCase - + #TODO ensure authentication for all tests here setup do - @user = FactoryGirl.build(:user) - @user.save @message = Message.new(:text => 'a test message') @message.save - @user_message = UserMessage.new(:message_id => @message.id, :user_id => @user.id) - @user_message.save + @user = FactoryGirl.build(:user) + @user.message_ids_to_see << @message.id + @user.save + + # @user_message = UserMessage.new(:message_id => @message.id, :user_id => @user.id) + # @user_message.save + end teardown do - @user_message.destroy + # @user_message.destroy @user.destroy @message.destroy end @@ -26,16 +29,24 @@ class V1::MessagesControllerTest < ActionController::TestCase end test "mark message read for user" do - assert !@user_message.seen + #assert !@user_message.seen + assert @user.message_ids_to_see.include?(@message.id) + assert !@user.message_ids_seen.include?(@message.id) + put :mark_read, :user_id => @user.id, :message_id => @message.id - @user_message.reload - assert @user_message.seen + #@user_message.reload + #assert @user_message.seen + @user.reload + assert !@user.message_ids_to_see.include?(@message.id) + assert @user.message_ids_seen.include?(@message.id) assert_json_response true end test "do not get seen messages" do - @user_message.seen = true - @user_message.save + # @user_message.seen = true + # @user_message.save + put :mark_read, :user_id => @user.id, :message_id => @message.id + @user.reload get :user_messages, :user_id => @user.id assert !(response.body.include? @message.text) assert !(response.body.include? @message.id) -- cgit v1.2.3 From f9c96f8844205afe3c310b5b914752068728d38f Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 31 Dec 2013 11:48:18 -0800 Subject: Cleanup of code for messages API and cron job for 1 month payment warning. Authentication still remaining piece. --- users/test/functional/v1/messages_controller_test.rb | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'users/test/functional') diff --git a/users/test/functional/v1/messages_controller_test.rb b/users/test/functional/v1/messages_controller_test.rb index d7ada82..7666ba3 100644 --- a/users/test/functional/v1/messages_controller_test.rb +++ b/users/test/functional/v1/messages_controller_test.rb @@ -10,14 +10,9 @@ class V1::MessagesControllerTest < ActionController::TestCase @user = FactoryGirl.build(:user) @user.message_ids_to_see << @message.id @user.save - - # @user_message = UserMessage.new(:message_id => @message.id, :user_id => @user.id) - # @user_message.save - end teardown do - # @user_message.destroy @user.destroy @message.destroy end @@ -29,13 +24,10 @@ class V1::MessagesControllerTest < ActionController::TestCase end test "mark message read for user" do - #assert !@user_message.seen assert @user.message_ids_to_see.include?(@message.id) assert !@user.message_ids_seen.include?(@message.id) put :mark_read, :user_id => @user.id, :message_id => @message.id - #@user_message.reload - #assert @user_message.seen @user.reload assert !@user.message_ids_to_see.include?(@message.id) assert @user.message_ids_seen.include?(@message.id) @@ -43,8 +35,6 @@ class V1::MessagesControllerTest < ActionController::TestCase end test "do not get seen messages" do - # @user_message.seen = true - # @user_message.save put :mark_read, :user_id => @user.id, :message_id => @message.id @user.reload get :user_messages, :user_id => @user.id -- cgit v1.2.3 From 47d9b62913789358aefe769de6b7e33da8547891 Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 31 Dec 2013 12:16:43 -0800 Subject: Add authentication to API, but not sure it is best way. --- users/test/functional/v1/messages_controller_test.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'users/test/functional') diff --git a/users/test/functional/v1/messages_controller_test.rb b/users/test/functional/v1/messages_controller_test.rb index 7666ba3..0bc09be 100644 --- a/users/test/functional/v1/messages_controller_test.rb +++ b/users/test/functional/v1/messages_controller_test.rb @@ -2,14 +2,13 @@ require 'test_helper' class V1::MessagesControllerTest < ActionController::TestCase - #TODO ensure authentication for all tests here - setup do @message = Message.new(:text => 'a test message') @message.save @user = FactoryGirl.build(:user) @user.message_ids_to_see << @message.id @user.save + login :is_admin? => true end teardown do @@ -52,4 +51,10 @@ class V1::MessagesControllerTest < ActionController::TestCase assert_json_response false end + test "fails if not admin" do + login :is_admin? => false + get :user_messages, :user_id => @user.id + assert_access_denied + end + end -- cgit v1.2.3 From c7e66852324714a166dd35dc3d5873a0053dcb9b Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 7 Jan 2014 12:57:01 -0800 Subject: Some refactoring, to simplify user model, optimize, and allow messages to be sorted by date (although are not now.) Also, rather than use whenever gem, will have cron job created to call task. --- .../test/functional/v1/messages_controller_test.rb | 45 ++++++++++------------ 1 file changed, 21 insertions(+), 24 deletions(-) (limited to 'users/test/functional') diff --git a/users/test/functional/v1/messages_controller_test.rb b/users/test/functional/v1/messages_controller_test.rb index 0bc09be..24a5b1f 100644 --- a/users/test/functional/v1/messages_controller_test.rb +++ b/users/test/functional/v1/messages_controller_test.rb @@ -3,57 +3,54 @@ require 'test_helper' class V1::MessagesControllerTest < ActionController::TestCase setup do - @message = Message.new(:text => 'a test message') - @message.save @user = FactoryGirl.build(:user) - @user.message_ids_to_see << @message.id @user.save - login :is_admin? => true + @message = Message.new(:text => 'a test message') + @message.user_ids_to_show << @user.id + @message.save end teardown do - @user.destroy @message.destroy + @user.destroy end test "get messages for user" do - get :user_messages, :user_id => @user.id + login @user + get :index assert response.body.include? @message.text assert response.body.include? @message.id end test "mark message read for user" do - assert @user.message_ids_to_see.include?(@message.id) - assert !@user.message_ids_seen.include?(@message.id) - - put :mark_read, :user_id => @user.id, :message_id => @message.id - @user.reload - assert !@user.message_ids_to_see.include?(@message.id) - assert @user.message_ids_seen.include?(@message.id) + login @user + assert @message.user_ids_to_show.include?(@user.id) + assert !@message.user_ids_have_shown.include?(@user.id) + put :update, :id => @message.id + @message.reload + assert !@message.user_ids_to_show.include?(@user.id) + assert @message.user_ids_have_shown.include?(@user.id) assert_json_response true end test "do not get seen messages" do - put :mark_read, :user_id => @user.id, :message_id => @message.id - @user.reload - get :user_messages, :user_id => @user.id + login @user + put :update, :id => @message.id + @message.reload + get :index assert !(response.body.include? @message.text) assert !(response.body.include? @message.id) end - test "empty messages for non-existing user" do - get :user_messages, :user_id => 'some random string' - assert_json_response [] - end test "mark read responds even with bad inputs" do - put :mark_read, :user_id => 'nonsense', :message_id => 'more nonsense' + login @user + put :update, :id => 'more nonsense' assert_json_response false end - test "fails if not admin" do - login :is_admin? => false - get :user_messages, :user_id => @user.id + test "fails if not authenticated" do + get :index, :format => :json assert_access_denied end -- cgit v1.2.3