Compare commits

...

15 Commits

Author SHA1 Message Date
bucky 5cec47efcc Merge pull request 'create-json' (#21) from create-json into master
Reviewed-on: #21
2023-10-12 14:36:38 -07:00
Adam Townsend cabb4daded split up the nil? and empty? cases, because they are different
situations
2023-10-12 14:27:31 -07:00
Adam Townsend 1e61a76ded added 2 more tests for empty and missing url data posting to links 2023-10-12 14:27:02 -07:00
Adam Townsend ac3e9f14cd added links route for API interactions 2023-10-12 11:42:43 -07:00
Adam Townsend 5ee9351362 added db initialization to make sure the db exists and is up to date
before tests
2023-10-12 11:42:14 -07:00
Adam Townsend 2964e06639 added before and after actions
+ before initializes the db
+ after cleans the db
2023-10-12 11:40:15 -07:00
Adam Townsend d1a0fc0222 change request to use the /links path, to fit more into a RESTful API
format

+ also renamed the test to better describe the behavior
2023-10-12 11:32:00 -07:00
Adam Townsend 4a82174526 implemented API create endpoint
+ added plugins to read json and access the header to parse the request
  for an API request
+ fixed logic where url param is nil
+ refactored the new code to a variable to be reused easier
+ if CONTENT_TYPE header is application/json, reply with json
- there could be more refactoring (maybe separating it to a different
  endpoint) to handle other scenarios, we'll work on that later
2023-10-11 19:21:43 -07:00
Adam Townsend 3ae20255f6 DRY it out a little 2023-10-11 19:21:21 -07:00
Adam Townsend f69c53bf29 removed unnecessary variable 2023-10-11 19:20:07 -07:00
Adam Townsend 7d6c098047 added more to the spec test for creating a new link 2023-10-11 19:18:47 -07:00
Adam Townsend 3421e18f21 added more logic for running tests without killing the whole pipeline, and outputting rspec results 2023-10-11 12:05:33 -07:00
Adam Townsend 0dec27a01c added rspec step in tests stage, adjusted should clause 2023-10-11 11:04:08 -07:00
Adam Townsend c45cf6ab51 started building out rspec specs for API functionality 2023-10-11 11:01:46 -07:00
Adam Townsend e50667b392 split homepage out into homepage and submit feature sets 2023-10-10 17:29:23 -07:00
5 changed files with 128 additions and 28 deletions

11
Jenkinsfile vendored
View File

@ -25,7 +25,12 @@ pipeline {
} }
stage('Run tests') { stage('Run tests') {
steps { steps {
sh 'cucumber features --format html --out coverage/cucumber.html' catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
sh 'cucumber features --format html --out coverage/cucumber.html'
}
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
sh 'rspec spec --format html --out coverage/spec.html'
}
} }
} }
stage('Report results') { stage('Report results') {
@ -36,9 +41,9 @@ pipeline {
alwaysLinkToLastBuild: false, alwaysLinkToLastBuild: false,
keepAll: true, keepAll: true,
reportDir: 'coverage', reportDir: 'coverage',
reportFiles: 'cucumber.html, index.html', reportFiles: 'cucumber.html, spec.html, index.html',
reportName: 'Test Results', reportName: 'Test Results',
reportTitles: 'Cucumber Results, Test Coverage']) reportTitles: 'Cucumber Results, RSpec Results, Test Coverage'])
} }
} }
} }

34
app.rb
View File

@ -7,6 +7,8 @@ class App < Roda
plugin :sessions, secret: ENV.delete('APP_SESSION_SECRET') plugin :sessions, secret: ENV.delete('APP_SESSION_SECRET')
plugin :render, escape: true plugin :render, escape: true
plugin :flash plugin :flash
plugin :json_parser
plugin :request_headers
DB = Sequel.sqlite("db/#{ENV['DB_NAME']}") DB = Sequel.sqlite("db/#{ENV['DB_NAME']}")
links = DB[:links] links = DB[:links]
@ -27,7 +29,7 @@ class App < Roda
r.post "create" do r.post "create" do
url = r.params['url'] url = r.params['url']
if url.empty? if url.nil? or url.empty?
flash['message'] = "Please enter a valid URL"; flash['message'] = "Please enter a valid URL";
r.redirect '/' r.redirect '/'
end end
@ -36,9 +38,37 @@ class App < Roda
links.insert(url: url, code: code) links.insert(url: url, code: code)
@message = "Link created" @message = "Link created"
end end
code = links.filter(:url => url).first[:code]
@message ||= "Link exists" @message ||= "Link exists"
@new_link = 'http://' + request.env['HTTP_HOST'] + '/' + links.filter(:url => url).first[:code] @new_link = 'http://' + request.env['HTTP_HOST'] + '/' + code
view :create view :create
end end
r.on "links" do
r.post do
if 'application/json' != r.headers['CONTENT_TYPE']
return {message: "Not a valid JSON request"}.to_json
end
url = r.params['url']
if url.nil?
response.status = 400
return {message: "missing url parameter"}.to_json
end
if url.empty?
response.status = 400
return {message: "invalid url parameter"}.to_json
end
if links.filter(:url => url).first.nil?
code = SecureRandom.urlsafe_base64 4
links.insert(url: url, code: code)
end
code = links.filter(:url => url).first[:code]
@new_link = 'http://' + request.env['HTTP_HOST'] + '/' + code
return {url: url, code: code, link: @new_link}.to_json
end
end
end end
end end

View File

@ -10,26 +10,3 @@ Feature: Homepage
And I should see the message "Enter a URL" And I should see the message "Enter a URL"
And I should see a form field "url" And I should see a form field "url"
And I should see a "Submit" button 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
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
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"

View File

@ -0,0 +1,28 @@
# features/submit.feature
Feature: Submit
Submitting URL's on the homepage
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
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
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"

View File

@ -0,0 +1,60 @@
require_relative '../.env'
ENV["DB_NAME"] = "test_#{ENV["DB_NAME"]}"
require_relative '../app'
require 'rubygems'
require 'roda'
require 'sequel'
require 'rspec'
require 'rack/test'
# DB initialization
Sequel.extension :migration
Sequel.sqlite("db/#{ENV['DB_NAME']}") do |db|
Sequel::Migrator.apply(db, "db/migrations")
end
def app
App
end
describe "Submit API request to create new link" do
include Rack::Test::Methods
before :each do
@links = Sequel.sqlite("db/#{ENV['DB_NAME']}")[:links]
end
after :each do
@links.delete
end
it "should return link data in json format when a valid url is submitted" do
data = {
url: 'http://google.com'
}
post('/links', data.to_json, "CONTENT_TYPE" => "application/json")
expect(last_response).to be_ok
response_json = JSON.parse(last_response.body)
expect(response_json['url']).to eq(data[:url])
expect(response_json['code']).not_to eq(nil)
expect(response_json['link']).to include(response_json['code'])
end
it "should return with a 400 status and 'invalid url parameter' message when an empty url is submitted" do
data = {
url: ''
}
post('/links', data.to_json, "CONTENT_TYPE" => "application/json")
expect(last_response.status).to eq(400)
response_json = JSON.parse(last_response.body)
expect(response_json['message']).to eq('invalid url parameter')
end
it "should return with a 400 status and 'missing url parameter' message when an empty url is submitted" do
data = {
}
post('/links', data.to_json, "CONTENT_TYPE" => "application/json")
expect(last_response.status).to eq(400)
response_json = JSON.parse(last_response.body)
expect(response_json['message']).to eq('missing url parameter')
end
end