From c51c0afcf20f4c162662a727f22128e4ba89ce2a Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Tue, 3 Oct 2023 15:55:37 -0700 Subject: [PATCH 01/10] ignore the local env file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 89f27ac..6b1c8a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.sw* .bundle *.db +.env.rb From fff6b0f5ddeb84201f2fa1245f86982da1685cca Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Tue, 3 Oct 2023 15:55:59 -0700 Subject: [PATCH 02/10] added more notes for the README --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cd93b2e..8f82e10 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,13 @@ if you want the development group included run this first: `bundle config set --local with 'development' +then create a .env.rb file in the root directory that contains the following ENV attributes: +APP_SESSION_SECRET +DB_NAME + after the dependencies are installed, you have to create the db -`sequel -m db/migrations sqlite://db/url_shortener.db` +`sequel -m db/migrations sqlite://db/{DB_NAME}` to start the application with Falcon: From b2b41f1aa18793529cc94f8579f922609e4a458d Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Tue, 3 Oct 2023 15:56:42 -0700 Subject: [PATCH 03/10] added flash plugin + added flash plugin, and the session plugin required by flash + set the DB name as an environment variable + set @message variable where it needs to be set to show on the UI --- app.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app.rb b/app.rb index 4b2b414..54554b6 100644 --- a/app.rb +++ b/app.rb @@ -4,11 +4,16 @@ require 'json' require 'sequel' class App < Roda - DB = Sequel.sqlite('db/url_shortener.db') - links = DB[:links] + plugin :sessions, secret: ENV.delete('APP_SESSION_SECRET') plugin :render, escape: true + plugin :flash + + DB = Sequel.sqlite("db/#{ENV['DB_NAME']}") + links = DB[:links] + route do |r| r.root do + @message = flash['message'] || "Enter a URL" view :home end @@ -19,11 +24,16 @@ class App < Roda r.post "create" do url = r.params['url'] - if url.empty? then r.redirect '/' end + if url.empty? + flash['message'] = "Please enter a valid URL"; + r.redirect '/' + end if nil == links.filter(:url => url).first code = SecureRandom.urlsafe_base64 4 links.insert(url: url, code: code) + @message = "Link created" end + @message ||= "Link exists" @new_link = 'http://' + request.env['HTTP_HOST'] + '/' + links.filter(:url => url).first[:code] view :create end From 84a047fb0c684541891a80bd031b6b69636dd72a Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Tue, 3 Oct 2023 15:58:01 -0700 Subject: [PATCH 04/10] require the .env file to load ENV variables set in there --- config.ru | 1 + 1 file changed, 1 insertion(+) diff --git a/config.ru b/config.ru index 90dc7d6..02b347a 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,4 @@ +require './.env' require './app' run App.app From 8639c785b7a09d095f3a74af60643de82267372d Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Tue, 3 Oct 2023 15:58:33 -0700 Subject: [PATCH 05/10] added a spot for messages to show up in the layout --- views/layout.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/views/layout.erb b/views/layout.erb index e1b8a4b..a15f97c 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -6,6 +6,7 @@

URL Shortener

+

<%== @message %>

