Skip to content

Commit

Permalink
Add api test
Browse files Browse the repository at this point in the history
  • Loading branch information
CuddlyBunion341 committed Nov 5, 2024
1 parent cdb8fb2 commit a4031ec
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 11 deletions.
22 changes: 11 additions & 11 deletions app/controllers/moirai/translation_files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def open_pr
Moirai::PullRequestCreator.new.create_pull_request(changes)

respond_to do |format|
format.html { redirect_back_or_to(root_path, notice: "I created an amazing Pull Request", status: :ok) }
format.html { redirect_back_or_to(root_path, notice: "I created an amazing Pull Request") }
format.json { render json: {message: "Pull Request created"}, status: :ok }
end
end
Expand All @@ -53,22 +53,22 @@ def handle_update(translation)
if translation_params[:value].blank? || translation_same_as_current?
translation.destroy
respond_to do |format|
format.html { redirect_to_translation_file(translation.file_path, notice: "Translation #{translation.key} was successfully deleted.", status: :ok) }
format.html { redirect_to_translation_file(translation.file_path, notice: "Translation #{translation.key} was successfully deleted.") }
format.json { render json: {message: "Translation deleted"}, status: :ok }
end
return
end

if translation.update(value: translation_params[:value])
respond_to do |format|
format.html { redirect_to_translation_file(translation.file_path, notice: "Translation #{translation.key} was successfully updated.", status: :ok) }
format.html { redirect_to_translation_file(translation.file_path, notice: "Translation #{translation.key} was successfully updated.") }
format.json { render json: {message: "Translation updated", translation: translation}, status: :ok }
end
else
respond_to do |format|
format.html {
flash.now[:alert] = translation.errors.full_messages.join(", ")
redirect_back(fallback_location: root_path, status: :unprocessable_entity)
redirect_back(fallback_location: root_path)
}
format.json { render json: {errors: translation.errors.full_messages}, status: :unprocessable_entity }
end
Expand All @@ -80,7 +80,7 @@ def handle_create
respond_to do |format|
format.html {
flash.now[:alert] = "Translation #{translation_params[:key]} already exists."
redirect_back(fallback_location: root_path, status: :unprocessable_entity)
redirect_back(fallback_location: root_path)
}
format.json { render json: {errors: ["Translation already exists"]}, status: :unprocessable_entity }
end
Expand All @@ -91,24 +91,24 @@ def handle_create

if translation.save
respond_to do |format|
format.html { redirect_to_translation_file(translation.file_path, notice: "Translation #{translation.key} was successfully created.", status: :ok) }
format.html { redirect_to_translation_file(translation.file_path, notice: "Translation #{translation.key} was successfully created.") }
format.json { render json: {message: "Translation created", translation: translation}, status: :ok }
end
else
respond_to do |format|
format.html {
flash.now[:alert] = translation.errors.full_messages.join(", ")
redirect_back(fallback_location: root_path, status: :unprocessable_entity)
redirect_back(fallback_location: root_path)
}
format.json { render json: {errors: translation.errors.full_messages}, status: :unprocessable_entity }
end
end
end

def redirect_to_translation_file(file_path, notice: nil, status: :ok)
def redirect_to_translation_file(file_path, notice: nil)
respond_to do |format|
format.html { redirect_to moirai_translation_file_path(Digest::SHA256.hexdigest(file_path)), notice: notice, status: status }
format.json { render json: {message: notice}, status: status }
format.html { redirect_to moirai_translation_file_path(Digest::SHA256.hexdigest(file_path)), notice: notice }
format.json { render json: {message: notice} }
end
end

Expand All @@ -118,7 +118,7 @@ def set_translation_file
respond_to do |format|
format.html {
flash[:alert] = "File not found"
redirect_to moirai_translation_files_path, status: :not_found
redirect_to moirai_translation_files_path
}
format.json { render json: {errors: ["File not found"]}, status: :not_found }
end
Expand Down
148 changes: 148 additions & 0 deletions test/controllers/moirai/translation_files_controller_api_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
require "test_helper"

class TranslationFilesControllerTest < ActionDispatch::IntegrationTest
setup do
@translation = Moirai::Translation.create(key: "locales.german", locale: "de", value: "Neudeutsch")
end

# Index action tests
test "index displays translation files" do
get index_url, as: :json

assert_response :success
json_response = JSON.parse(response.body)
assert json_response.any? { |file| file["name"] == "de.yml" }
assert json_response.any? { |file| file["name"] == "en.yml" }
assert json_response.any? { |file| file["name"] == "it.yml" }
end

# Show action tests
test "show existing translation file" do
get translation_file_url("config/locales/en.yml"), as: :json
assert_response :success

json_response = JSON.parse(response.body)
assert_equal "en", json_response["locale"]
assert json_response["translation_keys"].is_a?(Hash)
assert json_response["translations"].is_a?(Array)
end

test "show non-existing translation file" do
get translation_file_url("does_not_exist.yml"), as: :json
assert_response :not_found
end

# Create action tests
test "create translation with valid params" do
translation_count_before = Moirai::Translation.count
post translation_files_url, params: {translation: {key: "locales.german", locale: "en", value: "New Translation"}}, as: :json

assert_response :ok
json_response = JSON.parse(response.body)
assert_equal "Translation created", json_response["message"]
assert_equal "locales.german", json_response["translation"]["key"]
assert_equal "New Translation", json_response["translation"]["value"]
assert_equal translation_count_before + 1, Moirai::Translation.count
end

test "create translation with existing value" do
translation_count_before = Moirai::Translation.count

post translation_files_url, params: {translation: {key: "locales.german", locale: "en", value: "German"}}, as: :json

assert_response :unprocessable_entity
json_response = JSON.parse(response.body)
assert_includes json_response["errors"], "Translation already exists"
assert_equal translation_count_before, Moirai::Translation.count
end

test "create translation with invalid params" do
translation_count_before = Moirai::Translation.count

post translation_files_url, params: {translation: {key: "", locale: "", value: ""}}, as: :json

assert_response :unprocessable_entity
json_response = JSON.parse(response.body)
assert_includes json_response["errors"], "Key can't be blank"
assert_includes json_response["errors"], "Locale can't be blank"
assert_includes json_response["errors"], "Value can't be blank"
assert_equal translation_count_before, Moirai::Translation.count
end

# Update action tests
test "update translation with blank value" do
count_before = Moirai::Translation.count
post translation_files_url, params: {translation: {key: "locales.german", locale: "de", value: ""}}, as: :json

assert_response :ok
json_response = JSON.parse(response.body)
assert_equal "Translation deleted", json_response["message"]
assert_equal count_before - 1, Moirai::Translation.count
end

test "update translation with non-blank new value" do
post translation_files_url, params: {translation: {key: "locales.german", locale: "de", value: "Hochdeutsch"}}, as: :json

assert_response :ok
json_response = JSON.parse(response.body)
assert_equal "Translation updated", json_response["message"]
assert_equal "locales.german", json_response["translation"]["key"]
assert_equal "Hochdeutsch", json_response["translation"]["value"]
end

test "update translation with value from file" do
count_before = Moirai::Translation.count
post translation_files_url, params: {translation: {key: "locales.german", locale: "de", value: "Deutsch"}}, as: :json

assert_response :ok
json_response = JSON.parse(response.body)
assert_equal "Translation deleted", json_response["message"]
assert_equal count_before - 1, Moirai::Translation.count
end

test "creates a pull request with all the file changes" do
Moirai::Translation.create!(key: "locales.italian",
locale: "de",
value: "Italianese")

Moirai::Translation.create!(key: "locales.italian",
locale: "it",
value: "Italianese")

post moirai.moirai_open_pr_path, as: :json

assert_response :ok
json_response = JSON.parse(response.body)
assert_equal "Pull Request created", json_response["message"]

@pull_request_creator = Moirai::PullRequestCreator.new

pr = @pull_request_creator.existing_open_pull_request

assert pr
file = @pull_request_creator.github_client.contents(@pull_request_creator.github_repo_name,
path: "./config/locales/it.yml",
ref: @pull_request_creator.branch_name)
pr_file_content = Base64.decode64(file.content)
proposed_translations = YAML.load(pr_file_content, symbolize_names: true)
assert "Italianese", proposed_translations.dig(:it, :locales, :italian)

@pull_request_creator.cleanup
refute @pull_request_creator.existing_open_pull_request
end

private

def index_url
"/moirai"
end

def translation_files_url
"/moirai/translation_files"
end

def translation_file_url(local_path)
absolute_path = Rails.root.join(local_path).to_s
"/moirai/translation_files/#{Digest::SHA256.hexdigest(absolute_path)}"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class TranslationFilesControllerTest < ActionDispatch::IntegrationTest
post translation_files_url, params: {translation: {key: "locales.german", locale: "de", value: "Hochdeutsch"}}

assert_response :ok
assert_redirected_to translation_file_url("config/locales/en.yml")
assert_equal "Translation locales.german was successfully updated.", flash[:notice]
assert_equal Moirai::Translation.last.key, "locales.german"
assert_equal Moirai::Translation.last.value, "Hochdeutsch"
Expand Down

0 comments on commit a4031ec

Please sign in to comment.