diff --git a/app/models/command/go_to_card.rb b/app/models/command/go_to_card.rb index 06508c129..0943413f3 100644 --- a/app/models/command/go_to_card.rb +++ b/app/models/command/go_to_card.rb @@ -1,7 +1,7 @@ class Command::GoToCard < Command store_accessor :data, :card_id - validates_presence_of :card_id + validates_presence_of :card def title "Visit card '#{card.title}'" diff --git a/app/models/command/go_to_user.rb b/app/models/command/go_to_user.rb index 763a3f351..ffb275f53 100644 --- a/app/models/command/go_to_user.rb +++ b/app/models/command/go_to_user.rb @@ -1,7 +1,7 @@ class Command::GoToUser < Command store_accessor :data, :user_id - validates_presence_of :user_id + validates_presence_of :user def title "View profile of '#{user.name}'" @@ -13,6 +13,6 @@ class Command::GoToUser < Command private def user - User.find(user_id) + User.find_by_id(user_id) end end diff --git a/app/models/command/parser/context.rb b/app/models/command/parser/context.rb index 315c155a6..c4c0721c2 100644 --- a/app/models/command/parser/context.rb +++ b/app/models/command/parser/context.rb @@ -22,7 +22,7 @@ class Command::Parser::Context attr_reader :controller, :action, :params def extract_url_components(url) - uri = URI.parse(url) + uri = URI.parse(url || "") route = Rails.application.routes.recognize_path(uri.path) @controller = route[:controller] @action = route[:action] diff --git a/test/models/command/go_to_card_test.rb b/test/models/command/go_to_card_test.rb new file mode 100644 index 000000000..6078bb298 --- /dev/null +++ b/test/models/command/go_to_card_test.rb @@ -0,0 +1,23 @@ +require "test_helper" + +class Command::GoToCardTest < ActionDispatch::IntegrationTest + include CommandTestHelper + + setup do + @card = cards(:logo) + end + + test "redirects to the card perma" do + result = execute_command "#{@card.id}" + + assert_equal @card, result.url + end + + test "results in a regular search if the card does not exist" do + command = parse_command "123" + assert command.valid? + + result = command.execute + assert_equal cards_path(indexed_by: "newest", terms: [ "123" ]), result.url + end +end diff --git a/test/models/command/go_to_user_test.rb b/test/models/command/go_to_user_test.rb new file mode 100644 index 000000000..bb11648f5 --- /dev/null +++ b/test/models/command/go_to_user_test.rb @@ -0,0 +1,16 @@ +require "test_helper" + +class Command::GoToUserTest < ActionDispatch::IntegrationTest + include CommandTestHelper + + test "redirects to the user perma" do + result = execute_command "@kevin" + + assert_equal users(:kevin), result.url + end + + test "results in an invalid command if the user does not exist" do + command = parse_command "@not_a_user" + assert !command.valid? + end +end diff --git a/test/models/command/parser_test.rb b/test/models/command/parser_test.rb new file mode 100644 index 000000000..c4d442dee --- /dev/null +++ b/test/models/command/parser_test.rb @@ -0,0 +1 @@ +# The parser is tested through the tests of specific commands. See +Command::AssignTests+, etc. diff --git a/test/models/command/search_test.rb b/test/models/command/search_test.rb new file mode 100644 index 000000000..11476620d --- /dev/null +++ b/test/models/command/search_test.rb @@ -0,0 +1,17 @@ +require "test_helper" + +class Command::SearchTest < ActionDispatch::IntegrationTest + include CommandTestHelper + + test "redirects to the cards index filtering by search terms" do + result = execute_command "some text" + + assert_equal cards_path(indexed_by: "newest", terms: [ "some text" ]), result.url + end + + test "respects existing filters" do + result = execute_command "some text", context_url: "http://37signals.fizzy.localhost:3006/cards?collection_ids%5B%5D=#{collections(:writebook).id}" + + assert_equal cards_path(indexed_by: "newest", collection_ids: [collections(:writebook).id], terms: [ "some text" ]), result.url + end +end diff --git a/test/test_helpers/command_test_helper.rb b/test/test_helpers/command_test_helper.rb new file mode 100644 index 000000000..a0d760e8b --- /dev/null +++ b/test/test_helpers/command_test_helper.rb @@ -0,0 +1,13 @@ +module CommandTestHelper + def execute_command(string, context_url: nil, user: users(:david)) + parse_command(string, context_url: context_url, user: user).execute + end + + def parse_command(string, context_url: nil, user: users(:david)) + context = Command::Parser::Context.new(user, url: context_url) + parser = Command::Parser.new(context) + parser.parse(string).tap do |command| + command.user = user if command + end + end +end