From beb9b0c14a639fabd0e3374d25bf579b48604855 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Sat, 7 Oct 2023 13:27:00 -0700 Subject: [PATCH 1/5] added new scenario --- features/homepage.feature | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/features/homepage.feature b/features/homepage.feature index 2bccdcc..c0750d9 100644 --- a/features/homepage.feature +++ b/features/homepage.feature @@ -39,3 +39,8 @@ Feature: Homepage Given A link already exists with the url "https://google.com" and code "aaaaaa" When I visit the "/aaaaaa" location Then I should be redirected to "https://google.com" + + Scenario: Accessing a nonexistent URL code + Given I visit the "/aaaaaa" location + Then I should be redirected to "/" + And I should see the message "Link aaaaaa doesn't exist" -- 2.41.0 From b97ee3f3bba399f48092d1adaf8ba8fbb80e6a80 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Sat, 7 Oct 2023 13:37:51 -0700 Subject: [PATCH 2/5] use the correct clause format since staying on site --- features/homepage.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/homepage.feature b/features/homepage.feature index c0750d9..6394745 100644 --- a/features/homepage.feature +++ b/features/homepage.feature @@ -42,5 +42,5 @@ Feature: Homepage Scenario: Accessing a nonexistent URL code Given I visit the "/aaaaaa" location - Then I should be redirected to "/" + Then I should be on "/" page And I should see the message "Link aaaaaa doesn't exist" -- 2.41.0 From f8499a79c2c8cd6b5e495886dc436010945bd55a Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Sat, 7 Oct 2023 13:38:10 -0700 Subject: [PATCH 3/5] check if the link exists, redirects home if it doesn't --- app.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app.rb b/app.rb index 54554b6..44dae4c 100644 --- a/app.rb +++ b/app.rb @@ -18,8 +18,12 @@ class App < Roda end r.get String do | url_code | - link = links.filter(:code => url_code).first[:url] - r.redirect link + link = links.filter(:code => url_code) + if link.first.nil? + flash['message'] = "Link #{url_code} doesn't exist" + r.redirect '/' + end + r.redirect link.first[:url] end r.post "create" do -- 2.41.0 From c7fee27623fc152a17614c2a549def557393e391 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Sat, 7 Oct 2023 21:10:16 -0700 Subject: [PATCH 4/5] update for missing link scenario + if the link isn't found, don't redirect it, keep it on the same page + it should have a status code of 404 + it should have the form fields as well + added page status check --- features/homepage.feature | 5 ++++- features/step_definitions/steps.rb | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/features/homepage.feature b/features/homepage.feature index 6394745..9803b98 100644 --- a/features/homepage.feature +++ b/features/homepage.feature @@ -42,5 +42,8 @@ Feature: Homepage Scenario: Accessing a nonexistent URL code Given I visit the "/aaaaaa" location - Then I should be on "/" page + Then I should be on "/aaaaaa" page + And The status code should be 404 And I should see the message "Link aaaaaa doesn't exist" + And I should see a form field "url" + And I should see a "Submit" button diff --git a/features/step_definitions/steps.rb b/features/step_definitions/steps.rb index 68111a3..d1581a3 100644 --- a/features/step_definitions/steps.rb +++ b/features/step_definitions/steps.rb @@ -3,6 +3,7 @@ Before('@db-test') do @links = Sequel.sqlite("db/#{ENV['DB_NAME']}")[:links] end + # GIVEN Given('I visit the {string} page') do |string| @@ -17,6 +18,7 @@ Given('A link already exists with the url {string} and code {string}') do |url, @links.insert(url: url, code: code) end + # WHEN When('I click the {string} button') do |string| @@ -31,6 +33,7 @@ When('I visit the {string} location') do |string| visit string end + # THEN Then('I should see text {string}') do |string| @@ -58,6 +61,12 @@ Then('I should be redirected to {string}') do |string| location = actual.index(string) location.should equal(0) end + +Then('The status code should be {int}') do |code| + puts page.status_code.should eq(code) +end + + # AFTER After('@db-test') do -- 2.41.0 From 5bce31baf283a051d1213c30601355494939d8d5 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Sat, 7 Oct 2023 21:11:12 -0700 Subject: [PATCH 5/5] refactored missing link scenario to make more sense + 404 code, stay on the current page, and load the form + also simplified the logic for the create action if the link doesn't already exist --- app.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app.rb b/app.rb index 44dae4c..2e49f5e 100644 --- a/app.rb +++ b/app.rb @@ -19,11 +19,10 @@ class App < Roda r.get String do | url_code | link = links.filter(:code => url_code) - if link.first.nil? - flash['message'] = "Link #{url_code} doesn't exist" - r.redirect '/' - end - r.redirect link.first[:url] + r.redirect link.first[:url] unless link.first.nil? + @message = "Link #{url_code} doesn't exist" + response.status = 404 + view :home end r.post "create" do @@ -32,7 +31,7 @@ class App < Roda flash['message'] = "Please enter a valid URL"; r.redirect '/' end - if nil == links.filter(:url => url).first + if links.filter(:url => url).first.nil? code = SecureRandom.urlsafe_base64 4 links.insert(url: url, code: code) @message = "Link created" -- 2.41.0