<%== yield %>
From 8b2780820c20cedd66ac9b914a514744c6cc0a42 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Tue, 3 Oct 2023 15:58:56 -0700 Subject: [PATCH 06/10] update for features + added flash/message assertions + adjusted urls to be http://google.com for tests for now + added @db-test tags to tests that use db interactions + added a scenario for duplicate URLs being used + added before and after steps to init and clean up db tests + added initialization of a test db for tests instead of interacting with the production database --- features/homepage.feature | 15 ++++++++++++++- features/step_definitions/steps.rb | 19 +++++++++++++++++++ features/support/env.rb | 16 ++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/features/homepage.feature b/features/homepage.feature index 9215df1..4a96c4a 100644 --- a/features/homepage.feature +++ b/features/homepage.feature @@ -7,6 +7,7 @@ Feature: Homepage Scenario: Homepage Loads with a form Given I visit the "/" page Then I should see text "URL Shortener" + And I should see the message "Enter a URL" And I should see a form field "url" And I should see a "Submit" button @@ -14,9 +15,21 @@ Feature: Homepage Given I visit the "/" page When I click the "Submit" button Then I should be on "/" page + And I should see the message "Please enter a valid URL" + @db-test Scenario: Submitting the form with a correct URL Given I visit the "/" page - When I type "http://google.com/" in the "url" field + When I type "http://google.com" in the "url" field And I click the "Submit" button Then I should be on "/create" page + And I should see the message "Link created" + + @db-test + Scenario: Submitting the form with an existing URL + Given I visit the "/" page + And A link already exists with the url "http://google.com" + When I type "http://google.com" in the "url" field + And I click the "Submit" button + Then I should be on "/create" page + And I should see the message "Link exists" diff --git a/features/step_definitions/steps.rb b/features/step_definitions/steps.rb index 78eb083..9bc830e 100644 --- a/features/step_definitions/steps.rb +++ b/features/step_definitions/steps.rb @@ -1,9 +1,18 @@ +# BEFORE +Before('@db-test') do + @links = Sequel.sqlite("db/#{ENV['DB_NAME']}")[:links] +end + # GIVEN Given('I visit the {string} page') do |string| visit string end +Given('A link already exists with the url {string}') do |string| + @links.insert(url: string, code: "aaaaaa") +end + # WHEN When('I click the {string} button') do |string| @@ -31,3 +40,13 @@ end Then('I should be on {string} page') do |string| page.should have_current_path string end + +Then('I should see the message {string}') do |message| + page.should have_selector '#message', text: message +end + +# AFTER + +After('@db-test') do + @links.delete +end diff --git a/features/support/env.rb b/features/support/env.rb index 6ee27f9..c5ab798 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -1,11 +1,27 @@ +require_relative '../../.env' +ENV["DB_NAME"] = "test_#{ENV["DB_NAME"]}" require_relative '../../app' require 'rubygems' require 'roda' +require 'sequel' require 'capybara' require 'capybara/dsl' require 'rspec' +# DB initialization +Sequel.extension :migration +Sequel.sqlite("db/#{ENV['DB_NAME']}") do |db| + Sequel::Migrator.apply(db, "db/migrations") +end +# RSpec db setup +RSpec.configure do |c| + c.around(:each) do |example| + DB.transaction(rollback: :always, auto_savepoint: true){example.run} + end +end + +# attach app to Capybara Capybara.app = App include Capybara::DSL From 5ca4bf707b84221ec5cb48fc2b7f264d07d45232 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Tue, 3 Oct 2023 16:02:33 -0700 Subject: [PATCH 07/10] removed rspec db setup, i probably didn't have it set right to clean up after each task, implemented that in the before and after steps --- features/support/env.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/features/support/env.rb b/features/support/env.rb index c5ab798..5307da0 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -14,13 +14,6 @@ Sequel.sqlite("db/#{ENV['DB_NAME']}") do |db| Sequel::Migrator.apply(db, "db/migrations") end -# RSpec db setup -RSpec.configure do |c| - c.around(:each) do |example| - DB.transaction(rollback: :always, auto_savepoint: true){example.run} - end -end - # attach app to Capybara Capybara.app = App From f0bad17e896a60e75ac4c29127a7db128e29c3ed Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Tue, 3 Oct 2023 16:08:16 -0700 Subject: [PATCH 08/10] some formatting and more context in README --- README.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8f82e10..a860e39 100644 --- a/README.md +++ b/README.md @@ -10,18 +10,25 @@ first you have to install the dependencies: `bundle install` + if you want the development group included run this first: -`bundle config set --local with 'development' +`bundle config set --local with 'development'` + then create a .env.rb file in the root directory that contains the following ENV attributes: -APP_SESSION_SECRET -DB_NAME + + +ENV["APP_SESSION_SECRET"] = {output of a random 64 byte secret} + +ENV["DB_NAME"] = {db file name} + after the dependencies are installed, you have to create the db `sequel -m db/migrations sqlite://db/{DB_NAME}` + to start the application with Falcon: `rackup -o {ip address} -p {port} -s falcon` From d93a8df199120d87b1e1aa5947ade7282283eaea Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Tue, 3 Oct 2023 16:08:53 -0700 Subject: [PATCH 09/10] formatting --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a860e39..09ae965 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,11 @@ if you want the development group included run this first: then create a .env.rb file in the root directory that contains the following ENV attributes: - +``` ENV["APP_SESSION_SECRET"] = {output of a random 64 byte secret} ENV["DB_NAME"] = {db file name} +``` after the dependencies are installed, you have to create the db From 80fae88228e91b2cbf2efe6b64af51a1fba888b6 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Tue, 3 Oct 2023 16:11:01 -0700 Subject: [PATCH 10/10] sqlite3 note --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 09ae965..7958b5a 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ Roda, Falcon, Sequel, and SQLite the point of this project is to quickly build something and work on continuous deployment while making small refinements to the functional pieces. +the only outside piece of software that this project relies on is sqlite3. + first you have to install the dependencies: