Skip to content

Commit

Permalink
tests: update error messages assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdonado committed Jul 31, 2024
1 parent a931894 commit afa9b33
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
42 changes: 18 additions & 24 deletions spec/integration/link_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe "App::Controllers::Link" do
it "should return existing link if url already exists" do
test_user = create_test_user()

payload = {"url" => "https://kagi.com"}
payload = {"url" => "http://idonthavespotify.donado.co"}
post(
"/api/links",
headers: HTTP::Headers{"Content-Type" => "application/json", "X-Api-Key" => test_user.api_key.to_s},
Expand Down Expand Up @@ -75,15 +75,15 @@ describe "App::Controllers::Link" do
payload = {"url" => "https://kagi.com"}
post("/api/links", headers: HTTP::Headers{"Content-Type" => "application/json"}, body: payload.to_json)

expected = {"error" => "Unauthorized"}.to_json
expected = {"error" => "Unauthorized access"}.to_json
response.status_code.should eq(401)
response.body.should eq(expected)
end
end

describe "Index" do
it "should redirect to origin domain" do
link = "https://kagi.com"
link = "https://test.com"
test_user = create_test_user()

test_link = create_test_link(test_user, link)
Expand All @@ -95,7 +95,7 @@ describe "App::Controllers::Link" do
end

it "should create a new click after redirect" do
link = "https://kagi.com"
link = "https://sjdonado.com"
test_user = create_test_user()

test_link = create_test_link(test_user, link)
Expand All @@ -112,17 +112,11 @@ describe "App::Controllers::Link" do
end

it "should return 404 - link does not exist" do
link = "https://kagi.com"
test_user = create_test_user()

test_link = create_test_link(test_user, link)
serialized_link = App::Serializers::Link.new(test_link)

delete_test_link(test_link.id)
get("https://localhost:4001/R4kj2")

get(serialized_link.refer, headers: HTTP::Headers{"X-Api-Key" => test_user.api_key.to_s})

expected = {"error" => "Not Found"}.to_json
expected = {"error" => "Resource not found"}.to_json
response.status_code.should eq(404)
response.body.should eq(expected)
end
Expand All @@ -146,7 +140,7 @@ describe "App::Controllers::Link" do
end

it "should return owned links only" do
links = ["https://google.com", "google.com", "google.com.co", "kagi.com"]
links = ["https://google.de", "google.de", "google.edu.co", "x.com"]
test_user = create_test_user()

links[0..2].each do |link|
Expand All @@ -168,15 +162,15 @@ describe "App::Controllers::Link" do
it "should return 401 - missing api key" do
get "/api/links"

expected = {"error" => "Unauthorized"}.to_json
expected = {"error" => "Unauthorized access"}.to_json
response.status_code.should eq(401)
response.body.should eq(expected)
end
end

describe "Get" do
it "should return the specified link with click details" do
link = "https://kagi.com"
link = "https://bing.com"
test_user = create_test_user()
test_link = create_test_link(test_user, link)

Expand All @@ -192,27 +186,27 @@ describe "App::Controllers::Link" do

get("/api/links/1", headers: HTTP::Headers{"X-Api-Key" => test_user.api_key.to_s})

expected = {"error" => "Not Found"}.to_json
expected = {"error" => "Resource not found"}.to_json
response.status_code.should eq(404)
response.body.should eq(expected)
end

it "should return 401 - missing api key" do
get "/api/links/1"

expected = {"error" => "Unauthorized"}.to_json
expected = {"error" => "Unauthorized access"}.to_json
response.status_code.should eq(401)
response.body.should eq(expected)
end
end

describe "Update" do
it "should update link url" do
link = "https://kagi.com"
link = "https://github.com"
test_user = create_test_user()
test_link = create_test_link(test_user, link)

payload = {"url" => "https://kagi.com.co"}
payload = {"url" => "https://github.com.co"}
put(
"/api/links/#{test_link.id}",
headers: HTTP::Headers{"Content-Type" => "application/json", "X-Api-Key" => test_user.api_key.to_s},
Expand All @@ -233,7 +227,7 @@ describe "App::Controllers::Link" do
body: payload.to_json
)

expected = {"error" => "Not Found"}.to_json
expected = {"error" => "Resource not found"}.to_json
response.status_code.should eq(404)
response.body.should eq(expected)
end
Expand All @@ -246,15 +240,15 @@ describe "App::Controllers::Link" do
body: payload.to_json
)

expected = {"error" => "Unauthorized"}.to_json
expected = {"error" => "Unauthorized access"}.to_json
response.status_code.should eq(401)
response.body.should eq(expected)
end
end

describe "Delete" do
it "should delete link url" do
link = "https://kagi.com"
link = "https://news.ycombinator.com"
test_user = create_test_user()
test_link = create_test_link(test_user, link)

Expand All @@ -268,15 +262,15 @@ describe "App::Controllers::Link" do

delete("/api/links/1", headers: HTTP::Headers{"X-Api-Key" => test_user.api_key.to_s})

expected = {"error" => "Not Found"}.to_json
expected = {"error" => "Resource not found"}.to_json
response.status_code.should eq(404)
response.body.should eq(expected)
end

it "should return 401 - missing api key" do
delete "/api/links/1"

expected = {"error" => "Unauthorized"}.to_json
expected = {"error" => "Unauthorized access"}.to_json
response.status_code.should eq(401)
response.body.should eq(expected)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def create_test_link(user, url)
changeset = App::Lib::Database.insert(link)
unless changeset.valid?
error_messages = changeset.errors.map { |error| "#{error}" }.join(", ")
raise "Test link creation failed: #{error_messages} #{url} #{link.slug}"
raise "Test link creation failed: #{error_messages}"
end

link.clicks = [] of App::Models::Click
Expand Down

0 comments on commit afa9b33

Please sign in to comment.