diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index 7db232e3dfc51eefdbdfd7f7ae4e962795a110a8..da6f04b0020ef37e3048ae2c01ecef66a3930c89 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -62,4 +62,42 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase users(user_or_fixture) end end + + # In the post form, this method will select the given tag. + # + # @param tag_name [String] the name of the tag + # @param create_new [Boolean] whether creating a new tag is allowed (default false) + def post_form_select_tag(tag_name, create_new = false) + # First enter the tag name into the select2 search field for the tag + within find_field('Tags (at least one):').find(:xpath, '..') do + find('.select2-search__field').fill_in(with: tag_name) + end + + # Get the first item listed that is not the "Searching..." item + first_option = find('#select2-post_tags_cache-results li:first-child') { |el| el.text != 'Searching…' } + + if first_option.first('span').text == tag_name + # If the text matches the tag name, first check whether we are creating a new tag. + # If so, confirm that we are allowed to. If all is good, actually click on the item. + if create_new || !first_option.text.include?('Create new tag') + first_option.click + else + raise "Expected to find tag with the name #{tag_name}, " \ + 'but could not select it from options without creating a new tag.' + end + elsif create_new + # The first item returned is not the tag we were looking for (another tag partial match + not existing) + # If we are allowed to create a tag, select the last option from the list, which is always the tag creation. + last_option = find('#select2-post_tags_cache-results li:last-child') + if last_option.first('span').text == tag_name + last_option.click + else + raise "Tried to select tag #{tag_name} for creation, but it does not seem to be a presented option." + end + else + # The first item returned is not the tag we were looking for, and we are not allowed to create a tag. + raise "Expected to find tag with the name #{tag_name}, " \ + 'but could not select it from options without creating a new tag.' + end + end end diff --git a/test/system/post_test.rb b/test/system/post_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..9822b98ccf341e7669ad311a88adc534394f33ba --- /dev/null +++ b/test/system/post_test.rb @@ -0,0 +1,27 @@ +require 'application_system_test_case' + +class PostTest < ApplicationSystemTestCase + test 'Not-signed in user cannot create a post' do + visit root_url + click_on 'Create Post' + + assert_current_path new_user_session_url + end + + test 'Signed in user can create a post' do + category = categories(:meta) + log_in :standard_user + visit category_path(category) + click_on 'Create Post' + + fill_in 'Body', with: "When running QPixel, users are generally supposed to be able to create posts.\n" \ + 'Does that actually work?' + fill_in 'Summarize your post with a title:', with: 'Can a signed-in user create a post?' + post_form_select_tag tags(:faq).name + click_on "Save Post in #{category.name}a" + end + + # TODO: Post validations + + # TODO: Sort urls +end \ No newline at end of file