require 'test_helper' module Vulgata class TranslationStatesControllerTest < ActionDispatch::IntegrationTest include Engine.routes.url_helpers setup do @post = create(:not_translated_post) @translation_state = translation_state_by_locale(@post, :de) german_title = "Mein erster Blogpost" german_body = "Ich liebe Hummus" translation_params = { title: german_title, body: german_body } user = create(:bob_the_translator) @translation_state.save_translation_version translation_params, user @translation_state.reload @routes = Engine.routes end test "should get index" do login_as create(:johnny_the_admin) get translation_states_path assert_response :success end test "should fail on get index because not admin" do login_as create(:bob_the_translator) get translation_states_path assert_redirected_to root_path end test "should get show because admin" do login_as create(:johnny_the_admin) @translation_state = translation_state_by_locale create(:translated_post), :de get translation_state_path(id: @translation_state) assert_response :success end test "should get show because assigned to translation state" do bob_the_translator = create(:bob_the_translator) @translation_state.user = bob_the_translator @translation_state.save login_as bob_the_translator get translation_state_path(id: @translation_state) assert_response :success end test "should fail on show and redirect to root path because not admin" do bob_the_translator = create(:bob_the_translator) login_as bob_the_translator get translation_state_path(id: @translation_state) assert_redirected_to root_path end test "should get edit because admin" do login_as create(:johnny_the_admin) get edit_translation_state_path(id: @translation_state) assert_response :success end test "should fail on edit because not admin" do bob_the_translator = create(:bob_the_translator) login_as bob_the_translator get edit_translation_state_path(id: @translation_state) assert_redirected_to root_path end test "should update translation state" do login_as create(:johnny_the_admin) new_status = :pending new_translator = create(:bob_the_translator) new_priority = 2 patch translation_state_path(id: @translation_state, translation_state: { status: new_status, user_id: new_translator, priority: new_priority }) @translation_state.reload assert_equal new_status, @translation_state.status assert_equal new_translator, @translation_state.user assert_equal new_priority, @translation_state.priority assert_redirected_to translation_state_path(@translation_state) end test "should approve translation state" do login_as create(:johnny_the_admin) patch translation_state_path(id: @translation_state, translation_state: { status: :approved }) @translation_state.reload assert_equal :approved, @translation_state.status assert_redirected_to translation_state_path(@translation_state) end test "should reject translation state" do login_as create(:johnny_the_admin) patch translation_state_path(id: @translation_state, translation_state: { status: :rejected }) @translation_state.reload assert_equal :pending, @translation_state.status assert_redirected_to translation_state_path(@translation_state) end test "should fail on update because not admin" do login_as create(:bob_the_translator) patch translation_state_path(id: @translation_state, translation_state: { priority: 4 }) assert_redirected_to root_path end end end