diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ee84da --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.sw* diff --git a/Gemfile b/Gemfile index 938fe2e..6b6e103 100644 --- a/Gemfile +++ b/Gemfile @@ -2,8 +2,6 @@ source "https://rubygems.org" -# gem "rails" - gem "falcon", "~> 0.42.3" gem "roda", "~> 3.72" @@ -11,3 +9,15 @@ gem "roda", "~> 3.72" gem "sequel", "~> 5.72" gem "tilt", "~> 2.2" + +gem "cucumber", "~> 9.0" + +gem "sqlite3", "~> 1.6" + +gem "erubi", "~> 1.12" + +gem "capybara", "~> 3.39" + +gem "rspec", "~> 3.12" + +gem "selenium-webdriver", "~> 4.13" diff --git a/Gemfile.lock b/Gemfile.lock index 1d03b88..d856b94 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,8 @@ GEM remote: https://rubygems.org/ specs: + addressable (2.8.5) + public_suffix (>= 2.0.2, < 6.0) async (2.6.4) console (~> 1.10) fiber-annotation @@ -25,9 +27,45 @@ GEM async (>= 1.25) bigdecimal (3.1.4) build-environment (1.13.0) + builder (3.2.4) + capybara (3.39.2) + addressable + matrix + mini_mime (>= 0.1.3) + nokogiri (~> 1.8) + rack (>= 1.6.0) + rack-test (>= 0.6.3) + regexp_parser (>= 1.5, < 3.0) + xpath (~> 3.2) console (1.23.2) fiber-annotation fiber-local + cucumber (9.0.2) + builder (~> 3.2, >= 3.2.4) + cucumber-ci-environment (~> 9.2, >= 9.2.0) + cucumber-core (~> 11.1, >= 11.1.0) + cucumber-cucumber-expressions (~> 16.1, >= 16.1.2) + cucumber-gherkin (>= 24, < 26.2.1) + cucumber-html-formatter (~> 20.4, >= 20.4.0) + cucumber-messages (>= 19, < 23) + diff-lcs (~> 1.5, >= 1.5.0) + mini_mime (~> 1.1, >= 1.1.5) + multi_test (~> 1.1, >= 1.1.0) + sys-uname (~> 1.2, >= 1.2.3) + cucumber-ci-environment (9.2.0) + cucumber-core (11.1.0) + cucumber-gherkin (>= 24, < 27) + cucumber-messages (>= 19, < 22) + cucumber-tag-expressions (~> 4.1, >= 4.1.0) + cucumber-cucumber-expressions (16.1.2) + cucumber-gherkin (26.2.0) + cucumber-messages (>= 19.1.4, < 22.1) + cucumber-html-formatter (20.4.0) + cucumber-messages (>= 18.0, < 22.1) + cucumber-messages (21.0.1) + cucumber-tag-expressions (4.1.0) + diff-lcs (1.5.0) + erubi (1.12.0) falcon (0.42.3) async async-container (~> 0.16.0) @@ -41,11 +79,19 @@ GEM process-metrics (~> 0.2.0) protocol-rack (~> 0.1) samovar (~> 2.1) + ffi (1.15.5) fiber-annotation (0.2.0) fiber-local (1.0.0) io-event (1.3.2) localhost (1.1.10) mapping (1.1.1) + matrix (0.4.2) + mini_mime (1.1.5) + mini_portile2 (2.8.4) + multi_test (1.1.0) + nokogiri (1.15.4) + mini_portile2 (~> 2.8.2) + racc (~> 1.4) openssl (3.1.0) process-metrics (0.2.1) console (~> 1.8) @@ -60,25 +106,62 @@ GEM protocol-rack (0.2.6) protocol-http (~> 0.23) rack (>= 1.0) + public_suffix (5.0.3) + racc (1.7.1) rack (3.0.8) + rack-test (2.1.0) + rack (>= 1.3) + regexp_parser (2.8.1) + rexml (3.2.6) roda (3.72.0) rack + rspec (3.12.0) + rspec-core (~> 3.12.0) + rspec-expectations (~> 3.12.0) + rspec-mocks (~> 3.12.0) + rspec-core (3.12.2) + rspec-support (~> 3.12.0) + rspec-expectations (3.12.3) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-mocks (3.12.6) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-support (3.12.1) + rubyzip (2.3.2) samovar (2.2.0) console (~> 1.0) mapping (~> 1.0) + selenium-webdriver (4.13.1) + rexml (~> 3.2, >= 3.2.5) + rubyzip (>= 1.2.2, < 3.0) + websocket (~> 1.0) sequel (5.72.0) bigdecimal + sqlite3 (1.6.6) + mini_portile2 (~> 2.8.0) + sys-uname (1.2.3) + ffi (~> 1.1) tilt (2.2.0) timers (4.3.5) traces (0.11.1) + websocket (1.2.10) + xpath (3.2.0) + nokogiri (~> 1.8) PLATFORMS x86_64-freebsd-13 DEPENDENCIES + capybara (~> 3.39) + cucumber (~> 9.0) + erubi (~> 1.12) falcon (~> 0.42.3) roda (~> 3.72) + rspec (~> 3.12) + selenium-webdriver (~> 4.13) sequel (~> 5.72) + sqlite3 (~> 1.6) tilt (~> 2.2) BUNDLED WITH diff --git a/app.rb b/app.rb index fbc0383..4b2b414 100644 --- a/app.rb +++ b/app.rb @@ -2,7 +2,6 @@ require 'roda' require 'securerandom' require 'json' require 'sequel' -require 'logger' class App < Roda DB = Sequel.sqlite('db/url_shortener.db') @@ -20,8 +19,11 @@ class App < Roda r.post "create" do url = r.params['url'] - code = SecureRandom.urlsafe_base64 4 - links.insert(url: url, code: code) + if url.empty? then r.redirect '/' end + if nil == links.filter(:url => url).first + code = SecureRandom.urlsafe_base64 4 + links.insert(url: url, code: code) + end @new_link = 'http://' + request.env['HTTP_HOST'] + '/' + links.filter(:url => url).first[:code] view :create end diff --git a/features/homepage.feature b/features/homepage.feature new file mode 100644 index 0000000..3322c55 --- /dev/null +++ b/features/homepage.feature @@ -0,0 +1,17 @@ +# features/home.feature + +Feature: Homepage + + Loading the homepage is the initial starting point for this tool + + Scenario: Homepage Loads with a form + Given I visit the '/' page + Then I should see text "URL Shortener" + And I should see a form field "url" + And I should see a "Submit" button + + Scenario: Submitting the form without entering a URL + Given I visit the '/' page + When I click the "Submit" button + Then I should be on "/" page + diff --git a/features/step_definitions/steps.rb b/features/step_definitions/steps.rb new file mode 100644 index 0000000..7698ccb --- /dev/null +++ b/features/step_definitions/steps.rb @@ -0,0 +1,29 @@ +# GIVEN + +Given('I visit the {string} page') do |string| + visit string +end + +# WHEN + +When('I click the {string} button') do |string| + click_button {string} +end + +# THEN + +Then('I should see text {string}') do |string| + page.should have_content string +end + +Then('I should see a form field {string}') do |string| + page.should have_field string +end + +Then('I should see a {string} button') do |string| + page.should have_button string +end + +Then('I should be on {string} page') do |string| + page.should have_current_path string +end diff --git a/features/support/env.rb b/features/support/env.rb new file mode 100644 index 0000000..6ee27f9 --- /dev/null +++ b/features/support/env.rb @@ -0,0 +1,13 @@ +require_relative '../../app' +require 'rubygems' +require 'roda' +require 'capybara' +require 'capybara/dsl' +require 'rspec' + + +Capybara.app = App + +include Capybara::DSL +include RSpec::Expectations +include RSpec::Matchers diff --git a/views/create.erb b/views/create.erb index b70ceb7..2cf03d4 100644 --- a/views/create.erb +++ b/views/create.erb @@ -1,2 +1,2 @@ -Home -