Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
Add json_error rspec helper to validate error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
KrauseFx committed Jul 12, 2018
1 parent 49951ce commit 73eeadf
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
25 changes: 16 additions & 9 deletions spec/features-json/api_controller_spec.rb
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions spec/features-json/artifact_json_controller_spec.rb
Expand Up @@ -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
16 changes: 10 additions & 6 deletions spec/features-json/setting_json_controller_spec.rb
Expand Up @@ -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

Expand All @@ -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
9 changes: 6 additions & 3 deletions spec/features-json/user_json_spec.rb
Expand Up @@ -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
7 changes: 7 additions & 0 deletions spec/helper_functions.rb
Expand Up @@ -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

0 comments on commit 73eeadf

Please sign in to comment.