diff --git a/spec/features-json/api_controller_spec.rb b/spec/features-json/api_controller_spec.rb index e5146391..f91de2b9 100644 --- a/spec/features-json/api_controller_spec.rb +++ b/spec/features-json/api_controller_spec.rb @@ -27,9 +27,12 @@ class MySecureApiController < FastlaneCI::APIController describe "unauthenticated request" do it "index is not successful" do get("/") - expect(last_response.status).to eq(401) - expect(json["message"]).to eq("A token must be passed") - expect(json["key"]).to eq("Authentication.Token.Missing") + + expect_json_error( + message: "A token must be passed", + key: "Authentication.Token.Missing", + status: 401 + ) end it "public is successful" do @@ -40,9 +43,11 @@ class MySecureApiController < FastlaneCI::APIController it "private is not successful" do get("/private") - expect(last_response.status).to eq(401) - expect(json["message"]).to eq("A token must be passed") - expect(json["key"]).to eq("Authentication.Token.Missing") + expect_json_error( + message: "A token must be passed", + key: "Authentication.Token.Missing", + status: 401 + ) end end @@ -89,9 +94,11 @@ class MyMixedAuthApiController < FastlaneCI::APIController it "private is not successful" do get("/private") - expect(last_response.status).to eq(401) - expect(json["message"]).to eq("A token must be passed") - expect(json["key"]).to eq("Authentication.Token.Missing") + expect_json_error( + message: "A token must be passed", + key: "Authentication.Token.Missing", + status: 401 + ) end end diff --git a/spec/features-json/artifact_json_controller_spec.rb b/spec/features-json/artifact_json_controller_spec.rb index 9c799ba2..df46fcb7 100644 --- a/spec/features-json/artifact_json_controller_spec.rb +++ b/spec/features-json/artifact_json_controller_spec.rb @@ -54,9 +54,11 @@ project_id = "project_id" get("/data/project/#{project_id}/build/#{build_number}/artifact/not_here") - expect(last_response.status).to eq(404) - expect(json["message"]).to eq("Couldn't find artifact") - expect(json["key"]).to eq("Artifact.Missing") + expect_json_error( + message: "Couldn't find artifact", + key: "Artifact.Missing", + status: 404 + ) end end end diff --git a/spec/features-json/setting_json_controller_spec.rb b/spec/features-json/setting_json_controller_spec.rb index 79d98358..ab3cc7d4 100644 --- a/spec/features-json/setting_json_controller_spec.rb +++ b/spec/features-json/setting_json_controller_spec.rb @@ -49,9 +49,11 @@ non_existent_key = "non_existent_key" post("/data/settings/#{non_existent_key}") - expect(last_response.status).to eq(404) - expect(json["message"]).to eq("`non_existent_key` not found.") - expect(json["key"]).to eq("InvalidParameter.KeyNotFound") + expect_json_error( + message: "`non_existent_key` not found.", + key: "InvalidParameter.KeyNotFound", + status: 404 + ) end end @@ -72,9 +74,11 @@ non_existent_key = "non_existent_key" delete("/data/settings/#{non_existent_key}") - expect(last_response.status).to eq(404) - expect(json["message"]).to eq("`non_existent_key` not found.") - expect(json["key"]).to eq("InvalidParameter.KeyNotFound") + expect_json_error( + message: "`non_existent_key` not found.", + key: "InvalidParameter.KeyNotFound", + status: 404 + ) end end end diff --git a/spec/features-json/user_json_spec.rb b/spec/features-json/user_json_spec.rb index ba0c5a13..475bc83a 100644 --- a/spec/features-json/user_json_spec.rb +++ b/spec/features-json/user_json_spec.rb @@ -37,9 +37,12 @@ def app allow(FastlaneCI::Services.user_service.user_data_source).to receive(:user_exist?).with({ email: fake_email }).and_return(true) post "/api/user", { github_token: "github_token", password: "password" }.to_json, { "CONTENT_TYPE" => "application/json" } - expect(last_response.status).to eq(400) - expect(json["key"]).to eq("User.Error") - expect(json["message"]).to eq("Error creating new user") + + expect_json_error( + message: "Error creating new user", + key: "User.Error", + status: 400 + ) end end end diff --git a/spec/helper_functions.rb b/spec/helper_functions.rb index 87bcb13f..872c1930 100644 --- a/spec/helper_functions.rb +++ b/spec/helper_functions.rb @@ -48,4 +48,11 @@ def notification_service ) ) end + + def expect_json_error(message:, key:, status: nil) + expect(last_response.status).to eq(status) if status + + expect(json["message"]).to eq(message) + expect(json["key"]).to eq(key) + end end